Skip to content

Commit

Permalink
Arrange code according to the second set of comments on the PR.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfclere committed Sep 11, 2023
1 parent 27478a7 commit b6c780b
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 35 deletions.
128 changes: 100 additions & 28 deletions modules/proxy/proxy_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -4987,48 +4987,120 @@ PROXY_DECLARE(apr_status_t) ap_proxy_tunnel_create(proxy_tunnel_rec **ptunnel,

PROXY_DECLARE(apr_status_t) decrement_busy_count(void *worker_)
{
apr_size_t val;
proxy_worker *worker = worker_;

if (sizeof(worker->s->busy) == 4) {
apr_uint32_t val = apr_atomic_add32((volatile apr_uint32_t *)&(worker->s->busy), -1);
if (val == 0 || val > INT_MAX)
apr_atomic_inc32((volatile apr_uint32_t *)&(worker->s->busy));
} else {
apr_uint64_t val = apr_atomic_add64((volatile apr_uint64_t *)&(worker->s->busy), -1);
if (val == 0 || val > INT_MAX)
apr_atomic_inc64((volatile apr_uint64_t *)&(worker->s->busy));
#if APR_SIZEOF_VOIDP == 4
AP_DEBUG_ASSERT(sizeof(apr_size_t) == sizeof(apr_uint32_t));
val = apr_atomic_read32(&worker->s->busy);
while (val > 0) {
apr_size_t old = val;
val = apr_atomic_cas32(&worker->s->busy, val - 1, old);
if (val == old) {
break;
}
}
#elif APR_VERSION_AT_LEAST(1,7,4) /* APR 64bit atomics not safe before 1.7.4 */
AP_DEBUG_ASSERT(sizeof(apr_size_t) == sizeof(apr_uint64_t));
val = apr_atomic_read64(&worker->s->busy);
while (val > 0) {
apr_size_t old = val;
val = apr_atomic_cas64(&worker->s->busy, val - 1, old);
if (val == old) {
break;
}
}
#else /* Use atomics for (64bit) pointers */
void *volatile *busy_p = (void *)&worker->s->busy;
AP_DEBUG_ASSERT(sizeof(apr_size_t) == sizeof(void*));
AP_DEBUG_ASSERT((apr_uintptr_t)busy_p % sizeof(void*) == 0);
val = (apr_uintptr_t)apr_atomic_casptr((void *)busy_p, NULL, NULL);
while (val > 0) {
apr_size_t old = val;
val = (apr_uintptr_t)apr_atomic_casptr((void *)busy_p,
(void *)(apr_uintptr_t)(val - 1),
(void *)(apr_uintptr_t)old);
if (val == old) {
break;
}
}
#endif
return APR_SUCCESS;
}

PROXY_DECLARE(void) increment_busy_count(void *worker_)
PROXY_DECLARE(void) increment_busy_count(proxy_worker *worker)
{
proxy_worker *worker = worker_;
if (sizeof(worker->s->busy) == 4) {
apr_atomic_inc32((volatile apr_uint32_t *)&(worker->s->busy));
} else {
apr_atomic_inc64((volatile apr_uint64_t *)&(worker->s->busy));
apr_size_t val;
#if APR_SIZEOF_VOIDP == 4
AP_DEBUG_ASSERT(sizeof(apr_size_t) == sizeof(apr_uint32_t));
val = apr_atomic_read32(&worker->s->busy);
while (val < APR_INT32_MAX) {
apr_size_t old = val;
val = apr_atomic_cas32(&worker->s->busy, val + 1, old);
if (val == old) {
break;
}
}
#elif APR_VERSION_AT_LEAST(1,7,4) /* APR 64bit atomics not safe before 1.7.4 */
AP_DEBUG_ASSERT(sizeof(apr_size_t) == sizeof(apr_uint64_t));
val = apr_atomic_read64(&worker->s->busy);
while (val < APR_INT64_MAX) {
apr_size_t old = val;
val = apr_atomic_cas64(&worker->s->busy, val + 1, old);
if (val == old) {
break;
}
}
#else /* Use atomics for (64bit) pointers */
void *volatile *busy_p = (void *)&worker->s->busy;
AP_DEBUG_ASSERT(sizeof(apr_size_t) == sizeof(void*));
AP_DEBUG_ASSERT((apr_uintptr_t)busy_p % sizeof(void*) == 0);
val = (apr_uintptr_t)apr_atomic_casptr((void *)busy_p, NULL, NULL);
while (val < APR_INT64_MAX) {
apr_size_t old = val;
val = (apr_uintptr_t)apr_atomic_casptr((void *)busy_p,
(void *)(apr_uintptr_t)(val + 1),
(void *)(apr_uintptr_t)old);
if (val == old) {
break;
}
}
#endif
}

PROXY_DECLARE(apr_size_t) getbusy_count(void *worker_)
PROXY_DECLARE(apr_size_t) getbusy_count(proxy_worker *worker)
{
proxy_worker *worker = worker_;
if (sizeof(worker->s->busy) == 4) {
return(apr_atomic_read32((volatile apr_uint32_t *)&(worker->s->busy)));
} else {
return(apr_atomic_read64((volatile apr_uint64_t *)&(worker->s->busy)));
}
apr_size_t val;
#if APR_SIZEOF_VOIDP == 4
AP_DEBUG_ASSERT(sizeof(apr_size_t) == sizeof(apr_uint32_t));
val = apr_atomic_read32(&worker->s->busy);
#elif APR_VERSION_AT_LEAST(1,7,4) /* APR 64bit atomics not safe before 1.7.4 */
AP_DEBUG_ASSERT(sizeof(apr_size_t) == sizeof(apr_uint64_t));
val = apr_atomic_read64(&worker->s->busy);
#else /* Use atomics for (64bit) pointers */
void *volatile *busy_p = (void *)&worker->s->busy;
AP_DEBUG_ASSERT(sizeof(apr_size_t) == sizeof(void*));
AP_DEBUG_ASSERT((apr_uintptr_t)busy_p % sizeof(void*) == 0);
val = (apr_uintptr_t)apr_atomic_casptr((void *)busy_p, NULL, NULL);
#endif

return val;
}

PROXY_DECLARE(void) setbusy_count(void *worker_, apr_size_t busy)
PROXY_DECLARE(void) setbusy_count(proxy_worker *worker, apr_size_t to)
{
proxy_worker *worker = worker_;
if (sizeof(worker->s->busy) == 4) {
apr_atomic_set32((volatile apr_uint32_t *)&worker->s->busy, (apr_uint32_t) busy);
} else {
apr_atomic_set64((volatile apr_uint64_t *)&worker->s->busy, (apr_uint64_t) busy);
}
#if APR_SIZEOF_VOIDP == 4
AP_DEBUG_ASSERT(sizeof(apr_size_t) == sizeof(apr_uint32_t));
apr_atomic_set32(&worker->s->busy, to);
#elif APR_VERSION_AT_LEAST(1,7,4) /* APR 64bit atomics not safe before 1.7.4 */
AP_DEBUG_ASSERT(sizeof(apr_size_t) == sizeof(apr_uint64_t));
apr_atomic_set64(&worker->s->busy, to);
#else /* Use atomics for (64bit) pointers */
void *volatile *busy_p = (void *)&worker->s->busy;
AP_DEBUG_ASSERT(sizeof(apr_size_t) == sizeof(void*));
AP_DEBUG_ASSERT((apr_uintptr_t)busy_p % sizeof(void*) == 0);
apr_atomic_xchgptr((void *)busy_p, (void *)(apr_uintptr_t)to);
#endif
}

static void add_pollset(apr_pollset_t *pollset, apr_pollfd_t *pfd,
Expand Down
16 changes: 9 additions & 7 deletions modules/proxy/proxy_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,24 @@ void proxy_util_register_hooks(apr_pool_t *p);
/*
* Get the busy counter from the shared worker memory
*
* @param worker_ Pointer to the worker structure.
* @param worker Pointer to the worker structure.
* @return apr_size_t value atomically read for the worker.
*/
PROXY_DECLARE(apr_size_t) getbusy_count(void *worker_);
PROXY_DECLARE(apr_size_t) getbusy_count(proxy_worker *worker);

/*
* Set the busy counter from the shared worker memory
*
* @param worker_ Pointer to the worker structure.
* @param busy value to set the busy counter.
* @param worker Pointer to the worker structure.
* @param to value to set the busy counter.
* @return void
*/
PROXY_DECLARE(void) setbusy_count(void *worker_, apr_size_t busy);
PROXY_DECLARE(void) setbusy_count(proxy_worker *worker, apr_size_t to);

/*
* decrement the busy counter from the shared worker memory
* note it is called by apr_pool_cleanup_register()
* therfore the void * and apr_status_t.
*
* @param worker_ Pointer to the worker structure.
* @return apr_status_t returns APR_SUCCESS.
Expand All @@ -68,10 +70,10 @@ PROXY_DECLARE(apr_status_t) decrement_busy_count(void *worker_);
/*
* increment the busy counter from the shared worker memory
*
* @param worker_ Pointer to the worker structure.
* @param worker Pointer to the worker structure.
* @return void
*/
PROXY_DECLARE(void) increment_busy_count(void *worker_);
PROXY_DECLARE(void) increment_busy_count(proxy_worker *worker);

/** @} */

Expand Down

0 comments on commit b6c780b

Please sign in to comment.