From 2f0f68e4d537de50896f681e22255c53e8f2cedf Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 3 Jun 2017 19:08:24 +0200 Subject: [PATCH] Rework thread locks (since we don't need different kinds anymore) --- gc.c | 4 ++-- parser.c | 4 ++-- routines.h | 5 ++--- socket.c | 4 ++-- threads.c | 37 +++++++++---------------------------- 5 files changed, 17 insertions(+), 37 deletions(-) diff --git a/gc.c b/gc.c index dc2d57ad..828df41a 100644 --- a/gc.c +++ b/gc.c @@ -17,7 +17,7 @@ void *GC_thread(void *val) // Lock FTL's data structure, since it is likely that it will be changed here // Requests should not be processed/answered when data is about to change - enable_read_write_lock("GC_thread"); + enable_thread_lock("GC_thread"); // Get minimum time stamp to keep int differencetofullhour = time(NULL) % GCinterval; @@ -141,7 +141,7 @@ void *GC_thread(void *val) } // Release thread lock - disable_thread_locks("GC_thread"); + disable_thread_lock("GC_thread"); return NULL; diff --git a/parser.c b/parser.c index 3d498108..4e22ebb5 100644 --- a/parser.c +++ b/parser.c @@ -97,7 +97,7 @@ void *pihole_log_thread(void *val) { // Lock FTL's data structure, since it is likely that it will be changed here // Requests should not be processed/answered when data is about to change - enable_read_write_lock("pihole_log_thread"); + enable_thread_lock("pihole_log_thread"); if(newdata > 0 && !flush) { @@ -123,7 +123,7 @@ void *pihole_log_thread(void *val) } // Release thread lock - disable_thread_locks("pihole_log_thread"); + disable_thread_lock("pihole_log_thread"); } // Wait some time before looking again at the log files diff --git a/routines.h b/routines.h index d405e0b1..ce02bb33 100644 --- a/routines.h +++ b/routines.h @@ -65,9 +65,8 @@ void parse_args(int argc, char* argv[]); char* find_equals(const char* s); -void enable_read_lock(const char *message); -void enable_read_write_lock(const char *message); -void disable_thread_locks(const char *message); +void enable_thread_lock(const char *message); +void disable_thread_lock(const char *message); void read_FTLconf(void); diff --git a/socket.c b/socket.c index 66a7c45c..3c0d2fc7 100644 --- a/socket.c +++ b/socket.c @@ -208,13 +208,13 @@ void *connection_handler_thread(void *socket_desc) // Lock FTL data structure, since it is likely that it will be changed here // Requests should not be processed/answered when data is about to change - enable_read_lock(threadname); + enable_thread_lock(threadname); process_request(message, &sock); free(message); // Release thread lock - disable_thread_locks("connection_handler_thread"); + disable_thread_lock(threadname); if(sock == 0) { diff --git a/threads.c b/threads.c index 687ae7c9..fd85e1d3 100644 --- a/threads.c +++ b/threads.c @@ -13,43 +13,24 @@ // Logic of the locks: // Any of the various threads (logparser, GC, client threads) is accessing FTL's data structure. Hence, they should // never run at the same time since the data can change half-way through, leading to unspecified behavior. -// threadwritelock: The threadwritelock ensures that only one thread with write-access to FTL's data structure can -// be active at any given time -// threadreadlock: An expection to the rule of non-concurrency are the client threads, as they do need read-access -// Therefore, it is no problem to have several of them running concurrently. Accordingly, client -// threads do *not* have to wait at the lock if threadreadlocks is true (i.e. a client listener -// thread has activated this thread lock earlier) -bool threadwritelock = false; -bool threadreadlock = false; +// threadlock: The threadlock ensures that only one thread can be active at any given time +bool threadlock = false; -void enable_read_lock(const char *message) +void enable_thread_lock(const char *message) { - while(threadwritelock) sleepms(5); + while(threadlock) sleepms(5); if(debugthreads) - logg("Thread lock enabled (R ): %s", message); + logg("Thread lock enabled: %s", message); - // Set threadwritelock - threadwritelock = false; - // Set threadreadlock (see above) - threadreadlock = true; + // Set threadlock + threadlock = true; } -void enable_read_write_lock(const char *message) +void disable_thread_lock(const char *message) { - while(threadwritelock || threadreadlock) sleepms(5); + threadlock = false; - if(debugthreads) - logg("Thread lock enabled (RW): %s", message); - - // Set threadwritelock - threadwritelock = true; -} - -void disable_thread_locks(const char *message) -{ - threadwritelock = false; - threadreadlock = false; if(debugthreads) logg("Thread lock disabled: %s", message); }