threads: don't sleep under lock

pull/2572/head
Victor Julien 9 years ago
parent cc4010343d
commit f380871057

@ -136,7 +136,6 @@ void FlowDisableFlowManagerThread(void)
return; return;
#endif #endif
ThreadVars *tv = NULL; ThreadVars *tv = NULL;
int cnt = 0;
/* wake up threads */ /* wake up threads */
uint32_t u; uint32_t u;
@ -144,25 +143,37 @@ void FlowDisableFlowManagerThread(void)
SCCtrlCondSignal(&flow_manager_ctrl_cond); SCCtrlCondSignal(&flow_manager_ctrl_cond);
SCMutexLock(&tv_root_lock); SCMutexLock(&tv_root_lock);
/* flow manager thread(s) is/are a part of mgmt threads */ /* flow manager thread(s) is/are a part of mgmt threads */
tv = tv_root[TVT_MGMT]; tv = tv_root[TVT_MGMT];
while (tv != NULL) while (tv != NULL)
{ {
if (strncasecmp(tv->name, thread_name_flow_mgr, if (strncasecmp(tv->name, thread_name_flow_mgr,
strlen(thread_name_flow_mgr)) == 0) strlen(thread_name_flow_mgr)) == 0)
{ {
TmThreadsSetFlag(tv, THV_KILL); TmThreadsSetFlag(tv, THV_KILL);
cnt++; }
tv = tv->next;
}
SCMutexUnlock(&tv_root_lock);
double total_wait_time = 0;
/* value in seconds */ /* value in seconds */
#define THREAD_KILL_MAX_WAIT_TIME 60 #define THREAD_KILL_MAX_WAIT_TIME 60
/* value in microseconds */ /* value in microseconds */
#define WAIT_TIME 100 #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); usleep(WAIT_TIME);
total_wait_time += WAIT_TIME / 1000000.0; total_wait_time += WAIT_TIME / 1000000.0;
if (total_wait_time > THREAD_KILL_MAX_WAIT_TIME) { if (total_wait_time > THREAD_KILL_MAX_WAIT_TIME) {
@ -171,6 +182,7 @@ void FlowDisableFlowManagerThread(void)
"Killing engine", tv->name); "Killing engine", tv->name);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
goto again;
} }
} }
tv = tv->next; tv = tv->next;
@ -1038,10 +1050,8 @@ void FlowDisableFlowRecyclerThread(void)
SCCtrlCondSignal(&flow_recycler_ctrl_cond); SCCtrlCondSignal(&flow_recycler_ctrl_cond);
SCMutexLock(&tv_root_lock); SCMutexLock(&tv_root_lock);
/* flow recycler thread(s) is/are a part of mgmt threads */ /* flow recycler thread(s) is/are a part of mgmt threads */
tv = tv_root[TVT_MGMT]; tv = tv_root[TVT_MGMT];
while (tv != NULL) while (tv != NULL)
{ {
if (strncasecmp(tv->name, thread_name_flow_rec, if (strncasecmp(tv->name, thread_name_flow_rec,
@ -1049,22 +1059,36 @@ void FlowDisableFlowRecyclerThread(void)
{ {
TmThreadsSetFlag(tv, THV_KILL); TmThreadsSetFlag(tv, THV_KILL);
cnt++; cnt++;
}
tv = tv->next;
}
SCMutexUnlock(&tv_root_lock);
double total_wait_time = 0;
/* value in seconds */ /* value in seconds */
#define THREAD_KILL_MAX_WAIT_TIME 60 #define THREAD_KILL_MAX_WAIT_TIME 60
/* value in microseconds */ /* value in microseconds */
#define WAIT_TIME 100 #define WAIT_TIME 100
double total_wait_time = 0; again:
while (!TmThreadsCheckFlag(tv, THV_RUNNING_DONE)) { SCMutexLock(&tv_root_lock);
usleep(WAIT_TIME); tv = tv_root[TVT_MGMT];
total_wait_time += WAIT_TIME / 1000000.0; 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) { if (total_wait_time > THREAD_KILL_MAX_WAIT_TIME) {
SCLogError(SC_ERR_FATAL, "Engine unable to " SCLogError(SC_ERR_FATAL, "Engine unable to "
"disable detect thread - \"%s\". " "disable detect thread - \"%s\". "
"Killing engine", tv->name); "Killing engine", tv->name);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
SCMutexUnlock(&tv_root_lock);
usleep(WAIT_TIME);
total_wait_time += WAIT_TIME / 1000000.0;
goto again;
} }
} }
tv = tv->next; tv = tv->next;

@ -1660,6 +1660,8 @@ again:
/* wait for it to enter the 'flow loop' stage */ /* wait for it to enter the 'flow loop' stage */
while (!TmThreadsCheckFlag(tv, THV_FLOW_LOOP)) { while (!TmThreadsCheckFlag(tv, THV_FLOW_LOOP)) {
SCMutexUnlock(&tv_root_lock);
usleep(WAIT_TIME); usleep(WAIT_TIME);
total_wait_time += WAIT_TIME / 1000000.0; total_wait_time += WAIT_TIME / 1000000.0;
if (total_wait_time > THREAD_KILL_MAX_WAIT_TIME) { if (total_wait_time > THREAD_KILL_MAX_WAIT_TIME) {
@ -1668,6 +1670,7 @@ again:
"Killing engine", tv->name); "Killing engine", tv->name);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
goto again;
} }
} }
@ -1743,6 +1746,8 @@ again:
} }
while (!TmThreadsCheckFlag(tv, THV_RUNNING_DONE)) { while (!TmThreadsCheckFlag(tv, THV_RUNNING_DONE)) {
SCMutexUnlock(&tv_root_lock);
usleep(WAIT_TIME); usleep(WAIT_TIME);
total_wait_time += WAIT_TIME / 1000000.0; total_wait_time += WAIT_TIME / 1000000.0;
if (total_wait_time > THREAD_KILL_MAX_WAIT_TIME) { if (total_wait_time > THREAD_KILL_MAX_WAIT_TIME) {
@ -1751,6 +1756,7 @@ again:
"Killing engine", tv->name); "Killing engine", tv->name);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
goto again;
} }
tv = tv->next; tv = tv->next;
@ -2122,19 +2128,17 @@ TmEcode TmThreadWaitOnThreadInit(void)
int i = 0; int i = 0;
uint16_t mgt_num = 0; uint16_t mgt_num = 0;
uint16_t ppt_num = 0; uint16_t ppt_num = 0;
again:
SCMutexLock(&tv_root_lock); SCMutexLock(&tv_root_lock);
for (i = 0; i < TVT_MAX; i++) { for (i = 0; i < TVT_MAX; i++) {
tv = tv_root[i]; tv = tv_root[i];
while (tv != NULL) { while (tv != NULL) {
char started = FALSE; if (!(TmThreadsCheckFlag(tv, THV_INIT_DONE))) {
while (started == FALSE) { SCMutexUnlock(&tv_root_lock);
if (TmThreadsCheckFlag(tv, THV_INIT_DONE)) {
started = TRUE;
} else {
/* sleep a little to give the thread some /* sleep a little to give the thread some
* time to finish initialization */ * time to finish initialization */
usleep(100); usleep(100);
goto again;
} }
if (TmThreadsCheckFlag(tv, THV_FAILED)) { if (TmThreadsCheckFlag(tv, THV_FAILED)) {
@ -2149,10 +2153,11 @@ TmEcode TmThreadWaitOnThreadInit(void)
"initialization.", tv->name); "initialization.", tv->name);
return TM_ECODE_FAILED; return TM_ECODE_FAILED;
} }
}
if (i == TVT_MGMT) mgt_num++; if (i == TVT_MGMT)
else if (i == TVT_PPT) ppt_num++; mgt_num++;
else if (i == TVT_PPT)
ppt_num++;
tv = tv->next; tv = tv->next;
} }

@ -610,8 +610,8 @@ void UnixKillUnixManagerThread(void)
ThreadVars *tv = NULL; ThreadVars *tv = NULL;
int cnt = 0; int cnt = 0;
again:
SCCtrlCondSignal(&unix_manager_ctrl_cond); SCCtrlCondSignal(&unix_manager_ctrl_cond);
SCMutexLock(&tv_root_lock); SCMutexLock(&tv_root_lock);
/* flow manager thread(s) is/are a part of mgmt threads */ /* flow manager thread(s) is/are a part of mgmt threads */
@ -623,8 +623,10 @@ void UnixKillUnixManagerThread(void)
TmThreadsSetFlag(tv, THV_DEINIT); TmThreadsSetFlag(tv, THV_DEINIT);
/* be sure it has shut down */ /* be sure it has shut down */
while (!TmThreadsCheckFlag(tv, THV_CLOSED)) { if(!(TmThreadsCheckFlag(tv, THV_CLOSED))) {
SCMutexUnlock(&tv_root_lock);
usleep(100); usleep(100);
goto again;
} }
cnt++; cnt++;
} }
@ -1037,6 +1039,7 @@ void UnixSocketKillSocketThread(void)
{ {
ThreadVars *tv = NULL; ThreadVars *tv = NULL;
again:
SCMutexLock(&tv_root_lock); SCMutexLock(&tv_root_lock);
/* unix manager thread(s) is/are a part of command threads */ /* 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_KILL);
TmThreadsSetFlag(tv, THV_DEINIT); TmThreadsSetFlag(tv, THV_DEINIT);
/* Be sure it has shut down */ /* Be sure it has shut down */
while (!TmThreadsCheckFlag(tv, THV_CLOSED)) { if (!TmThreadsCheckFlag(tv, THV_CLOSED)) {
SCMutexUnlock(&tv_root_lock);
usleep(100); usleep(100);
goto again;
} }
} }
tv = tv->next; tv = tv->next;

Loading…
Cancel
Save