--- asterisk-1.4.9.orig/channels/chan_iax2.c 2007-07-24 12:32:20.000000000 -0400 +++ asterisk-1.4.9/channels/chan_iax2.c 2007-08-01 14:23:36.000000000 -0400 @@ -875,6 +875,7 @@ pthread_attr_t attr; struct iax2_thread *thread = NULL; +retry: /* Pop the head of the list off */ AST_LIST_LOCK(&idle_list); thread = AST_LIST_REMOVE_HEAD(&idle_list, list); @@ -900,6 +901,13 @@ } else { /* All went well and the thread is up, so increment our count */ iaxdynamicthreadcount++; + + /* + * We want to give this thread a chance to grab its mutex before returning it + * to the caller so we wait for the thread to show up in the dynamic queue + */ + AST_LIST_UNLOCK(&dynamic_list); + goto retry; } } } @@ -7848,6 +7856,13 @@ struct timespec ts; int put_into_idle = 0; + /* + * We want to add ourselves to the dynamic list if we're a dynamic thread; otherwise there's a race between our + * creation, when we wait to be signalled, and when we actually lock ourselves and wait. + */ + if(thread->type == IAX_TYPE_DYNAMIC) + put_into_idle = 1; + ast_atomic_fetchadd_int(&iaxactivethreadcount,1); pthread_cleanup_push(iax2_process_thread_cleanup, data); for(;;) {