]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
threads: don't sleep under lock
authorVictor Julien <victor@inliniac.net>
Mon, 28 Nov 2016 14:36:29 +0000 (15:36 +0100)
committerVictor Julien <victor@inliniac.net>
Thu, 16 Feb 2017 14:20:27 +0000 (15:20 +0100)
src/flow-manager.c
src/tm-threads.c
src/unix-manager.c

index 87292d876d5b7e60b2a28fecbcee01a51ec4502d..4f92ec2c12ad13470330b42248169d1c0ec3b6c4 100644 (file)
@@ -136,7 +136,6 @@ void FlowDisableFlowManagerThread(void)
     return;
 #endif
     ThreadVars *tv = NULL;
-    int cnt = 0;
 
     /* wake up threads */
     uint32_t u;
@@ -144,25 +143,37 @@ void FlowDisableFlowManagerThread(void)
         SCCtrlCondSignal(&flow_manager_ctrl_cond);
 
     SCMutexLock(&tv_root_lock);
-
     /* flow manager thread(s) is/are a part of mgmt threads */
     tv = tv_root[TVT_MGMT];
-
     while (tv != NULL)
     {
         if (strncasecmp(tv->name, thread_name_flow_mgr,
             strlen(thread_name_flow_mgr)) == 0)
         {
             TmThreadsSetFlag(tv, THV_KILL);
-            cnt++;
+        }
+        tv = tv->next;
+    }
+    SCMutexUnlock(&tv_root_lock);
 
-            /* value in seconds */
-#define THREAD_KILL_MAX_WAIT_TIME 60
-            /* value in microseconds */
-#define WAIT_TIME 100
+    double total_wait_time = 0;
+    /* value in seconds */
+    #define THREAD_KILL_MAX_WAIT_TIME 60
+    /* value in microseconds */
+    #define WAIT_TIME 100
+
+again:
+    SCMutexLock(&tv_root_lock);
+
+    tv = tv_root[TVT_MGMT];
+    while (tv != NULL)
+    {
+        if (strncasecmp(tv->name, thread_name_flow_mgr,
+            strlen(thread_name_flow_mgr)) == 0)
+        {
+            if (!TmThreadsCheckFlag(tv, THV_RUNNING_DONE)) {
+                SCMutexUnlock(&tv_root_lock);
 
-            double total_wait_time = 0;
-            while (!TmThreadsCheckFlag(tv, THV_RUNNING_DONE)) {
                 usleep(WAIT_TIME);
                 total_wait_time += WAIT_TIME / 1000000.0;
                 if (total_wait_time > THREAD_KILL_MAX_WAIT_TIME) {
@@ -171,6 +182,7 @@ void FlowDisableFlowManagerThread(void)
                             "Killing engine", tv->name);
                     exit(EXIT_FAILURE);
                 }
+                goto again;
             }
         }
         tv = tv->next;
@@ -1038,10 +1050,8 @@ void FlowDisableFlowRecyclerThread(void)
         SCCtrlCondSignal(&flow_recycler_ctrl_cond);
 
     SCMutexLock(&tv_root_lock);
-
     /* flow recycler thread(s) is/are a part of mgmt threads */
     tv = tv_root[TVT_MGMT];
-
     while (tv != NULL)
     {
         if (strncasecmp(tv->name, thread_name_flow_rec,
@@ -1049,22 +1059,36 @@ void FlowDisableFlowRecyclerThread(void)
         {
             TmThreadsSetFlag(tv, THV_KILL);
             cnt++;
+        }
+        tv = tv->next;
+    }
+    SCMutexUnlock(&tv_root_lock);
 
-            /* value in seconds */
+    double total_wait_time = 0;
+    /* value in seconds */
 #define THREAD_KILL_MAX_WAIT_TIME 60
-            /* value in microseconds */
+    /* value in microseconds */
 #define WAIT_TIME 100
 
-            double total_wait_time = 0;
-            while (!TmThreadsCheckFlag(tv, THV_RUNNING_DONE)) {
-                usleep(WAIT_TIME);
-                total_wait_time += WAIT_TIME / 1000000.0;
+again:
+    SCMutexLock(&tv_root_lock);
+    tv = tv_root[TVT_MGMT];
+    while (tv != NULL)
+    {
+        if (strncasecmp(tv->name, thread_name_flow_rec,
+            strlen(thread_name_flow_rec)) == 0)
+        {
+            if (!TmThreadsCheckFlag(tv, THV_RUNNING_DONE)) {
                 if (total_wait_time > THREAD_KILL_MAX_WAIT_TIME) {
                     SCLogError(SC_ERR_FATAL, "Engine unable to "
                             "disable detect thread - \"%s\".  "
                             "Killing engine", tv->name);
                     exit(EXIT_FAILURE);
                 }
+                SCMutexUnlock(&tv_root_lock);
+                usleep(WAIT_TIME);
+                total_wait_time += WAIT_TIME / 1000000.0;
+                goto again;
             }
         }
         tv = tv->next;
index bc866aa215dc8d9744ac8bdb818916c95b26c1f5..b67f3d5c6ed42c3bd0da483c783196368bd30b80 100644 (file)
@@ -1660,6 +1660,8 @@ again:
 
             /* wait for it to enter the 'flow loop' stage */
             while (!TmThreadsCheckFlag(tv, THV_FLOW_LOOP)) {
+                SCMutexUnlock(&tv_root_lock);
+
                 usleep(WAIT_TIME);
                 total_wait_time += WAIT_TIME / 1000000.0;
                 if (total_wait_time > THREAD_KILL_MAX_WAIT_TIME) {
@@ -1668,6 +1670,7 @@ again:
                                "Killing engine", tv->name);
                     exit(EXIT_FAILURE);
                 }
+                goto again;
             }
         }
 
@@ -1743,6 +1746,8 @@ again:
         }
 
         while (!TmThreadsCheckFlag(tv, THV_RUNNING_DONE)) {
+            SCMutexUnlock(&tv_root_lock);
+
             usleep(WAIT_TIME);
             total_wait_time += WAIT_TIME / 1000000.0;
             if (total_wait_time > THREAD_KILL_MAX_WAIT_TIME) {
@@ -1751,6 +1756,7 @@ again:
                         "Killing engine", tv->name);
                 exit(EXIT_FAILURE);
             }
+            goto again;
         }
 
         tv = tv->next;
@@ -2122,37 +2128,36 @@ TmEcode TmThreadWaitOnThreadInit(void)
     int i = 0;
     uint16_t mgt_num = 0;
     uint16_t ppt_num = 0;
-
+again:
     SCMutexLock(&tv_root_lock);
     for (i = 0; i < TVT_MAX; i++) {
         tv = tv_root[i];
         while (tv != NULL) {
-            char started = FALSE;
-            while (started == FALSE) {
-                if (TmThreadsCheckFlag(tv, THV_INIT_DONE)) {
-                    started = TRUE;
-                } else {
-                    /* sleep a little to give the thread some
-                     * time to finish initialization */
-                    usleep(100);
-                }
+            if (!(TmThreadsCheckFlag(tv, THV_INIT_DONE))) {
+                SCMutexUnlock(&tv_root_lock);
+                /* sleep a little to give the thread some
+                 * time to finish initialization */
+                usleep(100);
+                goto again;
+            }
 
-                if (TmThreadsCheckFlag(tv, THV_FAILED)) {
-                    SCMutexUnlock(&tv_root_lock);
-                    SCLogError(SC_ERR_THREAD_INIT, "thread \"%s\" failed to "
-                            "initialize.", tv->name);
-                    return TM_ECODE_FAILED;
-                }
-                if (TmThreadsCheckFlag(tv, THV_CLOSED)) {
-                    SCMutexUnlock(&tv_root_lock);
-                    SCLogError(SC_ERR_THREAD_INIT, "thread \"%s\" closed on "
-                            "initialization.", tv->name);
-                    return TM_ECODE_FAILED;
-                }
+            if (TmThreadsCheckFlag(tv, THV_FAILED)) {
+                SCMutexUnlock(&tv_root_lock);
+                SCLogError(SC_ERR_THREAD_INIT, "thread \"%s\" failed to "
+                        "initialize.", tv->name);
+                return TM_ECODE_FAILED;
+            }
+            if (TmThreadsCheckFlag(tv, THV_CLOSED)) {
+                SCMutexUnlock(&tv_root_lock);
+                SCLogError(SC_ERR_THREAD_INIT, "thread \"%s\" closed on "
+                        "initialization.", tv->name);
+                return TM_ECODE_FAILED;
             }
 
-            if (i == TVT_MGMT) mgt_num++;
-            else if (i == TVT_PPT) ppt_num++;
+            if (i == TVT_MGMT)
+                mgt_num++;
+            else if (i == TVT_PPT)
+                ppt_num++;
 
             tv = tv->next;
         }
index 2e8cf5a94a31dc3aee692421a21220bbb298ba49..702b63d46fdb228eb778e5a6bd311aa86394058c 100644 (file)
@@ -610,8 +610,8 @@ void UnixKillUnixManagerThread(void)
     ThreadVars *tv = NULL;
     int cnt = 0;
 
+again:
     SCCtrlCondSignal(&unix_manager_ctrl_cond);
-
     SCMutexLock(&tv_root_lock);
 
     /* flow manager thread(s) is/are a part of mgmt threads */
@@ -623,8 +623,10 @@ void UnixKillUnixManagerThread(void)
             TmThreadsSetFlag(tv, THV_DEINIT);
 
             /* be sure it has shut down */
-            while (!TmThreadsCheckFlag(tv, THV_CLOSED)) {
+            if(!(TmThreadsCheckFlag(tv, THV_CLOSED))) {
+                SCMutexUnlock(&tv_root_lock);
                 usleep(100);
+                goto again;
             }
             cnt++;
         }
@@ -1037,6 +1039,7 @@ void UnixSocketKillSocketThread(void)
 {
     ThreadVars *tv = NULL;
 
+again:
     SCMutexLock(&tv_root_lock);
 
     /* unix manager thread(s) is/are a part of command threads */
@@ -1057,8 +1060,10 @@ void UnixSocketKillSocketThread(void)
             TmThreadsSetFlag(tv, THV_KILL);
             TmThreadsSetFlag(tv, THV_DEINIT);
             /* Be sure it has shut down */
-            while (!TmThreadsCheckFlag(tv, THV_CLOSED)) {
+            if (!TmThreadsCheckFlag(tv, THV_CLOSED)) {
+                SCMutexUnlock(&tv_root_lock);
                 usleep(100);
+                goto again;
             }
         }
         tv = tv->next;