diff options
author | bojan <bojan@13f79535-47bb-0310-9956-ffa450edef68> | 2008-07-28 21:39:20 +0000 |
---|---|---|
committer | bojan <bojan@13f79535-47bb-0310-9956-ffa450edef68> | 2008-07-28 21:39:20 +0000 |
commit | bea619f8f0f1f7f9d94f30cc07f2979587009e5a (patch) | |
tree | 8d9e5729362c611c32e14e7a9d05db50475916f4 | |
parent | f176858ce0ed2ea55701303e5c0c57dcef3fc9c1 (diff) | |
download | libapr-util-bea619f8f0f1f7f9d94f30cc07f2979587009e5a.tar.gz |
Implement resource list when threads are unavailable.
git-svn-id: http://svn.apache.org/repos/asf/apr/apr-util/trunk@680514 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | CHANGES | 3 | ||||
-rw-r--r-- | include/apr_reslist.h | 7 | ||||
-rw-r--r-- | misc/apr_reslist.c | 61 |
3 files changed, 63 insertions, 8 deletions
@@ -26,6 +26,9 @@ Changes with APR-util 1.4.0 *) Give MySQL DBD driver reconnect option. PR 45407 [Bojan Smojver] + *) Implement resource list when threads are unavailable. + [Bojan Smojver] + Changes with APR-util 1.3.0 *) apr_reslist: destroy all resources in apr_cleanup. diff --git a/include/apr_reslist.h b/include/apr_reslist.h index c4f81259..51c545b3 100644 --- a/include/apr_reslist.h +++ b/include/apr_reslist.h @@ -28,8 +28,6 @@ #include "apr_errno.h" #include "apr_time.h" -#if APR_HAS_THREADS - /** * @defgroup APR_Util_RL Resource List Routines * @ingroup APR_Util @@ -78,6 +76,9 @@ typedef apr_status_t (*apr_reslist_destructor)(void *resource, void *params, * @param pool The pool from which to create this resoure list. Also the * same pool that is passed to the constructor and destructor * routines. + * @remark If APR has been compiled without thread support, hmax will be + * automatically set to 1 and values of min and smax will be forced to + * 1 for any non-zero value. */ APU_DECLARE(apr_status_t) apr_reslist_create(apr_reslist_t **reslist, int min, int smax, int hmax, @@ -144,6 +145,4 @@ APU_DECLARE(apr_status_t) apr_reslist_invalidate(apr_reslist_t *reslist, /** @} */ -#endif /* APR_HAS_THREADS */ - #endif /* ! APR_RESLIST_H */ diff --git a/misc/apr_reslist.c b/misc/apr_reslist.c index a3a68a59..19871203 100644 --- a/misc/apr_reslist.c +++ b/misc/apr_reslist.c @@ -24,8 +24,6 @@ #include "apr_thread_cond.h" #include "apr_ring.h" -#if APR_HAS_THREADS - /** * A single resource element. */ @@ -56,8 +54,10 @@ struct apr_reslist_t { void *params; /* opaque data passed to constructor and destructor calls */ apr_resring_t avail_list; apr_resring_t free_list; +#if APR_HAS_THREADS apr_thread_mutex_t *listlock; apr_thread_cond_t *avail; +#endif }; /** @@ -141,7 +141,9 @@ static apr_status_t reslist_cleanup(void *data_) apr_reslist_t *rl = data_; apr_res_t *res; +#if APR_HAS_THREADS apr_thread_mutex_lock(rl->listlock); +#endif while (rl->nidle > 0) { apr_status_t rv1; @@ -158,9 +160,11 @@ static apr_status_t reslist_cleanup(void *data_) assert(rl->nidle == 0); assert(rl->ntotal == 0); +#if APR_HAS_THREADS apr_thread_mutex_unlock(rl->listlock); apr_thread_mutex_destroy(rl->listlock); apr_thread_cond_destroy(rl->avail); +#endif return rv; } @@ -176,7 +180,9 @@ static apr_status_t reslist_maint(apr_reslist_t *reslist) apr_res_t *res; int created_one = 0; +#if APR_HAS_THREADS apr_thread_mutex_lock(reslist->listlock); +#endif /* Check if we need to create more resources, and if we are allowed to. */ while (reslist->nidle < reslist->min && reslist->ntotal < reslist->hmax) { @@ -184,7 +190,9 @@ static apr_status_t reslist_maint(apr_reslist_t *reslist) rv = create_resource(reslist, &res); if (rv != APR_SUCCESS) { free_container(reslist, res); +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return rv; } /* Add it to the list */ @@ -192,17 +200,21 @@ static apr_status_t reslist_maint(apr_reslist_t *reslist) /* Update our counters */ reslist->ntotal++; /* If someone is waiting on that guy, wake them up. */ +#if APR_HAS_THREADS rv = apr_thread_cond_signal(reslist->avail); if (rv != APR_SUCCESS) { apr_thread_mutex_unlock(reslist->listlock); return rv; } +#endif created_one++; } /* We don't need to see if we're over the max if we were under it before */ if (created_one) { +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return APR_SUCCESS; } @@ -223,12 +235,16 @@ static apr_status_t reslist_maint(apr_reslist_t *reslist) rv = destroy_resource(reslist, res); free_container(reslist, res); if (rv != APR_SUCCESS) { +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return rv; } } +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return APR_SUCCESS; } @@ -250,6 +266,17 @@ APU_DECLARE(apr_status_t) apr_reslist_create(apr_reslist_t **reslist, return APR_EINVAL; } +#if !APR_HAS_THREADS + /* There can be only one resource when we have no threads. */ + if (min > 0) { + min = 1; + } + if (smax > 0) { + smax = 1; + } + hmax = 1; +#endif + rl = apr_pcalloc(pool, sizeof(*rl)); rl->pool = pool; rl->min = min; @@ -263,6 +290,7 @@ APU_DECLARE(apr_status_t) apr_reslist_create(apr_reslist_t **reslist, APR_RING_INIT(&rl->avail_list, apr_res_t, link); APR_RING_INIT(&rl->free_list, apr_res_t, link); +#if APR_HAS_THREADS rv = apr_thread_mutex_create(&rl->listlock, APR_THREAD_MUTEX_DEFAULT, pool); if (rv != APR_SUCCESS) { @@ -272,6 +300,7 @@ APU_DECLARE(apr_status_t) apr_reslist_create(apr_reslist_t **reslist, if (rv != APR_SUCCESS) { return rv; } +#endif rv = reslist_maint(rl); if (rv != APR_SUCCESS) { @@ -304,7 +333,9 @@ APU_DECLARE(apr_status_t) apr_reslist_acquire(apr_reslist_t *reslist, apr_res_t *res; apr_time_t now; +#if APR_HAS_THREADS apr_thread_mutex_lock(reslist->listlock); +#endif /* If there are idle resources on the available list, use * them right away. */ now = apr_time_now(); @@ -317,19 +348,24 @@ APU_DECLARE(apr_status_t) apr_reslist_acquire(apr_reslist_t *reslist, rv = destroy_resource(reslist, res); free_container(reslist, res); if (rv != APR_SUCCESS) { +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return rv; /* FIXME: this might cause unnecessary fails */ } continue; } *resource = res->opaque; free_container(reslist, res); +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return APR_SUCCESS; } /* If we've hit our max, block until we're allowed to create * a new one, or something becomes free. */ while (reslist->ntotal >= reslist->hmax && reslist->nidle <= 0) { +#if APR_HAS_THREADS if (reslist->timeout) { if ((rv = apr_thread_cond_timedwait(reslist->avail, reslist->listlock, reslist->timeout)) != APR_SUCCESS) { @@ -340,6 +376,9 @@ APU_DECLARE(apr_status_t) apr_reslist_acquire(apr_reslist_t *reslist, else { apr_thread_cond_wait(reslist->avail, reslist->listlock); } +#else + return APR_EAGAIN; +#endif } /* If we popped out of the loop, first try to see if there * are new resources available for immediate use. */ @@ -347,7 +386,9 @@ APU_DECLARE(apr_status_t) apr_reslist_acquire(apr_reslist_t *reslist, res = pop_resource(reslist); *resource = res->opaque; free_container(reslist, res); +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return APR_SUCCESS; } /* Otherwise the reason we dropped out of the loop @@ -360,7 +401,9 @@ APU_DECLARE(apr_status_t) apr_reslist_acquire(apr_reslist_t *reslist, *resource = res->opaque; } free_container(reslist, res); +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return rv; } } @@ -370,12 +413,16 @@ APU_DECLARE(apr_status_t) apr_reslist_release(apr_reslist_t *reslist, { apr_res_t *res; +#if APR_HAS_THREADS apr_thread_mutex_lock(reslist->listlock); +#endif res = get_container(reslist); res->opaque = resource; push_resource(reslist, res); +#if APR_HAS_THREADS apr_thread_cond_signal(reslist->avail); apr_thread_mutex_unlock(reslist->listlock); +#endif return reslist_maint(reslist); } @@ -390,9 +437,13 @@ APU_DECLARE(apr_uint32_t) apr_reslist_acquired_count(apr_reslist_t *reslist) { apr_uint32_t count; +#if APR_HAS_THREADS apr_thread_mutex_lock(reslist->listlock); +#endif count = reslist->ntotal - reslist->nidle; +#if APR_HAS_THREADS apr_thread_mutex_unlock(reslist->listlock); +#endif return count; } @@ -401,12 +452,14 @@ APU_DECLARE(apr_status_t) apr_reslist_invalidate(apr_reslist_t *reslist, void *resource) { apr_status_t ret; +#if APR_HAS_THREADS apr_thread_mutex_lock(reslist->listlock); +#endif ret = reslist->destructor(resource, reslist->params, reslist->pool); reslist->ntotal--; +#if APR_HAS_THREADS apr_thread_cond_signal(reslist->avail); apr_thread_mutex_unlock(reslist->listlock); +#endif return ret; } - -#endif /* APR_HAS_THREADS */ |