Apache HTTPD
mpm_fdqueue.c
Go to the documentation of this file.
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 "mpm_fdqueue.h"
18
19#if APR_HAS_THREADS
20
21#include <apr_atomic.h>
22
23static const apr_uint32_t zero_pt = APR_UINT32_MAX/2;
24
25struct recycled_pool
26{
28 struct recycled_pool *next;
29};
30
31struct fd_queue_info_t
32{
33 apr_uint32_t volatile idlers;
40 int terminated;
41 int max_idlers;
44 struct recycled_pool *volatile recycled_pools;
45};
46
47struct fd_queue_elem_t
48{
49 apr_socket_t *sd;
50 void *sd_baton;
52};
53
55{
57 apr_thread_cond_destroy(qi->wait_for_idler);
58 apr_thread_mutex_destroy(qi->idlers_mutex);
59
60 /* Clean up any pools in the recycled list */
61 for (;;) {
62 struct recycled_pool *first_pool = qi->recycled_pools;
63 if (first_pool == NULL) {
64 break;
65 }
66 if (apr_atomic_casptr((void *)&qi->recycled_pools, first_pool->next,
69 }
70 }
71
72 return APR_SUCCESS;
73}
74
78{
79 apr_status_t rv;
81
82 qi = apr_pcalloc(pool, sizeof(*qi));
83
85 pool);
86 if (rv != APR_SUCCESS) {
87 return rv;
88 }
89 rv = apr_thread_cond_create(&qi->wait_for_idler, pool);
90 if (rv != APR_SUCCESS) {
91 return rv;
92 }
93 qi->recycled_pools = NULL;
94 qi->max_recycled_pools = max_recycled_pools;
95 qi->max_idlers = max_idlers;
96 qi->idlers = zero_pt;
99
100 *queue_info = qi;
101
102 return APR_SUCCESS;
103}
104
107{
108 apr_status_t rv;
109
111
112 /* If other threads are waiting on a worker, wake one up */
113 if (apr_atomic_inc32(&queue_info->idlers) < zero_pt) {
114 rv = apr_thread_mutex_lock(queue_info->idlers_mutex);
115 if (rv != APR_SUCCESS) {
117 return rv;
118 }
119 rv = apr_thread_cond_signal(queue_info->wait_for_idler);
120 if (rv != APR_SUCCESS) {
121 apr_thread_mutex_unlock(queue_info->idlers_mutex);
122 return rv;
123 }
124 rv = apr_thread_mutex_unlock(queue_info->idlers_mutex);
125 if (rv != APR_SUCCESS) {
126 return rv;
127 }
128 }
129
130 return APR_SUCCESS;
131}
132
134{
135 /* Don't block if there isn't any idle worker. */
136 for (;;) {
138 if (idlers <= zero_pt) {
139 return APR_EAGAIN;
140 }
141 if (apr_atomic_cas32(&queue_info->idlers, idlers - 1,
142 idlers) == idlers) {
143 return APR_SUCCESS;
144 }
145 }
146}
147
149 int *had_to_block)
150{
151 apr_status_t rv;
152
153 /* Block if there isn't any idle worker.
154 * apr_atomic_add32(x, -1) does the same as dec32(x), except
155 * that it returns the previous value (unlike dec32's bool).
156 */
157 if (apr_atomic_add32(&queue_info->idlers, -1) <= zero_pt) {
158 rv = apr_thread_mutex_lock(queue_info->idlers_mutex);
159 if (rv != APR_SUCCESS) {
161 apr_atomic_inc32(&(queue_info->idlers)); /* back out dec */
162 return rv;
163 }
164 /* Re-check the idle worker count to guard against a
165 * race condition. Now that we're in the mutex-protected
166 * region, one of two things may have happened:
167 * - If the idle worker count is still negative, the
168 * workers are all still busy, so it's safe to
169 * block on a condition variable.
170 * - If the idle worker count is non-negative, then a
171 * worker has become idle since the first check
172 * of queue_info->idlers above. It's possible
173 * that the worker has also signaled the condition
174 * variable--and if so, the listener missed it
175 * because it wasn't yet blocked on the condition
176 * variable. But if the idle worker count is
177 * now non-negative, it's safe for this function to
178 * return immediately.
179 *
180 * A "negative value" (relative to zero_pt) in
181 * queue_info->idlers tells how many
182 * threads are waiting on an idle worker.
183 */
184 if (queue_info->idlers < zero_pt) {
185 if (had_to_block) {
186 *had_to_block = 1;
187 }
188 rv = apr_thread_cond_wait(queue_info->wait_for_idler,
189 queue_info->idlers_mutex);
190 if (rv != APR_SUCCESS) {
192 apr_thread_mutex_unlock(queue_info->idlers_mutex);
193 return rv;
194 }
195 }
196 rv = apr_thread_mutex_unlock(queue_info->idlers_mutex);
197 if (rv != APR_SUCCESS) {
198 return rv;
199 }
200 }
201
202 if (queue_info->terminated) {
203 return APR_EOF;
204 }
205 else {
206 return APR_SUCCESS;
207 }
208}
209
211{
213 val = apr_atomic_read32(&queue_info->idlers);
214 return (val > zero_pt) ? val - zero_pt : 0;
215}
216
219{
221 /* If we have been given a pool to recycle, atomically link
222 * it into the queue_info's list of recycled pools
223 */
224 if (!pool_to_recycle)
225 return;
226
227 if (queue_info->max_recycled_pools >= 0) {
228 apr_uint32_t n = apr_atomic_read32(&queue_info->recycled_pools_count);
229 if (n >= queue_info->max_recycled_pools) {
231 return;
232 }
233 apr_atomic_inc32(&queue_info->recycled_pools_count);
234 }
235
239 for (;;) {
240 /*
241 * Save queue_info->recycled_pool in local variable next because
242 * new_recycle->next can be changed after apr_atomic_casptr
243 * function call. For gory details see PR 44402.
244 */
245 struct recycled_pool *next = queue_info->recycled_pools;
246 new_recycle->next = next;
247 if (apr_atomic_casptr((void *)&queue_info->recycled_pools,
248 new_recycle, next) == next)
249 break;
250 }
251}
252
255{
256 /* Atomically pop a pool from the recycled list */
257
258 /* This function is safe only as long as it is single threaded because
259 * it reaches into the queue and accesses "next" which can change.
260 * We are OK today because it is only called from the listener thread.
261 * cas-based pushes do not have the same limitation - any number can
262 * happen concurrently with a single cas-based pop.
263 */
264
266
267
268 /* Atomically pop a pool from the recycled list */
269 for (;;) {
270 struct recycled_pool *first_pool = queue_info->recycled_pools;
271 if (first_pool == NULL) {
272 break;
273 }
274 if (apr_atomic_casptr((void *)&queue_info->recycled_pools,
275 first_pool->next, first_pool) == first_pool) {
276 *recycled_pool = first_pool->pool;
277 if (queue_info->max_recycled_pools >= 0)
278 apr_atomic_dec32(&queue_info->recycled_pools_count);
279 break;
280 }
281 }
282}
283
285{
286 apr_pool_t *p;
287
288 queue_info->max_recycled_pools = 0;
289 for (;;) {
291 if (p == NULL)
292 break;
294 }
295 apr_atomic_set32(&queue_info->recycled_pools_count, 0);
296}
297
298
300{
301 apr_status_t rv;
302
303 rv = apr_thread_mutex_lock(queue_info->idlers_mutex);
304 if (rv != APR_SUCCESS) {
305 return rv;
306 }
307
308 queue_info->terminated = 1;
309 apr_thread_cond_broadcast(queue_info->wait_for_idler);
310
311 return apr_thread_mutex_unlock(queue_info->idlers_mutex);
312}
313
318#define ap_queue_full(queue) ((queue)->nelts == (queue)->bounds)
319
324#define ap_queue_empty(queue) ((queue)->nelts == 0 && \
325 APR_RING_EMPTY(&queue->timers, \
326 timer_event_t, link))
327
333{
335
336 /* Ignore errors here, we can't do anything about them anyway.
337 * XXX: We should at least try to signal an error here, it is
338 * indicative of a programmer error. -aaron */
339 apr_thread_cond_destroy(queue->not_empty);
340 apr_thread_mutex_destroy(queue->one_big_mutex);
341
342 return APR_SUCCESS;
343}
344
349{
350 apr_status_t rv;
352
353 queue = apr_pcalloc(p, sizeof *queue);
354
355 if ((rv = apr_thread_mutex_create(&queue->one_big_mutex,
357 p)) != APR_SUCCESS) {
358 return rv;
359 }
360 if ((rv = apr_thread_cond_create(&queue->not_empty, p)) != APR_SUCCESS) {
361 return rv;
362 }
363
364 APR_RING_INIT(&queue->timers, timer_event_t, link);
365
366 queue->data = apr_pcalloc(p, capacity * sizeof(fd_queue_elem_t));
367 queue->bounds = capacity;
368
371 *pqueue = queue;
372
373 return APR_SUCCESS;
374}
375
383 apr_socket_t *sd, void *sd_baton,
384 apr_pool_t *p)
385{
387 apr_status_t rv;
388
389 if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
390 return rv;
391 }
392
393 AP_DEBUG_ASSERT(!queue->terminated);
395
396 elem = &queue->data[queue->in++];
397 if (queue->in >= queue->bounds)
398 queue->in -= queue->bounds;
399 elem->sd = sd;
400 elem->sd_baton = sd_baton;
401 elem->p = p;
402 queue->nelts++;
403
404 apr_thread_cond_signal(queue->not_empty);
405
406 return apr_thread_mutex_unlock(queue->one_big_mutex);
407}
408
410{
411 apr_status_t rv;
412
413 if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
414 return rv;
415 }
416
417 AP_DEBUG_ASSERT(!queue->terminated);
418
419 APR_RING_INSERT_TAIL(&queue->timers, te, timer_event_t, link);
420
421 apr_thread_cond_signal(queue->not_empty);
422
423 return apr_thread_mutex_unlock(queue->one_big_mutex);
424}
425
433 apr_socket_t **sd, void **sd_baton,
435{
438 apr_status_t rv;
439
440 if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
441 return rv;
442 }
443
444 /* Keep waiting until we wake up and find that the queue is not empty. */
445 if (ap_queue_empty(queue)) {
446 if (!queue->terminated) {
447 apr_thread_cond_wait(queue->not_empty, queue->one_big_mutex);
448 }
449 /* If we wake up and it's still empty, then we were interrupted */
450 if (ap_queue_empty(queue)) {
451 rv = apr_thread_mutex_unlock(queue->one_big_mutex);
452 if (rv != APR_SUCCESS) {
453 return rv;
454 }
455 if (queue->terminated) {
456 return APR_EOF; /* no more elements ever again */
457 }
458 else {
459 return APR_EINTR;
460 }
461 }
462 }
463
464 te = NULL;
465 if (te_out) {
466 if (!APR_RING_EMPTY(&queue->timers, timer_event_t, link)) {
467 te = APR_RING_FIRST(&queue->timers);
468 APR_RING_REMOVE(te, link);
469 }
470 *te_out = te;
471 }
472 if (!te) {
473 elem = &queue->data[queue->out++];
474 if (queue->out >= queue->bounds)
475 queue->out -= queue->bounds;
476 queue->nelts--;
477
478 *sd = elem->sd;
479 if (sd_baton) {
480 *sd_baton = elem->sd_baton;
481 }
482 *p = elem->p;
483#ifdef AP_DEBUG
484 elem->sd = NULL;
485 elem->p = NULL;
486#endif /* AP_DEBUG */
487 }
488
489 return apr_thread_mutex_unlock(queue->one_big_mutex);
490}
491
493{
494 apr_status_t rv;
495
496 if (queue->terminated) {
497 return APR_EOF;
498 }
499
500 if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) {
501 return rv;
502 }
503
504 /* we must hold one_big_mutex when setting this... otherwise,
505 * we could end up setting it and waking everybody up just after a
506 * would-be popper checks it but right before they block
507 */
508 if (term) {
509 queue->terminated = 1;
510 }
511 if (all)
513 else
514 apr_thread_cond_signal(queue->not_empty);
515
516 return apr_thread_mutex_unlock(queue->one_big_mutex);
517}
518
520{
521 return queue_interrupt(queue, 1, 0);
522}
523
525{
526 return queue_interrupt(queue, 0, 0);
527}
528
530{
531 return queue_interrupt(queue, 1, 1);
532}
533
534#endif /* APR_HAS_THREADS */
int n
Definition ap_regex.h:278
APR Atomic Operations.
apr_uint32_t apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val)
Definition atomic.c:30
apr_uint32_t apr_atomic_inc32(volatile apr_uint32_t *mem)
Definition atomic.c:51
int apr_atomic_dec32(volatile apr_uint32_t *mem)
Definition atomic.c:56
void apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val)
Definition atomic.c:73
apr_uint32_t apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t swap, apr_uint32_t cmp)
Definition atomic.c:78
apr_uint32_t apr_atomic_read32(volatile apr_uint32_t *mem)
Definition atomic.c:68
#define APR_EAGAIN
Definition apr_errno.h:730
#define APR_EOF
Definition apr_errno.h:461
#define APR_EINTR
Definition apr_errno.h:737
#define AP_DEBUG_ASSERT(exp)
Definition httpd.h:2283
apr_size_t size
apr_uint32_t val
Definition apr_atomic.h:66
const char int apr_pool_t * pool
Definition apr_cstr.h:84
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
void * data
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
#define APR_RING_INSERT_TAIL(hp, nep, elem, link)
Definition apr_ring.h:328
#define APR_RING_INIT(hp, elem, link)
Definition apr_ring.h:192
#define APR_RING_EMPTY(hp, elem, link)
Definition apr_ring.h:204
#define APR_RING_REMOVE(ep, link)
Definition apr_ring.h:381
#define APR_RING_FIRST(hp)
Definition apr_ring.h:166
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
fd queue declarations