From c88a277f8f37ed1ee0e436093b5ac6e9cca32efc Mon Sep 17 00:00:00 2001 From: "William A. Rowe Jr" Date: Tue, 23 May 2017 17:08:59 +0000 Subject: Revert 1790633 (r1790632 from trunk), was; apr_{thread,proc,global}_timedlock() with negative timeout is now equivalent to apr_{thread,proc,global}_trylock(), i.e. immediate attempt to acquire the lock (but returning APR_TIMEUP if busy). git-svn-id: https://svn.apache.org/repos/asf/apr/apr/branches/1.6.x@1795937 13f79535-47bb-0310-9956-ffa450edef68 --- include/apr_global_mutex.h | 4 +- include/apr_proc_mutex.h | 4 +- include/apr_thread_mutex.h | 4 +- locks/beos/proc_mutex.c | 5 ++- locks/beos/thread_mutex.c | 5 ++- locks/netware/thread_mutex.c | 10 ++++- locks/os2/proc_mutex.c | 5 ++- locks/os2/thread_mutex.c | 5 ++- locks/unix/proc_mutex.c | 88 ++++++++++++++++++++++++-------------------- locks/unix/thread_mutex.c | 20 ++++++++-- locks/win32/proc_mutex.c | 15 +++++--- locks/win32/thread_mutex.c | 8 +++- 12 files changed, 109 insertions(+), 64 deletions(-) diff --git a/include/apr_global_mutex.h b/include/apr_global_mutex.h index e3cba23ff..b1c7f3324 100644 --- a/include/apr_global_mutex.h +++ b/include/apr_global_mutex.h @@ -114,9 +114,7 @@ APR_DECLARE(apr_status_t) apr_global_mutex_trylock(apr_global_mutex_t *mutex); * Attempt to acquire the lock for the given mutex until timeout expires. * If the acquisition time outs, the call returns with APR_TIMEUP. * @param mutex the mutex on which to attempt the lock acquiring. - * @param timeout the relative timeout (microseconds). - * @note A negative or nul timeout means immediate attempt, returning - * APR_TIMEUP without blocking if it the lock is already acquired. + * @param timeout the relative timeout (microseconds) */ APR_DECLARE(apr_status_t) apr_global_mutex_timedlock(apr_global_mutex_t *mutex, apr_interval_time_t timeout); diff --git a/include/apr_proc_mutex.h b/include/apr_proc_mutex.h index 418c95048..75e255a4c 100644 --- a/include/apr_proc_mutex.h +++ b/include/apr_proc_mutex.h @@ -119,9 +119,7 @@ APR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex); * Attempt to acquire the lock for the given mutex until timeout expires. * If the acquisition time outs, the call returns with APR_TIMEUP. * @param mutex the mutex on which to attempt the lock acquiring. - * @param timeout the relative timeout (microseconds). - * @note A negative or nul timeout means immediate attempt, returning - * APR_TIMEUP without blocking if it the lock is already acquired. + * @param timeout the relative timeout (microseconds) */ APR_DECLARE(apr_status_t) apr_proc_mutex_timedlock(apr_proc_mutex_t *mutex, apr_interval_time_t timeout); diff --git a/include/apr_thread_mutex.h b/include/apr_thread_mutex.h index 8eb617291..5279a9fa6 100644 --- a/include/apr_thread_mutex.h +++ b/include/apr_thread_mutex.h @@ -87,9 +87,7 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex); * Attempt to acquire the lock for the given mutex until timeout expires. * If the acquisition time outs, the call returns with APR_TIMEUP. * @param mutex the mutex on which to attempt the lock acquiring. - * @param timeout the relative timeout (microseconds). - * @note A timeout negative or nul means immediate attempt, returning - * APR_TIMEUP without blocking if it the lock is already acquired. + * @param timeout the relative timeout (microseconds) */ APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex, apr_interval_time_t timeout); diff --git a/locks/beos/proc_mutex.c b/locks/beos/proc_mutex.c index d253ad5be..e97a2ee80 100644 --- a/locks/beos/proc_mutex.c +++ b/locks/beos/proc_mutex.c @@ -114,9 +114,12 @@ APR_DECLARE(apr_status_t) apr_proc_mutex_timedlock(apr_proc_mutex_t *mutex, int32 stat; if (atomic_add(&mutex->LockCount, 1) > 0) { - if (timeout <= 0) { + if (!timeout) { stat = B_TIMED_OUT; } + else if (timeout < 0) { + stat = acquire_sem(mutex->Lock); + } else { stat = acquire_sem_etc(mutex->Lock, 1, B_RELATIVE_TIMEOUT, timeout); diff --git a/locks/beos/thread_mutex.c b/locks/beos/thread_mutex.c index e4099d882..f9353509f 100644 --- a/locks/beos/thread_mutex.c +++ b/locks/beos/thread_mutex.c @@ -143,9 +143,12 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex, } if (atomic_add(&mutex->LockCount, 1) > 0) { - if (timeout <= 0) { + if (!timeout) { stat = B_TIMED_OUT; } + else if (timeout < 0) { + stat = acquire_sem(mutex->Lock); + } else { stat = acquire_sem_etc(mutex->Lock, 1, B_RELATIVE_TIMEOUT, timeout); diff --git a/locks/netware/thread_mutex.c b/locks/netware/thread_mutex.c index 9c2164278..127610d3e 100644 --- a/locks/netware/thread_mutex.c +++ b/locks/netware/thread_mutex.c @@ -118,12 +118,18 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex, apr_status_t rv; NXLock(mutex->mutex); if (mutex->locked) { - if (timeout <= 0) { + if (!timeout) { rv = APR_TIMEUP; } else { mutex->num_waiters++; - rv = apr_thread_cond_timedwait(mutex->cond, mutex, timeout); + if (timeout < 0) { + rv = apr_thread_cond_wait(mutex->cond, mutex); + } + else { + rv = apr_thread_cond_timedwait(mutex->cond, mutex, + timeout); + } mutex->num_waiters--; } } diff --git a/locks/os2/proc_mutex.c b/locks/os2/proc_mutex.c index de2e66aeb..564c49cf6 100644 --- a/locks/os2/proc_mutex.c +++ b/locks/os2/proc_mutex.c @@ -166,7 +166,10 @@ APR_DECLARE(apr_status_t) apr_proc_mutex_timedlock(apr_proc_mutex_t *mutex, { ULONG rc; - if (timeout <= 0) { + if (timeout < 0) { + rc = DosRequestMutexSem(mutex->hMutex, SEM_INDEFINITE_WAIT); + } + else if (!timeout) { rc = DosRequestMutexSem(mutex->hMutex, SEM_IMMEDIATE_RETURN); } else { diff --git a/locks/os2/thread_mutex.c b/locks/os2/thread_mutex.c index fea188d04..f95ba0fec 100644 --- a/locks/os2/thread_mutex.c +++ b/locks/os2/thread_mutex.c @@ -77,7 +77,10 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex, { ULONG rc; - if (timeout <= 0) { + if (timeout < 0) { + rc = DosRequestMutexSem(mutex->hMutex, SEM_INDEFINITE_WAIT); + } + else if (!timeout) { rc = DosRequestMutexSem(mutex->hMutex, SEM_IMMEDIATE_RETURN); } else { diff --git a/locks/unix/proc_mutex.c b/locks/unix/proc_mutex.c index 2bd695dfd..7d111df8a 100644 --- a/locks/unix/proc_mutex.c +++ b/locks/unix/proc_mutex.c @@ -58,27 +58,32 @@ static apr_status_t proc_mutex_no_perms_set(apr_proc_mutex_t *mutex, static apr_status_t proc_mutex_spinsleep_timedacquire(apr_proc_mutex_t *mutex, apr_interval_time_t timeout) { -#define SLEEP_TIME apr_time_from_msec(10) apr_status_t rv; - for (;;) { - rv = apr_proc_mutex_trylock(mutex); - if (!APR_STATUS_IS_EBUSY(rv)) { - if (rv == APR_SUCCESS) { - mutex->curr_locked = 1; + if (timeout < 0) { + rv = apr_proc_mutex_lock(mutex); + } + else { +#define SLEEP_TIME apr_time_from_msec(10) + for (;;) { + rv = apr_proc_mutex_trylock(mutex); + if (!APR_STATUS_IS_EBUSY(rv)) { + if (rv == APR_SUCCESS) { + mutex->curr_locked = 1; + } + break; + } + if (!timeout) { + rv = APR_TIMEUP; + break; + } + if (timeout > SLEEP_TIME) { + apr_sleep(SLEEP_TIME); + timeout -= SLEEP_TIME; + } + else { + apr_sleep(timeout); + timeout = 0; } - break; - } - if (timeout <= 0) { - rv = APR_TIMEUP; - break; - } - if (timeout > SLEEP_TIME) { - apr_sleep(SLEEP_TIME); - timeout -= SLEEP_TIME; - } - else { - apr_sleep(timeout); - timeout = 0; } } return rv; @@ -225,7 +230,10 @@ static apr_status_t proc_mutex_posix_tryacquire(apr_proc_mutex_t *mutex) static apr_status_t proc_mutex_posix_timedacquire(apr_proc_mutex_t *mutex, apr_interval_time_t timeout) { - if (timeout <= 0) { + if (timeout < 0) { + return proc_mutex_posix_acquire(mutex); + } + else if (!timeout) { apr_status_t rv = proc_mutex_posix_tryacquire(mutex); return (rv == APR_EBUSY) ? APR_TIMEUP : rv; } @@ -380,7 +388,10 @@ static apr_status_t proc_mutex_sysv_tryacquire(apr_proc_mutex_t *mutex) static apr_status_t proc_mutex_sysv_timedacquire(apr_proc_mutex_t *mutex, apr_interval_time_t timeout) { - if (timeout <= 0) { + if (timeout < 0) { + return proc_mutex_sysv_acquire(mutex); + } + else if (!timeout) { apr_status_t rv = proc_mutex_sysv_tryacquire(mutex); return (rv == APR_EBUSY) ? APR_TIMEUP : rv; } @@ -504,6 +515,9 @@ typedef struct { } proc_pthread_mutex_t; +static apr_status_t proc_mutex_pthread_timedacquire(apr_proc_mutex_t *mutex, + apr_interval_time_t timeout); + static APR_INLINE int proc_pthread_mutex_inc(apr_proc_mutex_t *mutex) { if (mutex->pthread_refcounting) { @@ -684,8 +698,19 @@ static apr_status_t proc_mutex_pthread_child_init(apr_proc_mutex_t **mutex, return APR_SUCCESS; } -static apr_status_t proc_mutex_pthread_acquire_ex(apr_proc_mutex_t *mutex, - apr_interval_time_t timeout) +static apr_status_t proc_mutex_pthread_acquire(apr_proc_mutex_t *mutex) +{ + return proc_mutex_pthread_timedacquire(mutex, -1); +} + +static apr_status_t proc_mutex_pthread_tryacquire(apr_proc_mutex_t *mutex) +{ + apr_status_t rv = proc_mutex_pthread_timedacquire(mutex, 0); + return (rv == APR_TIMEUP) ? APR_EBUSY : rv; +} + +static apr_status_t proc_mutex_pthread_timedacquire(apr_proc_mutex_t *mutex, + apr_interval_time_t timeout) { #if !APR_USE_PROC_PTHREAD_MUTEX_COND && !defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK) return proc_mutex_spinsleep_timedacquire(mutex, timeout); @@ -818,23 +843,6 @@ static apr_status_t proc_mutex_pthread_acquire_ex(apr_proc_mutex_t *mutex, #endif } -static apr_status_t proc_mutex_pthread_acquire(apr_proc_mutex_t *mutex) -{ - return proc_mutex_pthread_acquire_ex(mutex, -1); -} - -static apr_status_t proc_mutex_pthread_tryacquire(apr_proc_mutex_t *mutex) -{ - apr_status_t rv = proc_mutex_pthread_acquire_ex(mutex, 0); - return (rv == APR_TIMEUP) ? APR_EBUSY : rv; -} - -static apr_status_t proc_mutex_pthread_timedacquire(apr_proc_mutex_t *mutex, - apr_interval_time_t timeout) -{ - return proc_mutex_pthread_acquire_ex(mutex, (timeout <= 0) ? 0 : timeout); -} - static apr_status_t proc_mutex_pthread_release(apr_proc_mutex_t *mutex) { apr_status_t rv; diff --git a/locks/unix/thread_mutex.c b/locks/unix/thread_mutex.c index cf859e6ad..6a26ff239 100644 --- a/locks/unix/thread_mutex.c +++ b/locks/unix/thread_mutex.c @@ -195,7 +195,15 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex, apr_status_t rv = APR_ENOTIMPL; #ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK - if (timeout <= 0) { + if (timeout < 0) { + rv = pthread_mutex_lock(&mutex->mutex); +#ifdef HAVE_ZOS_PTHREADS + if (rv) { + rv = errno; + } +#endif + } + else if (!timeout) { rv = pthread_mutex_trylock(&mutex->mutex); if (rv) { #ifdef HAVE_ZOS_PTHREADS @@ -236,12 +244,18 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex, } if (mutex->locked) { - if (timeout <= 0) { + if (!timeout) { rv = APR_TIMEUP; } else { mutex->num_waiters++; - rv = apr_thread_cond_timedwait(mutex->cond, mutex, timeout); + if (timeout < 0) { + rv = apr_thread_cond_wait(mutex->cond, mutex); + } + else { + rv = apr_thread_cond_timedwait(mutex->cond, mutex, + timeout); + } #ifdef HAVE_ZOS_PTHREADS if (rv) { rv = errno; diff --git a/locks/win32/proc_mutex.c b/locks/win32/proc_mutex.c index 648c7a14d..c85853d63 100644 --- a/locks/win32/proc_mutex.c +++ b/locks/win32/proc_mutex.c @@ -167,13 +167,18 @@ APR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex) APR_DECLARE(apr_status_t) apr_proc_mutex_timedlock(apr_proc_mutex_t *mutex, apr_interval_time_t timeout) { - DWORD rv, timeout_ms = (timeout <= 0) ? 0 : apr_time_as_msec(timeout); - - rv = WaitForSingleObject(mutex->handle, timeout_ms); + DWORD rv; - if (rv == WAIT_TIMEOUT) { - return APR_TIMEUP; + if (timeout < 0) { + rv = WaitForSingleObject(mutex->handle, INFINITE); + } + else { + rv = WaitForSingleObject(mutex->handle, apr_time_as_msec(timeout)); + if (rv == WAIT_TIMEOUT) { + return APR_TIMEUP; + } } + if (rv == WAIT_OBJECT_0 || rv == WAIT_ABANDONED) { return APR_SUCCESS; } diff --git a/locks/win32/thread_mutex.c b/locks/win32/thread_mutex.c index 2eac69cdb..49198427a 100644 --- a/locks/win32/thread_mutex.c +++ b/locks/win32/thread_mutex.c @@ -118,7 +118,13 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex, apr_interval_time_t timeout) { if (mutex->type != thread_mutex_critical_section) { - DWORD rv, timeout_ms = (timeout <= 0) ? 0 : apr_time_as_msec(timeout); + DWORD rv, timeout_ms; + if (timeout < 0) { + timeout_ms = INFINITE; + } + else { + timeout_ms = apr_time_as_msec(timeout); + } rv = WaitForSingleObject(mutex->handle, timeout_ms); if ((rv != WAIT_OBJECT_0) && (rv != WAIT_ABANDONED)) { return (rv == WAIT_TIMEOUT) ? APR_TIMEUP : apr_get_os_error(); -- cgit v1.2.1