From f38087105723f9fc52df24a66858bb4e51109b92 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Mon, 28 Nov 2016 15:36:29 +0100 Subject: [PATCH] threads: don't sleep under lock --- src/flow-manager.c | 60 ++++++++++++++++++++++++++++++++-------------- src/tm-threads.c | 53 +++++++++++++++++++++------------------- src/unix-manager.c | 11 ++++++--- 3 files changed, 79 insertions(+), 45 deletions(-) diff --git a/src/flow-manager.c b/src/flow-manager.c index 87292d876d..4f92ec2c12 100644 --- a/src/flow-manager.c +++ b/src/flow-manager.c @@ -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; diff --git a/src/tm-threads.c b/src/tm-threads.c index bc866aa215..b67f3d5c6e 100644 --- a/src/tm-threads.c +++ b/src/tm-threads.c @@ -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; } diff --git a/src/unix-manager.c b/src/unix-manager.c index 2e8cf5a94a..702b63d46f 100644 --- a/src/unix-manager.c +++ b/src/unix-manager.c @@ -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;