Use low-level mutex locks to ensure FTLs main datastructure can only be accessed by one thread at a time.

This commit is contained in:
DL6ER
2017-07-01 18:39:19 +02:00
parent 9976ff7425
commit d8d526aff3
3 changed files with 23 additions and 8 deletions
+1
View File
@@ -29,6 +29,7 @@ int main (int argc, char* argv[]) {
logg("FTL hash: %s", GIT_VERSION);
logg("FTL date: %s", GIT_DATE);
logg("FTL user: %s", username);
init_thread_lock();
// pihole-FTL should really be run as user "pihole" to not mess up with the file permissions
// still allow this if "debug" flag is set
+1
View File
@@ -61,6 +61,7 @@ char* find_equals(const char* s);
void enable_thread_lock(const char *message);
void disable_thread_lock(const char *message);
void init_thread_lock(void);
void read_FTLconf(void);
+21 -8
View File
@@ -14,23 +14,36 @@
// 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.
// threadlock: The threadlock ensures that only one thread can be active at any given time
bool threadlock = false;
pthread_mutex_t threadlock;
void enable_thread_lock(const char *message)
{
while(threadlock) sleepms(5);
int ret = pthread_mutex_lock(&threadlock);
if(ret != 0)
logg("Thread lock error: %i",ret);
if(debugthreads)
logg("Thread lock enabled: %s", message);
// Set threadlock
threadlock = true;
logg("Thread locked: %s", message);
}
void disable_thread_lock(const char *message)
{
threadlock = false;
int ret = pthread_mutex_unlock(&threadlock);
if(ret != 0)
logg("Thread unlock error: %i",ret);
if(debugthreads)
logg("Thread lock disabled: %s", message);
logg("Thread unlocked: %s", message);
}
void init_thread_lock(void)
{
if (pthread_mutex_init(&threadlock, NULL) != 0)
{
logg("FATAL: Thread mutex init failed\n");
// Return failure
exit(EXIT_FAILURE);
}
}