1 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include "apr_reslist.h"
21 #include "apr_errno.h"
22 #include "apr_strings.h"
23 #include "apr_thread_mutex.h"
24 #include "apr_thread_cond.h"
30 * A single resource element.
35 APR_RING_ENTRY(apr_res_t) link;
37 typedef struct apr_res_t apr_res_t;
40 * A ring of resources representing the list of available resources.
42 APR_RING_HEAD(apr_resring_t, apr_res_t);
43 typedef struct apr_resring_t apr_resring_t;
45 struct apr_reslist_t {
46 apr_pool_t *pool; /* the pool used in constructor and destructor calls */
47 int ntotal; /* total number of resources managed by this list */
48 int nidle; /* number of available resources */
49 int min; /* desired minimum number of available resources */
50 int smax; /* soft maximum on the total number of resources */
51 int hmax; /* hard maximum on the total number of resources */
52 apr_interval_time_t ttl; /* TTL when we have too many resources */
53 apr_interval_time_t timeout; /* Timeout for waiting on resource */
54 apr_reslist_constructor constructor;
55 apr_reslist_destructor destructor;
56 void *params; /* opaque data passed to constructor and destructor calls */
57 apr_resring_t avail_list;
58 apr_resring_t free_list;
59 apr_thread_mutex_t *listlock;
60 apr_thread_cond_t *avail;
64 * Grab a resource from the front of the resource list.
65 * Assumes: that the reslist is locked.
67 static apr_res_t *pop_resource(apr_reslist_t *reslist)
70 res = APR_RING_FIRST(&reslist->avail_list);
71 APR_RING_REMOVE(res, link);
77 * Add a resource to the end of the list, set the time at which
78 * it was added to the list.
79 * Assumes: that the reslist is locked.
81 static void push_resource(apr_reslist_t *reslist, apr_res_t *resource)
83 APR_RING_INSERT_TAIL(&reslist->avail_list, resource, apr_res_t, link);
84 resource->freed = apr_time_now();
89 * Get an empty resource container from the free list.
91 static apr_res_t *get_container(apr_reslist_t *reslist)
95 assert(!APR_RING_EMPTY(&reslist->free_list, apr_res_t, link));
97 res = APR_RING_FIRST(&reslist->free_list);
98 APR_RING_REMOVE(res, link);
104 * Free up a resource container by placing it on the free list.
106 static void free_container(apr_reslist_t *reslist, apr_res_t *container)
108 APR_RING_INSERT_TAIL(&reslist->free_list, container, apr_res_t, link);
112 * Create a new resource and return it.
113 * Assumes: that the reslist is locked.
115 static apr_status_t create_resource(apr_reslist_t *reslist, apr_res_t **ret_res)
120 res = apr_pcalloc(reslist->pool, sizeof(*res));
122 rv = reslist->constructor(&res->opaque, reslist->params, reslist->pool);
129 * Destroy a single idle resource.
130 * Assumes: that the reslist is locked.
132 static apr_status_t destroy_resource(apr_reslist_t *reslist, apr_res_t *res)
134 return reslist->destructor(res->opaque, reslist->params, reslist->pool);
137 static apr_status_t reslist_cleanup(void *data_)
140 apr_reslist_t *rl = data_;
143 apr_thread_mutex_lock(rl->listlock);
145 while (rl->nidle > 0) {
146 res = pop_resource(rl);
148 rv = destroy_resource(rl, res);
149 if (rv != APR_SUCCESS) {
152 free_container(rl, res);
155 assert(rl->nidle == 0);
156 assert(rl->ntotal == 0);
158 apr_thread_mutex_destroy(rl->listlock);
159 apr_thread_cond_destroy(rl->avail);
165 * Perform routine maintenance on the resource list. This call
166 * may instantiate new resources or expire old resources.
168 static apr_status_t reslist_maint(apr_reslist_t *reslist)
175 apr_thread_mutex_lock(reslist->listlock);
177 /* Check if we need to create more resources, and if we are allowed to. */
178 while (reslist->nidle < reslist->min && reslist->ntotal <= reslist->hmax) {
179 /* Create the resource */
180 rv = create_resource(reslist, &res);
181 if (rv != APR_SUCCESS) {
182 free_container(reslist, res);
183 apr_thread_mutex_unlock(reslist->listlock);
186 /* Add it to the list */
187 push_resource(reslist, res);
188 /* Update our counters */
190 /* If someone is waiting on that guy, wake them up. */
191 rv = apr_thread_cond_signal(reslist->avail);
192 if (rv != APR_SUCCESS) {
193 apr_thread_mutex_unlock(reslist->listlock);
199 /* We don't need to see if we're over the max if we were under it before */
201 apr_thread_mutex_unlock(reslist->listlock);
205 /* Check if we need to expire old resources */
206 now = apr_time_now();
207 while (reslist->nidle > reslist->smax && reslist->nidle > 0) {
208 /* Peak at the first resource in the list */
209 res = APR_RING_FIRST(&reslist->avail_list);
210 /* See if the oldest entry should be expired */
211 if (now - res->freed < reslist->ttl) {
212 /* If this entry is too young, none of the others
213 * will be ready to be expired either, so we are done. */
216 res = pop_resource(reslist);
218 rv = destroy_resource(reslist, res);
219 if (rv != APR_SUCCESS) {
220 apr_thread_mutex_unlock(reslist->listlock);
223 free_container(reslist, res);
226 apr_thread_mutex_unlock(reslist->listlock);
230 APU_DECLARE(apr_status_t) apr_reslist_create(apr_reslist_t **reslist,
231 int min, int smax, int hmax,
232 apr_interval_time_t ttl,
233 apr_reslist_constructor con,
234 apr_reslist_destructor de,
241 /* Do some sanity checks so we don't thrash around in the
242 * maintenance routine later. */
243 if (min > smax || min > hmax || smax > hmax || ttl < 0) {
247 rl = apr_pcalloc(pool, sizeof(*rl));
253 rl->constructor = con;
257 APR_RING_INIT(&rl->avail_list, apr_res_t, link);
258 APR_RING_INIT(&rl->free_list, apr_res_t, link);
260 rv = apr_thread_mutex_create(&rl->listlock, APR_THREAD_MUTEX_DEFAULT,
262 if (rv != APR_SUCCESS) {
265 rv = apr_thread_cond_create(&rl->avail, pool);
266 if (rv != APR_SUCCESS) {
270 rv = reslist_maint(rl);
271 if (rv != APR_SUCCESS) {
275 apr_pool_cleanup_register(rl->pool, rl, reslist_cleanup,
276 apr_pool_cleanup_null);
283 APU_DECLARE(apr_status_t) apr_reslist_destroy(apr_reslist_t *reslist)
285 return apr_pool_cleanup_run(reslist->pool, reslist, reslist_cleanup);
288 APU_DECLARE(apr_status_t) apr_reslist_acquire(apr_reslist_t *reslist,
294 apr_thread_mutex_lock(reslist->listlock);
295 /* If there are idle resources on the available list, use
296 * them right away. */
297 if (reslist->nidle > 0) {
298 /* Pop off the first resource */
299 res = pop_resource(reslist);
300 *resource = res->opaque;
301 free_container(reslist, res);
302 apr_thread_mutex_unlock(reslist->listlock);
305 /* If we've hit our max, block until we're allowed to create
306 * a new one, or something becomes free. */
307 else while (reslist->ntotal >= reslist->hmax
308 && reslist->nidle <= 0) {
309 if (reslist->timeout) {
310 if ((rv = apr_thread_cond_timedwait(reslist->avail,
311 reslist->listlock, reslist->timeout)) != APR_SUCCESS) {
312 apr_thread_mutex_unlock(reslist->listlock);
317 apr_thread_cond_wait(reslist->avail, reslist->listlock);
319 /* If we popped out of the loop, first try to see if there
320 * are new resources available for immediate use. */
321 if (reslist->nidle > 0) {
322 res = pop_resource(reslist);
323 *resource = res->opaque;
324 free_container(reslist, res);
325 apr_thread_mutex_unlock(reslist->listlock);
328 /* Otherwise the reason we dropped out of the loop
329 * was because there is a new slot available, so create
330 * a resource to fill the slot and use it. */
332 rv = create_resource(reslist, &res);
333 if (rv == APR_SUCCESS) {
335 *resource = res->opaque;
337 free_container(reslist, res);
338 apr_thread_mutex_unlock(reslist->listlock);
343 APU_DECLARE(apr_status_t) apr_reslist_release(apr_reslist_t *reslist,
348 apr_thread_mutex_lock(reslist->listlock);
349 res = get_container(reslist);
350 res->opaque = resource;
351 push_resource(reslist, res);
352 apr_thread_cond_signal(reslist->avail);
353 apr_thread_mutex_unlock(reslist->listlock);
355 return reslist_maint(reslist);
358 APU_DECLARE(void) apr_reslist_timeout_set(apr_reslist_t *reslist,
359 apr_interval_time_t timeout)
361 reslist->timeout = timeout;
364 APU_DECLARE(apr_status_t) apr_reslist_invalidate(apr_reslist_t *reslist,
368 apr_thread_mutex_lock(reslist->listlock);
369 ret = reslist->destructor(resource, reslist->params, reslist->pool);
371 apr_thread_mutex_unlock(reslist->listlock);
375 #endif /* APR_HAS_THREADS */