1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. 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"
28 * A single resource element.
33 APR_RING_ENTRY(apr_res_t) link;
35 typedef struct apr_res_t apr_res_t;
38 * A ring of resources representing the list of available resources.
40 APR_RING_HEAD(apr_resring_t, apr_res_t);
41 typedef struct apr_resring_t apr_resring_t;
43 struct apr_reslist_t {
44 apr_pool_t *pool; /* the pool used in constructor and destructor calls */
45 int ntotal; /* total number of resources managed by this list */
46 int nidle; /* number of available resources */
47 int min; /* desired minimum number of available resources */
48 int smax; /* soft maximum on the total number of resources */
49 int hmax; /* hard maximum on the total number of resources */
50 apr_interval_time_t ttl; /* TTL when we have too many resources */
51 apr_interval_time_t timeout; /* Timeout for waiting on resource */
52 apr_reslist_constructor constructor;
53 apr_reslist_destructor destructor;
54 void *params; /* opaque data passed to constructor and destructor calls */
55 apr_resring_t avail_list;
56 apr_resring_t free_list;
58 apr_thread_mutex_t *listlock;
59 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 beginning 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_HEAD(&reslist->avail_list, resource, apr_res_t, link);
84 resource->freed = apr_time_now();
89 * Get an resource container from the free list or create a new one.
91 static apr_res_t *get_container(apr_reslist_t *reslist)
95 if (!APR_RING_EMPTY(&reslist->free_list, apr_res_t, link)) {
96 res = APR_RING_FIRST(&reslist->free_list);
97 APR_RING_REMOVE(res, link);
100 res = apr_pcalloc(reslist->pool, sizeof(*res));
105 * Free up a resource container by placing it on the free list.
107 static void free_container(apr_reslist_t *reslist, apr_res_t *container)
109 APR_RING_INSERT_TAIL(&reslist->free_list, container, apr_res_t, link);
113 * Create a new resource and return it.
114 * Assumes: that the reslist is locked.
116 static apr_status_t create_resource(apr_reslist_t *reslist, apr_res_t **ret_res)
121 res = get_container(reslist);
123 rv = reslist->constructor(&res->opaque, reslist->params, reslist->pool);
130 * Destroy a single idle resource.
131 * Assumes: that the reslist is locked.
133 static apr_status_t destroy_resource(apr_reslist_t *reslist, apr_res_t *res)
135 return reslist->destructor(res->opaque, reslist->params, reslist->pool);
138 static apr_status_t reslist_cleanup(void *data_)
140 apr_status_t rv = APR_SUCCESS;
141 apr_reslist_t *rl = data_;
145 apr_thread_mutex_lock(rl->listlock);
148 while (rl->nidle > 0) {
150 res = pop_resource(rl);
152 rv1 = destroy_resource(rl, res);
153 if (rv1 != APR_SUCCESS) {
154 rv = rv1; /* loses info in the unlikely event of
155 * multiple *different* failures */
157 free_container(rl, res);
160 assert(rl->nidle == 0);
161 assert(rl->ntotal == 0);
164 apr_thread_mutex_unlock(rl->listlock);
165 apr_thread_mutex_destroy(rl->listlock);
166 apr_thread_cond_destroy(rl->avail);
173 * Perform routine maintenance on the resource list. This call
174 * may instantiate new resources or expire old resources.
176 APU_DECLARE(apr_status_t) apr_reslist_maintain(apr_reslist_t *reslist)
184 apr_thread_mutex_lock(reslist->listlock);
187 /* Check if we need to create more resources, and if we are allowed to. */
188 while (reslist->nidle < reslist->min && reslist->ntotal < reslist->hmax) {
189 /* Create the resource */
190 rv = create_resource(reslist, &res);
191 if (rv != APR_SUCCESS) {
192 free_container(reslist, res);
194 apr_thread_mutex_unlock(reslist->listlock);
198 /* Add it to the list */
199 push_resource(reslist, res);
200 /* Update our counters */
202 /* If someone is waiting on that guy, wake them up. */
204 rv = apr_thread_cond_signal(reslist->avail);
205 if (rv != APR_SUCCESS) {
206 apr_thread_mutex_unlock(reslist->listlock);
213 /* We don't need to see if we're over the max if we were under it before */
216 apr_thread_mutex_unlock(reslist->listlock);
221 /* Check if we need to expire old resources */
222 now = apr_time_now();
223 while (reslist->nidle > reslist->smax && reslist->nidle > 0) {
224 /* Peak at the last resource in the list */
225 res = APR_RING_LAST(&reslist->avail_list);
226 /* See if the oldest entry should be expired */
227 if (now - res->freed < reslist->ttl) {
228 /* If this entry is too young, none of the others
229 * will be ready to be expired either, so we are done. */
232 APR_RING_REMOVE(res, link);
235 rv = destroy_resource(reslist, res);
236 free_container(reslist, res);
237 if (rv != APR_SUCCESS) {
239 apr_thread_mutex_unlock(reslist->listlock);
246 apr_thread_mutex_unlock(reslist->listlock);
251 APU_DECLARE(apr_status_t) apr_reslist_create(apr_reslist_t **reslist,
252 int min, int smax, int hmax,
253 apr_interval_time_t ttl,
254 apr_reslist_constructor con,
255 apr_reslist_destructor de,
262 /* Do some sanity checks so we don't thrash around in the
263 * maintenance routine later. */
264 if (min < 0 || min > smax || min > hmax || smax > hmax || hmax == 0 ||
270 /* There can be only one resource when we have no threads. */
280 rl = apr_pcalloc(pool, sizeof(*rl));
286 rl->constructor = con;
290 APR_RING_INIT(&rl->avail_list, apr_res_t, link);
291 APR_RING_INIT(&rl->free_list, apr_res_t, link);
294 rv = apr_thread_mutex_create(&rl->listlock, APR_THREAD_MUTEX_DEFAULT,
296 if (rv != APR_SUCCESS) {
299 rv = apr_thread_cond_create(&rl->avail, pool);
300 if (rv != APR_SUCCESS) {
305 rv = apr_reslist_maintain(rl);
306 if (rv != APR_SUCCESS) {
307 /* Destroy what we've created so far.
313 apr_pool_cleanup_register(rl->pool, rl, reslist_cleanup,
314 apr_pool_cleanup_null);
321 APU_DECLARE(apr_status_t) apr_reslist_destroy(apr_reslist_t *reslist)
323 return apr_pool_cleanup_run(reslist->pool, reslist, reslist_cleanup);
326 APU_DECLARE(apr_status_t) apr_reslist_acquire(apr_reslist_t *reslist,
334 apr_thread_mutex_lock(reslist->listlock);
336 /* If there are idle resources on the available list, use
337 * them right away. */
338 now = apr_time_now();
339 while (reslist->nidle > 0) {
340 /* Pop off the first resource */
341 res = pop_resource(reslist);
342 if (reslist->ttl && (now - res->freed >= reslist->ttl)) {
343 /* this res is expired - kill it */
345 rv = destroy_resource(reslist, res);
346 free_container(reslist, res);
347 if (rv != APR_SUCCESS) {
349 apr_thread_mutex_unlock(reslist->listlock);
351 return rv; /* FIXME: this might cause unnecessary fails */
355 *resource = res->opaque;
356 free_container(reslist, res);
358 apr_thread_mutex_unlock(reslist->listlock);
362 /* If we've hit our max, block until we're allowed to create
363 * a new one, or something becomes free. */
364 while (reslist->ntotal >= reslist->hmax && reslist->nidle <= 0) {
366 if (reslist->timeout) {
367 if ((rv = apr_thread_cond_timedwait(reslist->avail,
368 reslist->listlock, reslist->timeout)) != APR_SUCCESS) {
369 apr_thread_mutex_unlock(reslist->listlock);
374 apr_thread_cond_wait(reslist->avail, reslist->listlock);
380 /* If we popped out of the loop, first try to see if there
381 * are new resources available for immediate use. */
382 if (reslist->nidle > 0) {
383 res = pop_resource(reslist);
384 *resource = res->opaque;
385 free_container(reslist, res);
387 apr_thread_mutex_unlock(reslist->listlock);
391 /* Otherwise the reason we dropped out of the loop
392 * was because there is a new slot available, so create
393 * a resource to fill the slot and use it. */
395 rv = create_resource(reslist, &res);
396 if (rv == APR_SUCCESS) {
398 *resource = res->opaque;
400 free_container(reslist, res);
402 apr_thread_mutex_unlock(reslist->listlock);
408 APU_DECLARE(apr_status_t) apr_reslist_release(apr_reslist_t *reslist,
414 apr_thread_mutex_lock(reslist->listlock);
416 res = get_container(reslist);
417 res->opaque = resource;
418 push_resource(reslist, res);
420 apr_thread_cond_signal(reslist->avail);
421 apr_thread_mutex_unlock(reslist->listlock);
424 return apr_reslist_maintain(reslist);
427 APU_DECLARE(void) apr_reslist_timeout_set(apr_reslist_t *reslist,
428 apr_interval_time_t timeout)
430 reslist->timeout = timeout;
433 APU_DECLARE(apr_uint32_t) apr_reslist_acquired_count(apr_reslist_t *reslist)
438 apr_thread_mutex_lock(reslist->listlock);
440 count = reslist->ntotal - reslist->nidle;
442 apr_thread_mutex_unlock(reslist->listlock);
448 APU_DECLARE(apr_status_t) apr_reslist_invalidate(apr_reslist_t *reslist,
453 apr_thread_mutex_lock(reslist->listlock);
455 ret = reslist->destructor(resource, reslist->params, reslist->pool);
458 apr_thread_cond_signal(reslist->avail);
459 apr_thread_mutex_unlock(reslist->listlock);
464 APU_DECLARE(void) apr_reslist_cleanup_order_set(apr_reslist_t *rl,
467 apr_pool_cleanup_kill(rl->pool, rl, reslist_cleanup);
468 if (mode == APR_RESLIST_CLEANUP_FIRST)
469 apr_pool_pre_cleanup_register(rl->pool, rl, reslist_cleanup);
471 apr_pool_cleanup_register(rl->pool, rl, reslist_cleanup,
472 apr_pool_cleanup_null);