/* Licensed to the Apache Software Foundation (ASF) under one or more *contributorlicenseagreements.SeetheNOTICEfiledistributedwith *thisworkforadditionalinformationregardingcopyrightownership. *TheASFlicensesthisfiletoYouundertheApacheLicense,Version2.0 *(the"License");youmaynotusethisfileexceptincompliancewith *theLicense.YoumayobtainacopyoftheLicenseat * *http://www.apache.org/licenses/LICENSE-2.0 * *Unlessrequiredbyapplicablelaworagreedtoinwriting,software *distributedundertheLicenseisdistributedonan"ASIS"BASIS, *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied. *SeetheLicenseforthespecificlanguagegoverningpermissionsand *limitationsundertheLicense.
*/
/* Clean up any pools in the recycled list */ for (;;) { struct recycled_pool *first_pool = qi->recycled_pools; if (first_pool == NULL) { break;
} if (apr_atomic_casptr((void *)&qi->recycled_pools, first_pool->next,
first_pool) == first_pool) {
apr_pool_destroy(first_pool->pool);
}
}
return APR_SUCCESS;
}
apr_status_t ap_queue_info_create(fd_queue_info_t **queue_info,
apr_pool_t *pool, int max_idlers, int max_recycled_pools)
{
apr_status_t rv;
fd_queue_info_t *qi;
/* If other threads are waiting on a worker, wake one up */ if (apr_atomic_inc32(&queue_info->idlers) < zero_pt) {
rv = apr_thread_mutex_lock(queue_info->idlers_mutex); if (rv != APR_SUCCESS) {
AP_DEBUG_ASSERT(0); return rv;
}
rv = apr_thread_cond_signal(queue_info->wait_for_idler); if (rv != APR_SUCCESS) {
apr_thread_mutex_unlock(queue_info->idlers_mutex); return rv;
}
rv = apr_thread_mutex_unlock(queue_info->idlers_mutex); if (rv != APR_SUCCESS) { return rv;
}
}
return APR_SUCCESS;
}
apr_status_t ap_queue_info_try_get_idler(fd_queue_info_t *queue_info)
{ /* Don't block if there isn't any idle worker. */ for (;;) {
apr_uint32_t idlers = queue_info->idlers; if (idlers <= zero_pt) { return APR_EAGAIN;
} if (apr_atomic_cas32(&queue_info->idlers, idlers - 1,
idlers) == idlers) { return APR_SUCCESS;
}
}
}
apr_status_t ap_queue_info_wait_for_idler(fd_queue_info_t *queue_info, int *had_to_block)
{
apr_status_t rv;
/* Block if there isn't any idle worker. *apr_atomic_add32(x,-1)doesthesameasdec32(x),except *thatitreturnsthepreviousvalue(unlikedec32'sbool).
*/ if (apr_atomic_add32(&queue_info->idlers, -1) <= zero_pt) {
rv = apr_thread_mutex_lock(queue_info->idlers_mutex); if (rv != APR_SUCCESS) {
AP_DEBUG_ASSERT(0);
apr_atomic_inc32(&(queue_info->idlers)); /* back out dec */ return rv;
} /* Re-check the idle worker count to guard against a *racecondition.Nowthatwe'reinthemutex-protected *region,oneoftwothingsmayhavehappened: *-Iftheidleworkercountisstillnegative,the *workersareallstillbusy,soit'ssafeto *blockonaconditionvariable. *-Iftheidleworkercountisnon-negative,thena *workerhasbecomeidlesincethefirstcheck *ofqueue_info->idlersabove.It'spossible *thattheworkerhasalsosignaledthecondition *variable--andifso,thelistenermissedit *becauseitwasn'tyetblockedonthecondition *variable.Butiftheidleworkercountis *nownon-negative,it'ssafeforthisfunctionto *returnimmediately. * *A"negativevalue"(relativetozero_pt)in *queue_info->idlerstellshowmany *threadsarewaitingonanidleworker.
*/ if (queue_info->idlers < zero_pt) { if (had_to_block) {
*had_to_block = 1;
}
rv = apr_thread_cond_wait(queue_info->wait_for_idler,
queue_info->idlers_mutex); if (rv != APR_SUCCESS) {
AP_DEBUG_ASSERT(0);
apr_thread_mutex_unlock(queue_info->idlers_mutex); return rv;
}
}
rv = apr_thread_mutex_unlock(queue_info->idlers_mutex); if (rv != APR_SUCCESS) { return rv;
}
}
apr_uint32_t ap_queue_info_num_idlers(fd_queue_info_t *queue_info)
{
apr_uint32_t val;
val = apr_atomic_read32(&queue_info->idlers); return (val > zero_pt) ? val - zero_pt : 0;
}
void ap_queue_info_push_pool(fd_queue_info_t *queue_info,
apr_pool_t *pool_to_recycle)
{ struct recycled_pool *new_recycle; /* If we have been given a pool to recycle, atomically link *itintothequeue_info'slistofrecycledpools
*/ if (!pool_to_recycle) return;
if (queue_info->max_recycled_pools >= 0) {
apr_uint32_t n = apr_atomic_read32(&queue_info->recycled_pools_count); if (n >= queue_info->max_recycled_pools) {
apr_pool_destroy(pool_to_recycle); return;
}
apr_atomic_inc32(&queue_info->recycled_pools_count);
}
void ap_queue_info_pop_pool(fd_queue_info_t *queue_info,
apr_pool_t **recycled_pool)
{ /* Atomically pop a pool from the recycled list */
/* This function is safe only as long as it is single threaded because *itreachesintothequeueandaccesses"next"whichcanchange. *WeareOKtodaybecauseitisonlycalledfromthelistenerthread. *cas-basedpushesdonothavethesamelimitation-anynumbercan *happenconcurrentlywithasinglecas-basedpop.
*/
*recycled_pool = NULL;
/* Atomically pop a pool from the recycled list */ for (;;) { struct recycled_pool *first_pool = queue_info->recycled_pools; if (first_pool == NULL) { break;
} if (apr_atomic_casptr((void *)&queue_info->recycled_pools,
first_pool->next, first_pool) == first_pool) {
*recycled_pool = first_pool->pool; if (queue_info->max_recycled_pools >= 0)
apr_atomic_dec32(&queue_info->recycled_pools_count); break;
}
}
}
/* Ignore errors here, we can't do anything about them anyway. *XXX:Weshouldatleasttrytosignalanerrorhere,itis
* indicative of a programmer error. -aaron */
apr_thread_cond_destroy(queue->not_empty);
apr_thread_mutex_destroy(queue->one_big_mutex);
if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) { return rv;
}
/* Keep waiting until we wake up and find that the queue is not empty. */ if (ap_queue_empty(queue)) { if (!queue->terminated) {
apr_thread_cond_wait(queue->not_empty, queue->one_big_mutex);
} /* If we wake up and it's still empty, then we were interrupted */ if (ap_queue_empty(queue)) {
rv = apr_thread_mutex_unlock(queue->one_big_mutex); if (rv != APR_SUCCESS) { return rv;
} if (queue->terminated) { return APR_EOF; /* no more elements ever again */
} else { return APR_EINTR;
}
}
}
te = NULL; if (te_out) { if (!APR_RING_EMPTY(&queue->timers, timer_event_t, link)) {
te = APR_RING_FIRST(&queue->timers);
APR_RING_REMOVE(te, link);
}
*te_out = te;
} if (!te) {
elem = &queue->data[queue->out++]; if (queue->out >= queue->bounds)
queue->out -= queue->bounds;
queue->nelts--;
static apr_status_t queue_interrupt(fd_queue_t *queue, int all, int term)
{
apr_status_t rv;
if (queue->terminated) { return APR_EOF;
}
if ((rv = apr_thread_mutex_lock(queue->one_big_mutex)) != APR_SUCCESS) { return rv;
}
/* we must hold one_big_mutex when setting this... otherwise, *wecouldendupsettingitandwakingeverybodyupjustaftera *would-bepopperchecksitbutrightbeforetheyblock
*/ if (term) {
queue->terminated = 1;
} if (all)
apr_thread_cond_broadcast(queue->not_empty); else
apr_thread_cond_signal(queue->not_empty);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.