Index: channels/chan_iax2.c =================================================================== --- channels/chan_iax2.c (revision 128639) +++ channels/chan_iax2.c (working copy) @@ -727,7 +727,8 @@ time_t checktime; ast_mutex_t lock; ast_cond_t cond; - unsigned int ready_for_signal:1; + ast_mutex_t init_lock; + ast_cond_t init_cond; /*! if this thread is processing a full frame, some information about that frame will be stored here, so we can avoid dispatching any more full @@ -925,6 +926,9 @@ thread->type = IAX_TYPE_DYNAMIC; ast_mutex_init(&thread->lock); ast_cond_init(&thread->cond, NULL); + ast_mutex_init(&thread->init_lock); + ast_cond_init(&thread->init_cond, NULL); + ast_mutex_lock(&thread->init_lock); pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (ast_pthread_create(&thread->threadid, &attr, iax2_process_thread, thread)) { @@ -935,8 +939,10 @@ iaxdynamicthreadcount++; /* Wait for the thread to be ready before returning it to the caller */ - while (!thread->ready_for_signal) - usleep(1); + ast_cond_wait(&thread->init_cond, &thread->init_lock); + + /* Done with init_lock */ + ast_mutex_unlock(&thread->init_lock); } } } @@ -8537,6 +8543,8 @@ struct iax2_thread *thread = data; ast_mutex_destroy(&thread->lock); ast_cond_destroy(&thread->cond); + ast_mutex_destroy(&thread->init_lock); + ast_cond_destroy(&thread->init_cond); free(thread); ast_atomic_dec_and_test(&iaxactivethreadcount); } @@ -8547,6 +8555,7 @@ struct timeval tv; struct timespec ts; int put_into_idle = 0; + int first_time = 1; ast_atomic_fetchadd_int(&iaxactivethreadcount,1); pthread_cleanup_push(iax2_process_thread_cleanup, data); @@ -8555,8 +8564,11 @@ ast_mutex_lock(&thread->lock); /* Flag that we're ready to accept signals */ - thread->ready_for_signal = 1; - + if (first_time) { + signal_condition(&thread->init_lock, &thread->init_cond); + first_time = 0; + } + /* Put into idle list if applicable */ if (put_into_idle) insert_idle_thread(thread);