upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr-util / misc / apr_reslist.c
1 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
2  * applicable.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <assert.h>
18
19 #include "apu.h"
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"
25 #include "apr_ring.h"
26
27 #if APR_HAS_THREADS
28
29 /**
30  * A single resource element.
31  */
32 struct apr_res_t {
33     apr_time_t freed;
34     void *opaque;
35     APR_RING_ENTRY(apr_res_t) link;
36 };
37 typedef struct apr_res_t apr_res_t;
38
39 /**
40  * A ring of resources representing the list of available resources.
41  */
42 APR_RING_HEAD(apr_resring_t, apr_res_t);
43 typedef struct apr_resring_t apr_resring_t;
44
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;
61 };
62
63 /**
64  * Grab a resource from the front of the resource list.
65  * Assumes: that the reslist is locked.
66  */
67 static apr_res_t *pop_resource(apr_reslist_t *reslist)
68 {
69     apr_res_t *res;
70     res = APR_RING_FIRST(&reslist->avail_list);
71     APR_RING_REMOVE(res, link);
72     reslist->nidle--;
73     return res;
74 }
75
76 /**
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.
80  */
81 static void push_resource(apr_reslist_t *reslist, apr_res_t *resource)
82 {
83     APR_RING_INSERT_TAIL(&reslist->avail_list, resource, apr_res_t, link);
84     resource->freed = apr_time_now();
85     reslist->nidle++;
86 }
87
88 /**
89  * Get an empty resource container from the free list.
90  */
91 static apr_res_t *get_container(apr_reslist_t *reslist)
92 {
93     apr_res_t *res;
94
95     assert(!APR_RING_EMPTY(&reslist->free_list, apr_res_t, link));
96
97     res = APR_RING_FIRST(&reslist->free_list);
98     APR_RING_REMOVE(res, link);
99
100     return res;
101 }
102
103 /**
104  * Free up a resource container by placing it on the free list.
105  */
106 static void free_container(apr_reslist_t *reslist, apr_res_t *container)
107 {
108     APR_RING_INSERT_TAIL(&reslist->free_list, container, apr_res_t, link);
109 }
110
111 /**
112  * Create a new resource and return it.
113  * Assumes: that the reslist is locked.
114  */
115 static apr_status_t create_resource(apr_reslist_t *reslist, apr_res_t **ret_res)
116 {
117     apr_status_t rv;
118     apr_res_t *res;
119
120     res = apr_pcalloc(reslist->pool, sizeof(*res));
121
122     rv = reslist->constructor(&res->opaque, reslist->params, reslist->pool);
123
124     *ret_res = res;
125     return rv;
126 }
127
128 /**
129  * Destroy a single idle resource.
130  * Assumes: that the reslist is locked.
131  */
132 static apr_status_t destroy_resource(apr_reslist_t *reslist, apr_res_t *res)
133 {
134     return reslist->destructor(res->opaque, reslist->params, reslist->pool);
135 }
136
137 static apr_status_t reslist_cleanup(void *data_)
138 {
139     apr_status_t rv;
140     apr_reslist_t *rl = data_;
141     apr_res_t *res;
142
143     apr_thread_mutex_lock(rl->listlock);
144
145     while (rl->nidle > 0) {
146         res = pop_resource(rl);
147         rl->ntotal--;
148         rv = destroy_resource(rl, res);
149         if (rv != APR_SUCCESS) {
150             return rv;
151         }
152         free_container(rl, res);
153     }
154
155     assert(rl->nidle == 0);
156     assert(rl->ntotal == 0);
157
158     apr_thread_mutex_destroy(rl->listlock);
159     apr_thread_cond_destroy(rl->avail);
160
161     return APR_SUCCESS;
162 }
163
164 /**
165  * Perform routine maintenance on the resource list. This call
166  * may instantiate new resources or expire old resources.
167  */
168 static apr_status_t reslist_maint(apr_reslist_t *reslist)
169 {
170     apr_time_t now;
171     apr_status_t rv;
172     apr_res_t *res;
173     int created_one = 0;
174
175     apr_thread_mutex_lock(reslist->listlock);
176
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);
184             return rv;
185         }
186         /* Add it to the list */
187         push_resource(reslist, res);
188         /* Update our counters */
189         reslist->ntotal++;
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);
194             return rv;
195         }
196         created_one++;
197     }
198
199     /* We don't need to see if we're over the max if we were under it before */
200     if (created_one) {
201         apr_thread_mutex_unlock(reslist->listlock);
202         return APR_SUCCESS;
203     }
204
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. */
214             break;
215         }
216         res = pop_resource(reslist);
217         reslist->ntotal--;
218         rv = destroy_resource(reslist, res);
219         if (rv != APR_SUCCESS) {
220             apr_thread_mutex_unlock(reslist->listlock);
221             return rv;
222         }
223         free_container(reslist, res);
224     }
225
226     apr_thread_mutex_unlock(reslist->listlock);
227     return APR_SUCCESS;
228 }
229
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,
235                                              void *params,
236                                              apr_pool_t *pool)
237 {
238     apr_status_t rv;
239     apr_reslist_t *rl;
240
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) {
244         return APR_EINVAL;
245     }
246
247     rl = apr_pcalloc(pool, sizeof(*rl));
248     rl->pool = pool;
249     rl->min = min;
250     rl->smax = smax;
251     rl->hmax = hmax;
252     rl->ttl = ttl;
253     rl->constructor = con;
254     rl->destructor = de;
255     rl->params = params;
256
257     APR_RING_INIT(&rl->avail_list, apr_res_t, link);
258     APR_RING_INIT(&rl->free_list, apr_res_t, link);
259
260     rv = apr_thread_mutex_create(&rl->listlock, APR_THREAD_MUTEX_DEFAULT,
261                                  pool);
262     if (rv != APR_SUCCESS) {
263         return rv;
264     }
265     rv = apr_thread_cond_create(&rl->avail, pool);
266     if (rv != APR_SUCCESS) {
267         return rv;
268     }
269
270     rv = reslist_maint(rl);
271     if (rv != APR_SUCCESS) {
272         return rv;
273     }
274
275     apr_pool_cleanup_register(rl->pool, rl, reslist_cleanup,
276                               apr_pool_cleanup_null);
277
278     *reslist = rl;
279
280     return APR_SUCCESS;
281 }
282
283 APU_DECLARE(apr_status_t) apr_reslist_destroy(apr_reslist_t *reslist)
284 {
285     return apr_pool_cleanup_run(reslist->pool, reslist, reslist_cleanup);
286 }
287
288 APU_DECLARE(apr_status_t) apr_reslist_acquire(apr_reslist_t *reslist,
289                                               void **resource)
290 {
291     apr_status_t rv;
292     apr_res_t *res;
293
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);
303         return APR_SUCCESS;
304     }
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);
313                 return rv;
314             }
315         }
316         else
317             apr_thread_cond_wait(reslist->avail, reslist->listlock);
318     }
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);
326         return APR_SUCCESS;
327     }
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. */
331     else {
332         rv = create_resource(reslist, &res);
333         if (rv == APR_SUCCESS) {
334             reslist->ntotal++;
335             *resource = res->opaque;
336         }
337         free_container(reslist, res);
338         apr_thread_mutex_unlock(reslist->listlock);
339         return rv;
340     }
341 }
342
343 APU_DECLARE(apr_status_t) apr_reslist_release(apr_reslist_t *reslist,
344                                               void *resource)
345 {
346     apr_res_t *res;
347
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);
354
355     return reslist_maint(reslist);
356 }
357
358 APU_DECLARE(void) apr_reslist_timeout_set(apr_reslist_t *reslist,
359                                           apr_interval_time_t timeout)
360 {
361     reslist->timeout = timeout;
362 }
363
364 APU_DECLARE(apr_status_t) apr_reslist_invalidate(apr_reslist_t *reslist,
365                                                  void *resource)
366 {
367     apr_status_t ret;
368     apr_thread_mutex_lock(reslist->listlock);
369     ret = reslist->destructor(resource, reslist->params, reslist->pool);
370     reslist->ntotal--;
371     apr_thread_mutex_unlock(reslist->listlock);
372     return ret;
373 }
374
375 #endif  /* APR_HAS_THREADS */