Clean up find_idle_thread function and use atomic operations for dynamic thread count.

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@48567 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.6.0
Joshua Colp 19 years ago
parent c17181aae3
commit 381d49c9af

@ -890,37 +890,42 @@ static struct iax2_thread *find_idle_thread(void)
{ {
struct iax2_thread *thread = NULL; struct iax2_thread *thread = NULL;
/* Pop the head of the list off */ /* Pop the head of the idle list off */
AST_LIST_LOCK(&idle_list); AST_LIST_LOCK(&idle_list);
thread = AST_LIST_REMOVE_HEAD(&idle_list, list); thread = AST_LIST_REMOVE_HEAD(&idle_list, list);
AST_LIST_UNLOCK(&idle_list); AST_LIST_UNLOCK(&idle_list);
/* If no idle thread is available from the regular list, try dynamic */ /* If we popped a thread off the idle list, just return it */
if (thread == NULL) { if (thread)
AST_LIST_LOCK(&dynamic_list); return thread;
thread = AST_LIST_FIRST(&dynamic_list);
if (thread != NULL) { /* Pop the head of the dynamic list off */
AST_LIST_REMOVE(&dynamic_list, thread, list); AST_LIST_LOCK(&dynamic_list);
} thread = AST_LIST_REMOVE_HEAD(&dynamic_list, list);
/* Make sure we absolutely have a thread... if not, try to make one if allowed */ AST_LIST_UNLOCK(&dynamic_list);
if (thread == NULL && iaxmaxthreadcount > iaxdynamicthreadcount) {
/* We need to MAKE a thread! */ /* If we popped a thread off the dynamic list, just return it */
thread = ast_calloc(1, sizeof(*thread)); if (thread)
if (thread != NULL) { return thread;
thread->threadnum = iaxdynamicthreadcount;
thread->type = IAX_THREAD_TYPE_DYNAMIC; /* If we can't create a new dynamic thread for any reason, return no thread at all */
ast_mutex_init(&thread->lock); if (iaxdynamicthreadcount >= iaxmaxthreadcount || !(thread = ast_calloc(1, sizeof(*thread))))
ast_cond_init(&thread->cond, NULL); return NULL;
if (ast_pthread_create(&thread->threadid, NULL, iax2_process_thread, thread)) {
free(thread); /* Set default values */
thread = NULL; thread->threadnum = ast_atomic_fetchadd_int(&iaxdynamicthreadcount, 1);
} else { thread->type = IAX_THREAD_TYPE_DYNAMIC;
/* All went well and the thread is up, so increment our count */
iaxdynamicthreadcount++; /* Initialize lock and condition */
} ast_mutex_init(&thread->lock);
} ast_cond_init(&thread->cond, NULL);
}
AST_LIST_UNLOCK(&dynamic_list); /* Create thread and send it on it's way */
if (ast_pthread_create(&thread->threadid, NULL, iax2_process_thread, thread)) {
ast_cond_destroy(&thread->cond);
ast_mutex_destroy(&thread->lock);
free(thread);
thread = NULL;
} }
return thread; return thread;
@ -7696,8 +7701,8 @@ static void *iax2_process_thread(void *data)
ast_mutex_unlock(&thread->lock); ast_mutex_unlock(&thread->lock);
AST_LIST_LOCK(&dynamic_list); AST_LIST_LOCK(&dynamic_list);
AST_LIST_REMOVE(&dynamic_list, thread, list); AST_LIST_REMOVE(&dynamic_list, thread, list);
iaxdynamicthreadcount--;
AST_LIST_UNLOCK(&dynamic_list); AST_LIST_UNLOCK(&dynamic_list);
ast_atomic_dec_and_test(&iaxdynamicthreadcount);
break; break;
} }
} else { } else {

Loading…
Cancel
Save