diff options
Diffstat (limited to 'srclib/apr-util/misc/apr_thread_pool.c')
-rw-r--r-- | srclib/apr-util/misc/apr_thread_pool.c | 63 |
1 files changed, 27 insertions, 36 deletions
diff --git a/srclib/apr-util/misc/apr_thread_pool.c b/srclib/apr-util/misc/apr_thread_pool.c index 0cbdd2df..a3b9bc20 100644 --- a/srclib/apr-util/misc/apr_thread_pool.c +++ b/srclib/apr-util/misc/apr_thread_pool.c @@ -72,7 +72,6 @@ struct apr_thread_pool struct apr_thread_list *busy_thds; struct apr_thread_list *idle_thds; apr_thread_mutex_t *lock; - apr_thread_mutex_t *cond_lock; apr_thread_cond_t *cond; volatile int terminated; struct apr_thread_pool_tasks *recycled_tasks; @@ -95,16 +94,9 @@ static apr_status_t thread_pool_construct(apr_thread_pool_t * me, if (APR_SUCCESS != rv) { return rv; } - rv = apr_thread_mutex_create(&me->cond_lock, APR_THREAD_MUTEX_UNNESTED, - me->pool); - if (APR_SUCCESS != rv) { - apr_thread_mutex_destroy(me->lock); - return rv; - } rv = apr_thread_cond_create(&me->cond, me->pool); if (APR_SUCCESS != rv) { apr_thread_mutex_destroy(me->lock); - apr_thread_mutex_destroy(me->cond_lock); return rv; } me->tasks = apr_palloc(me->pool, sizeof(*me->tasks)); @@ -148,7 +140,6 @@ static apr_status_t thread_pool_construct(apr_thread_pool_t * me, CATCH_ENOMEM: rv = APR_ENOMEM; apr_thread_mutex_destroy(me->lock); - apr_thread_mutex_destroy(me->cond_lock); apr_thread_cond_destroy(me->cond); FINAL_EXIT: return rv; @@ -321,16 +312,12 @@ static void *APR_THREAD_FUNC thread_pool_func(apr_thread_t * t, void *param) else wait = -1; - apr_thread_mutex_unlock(me->lock); - apr_thread_mutex_lock(me->cond_lock); if (wait >= 0) { - rv = apr_thread_cond_timedwait(me->cond, me->cond_lock, wait); + rv = apr_thread_cond_timedwait(me->cond, me->lock, wait); } else { - rv = apr_thread_cond_wait(me->cond, me->cond_lock); + rv = apr_thread_cond_wait(me->cond, me->lock); } - apr_thread_mutex_unlock(me->cond_lock); - apr_thread_mutex_lock(me->lock); } /* idle thread been asked to stop, will be joined */ @@ -350,7 +337,6 @@ static apr_status_t thread_pool_cleanup(void *me) apr_sleep(20 * 1000); /* spin lock with 20 ms */ } apr_thread_mutex_destroy(_self->lock); - apr_thread_mutex_destroy(_self->cond_lock); apr_thread_cond_destroy(_self->cond); return APR_SUCCESS; } @@ -362,33 +348,42 @@ APU_DECLARE(apr_status_t) apr_thread_pool_create(apr_thread_pool_t ** me, { apr_thread_t *t; apr_status_t rv = APR_SUCCESS; + apr_thread_pool_t *tp; - *me = apr_pcalloc(pool, sizeof(**me)); - if (!*me) { - return APR_ENOMEM; - } + *me = NULL; + tp = apr_pcalloc(pool, sizeof(apr_thread_pool_t)); - (*me)->pool = pool; + tp->pool = pool; - rv = thread_pool_construct(*me, init_threads, max_threads); + rv = thread_pool_construct(tp, init_threads, max_threads); if (APR_SUCCESS != rv) { - *me = NULL; return rv; } - apr_pool_cleanup_register(pool, *me, thread_pool_cleanup, + apr_pool_cleanup_register(pool, tp, thread_pool_cleanup, apr_pool_cleanup_null); while (init_threads) { - rv = apr_thread_create(&t, NULL, thread_pool_func, *me, (*me)->pool); + /* Grab the mutex as apr_thread_create() and thread_pool_func() will + * allocate from (*me)->pool. This is dangerous if there are multiple + * initial threads to create. + */ + apr_thread_mutex_lock(tp->lock); + rv = apr_thread_create(&t, NULL, thread_pool_func, tp, tp->pool); + apr_thread_mutex_unlock(tp->lock); if (APR_SUCCESS != rv) { break; } - ++(*me)->thd_cnt; - if ((*me)->thd_cnt > (*me)->thd_high) - (*me)->thd_high = (*me)->thd_cnt; + tp->thd_cnt++; + if (tp->thd_cnt > tp->thd_high) { + tp->thd_high = tp->thd_cnt; + } --init_threads; } + if (rv == APR_SUCCESS) { + *me = tp; + } + return rv; } @@ -521,10 +516,8 @@ static apr_status_t schedule_task(apr_thread_pool_t *me, me->thd_high = me->thd_cnt; } } - apr_thread_mutex_unlock(me->lock); - apr_thread_mutex_lock(me->cond_lock); apr_thread_cond_signal(me->cond); - apr_thread_mutex_unlock(me->cond_lock); + apr_thread_mutex_unlock(me->lock); return rv; } @@ -576,11 +569,9 @@ static apr_status_t add_task(apr_thread_pool_t *me, apr_thread_start_t func, me->thd_high = me->thd_cnt; } } - apr_thread_mutex_unlock(me->lock); - apr_thread_mutex_lock(me->cond_lock); apr_thread_cond_signal(me->cond); - apr_thread_mutex_unlock(me->cond_lock); + apr_thread_mutex_unlock(me->lock); return rv; } @@ -838,9 +829,9 @@ static apr_size_t trim_idle_threads(apr_thread_pool_t *me, apr_size_t cnt) elt = trim_threads(me, &cnt, 1); - apr_thread_mutex_lock(me->cond_lock); + apr_thread_mutex_lock(me->lock); apr_thread_cond_broadcast(me->cond); - apr_thread_mutex_unlock(me->cond_lock); + apr_thread_mutex_unlock(me->lock); n_dbg = 0; if (NULL != (head = elt)) { |