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;
#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;

@ -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;
}

@ -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;

Loading…
Cancel
Save