~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to server/mpm/experimental/event/fdqueue.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
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 "fdqueue.h"
 
18
#include "apr_atomic.h"
 
19
 
 
20
typedef struct recycled_pool
 
21
{
 
22
    apr_pool_t *pool;
 
23
    struct recycled_pool *next;
 
24
} recycled_pool;
 
25
 
 
26
struct fd_queue_info_t
 
27
{
 
28
    apr_int32_t idlers;      /**
 
29
                                  * 0 or positive: number of idle worker threads
 
30
                              * negative: number of threads blocked waiting
 
31
                              *           for an idle worker
 
32
                              */
 
33
    apr_thread_mutex_t *idlers_mutex;
 
34
    apr_thread_cond_t *wait_for_idler;
 
35
    int terminated;
 
36
    int max_idlers;
 
37
    recycled_pool *recycled_pools;
 
38
};
 
39
 
 
40
static apr_status_t queue_info_cleanup(void *data_)
 
41
{
 
42
    fd_queue_info_t *qi = data_;
 
43
    apr_thread_cond_destroy(qi->wait_for_idler);
 
44
    apr_thread_mutex_destroy(qi->idlers_mutex);
 
45
 
 
46
    /* Clean up any pools in the recycled list */
 
47
    for (;;) {
 
48
        struct recycled_pool *first_pool = qi->recycled_pools;
 
49
        if (first_pool == NULL) {
 
50
            break;
 
51
        }
 
52
        if (apr_atomic_casptr
 
53
            ((volatile void **) &(qi->recycled_pools), first_pool->next,
 
54
             first_pool) == first_pool) {
 
55
            apr_pool_destroy(first_pool->pool);
 
56
        }
 
57
    }
 
58
 
 
59
    return APR_SUCCESS;
 
60
}
 
61
 
 
62
apr_status_t ap_queue_info_create(fd_queue_info_t ** queue_info,
 
63
                                  apr_pool_t * pool, int max_idlers)
 
64
{
 
65
    apr_status_t rv;
 
66
    fd_queue_info_t *qi;
 
67
 
 
68
    qi = apr_palloc(pool, sizeof(*qi));
 
69
    memset(qi, 0, sizeof(*qi));
 
70
 
 
71
    rv = apr_thread_mutex_create(&qi->idlers_mutex, APR_THREAD_MUTEX_DEFAULT,
 
72
                                 pool);
 
73
    if (rv != APR_SUCCESS) {
 
74
        return rv;
 
75
    }
 
76
    rv = apr_thread_cond_create(&qi->wait_for_idler, pool);
 
77
    if (rv != APR_SUCCESS) {
 
78
        return rv;
 
79
    }
 
80
    qi->recycled_pools = NULL;
 
81
    qi->max_idlers = max_idlers;
 
82
    apr_pool_cleanup_register(pool, qi, queue_info_cleanup,
 
83
                              apr_pool_cleanup_null);
 
84
 
 
85
    *queue_info = qi;
 
86
 
 
87
    return APR_SUCCESS;
 
88
}
 
89
 
 
90
apr_status_t ap_queue_info_set_idle(fd_queue_info_t * queue_info,
 
91
                                    apr_pool_t * pool_to_recycle)
 
92
{
 
93
    apr_status_t rv;
 
94
    int prev_idlers;
 
95
 
 
96
    ap_push_pool(queue_info, pool_to_recycle);
 
97
 
 
98
    /* Atomically increment the count of idle workers */
 
99
    prev_idlers = apr_atomic_inc32(&(queue_info->idlers));
 
100
 
 
101
    /* If other threads are waiting on a worker, wake one up */
 
102
    if (prev_idlers < 0) {
 
103
        rv = apr_thread_mutex_lock(queue_info->idlers_mutex);
 
104
        if (rv != APR_SUCCESS) {
 
105
            AP_DEBUG_ASSERT(0);
 
106
            return rv;
 
107
        }
 
108
        rv = apr_thread_cond_signal(queue_info->wait_for_idler);
 
109
        if (rv != APR_SUCCESS) {
 
110
            apr_thread_mutex_unlock(queue_info->idlers_mutex);
 
111
            return rv;
 
112
        }
 
113
        rv = apr_thread_mutex_unlock(queue_info->idlers_mutex);
 
114
        if (rv != APR_SUCCESS) {
 
115
            return rv;
 
116
        }
 
117
    }
 
118
 
 
119
    return APR_SUCCESS;
 
120
}
 
121
 
 
122
apr_status_t ap_queue_info_wait_for_idler(fd_queue_info_t * queue_info)
 
123
{
 
124
    apr_status_t rv;
 
125
    int prev_idlers;
 
126
 
 
127
    /* Atomically decrement the idle worker count, saving the old value */
 
128
    prev_idlers = apr_atomic_add32(&(queue_info->idlers), -1);
 
129
 
 
130
    /* Block if there weren't any idle workers */
 
131
    if (prev_idlers <= 0) {
 
132
        rv = apr_thread_mutex_lock(queue_info->idlers_mutex);
 
133
        if (rv != APR_SUCCESS) {
 
134
            AP_DEBUG_ASSERT(0);
 
135
            apr_atomic_inc32(&(queue_info->idlers));    /* back out dec */
 
136
            return rv;
 
137
        }
 
138
        /* Re-check the idle worker count to guard against a
 
139
         * race condition.  Now that we're in the mutex-protected
 
140
         * region, one of two things may have happened:
 
141
         *   - If the idle worker count is still negative, the
 
142
         *     workers are all still busy, so it's safe to
 
143
         *     block on a condition variable.
 
144
         *   - If the idle worker count is non-negative, then a
 
145
         *     worker has become idle since the first check
 
146
         *     of queue_info->idlers above.  It's possible
 
147
         *     that the worker has also signaled the condition
 
148
         *     variable--and if so, the listener missed it
 
149
         *     because it wasn't yet blocked on the condition
 
150
         *     variable.  But if the idle worker count is
 
151
         *     now non-negative, it's safe for this function to
 
152
         *     return immediately.
 
153
         *
 
154
         *     A negative value in queue_info->idlers tells how many
 
155
         *     threads are waiting on an idle worker.
 
156
         */
 
157
        if (queue_info->idlers < 0) {
 
158
            rv = apr_thread_cond_wait(queue_info->wait_for_idler,
 
159
                                      queue_info->idlers_mutex);
 
160
            if (rv != APR_SUCCESS) {
 
161
                apr_status_t rv2;
 
162
                AP_DEBUG_ASSERT(0);
 
163
                rv2 = apr_thread_mutex_unlock(queue_info->idlers_mutex);
 
164
                if (rv2 != APR_SUCCESS) {
 
165
                    return rv2;
 
166
                }
 
167
                return rv;
 
168
            }
 
169
        }
 
170
        rv = apr_thread_mutex_unlock(queue_info->idlers_mutex);
 
171
        if (rv != APR_SUCCESS) {
 
172
            return rv;
 
173
        }
 
174
    }
 
175
 
 
176
    if (queue_info->terminated) {
 
177
        return APR_EOF;
 
178
    }
 
179
    else {
 
180
        return APR_SUCCESS;
 
181
    }
 
182
}
 
183
 
 
184
 
 
185
void ap_push_pool(fd_queue_info_t * queue_info,
 
186
                                    apr_pool_t * pool_to_recycle)
 
187
{
 
188
    /* If we have been given a pool to recycle, atomically link
 
189
     * it into the queue_info's list of recycled pools
 
190
     */
 
191
    if (pool_to_recycle) {
 
192
        struct recycled_pool *new_recycle;
 
193
        new_recycle = (struct recycled_pool *) apr_palloc(pool_to_recycle,
 
194
                                                          sizeof
 
195
                                                          (*new_recycle));
 
196
        new_recycle->pool = pool_to_recycle;
 
197
        for (;;) {
 
198
            new_recycle->next = queue_info->recycled_pools;
 
199
            if (apr_atomic_casptr
 
200
                ((volatile void **) &(queue_info->recycled_pools),
 
201
                 new_recycle, new_recycle->next) == new_recycle->next) {
 
202
                break;
 
203
            }
 
204
        }
 
205
    }
 
206
}
 
207
 
 
208
void ap_pop_pool(apr_pool_t ** recycled_pool, fd_queue_info_t * queue_info)
 
209
{
 
210
    /* Atomically pop a pool from the recycled list */
 
211
 
 
212
    /* This function is safe only as long as it is single threaded because
 
213
     * it reaches into the queue and accesses "next" which can change.
 
214
     * We are OK today because it is only called from the listener thread.
 
215
     * cas-based pushes do not have the same limitation - any number can
 
216
     * happen concurrently with a single cas-based pop.
 
217
     */
 
218
 
 
219
    *recycled_pool = NULL;
 
220
 
 
221
 
 
222
    /* Atomically pop a pool from the recycled list */
 
223
    for (;;) {
 
224
        struct recycled_pool *first_pool = queue_info->recycled_pools;
 
225
        if (first_pool == NULL) {
 
226
            break;
 
227
        }
 
228
        if (apr_atomic_casptr
 
229
            ((volatile void **) &(queue_info->recycled_pools),
 
230
             first_pool->next, first_pool) == first_pool) {
 
231
            *recycled_pool = first_pool->pool;
 
232
            break;
 
233
        }
 
234
    }
 
235
}
 
236
 
 
237
apr_status_t ap_queue_info_term(fd_queue_info_t * queue_info)
 
238
{
 
239
    apr_status_t rv;
 
240
    rv = apr_thread_mutex_lock(queue_info->idlers_mutex);
 
241
    if (rv != APR_SUCCESS) {
 
242
        return rv;
 
243
    }
 
244
    queue_info->terminated = 1;
 
245
    apr_thread_cond_broadcast(queue_info->wait_for_idler);
 
246
    return apr_thread_mutex_unlock(queue_info->idlers_mutex);
 
247
}
 
248
 
 
249
/**
 
250
 * Detects when the fd_queue_t is full. This utility function is expected
 
251
 * to be called from within critical sections, and is not threadsafe.
 
252
 */
 
253
#define ap_queue_full(queue) ((queue)->nelts == (queue)->bounds)
 
254
 
 
255
/**
 
256
 * Detects when the fd_queue_t is empty. This utility function is expected
 
257
 * to be called from within critical sections, and is not threadsafe.
 
258
 */
 
259
#define ap_queue_empty(queue) ((queue)->nelts == 0)
 
260
 
 
261
/**
 
262
 * Callback routine that is called to destroy this
 
263
 * fd_queue_t when its pool is destroyed.
 
264
 */
 
265
static apr_status_t ap_queue_destroy(void *data)
 
266
{
 
267
    fd_queue_t *queue = data;
 
268
 
 
269
    /* Ignore errors here, we can't do anything about them anyway.
 
270
     * XXX: We should at least try to signal an error here, it is
 
271
     * indicative of a programmer error. -aaron */
 
272
    apr_thread_cond_destroy(queue->not_empty);
 
273
    apr_thread_mutex_destroy(queue->one_big_mutex);
 
274
 
 
275
    return APR_SUCCESS;
 
276
}
 
277
 
 
278
/**
 
279
 * Initialize the fd_queue_t.
 
280
 */
 
281
apr_status_t ap_queue_init(fd_queue_t * queue, int queue_capacity,
 
282
                           apr_pool_t * a)
 
283
{
 
284
    int i;
 
285
    apr_status_t rv;
 
286
 
 
287
    if ((rv = apr_thread_mutex_create(&queue->one_big_mutex,
 
288
                                      APR_THREAD_MUTEX_DEFAULT,
 
289
                                      a)) != APR_SUCCESS) {
 
290
        return rv;
 
291
    }
 
292
    if ((rv = apr_thread_cond_create(&queue->not_empty, a)) != APR_SUCCESS) {
 
293
        return rv;
 
294
    }
 
295
 
 
296
    queue->data = apr_palloc(a, queue_capacity * sizeof(fd_queue_elem_t));
 
297
    queue->bounds = queue_capacity;
 
298
    queue->nelts = 0;
 
299
 
 
300
    /* Set all the sockets in the queue to NULL */
 
301
    for (i = 0; i < queue_capacity; ++i)
 
302
        queue->data[i].sd = NULL;
 
303
 
 
304
    apr_pool_cleanup_register(a, queue, ap_queue_destroy,
 
305
                              apr_pool_cleanup_null);
 
306
 
 
307
    return APR_SUCCESS;
 
308
}
 
309
 
 
310
/**
 
311
 * Push a new socket onto the queue.
 
312
 *
 
313
 * precondition: ap_queue_info_wait_for_idler has already been called
 
314
 *               to reserve an idle worker thread
 
315
 */
 
316
apr_status_t ap_queue_push(fd_queue_t * queue, apr_socket_t * sd,
 
317
                           conn_state_t * cs, apr_pool_t * p)
 
318
{
 
319
    fd_queue_elem_t *elem;
 
320
    apr_status_t rv;
 
321
 
 
322
    if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
 
323
        return rv;
 
324
    }
 
325
 
 
326
    AP_DEBUG_ASSERT(!queue->terminated);
 
327
    AP_DEBUG_ASSERT(!ap_queue_full(queue));
 
328
 
 
329
    elem = &queue->data[queue->nelts];
 
330
    elem->sd = sd;
 
331
    elem->cs = cs;
 
332
    elem->p = p;
 
333
    queue->nelts++;
 
334
 
 
335
    apr_thread_cond_signal(queue->not_empty);
 
336
 
 
337
    if ((rv = apr_thread_mutex_unlock(queue->one_big_mutex)) != APR_SUCCESS) {
 
338
        return rv;
 
339
    }
 
340
 
 
341
    return APR_SUCCESS;
 
342
}
 
343
 
 
344
/**
 
345
 * Retrieves the next available socket from the queue. If there are no
 
346
 * sockets available, it will block until one becomes available.
 
347
 * Once retrieved, the socket is placed into the address specified by
 
348
 * 'sd'.
 
349
 */
 
350
apr_status_t ap_queue_pop(fd_queue_t * queue, apr_socket_t ** sd,
 
351
                          conn_state_t ** cs, apr_pool_t ** p)
 
352
{
 
353
    fd_queue_elem_t *elem;
 
354
    apr_status_t rv;
 
355
 
 
356
    if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
 
357
        return rv;
 
358
    }
 
359
 
 
360
    /* Keep waiting until we wake up and find that the queue is not empty. */
 
361
    if (ap_queue_empty(queue)) {
 
362
        if (!queue->terminated) {
 
363
            apr_thread_cond_wait(queue->not_empty, queue->one_big_mutex);
 
364
        }
 
365
        /* If we wake up and it's still empty, then we were interrupted */
 
366
        if (ap_queue_empty(queue)) {
 
367
            rv = apr_thread_mutex_unlock(queue->one_big_mutex);
 
368
            if (rv != APR_SUCCESS) {
 
369
                return rv;
 
370
            }
 
371
            if (queue->terminated) {
 
372
                return APR_EOF; /* no more elements ever again */
 
373
            }
 
374
            else {
 
375
                return APR_EINTR;
 
376
            }
 
377
        }
 
378
    }
 
379
 
 
380
    elem = &queue->data[--queue->nelts];
 
381
    *sd = elem->sd;
 
382
    *cs = elem->cs;
 
383
    *p = elem->p;
 
384
#ifdef AP_DEBUG
 
385
    elem->sd = NULL;
 
386
    elem->p = NULL;
 
387
#endif /* AP_DEBUG */
 
388
 
 
389
    rv = apr_thread_mutex_unlock(queue->one_big_mutex);
 
390
    return rv;
 
391
}
 
392
 
 
393
apr_status_t ap_queue_interrupt_all(fd_queue_t * queue)
 
394
{
 
395
    apr_status_t rv;
 
396
 
 
397
    if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
 
398
        return rv;
 
399
    }
 
400
    apr_thread_cond_broadcast(queue->not_empty);
 
401
    return apr_thread_mutex_unlock(queue->one_big_mutex);
 
402
}
 
403
 
 
404
apr_status_t ap_queue_term(fd_queue_t * queue)
 
405
{
 
406
    apr_status_t rv;
 
407
 
 
408
    if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
 
409
        return rv;
 
410
    }
 
411
    /* we must hold one_big_mutex when setting this... otherwise,
 
412
     * we could end up setting it and waking everybody up just after a
 
413
     * would-be popper checks it but right before they block
 
414
     */
 
415
    queue->terminated = 1;
 
416
    if ((rv = apr_thread_mutex_unlock(queue->one_big_mutex)) != APR_SUCCESS) {
 
417
        return rv;
 
418
    }
 
419
    return ap_queue_interrupt_all(queue);
 
420
}