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

« back to all changes in this revision

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