Skip to content

Commit

Permalink
locks: thread: timedlock: better handling of spurious wakeups that ma…
Browse files Browse the repository at this point in the history
…y be

inherent to some native/OS condvar implementation.



git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1794266 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
ylavic committed May 7, 2017
1 parent 63c80e8 commit 10e4e46
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 36 deletions.
22 changes: 12 additions & 10 deletions locks/netware/thread_mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,24 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex,
apr_interval_time_t timeout)
{
if (mutex->cond) {
apr_status_t rv;
apr_status_t rv = APR_SUCCESS;

NXLock(mutex->mutex);
if (mutex->locked) {
if (timeout <= 0) {
rv = APR_TIMEUP;
}
else {
mutex->num_waiters++;
rv = apr_thread_cond_timedwait(mutex->cond, mutex, timeout);
do {
rv = apr_thread_cond_timedwait(mutex->cond, mutex,
timeout);
} while (rv == APR_SUCCESS && mutex->locked);
mutex->num_waiters--;
}
}
else {
if (rv == APR_SUCCESS) {
mutex->locked = 1;
rv = APR_SUCCESS;
}
NXUnlock(mutex->mutex);
return rv;
Expand All @@ -140,25 +143,24 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex,

APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex)
{
apr_status_t rv = APR_SUCCESS;

if (mutex->cond) {
apr_status_t rv;
NXLock(mutex->mutex);

if (!mutex->locked) {
rv = APR_EINVAL;
}
else if (mutex->num_waiters) {
rv = apr_thread_cond_signal(mutex->cond);
}
else {
if (rv == APR_SUCCESS) {
mutex->locked = 0;
rv = APR_SUCCESS;
}
NXUnlock(mutex->mutex);
return rv;
}

NXUnlock(mutex->mutex);
return APR_SUCCESS;
return rv;
}

APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex)
Expand Down
44 changes: 18 additions & 26 deletions locks/unix/thread_mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,23 +241,26 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex,
}
else {
mutex->num_waiters++;
rv = apr_thread_cond_timedwait(mutex->cond, mutex, timeout);
do {
rv = apr_thread_cond_timedwait(mutex->cond, mutex,
timeout);
if (rv) {
#ifdef HAVE_ZOS_PTHREADS
if (rv) {
rv = errno;
}
rv = errno;
#endif
break;
}
} while (mutex->locked);
mutex->num_waiters--;
}
}
else {
mutex->locked = 1;
}
if (rv) {
pthread_mutex_unlock(&mutex->mutex);
return rv;
if (rv) {
pthread_mutex_unlock(&mutex->mutex);
return rv;
}
}

mutex->locked = 1;

rv = pthread_mutex_unlock(&mutex->mutex);
if (rv) {
#ifdef HAVE_ZOS_PTHREADS
Expand All @@ -277,8 +280,6 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex)
apr_status_t status;

if (mutex->cond) {
apr_status_t stat2;

status = pthread_mutex_lock(&mutex->mutex);
if (status) {
#ifdef HAVE_ZOS_PTHREADS
Expand All @@ -293,21 +294,12 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex)
else if (mutex->num_waiters) {
status = apr_thread_cond_signal(mutex->cond);
}
else {
mutex->locked = 0;
status = APR_SUCCESS;
}

stat2 = pthread_mutex_unlock(&mutex->mutex);
if (stat2) {
#ifdef HAVE_ZOS_PTHREADS
status = errno;
#else
status = stat2;
#endif
if (status) {
pthread_mutex_unlock(&mutex->mutex);
return status;
}

return status;
mutex->locked = 0;
}

status = pthread_mutex_unlock(&mutex->mutex);
Expand Down

0 comments on commit 10e4e46

Please sign in to comment.