diff options
-rw-r--r-- | include/apr_global_mutex.h | 6 | ||||
-rw-r--r-- | include/apr_proc_mutex.h | 6 | ||||
-rw-r--r-- | include/apr_thread_mutex.h | 6 | ||||
-rw-r--r-- | include/arch/unix/apr_arch_proc_mutex.h | 2 | ||||
-rw-r--r-- | locks/beos/proc_mutex.c | 27 | ||||
-rw-r--r-- | locks/beos/thread_mutex.c | 27 | ||||
-rw-r--r-- | locks/netware/proc_mutex.c | 5 | ||||
-rw-r--r-- | locks/netware/thread_mutex.c | 26 | ||||
-rw-r--r-- | locks/os2/proc_mutex.c | 23 | ||||
-rw-r--r-- | locks/os2/thread_mutex.c | 25 | ||||
-rw-r--r-- | locks/unix/global_mutex.c | 21 | ||||
-rw-r--r-- | locks/unix/proc_mutex.c | 64 | ||||
-rw-r--r-- | locks/unix/thread_mutex.c | 59 | ||||
-rw-r--r-- | locks/win32/proc_mutex.c | 12 | ||||
-rw-r--r-- | locks/win32/thread_mutex.c | 12 | ||||
-rw-r--r-- | test/testlock.c | 4 | ||||
-rw-r--r-- | test/testlockperf.c | 2 | ||||
-rw-r--r-- | test/testprocmutex.c | 6 |
18 files changed, 203 insertions, 130 deletions
diff --git a/include/apr_global_mutex.h b/include/apr_global_mutex.h index a00770507..b966dbd76 100644 --- a/include/apr_global_mutex.h +++ b/include/apr_global_mutex.h @@ -114,10 +114,12 @@ 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) + * @param timeout the absolute time or relative timeout (microseconds) + * @param absolute whether the timeout given is absolute (!0) or relative (0) */ APR_DECLARE(apr_status_t) apr_global_mutex_timedlock(apr_global_mutex_t *mutex, - apr_time_t timeout); + apr_time_t timeout, + int absolute); /** * Release the lock for the given mutex. diff --git a/include/apr_proc_mutex.h b/include/apr_proc_mutex.h index 4c0f92bc0..15312b2bd 100644 --- a/include/apr_proc_mutex.h +++ b/include/apr_proc_mutex.h @@ -119,10 +119,12 @@ 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) + * @param timeout the absolute time or relative timeout (microseconds) + * @param absolute whether the timeout given is absolute (!0) or relative (0) */ APR_DECLARE(apr_status_t) apr_proc_mutex_timedlock(apr_proc_mutex_t *mutex, - apr_time_t timeout); + apr_time_t timeout, + int absolute); /** * Release the lock for the given mutex. diff --git a/include/apr_thread_mutex.h b/include/apr_thread_mutex.h index efc62f65a..5dd729d07 100644 --- a/include/apr_thread_mutex.h +++ b/include/apr_thread_mutex.h @@ -87,10 +87,12 @@ 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) + * @param timeout the absolute time or relative timeout (microseconds) + * @param absolute whether the timeout given is absolute (!0) or relative (0) */ APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex, - apr_time_t timeout); + apr_time_t timeout, + int absolute); /** * Release the lock for the given mutex. diff --git a/include/arch/unix/apr_arch_proc_mutex.h b/include/arch/unix/apr_arch_proc_mutex.h index 86aaab5dc..998824847 100644 --- a/include/arch/unix/apr_arch_proc_mutex.h +++ b/include/arch/unix/apr_arch_proc_mutex.h @@ -70,7 +70,7 @@ struct apr_proc_mutex_unix_lock_methods_t { apr_status_t (*create)(apr_proc_mutex_t *, const char *); apr_status_t (*acquire)(apr_proc_mutex_t *); apr_status_t (*tryacquire)(apr_proc_mutex_t *); - apr_status_t (*timedacquire)(apr_proc_mutex_t *, apr_time_t); + apr_status_t (*timedacquire)(apr_proc_mutex_t *, apr_time_t, int); apr_status_t (*release)(apr_proc_mutex_t *); apr_status_t (*cleanup)(void *); apr_status_t (*child_init)(apr_proc_mutex_t **, apr_pool_t *, const char *); diff --git a/locks/beos/proc_mutex.c b/locks/beos/proc_mutex.c index dba07c0a5..1b468714c 100644 --- a/locks/beos/proc_mutex.c +++ b/locks/beos/proc_mutex.c @@ -109,20 +109,33 @@ 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_time_t timeout) + apr_time_t timeout, + int absolute) { int32 stat; if (atomic_add(&mutex->LockCount, 1) > 0) { - if (!timeout) { - stat = B_TIMED_OUT; - } - else if (timeout < 0) { + if (timeout < 0) { stat = acquire_sem(mutex->Lock); } else { - stat = acquire_sem_etc(mutex->Lock, 1, B_RELATIVE_TIMEOUT, - timeout); + int flag = 0; + if (timeout > 0) { + if (absolute) { + apr_time_t now = apr_time_now(); + if (timeout > now) { + timeout -= now; + } + else { + timeout = 0; + } + flag = B_ABSOLUTE_TIMEOUT; + } + else { + flag = B_RELATIVE_TIMEOUT; + } + } + stat = acquire_sem_etc(mutex->Lock, 1, flag, timeout); } if (stat < B_NO_ERROR) { atomic_add(&mutex->LockCount, -1); diff --git a/locks/beos/thread_mutex.c b/locks/beos/thread_mutex.c index 62e81319e..2fa1bc70b 100644 --- a/locks/beos/thread_mutex.c +++ b/locks/beos/thread_mutex.c @@ -132,7 +132,8 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex) } APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex, - apr_time_t timeout) + apr_time_t timeout, + int absolute) { int32 stat; thread_id me = find_thread(NULL); @@ -143,15 +144,27 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex, } if (atomic_add(&mutex->LockCount, 1) > 0) { - if (!timeout) { - stat = B_TIMED_OUT; - } - else if (timeout < 0) { + if (timeout < 0) { stat = acquire_sem(mutex->Lock); } else { - stat = acquire_sem_etc(mutex->Lock, 1, B_RELATIVE_TIMEOUT, - timeout); + int flag = 0; + if (timeout > 0) { + if (absolute) { + apr_time_t now = apr_time_now(); + if (timeout > now) { + timeout -= now; + } + else { + timeout = 0; + } + flag = B_ABSOLUTE_TIMEOUT; + } + else { + flag = B_RELATIVE_TIMEOUT; + } + } + stat = acquire_sem_etc(mutex->Lock, 1, flag, timeout); } if (stat < B_NO_ERROR) { atomic_add(&mutex->LockCount, -1); diff --git a/locks/netware/proc_mutex.c b/locks/netware/proc_mutex.c index c619f08c0..31ee97fff 100644 --- a/locks/netware/proc_mutex.c +++ b/locks/netware/proc_mutex.c @@ -73,10 +73,11 @@ 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_time_t timeout) + apr_time_t timeout, + int absolute) { if (mutex) - return apr_thread_mutex_timedlock(mutex->mutex, timeout); + return apr_thread_mutex_timedlock(mutex->mutex, timeout, absolute); return APR_ENOLOCK; } diff --git a/locks/netware/thread_mutex.c b/locks/netware/thread_mutex.c index ecb0112f4..c634edfac 100644 --- a/locks/netware/thread_mutex.c +++ b/locks/netware/thread_mutex.c @@ -112,26 +112,30 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex) } APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex, - apr_time_t timeout) + apr_time_t timeout, + int absolute) { if (mutex->cond) { apr_status_t rv; NXLock(mutex->mutex); if (mutex->locked) { - if (!timeout) { - rv = APR_TIMEUP; + mutex->num_waiters++; + if (timeout < 0) { + rv = apr_thread_cond_wait(mutex->cond, mutex); } else { - mutex->num_waiters++; - if (timeout < 0) { - rv = apr_thread_cond_wait(mutex->cond, mutex); - } - else { - rv = apr_thread_cond_timedwait(mutex->cond, mutex, - timeout); + if (absolute) { + apr_time_t now = apr_time_now(); + if (timeout > now) { + timeout -= now; + } + else { + timeout = 0; + } } - mutex->num_waiters--; + rv = apr_thread_cond_timedwait(mutex->cond, mutex, timeout); } + mutex->num_waiters--; } else { mutex->locked = 1; diff --git a/locks/os2/proc_mutex.c b/locks/os2/proc_mutex.c index 2fe75afa0..847f775da 100644 --- a/locks/os2/proc_mutex.c +++ b/locks/os2/proc_mutex.c @@ -156,24 +156,35 @@ APR_DECLARE(apr_status_t) apr_proc_mutex_trylock(apr_proc_mutex_t *mutex) mutex->lock_count++; } - return (rc == ERROR_TIMEOUT) ? APR_EBUSY : APR_FROM_OS_ERROR(rc); + return APR_FROM_OS_ERROR(rc); } APR_DECLARE(apr_status_t) apr_proc_mutex_timedlock(apr_proc_mutex_t *mutex, - apr_time_t timeout) + apr_time_t timeout, + int absolute) { ULONG rc; if (timeout < 0) { rc = DosRequestMutexSem(mutex->hMutex, SEM_INDEFINITE_WAIT); } - else if (!timeout) { - rc = DosRequestMutexSem(mutex->hMutex, SEM_IMMEDIATE_RETURN); - } else { + if (absolute) { + apr_time_t now = apr_time_now(); + if (timeout > now) { + timeout -= now; + } + else { + timeout = 0; + } + } + rc = DosRequestMutexSem(mutex->hMutex, apr_time_as_msec(timeout)); + if (rc == ERROR_TIMEOUT) { + return APR_TIMEUP; + } } if (rc == 0) { @@ -181,7 +192,7 @@ APR_DECLARE(apr_status_t) apr_proc_mutex_timedlock(apr_proc_mutex_t *mutex, mutex->lock_count++; } - return (rc == ERROR_TIMEOUT) ? APR_TIMEUP : APR_FROM_OS_ERROR(rc); + return APR_FROM_OS_ERROR(rc); } diff --git a/locks/os2/thread_mutex.c b/locks/os2/thread_mutex.c index 6dd1f0b68..26245839d 100644 --- a/locks/os2/thread_mutex.c +++ b/locks/os2/thread_mutex.c @@ -66,28 +66,37 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_lock(apr_thread_mutex_t *mutex) APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex) { ULONG rc = DosRequestMutexSem(mutex->hMutex, SEM_IMMEDIATE_RETURN); - - return (rc == ERROR_TIMEOUT) ? APR_EBUSY : APR_FROM_OS_ERROR(rc); + return APR_OS2_STATUS(rc); } APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex, - apr_time_t timeout) + apr_time_t timeout, + int absolute) { ULONG rc; if (timeout < 0) { rc = DosRequestMutexSem(mutex->hMutex, SEM_INDEFINITE_WAIT); } - else if (!timeout) { - rc = DosRequestMutexSem(mutex->hMutex, SEM_IMMEDIATE_RETURN); - } else { - rc = DosRequestMutexSem(mutex->hMutex, apr_time_as_msec(timeout)); + if (absolute) { + apr_time_t now = apr_time_now(); + if (timeout > now) { + timeout -= now; + } + else { + timeout = 0; + } + } + rc = DosRequestMutexSem(mutex->hMutex, apr_time_as_msec(usec)); + if (rc == ERROR_TIMEOUT) { + return APR_TIMEUP; + } } - return (rc == ERROR_TIMEOUT) ? APR_TIMEUP : APR_FROM_OS_ERROR(rc); + return APR_FROM_OS_ERROR(rc); } diff --git a/locks/unix/global_mutex.c b/locks/unix/global_mutex.c index 9aa213eb2..b343e23ad 100644 --- a/locks/unix/global_mutex.c +++ b/locks/unix/global_mutex.c @@ -143,30 +143,27 @@ APR_DECLARE(apr_status_t) apr_global_mutex_trylock(apr_global_mutex_t *mutex) } APR_DECLARE(apr_status_t) apr_global_mutex_timedlock(apr_global_mutex_t *mutex, - apr_time_t timeout) + apr_time_t timeout, + int absolute) { apr_status_t rv; #if APR_HAS_THREADS if (mutex->thread_mutex) { - apr_time_t expiry = 0; - if (timeout > 0) { - expiry = apr_time_now() + timeout; + if (timeout >= 0 && !absolute) { + timeout += apr_time_now(); + absolute = 1; } - rv = apr_thread_mutex_timedlock(mutex->thread_mutex, timeout); + rv = apr_thread_mutex_timedlock(mutex->thread_mutex, timeout, + absolute); if (rv != APR_SUCCESS) { return rv; } - if (expiry) { - timeout = expiry - apr_time_now(); - if (timeout < 0) { - timeout = 0; - } - } } #endif /* APR_HAS_THREADS */ - rv = apr_proc_mutex_timedlock(mutex->proc_mutex, timeout); + rv = apr_proc_mutex_timedlock(mutex->proc_mutex, timeout, + absolute); #if APR_HAS_THREADS if (rv != APR_SUCCESS) { diff --git a/locks/unix/proc_mutex.c b/locks/unix/proc_mutex.c index 25ae89c9d..70051d5a9 100644 --- a/locks/unix/proc_mutex.c +++ b/locks/unix/proc_mutex.c @@ -56,9 +56,16 @@ static apr_status_t proc_mutex_no_perms_set(apr_proc_mutex_t *mutex, && !defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK) \ && !defined(HAVE_PTHREAD_CONDATTR_SETPSHARED)) static apr_status_t proc_mutex_spinsleep_timedacquire(apr_proc_mutex_t *mutex, - apr_time_t timeout) + apr_time_t timeout, + int absolute) { apr_status_t rv; + if (absolute) { + timeout -= apr_time_now(); + if (timeout < 0) { + timeout = 0; + } + } if (timeout < 0) { rv = apr_proc_mutex_lock(mutex); } @@ -228,20 +235,19 @@ static apr_status_t proc_mutex_posix_tryacquire(apr_proc_mutex_t *mutex) #if defined(HAVE_SEM_TIMEDWAIT) static apr_status_t proc_mutex_posix_timedacquire(apr_proc_mutex_t *mutex, - apr_time_t timeout) + apr_time_t timeout, + int absolute) { 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; - } else { int rc; struct timespec abstime; - timeout += apr_time_now(); + if (!absolute) { + timeout += apr_time_now(); + } abstime.tv_sec = apr_time_sec(timeout); abstime.tv_nsec = apr_time_usec(timeout) * 1000; /* nanoseconds */ @@ -386,22 +392,23 @@ static apr_status_t proc_mutex_sysv_tryacquire(apr_proc_mutex_t *mutex) #if defined(HAVE_SEMTIMEDOP) static apr_status_t proc_mutex_sysv_timedacquire(apr_proc_mutex_t *mutex, - apr_time_t timeout) + apr_time_t timeout, + int absolute) { 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; - } else { int rc; struct timespec reltime; - + if (absolute) { + timeout -= apr_time_now(); + if (timeout < 0) { + return proc_mutex_sysv_tryacquire(mutex); + } + } reltime.tv_sec = apr_time_sec(timeout); reltime.tv_nsec = apr_time_usec(timeout) * 1000; /* nanoseconds */ - do { rc = semtimedop(mutex->os.crossproc, &proc_mutex_op_on, 1, &reltime); @@ -516,7 +523,8 @@ typedef struct { static apr_status_t proc_mutex_pthread_timedacquire(apr_proc_mutex_t *mutex, - apr_time_t timeout); + apr_time_t timeout, + int absolute); static APR_INLINE int proc_pthread_mutex_inc(apr_proc_mutex_t *mutex) { @@ -700,20 +708,21 @@ static apr_status_t proc_mutex_pthread_child_init(apr_proc_mutex_t **mutex, static apr_status_t proc_mutex_pthread_acquire(apr_proc_mutex_t *mutex) { - return proc_mutex_pthread_timedacquire(mutex, -1); + return proc_mutex_pthread_timedacquire(mutex, -1, 0); } static apr_status_t proc_mutex_pthread_tryacquire(apr_proc_mutex_t *mutex) { - apr_status_t rv = proc_mutex_pthread_timedacquire(mutex, 0); + apr_status_t rv = proc_mutex_pthread_timedacquire(mutex, 0, 0); return (rv == APR_TIMEUP) ? APR_EBUSY : rv; } static apr_status_t proc_mutex_pthread_timedacquire(apr_proc_mutex_t *mutex, - apr_time_t timeout) + apr_time_t timeout, + int absolute) { #if !APR_USE_PROC_PTHREAD_MUTEX_COND && !defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK) - return proc_mutex_spinsleep_timedacquire(mutex, timeout); + return proc_mutex_spinsleep_timedacquire(mutex, timeout, absolute); #else apr_status_t rv; @@ -754,11 +763,11 @@ static apr_status_t proc_mutex_pthread_timedacquire(apr_proc_mutex_t *mutex, } else { struct timespec abstime; - - timeout += apr_time_now(); + if (!absolute) { + timeout += apr_time_now(); + } abstime.tv_sec = apr_time_sec(timeout); abstime.tv_nsec = apr_time_usec(timeout) * 1000; /* nanoseconds */ - rv = pthread_cond_timedwait(&proc_pthread_mutex_cond(mutex), &proc_pthread_mutex(mutex), &abstime); @@ -810,11 +819,11 @@ static apr_status_t proc_mutex_pthread_timedacquire(apr_proc_mutex_t *mutex, } else { struct timespec abstime; - - timeout += apr_time_now(); + if (!absolute) { + timeout += apr_time_now(); + } abstime.tv_sec = apr_time_sec(timeout); abstime.tv_nsec = apr_time_usec(timeout) * 1000; /* nanoseconds */ - rv = pthread_mutex_timedlock(&proc_pthread_mutex(mutex), &abstime); if (rv) { #ifdef HAVE_ZOS_PTHREADS @@ -1565,9 +1574,10 @@ 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_time_t timeout) + apr_time_t timeout, + int absolute) { - return mutex->meth->timedacquire(mutex, timeout); + return mutex->meth->timedacquire(mutex, timeout, absolute); } APR_DECLARE(apr_status_t) apr_proc_mutex_unlock(apr_proc_mutex_t *mutex) diff --git a/locks/unix/thread_mutex.c b/locks/unix/thread_mutex.c index be2d86586..fe515b2e6 100644 --- a/locks/unix/thread_mutex.c +++ b/locks/unix/thread_mutex.c @@ -190,34 +190,26 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex) } APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex, - apr_time_t timeout) + apr_time_t timeout, + int absolute) { apr_status_t rv = APR_ENOTIMPL; #ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK 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 rv = errno; #endif - if (rv == EBUSY) { - rv = APR_TIMEUP; - } } } else { struct timespec abstime; - timeout += apr_time_now(); + if (!absolute) { + timeout += apr_time_now(); + } abstime.tv_sec = apr_time_sec(timeout); abstime.tv_nsec = apr_time_usec(timeout) * 1000; /* nanoseconds */ @@ -235,6 +227,8 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex, #else /* HAVE_PTHREAD_MUTEX_TIMEDLOCK */ if (mutex->cond) { + apr_status_t rv2; + rv = pthread_mutex_lock(&mutex->mutex); if (rv) { #ifdef HAVE_ZOS_PTHREADS @@ -244,40 +238,35 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex, } if (mutex->locked) { - if (!timeout) { - rv = APR_TIMEUP; + mutex->num_waiters++; + if (timeout < 0) { + rv = apr_thread_cond_wait(mutex->cond, mutex); } else { - mutex->num_waiters++; - if (timeout < 0) { - rv = apr_thread_cond_wait(mutex->cond, mutex); - } - else { - rv = apr_thread_cond_timedwait(mutex->cond, mutex, - timeout); + if (absolute) { + apr_time_t now = apr_time_now(); + if (timeout > now) { + timeout -= now; + } + else { + timeout = 0; + } } -#ifdef HAVE_ZOS_PTHREADS - if (rv) { - rv = errno; - } -#endif - mutex->num_waiters--; + rv = apr_thread_cond_timedwait(mutex->cond, mutex, timeout); } + mutex->num_waiters--; } else { mutex->locked = 1; } - if (rv) { - pthread_mutex_unlock(&mutex->mutex); - return rv; - } - rv = pthread_mutex_unlock(&mutex->mutex); - if (rv) { + rv2 = pthread_mutex_unlock(&mutex->mutex); + if (rv2 && !rv) { #ifdef HAVE_ZOS_PTHREADS rv = errno; +#else + rv = rv2; #endif - return rv; } } diff --git a/locks/win32/proc_mutex.c b/locks/win32/proc_mutex.c index 7c3df23dc..b51f856c0 100644 --- a/locks/win32/proc_mutex.c +++ b/locks/win32/proc_mutex.c @@ -165,7 +165,8 @@ 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_time_t timeout) + apr_time_t timeout, + int absolute) { DWORD rv; @@ -173,6 +174,15 @@ APR_DECLARE(apr_status_t) apr_proc_mutex_timedlock(apr_proc_mutex_t *mutex, rv = WaitForSingleObject(mutex->handle, INFINITE); } else { + if (absolute) { + apr_time_t now = apr_time_now(); + if (timeout > now) { + timeout -= now; + } + else { + timeout = 0; + } + } rv = WaitForSingleObject(mutex->handle, apr_time_as_msec(timeout)); if (rv == WAIT_TIMEOUT) { return APR_TIMEUP; diff --git a/locks/win32/thread_mutex.c b/locks/win32/thread_mutex.c index 05dc3d5fb..cd0ccf5ba 100644 --- a/locks/win32/thread_mutex.c +++ b/locks/win32/thread_mutex.c @@ -115,7 +115,8 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex) } APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex, - apr_time_t timeout) + apr_time_t timeout, + int absolute) { if (mutex->type != thread_mutex_critical_section) { DWORD rv, timeout_ms; @@ -123,6 +124,15 @@ APR_DECLARE(apr_status_t) apr_thread_mutex_timedlock(apr_thread_mutex_t *mutex, timeout_ms = INFINITE; } else { + if (absolute) { + apr_time_t now = apr_time_now(); + if (timeout > now) { + timeout -= now; + } + else { + timeout = 0; + } + } timeout_ms = apr_time_as_msec(timeout); } rv = WaitForSingleObject(mutex->handle, timeout_ms); diff --git a/test/testlock.c b/test/testlock.c index 282904452..950a9bf41 100644 --- a/test/testlock.c +++ b/test/testlock.c @@ -91,7 +91,7 @@ static void *APR_THREAD_FUNC thread_mutex_function(apr_thread_t *thd, void *data while (1) { if (data) { - apr_thread_mutex_timedlock(thread_mutex, *(apr_time_t *)data); + apr_thread_mutex_timedlock(thread_mutex, *(apr_time_t *)data, 0); } else { apr_thread_mutex_lock(thread_mutex); @@ -361,7 +361,7 @@ static void test_timeoutmutex(abts_case *tc, void *data) ABTS_INT_EQUAL(tc, 0, apr_thread_mutex_lock(timeout_mutex)); for (i = 0; i < MAX_RETRY; i++) { begin = apr_time_now(); - s = apr_thread_mutex_timedlock(timeout_mutex, timeout); + s = apr_thread_mutex_timedlock(timeout_mutex, timeout, 0); end = apr_time_now(); if (s != APR_SUCCESS && !APR_STATUS_IS_TIMEUP(s)) { diff --git a/test/testlockperf.c b/test/testlockperf.c index c91f1a87b..10442710d 100644 --- a/test/testlockperf.c +++ b/test/testlockperf.c @@ -61,7 +61,7 @@ void * APR_THREAD_FUNC thread_mutex_func(apr_thread_t *thd, void *data) for (i = 0; i < max_counter; i++) { if (data) { - apr_thread_mutex_timedlock(thread_lock, *(apr_time_t *)data); + apr_thread_mutex_timedlock(thread_lock, *(apr_time_t *)data, 0); } else { apr_thread_mutex_lock(thread_lock); diff --git a/test/testprocmutex.c b/test/testprocmutex.c index 983aa953a..3aff25b88 100644 --- a/test/testprocmutex.c +++ b/test/testprocmutex.c @@ -88,7 +88,7 @@ static void make_child(abts_case *tc, int trylock, apr_proc_t **proc, apr_pool_t else if (trylock < 0) { int wait_usec = 0; - while ((rv = apr_proc_mutex_timedlock(proc_lock, 1))) { + while ((rv = apr_proc_mutex_timedlock(proc_lock, 1, 0))) { if (!APR_STATUS_IS_TIMEUP(rv)) exit(1); if (++wait_usec >= MAX_WAIT_USEC) @@ -184,7 +184,7 @@ static void test_exclusive(abts_case *tc, const char *lockname, *x == MAX_COUNTER); } - rv = apr_proc_mutex_timedlock(proc_lock, 1); + rv = apr_proc_mutex_timedlock(proc_lock, 1, 0); if (rv == APR_ENOTIMPL) { fprintf(stderr, "%s_timedlock() not implemented, ", mech->name); ABTS_ASSERT(tc, "Default timed timedlock not implemented", @@ -194,7 +194,7 @@ static void test_exclusive(abts_case *tc, const char *lockname, APR_ASSERT_SUCCESS(tc, "check for timedlock", rv); for (n = 0; n < 2; n++) { - rv = apr_proc_mutex_timedlock(proc_lock, 1); + rv = apr_proc_mutex_timedlock(proc_lock, 1, 0); /* Some mech (eg. flock or fcntl) may succeed when the * lock is re-acquired in the same process. */ |