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 static apr_status_t
reslist_maint(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
= reslist_maint(rl
);
306 if (rv
!= APR_SUCCESS
) {
310 /* Register a pool pre_cleanup.
311 * This will ensure that reslist_cleanup is run BEFORE
312 * any of the eventual child pools of this pool.
313 * If an child pool was created inside apr_reslist_destructor,
314 * this child pool can safely call apr_pool_destroy inside
315 * apr_reslist_destructor thus safely detaching himself.
317 apr_pool_pre_cleanup_register(rl
->pool
, rl
, reslist_cleanup
);
324 APU_DECLARE(apr_status_t
) apr_reslist_destroy(apr_reslist_t
*reslist
)
326 return apr_pool_cleanup_run(reslist
->pool
, reslist
, reslist_cleanup
);
329 APU_DECLARE(apr_status_t
) apr_reslist_acquire(apr_reslist_t
*reslist
,
337 apr_thread_mutex_lock(reslist
->listlock
);
339 /* If there are idle resources on the available list, use
340 * them right away. */
341 now
= apr_time_now();
342 while (reslist
->nidle
> 0) {
343 /* Pop off the first resource */
344 res
= pop_resource(reslist
);
345 if (reslist
->ttl
&& (now
- res
->freed
>= reslist
->ttl
)) {
346 /* this res is expired - kill it */
348 rv
= destroy_resource(reslist
, res
);
349 free_container(reslist
, res
);
350 if (rv
!= APR_SUCCESS
) {
352 apr_thread_mutex_unlock(reslist
->listlock
);
354 return rv
; /* FIXME: this might cause unnecessary fails */
358 *resource
= res
->opaque
;
359 free_container(reslist
, res
);
361 apr_thread_mutex_unlock(reslist
->listlock
);
365 /* If we've hit our max, block until we're allowed to create
366 * a new one, or something becomes free. */
367 while (reslist
->ntotal
>= reslist
->hmax
&& reslist
->nidle
<= 0) {
369 if (reslist
->timeout
) {
370 if ((rv
= apr_thread_cond_timedwait(reslist
->avail
,
371 reslist
->listlock
, reslist
->timeout
)) != APR_SUCCESS
) {
372 apr_thread_mutex_unlock(reslist
->listlock
);
377 apr_thread_cond_wait(reslist
->avail
, reslist
->listlock
);
383 /* If we popped out of the loop, first try to see if there
384 * are new resources available for immediate use. */
385 if (reslist
->nidle
> 0) {
386 res
= pop_resource(reslist
);
387 *resource
= res
->opaque
;
388 free_container(reslist
, res
);
390 apr_thread_mutex_unlock(reslist
->listlock
);
394 /* Otherwise the reason we dropped out of the loop
395 * was because there is a new slot available, so create
396 * a resource to fill the slot and use it. */
398 rv
= create_resource(reslist
, &res
);
399 if (rv
== APR_SUCCESS
) {
401 *resource
= res
->opaque
;
403 free_container(reslist
, res
);
405 apr_thread_mutex_unlock(reslist
->listlock
);
411 APU_DECLARE(apr_status_t
) apr_reslist_release(apr_reslist_t
*reslist
,
417 apr_thread_mutex_lock(reslist
->listlock
);
419 res
= get_container(reslist
);
420 res
->opaque
= resource
;
421 push_resource(reslist
, res
);
423 apr_thread_cond_signal(reslist
->avail
);
424 apr_thread_mutex_unlock(reslist
->listlock
);
427 return reslist_maint(reslist
);
430 APU_DECLARE(void) apr_reslist_timeout_set(apr_reslist_t
*reslist
,
431 apr_interval_time_t timeout
)
433 reslist
->timeout
= timeout
;
436 APU_DECLARE(apr_uint32_t
) apr_reslist_acquired_count(apr_reslist_t
*reslist
)
441 apr_thread_mutex_lock(reslist
->listlock
);
443 count
= reslist
->ntotal
- reslist
->nidle
;
445 apr_thread_mutex_unlock(reslist
->listlock
);
451 APU_DECLARE(apr_status_t
) apr_reslist_invalidate(apr_reslist_t
*reslist
,
456 ret
= reslist
->destructor(resource
, reslist
->params
, reslist
->pool
);
458 apr_thread_mutex_lock(reslist
->listlock
);
462 apr_thread_cond_signal(reslist
->avail
);
463 apr_thread_mutex_unlock(reslist
->listlock
);