summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Zhakov <ivan@apache.org>2019-05-26 13:41:49 +0000
committerIvan Zhakov <ivan@apache.org>2019-05-26 13:41:49 +0000
commitf0ec3634d7c1abad8cdb4da62b5a06821514e2af (patch)
tree6d680081057d02fb45275512e343867149ac5131
parent71d0990074e0ef4de584ae95fad7f84aceb4ca64 (diff)
downloadapr-f0ec3634d7c1abad8cdb4da62b5a06821514e2af.tar.gz
win32: Factor out common code to private function apr_wait_for_single_object().
This function almost the same as WaitForSingleObject() except it accepts timeout value in microseconds, instead of milliseconds. * include/arch/win32/apr_arch_misc.h * misc/win32/misc.c (apr_wait_for_single_object): New. Factored out from thread_cond_timedwait(). * locks/win32/proc_mutex.c * locks/win32/thread_cond.c * locks/win32/thread_mutex.c (apr_proc_mutex_timedlock, thread_cond_timedwait, apr_thread_mutex_timedlock): Use apr_wait_for_single_object(). git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1860075 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--include/arch/win32/apr_arch_misc.h5
-rw-r--r--locks/win32/proc_mutex.c23
-rw-r--r--locks/win32/thread_cond.c24
-rw-r--r--locks/win32/thread_mutex.c23
-rw-r--r--misc/win32/misc.c30
5 files changed, 43 insertions, 62 deletions
diff --git a/include/arch/win32/apr_arch_misc.h b/include/arch/win32/apr_arch_misc.h
index 2cf1c6545..4fbe79ff4 100644
--- a/include/arch/win32/apr_arch_misc.h
+++ b/include/arch/win32/apr_arch_misc.h
@@ -172,6 +172,11 @@ static APR_INLINE void* apr_realloc_dbg(void* userData, size_t newSize,
#endif /* ! _MSC_VER */
+/* Wrapper around WaitForSingleObject() that accepts apr_interval_time_t
+ * in microseconds instead of milliseconds. Values < 0 mean wait
+ * forever, 0 means do not wait at all. */
+DWORD apr_wait_for_single_object(HANDLE handle, apr_interval_time_t timeout);
+
typedef enum {
DLL_WINBASEAPI = 0, /* kernel32 From WinBase.h */
DLL_WINADVAPI = 1, /* advapi32 From WinBase.h */
diff --git a/locks/win32/proc_mutex.c b/locks/win32/proc_mutex.c
index e132e20a2..c43377e74 100644
--- a/locks/win32/proc_mutex.c
+++ b/locks/win32/proc_mutex.c
@@ -167,26 +167,9 @@ 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 = 0;
- apr_interval_time_t t = timeout;
-
- do {
- if (t > 0) {
- /* Given timeout is 64bit usecs whereas Windows timeouts are
- * 32bit msecs and below INFINITE (2^32 - 1), so we may need
- * multiple timed out waits...
- */
- if (t > apr_time_from_msec(INFINITE - 1)) {
- timeout_ms = INFINITE - 1;
- t -= apr_time_from_msec(INFINITE - 1);
- }
- else {
- timeout_ms = (DWORD)apr_time_as_msec(t);
- t = 0;
- }
- }
- rv = WaitForSingleObject(mutex->handle, timeout_ms);
- } while (rv == WAIT_TIMEOUT && t > 0);
+ DWORD rv;
+
+ rv = apr_wait_for_single_object(mutex->handle, timeout);
if (rv == WAIT_TIMEOUT) {
return APR_TIMEUP;
diff --git a/locks/win32/thread_cond.c b/locks/win32/thread_cond.c
index f571f9ec9..31a46d54e 100644
--- a/locks/win32/thread_cond.c
+++ b/locks/win32/thread_cond.c
@@ -20,6 +20,7 @@
#include "apr_strings.h"
#include "apr_arch_thread_mutex.h"
#include "apr_arch_thread_cond.h"
+#include "apr_arch_misc.h"
#include "apr_portable.h"
#include <limits.h>
@@ -79,28 +80,7 @@ static APR_INLINE apr_status_t thread_cond_timedwait(apr_thread_cond_t *cond,
apr_thread_mutex_unlock(mutex);
do {
- apr_interval_time_t t = timeout;
-
- do {
- if (t < 0) {
- timeout_ms = INFINITE;
- }
- else if (t > 0) {
- /* Given timeout is 64bit usecs whereas Windows timeouts are
- * 32bit msecs and below INFINITE (2^32 - 1), so we may need
- * multiple timed out waits...
- */
- if (t > apr_time_from_msec(INFINITE - 1)) {
- timeout_ms = INFINITE - 1;
- t -= apr_time_from_msec(INFINITE - 1);
- }
- else {
- timeout_ms = (DWORD)apr_time_as_msec(t);
- t = 0;
- }
- }
- res = WaitForSingleObject(cond->semaphore, timeout_ms);
- } while (res == WAIT_TIMEOUT && t > 0);
+ res = apr_wait_for_single_object(cond->semaphore, timeout);
EnterCriticalSection(&cond->csection);
diff --git a/locks/win32/thread_mutex.c b/locks/win32/thread_mutex.c
index f19152495..165e34c22 100644
--- a/locks/win32/thread_mutex.c
+++ b/locks/win32/thread_mutex.c
@@ -118,26 +118,9 @@ 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 = 0;
- apr_interval_time_t t = timeout;
-
- do {
- if (t > 0) {
- /* Given timeout is 64bit usecs whereas Windows timeouts are
- * 32bit msecs and below INFINITE (2^32 - 1), so we may need
- * multiple timed out waits...
- */
- if (t > apr_time_from_msec(INFINITE - 1)) {
- timeout_ms = INFINITE - 1;
- t -= apr_time_from_msec(INFINITE - 1);
- }
- else {
- timeout_ms = (DWORD)apr_time_as_msec(t);
- t = 0;
- }
- }
- rv = WaitForSingleObject(mutex->handle, timeout_ms);
- } while (rv == WAIT_TIMEOUT && t > 0);
+ DWORD rv;
+
+ rv = apr_wait_for_single_object(mutex->handle, timeout);
if ((rv != WAIT_OBJECT_0) && (rv != WAIT_ABANDONED)) {
return (rv == WAIT_TIMEOUT) ? APR_TIMEUP : apr_get_os_error();
diff --git a/misc/win32/misc.c b/misc/win32/misc.c
index b6ee202b0..11b5b6b18 100644
--- a/misc/win32/misc.c
+++ b/misc/win32/misc.c
@@ -188,6 +188,36 @@ FARPROC apr_load_dll_func(apr_dlltoken_e fnLib, char* fnName, int ordinal)
#endif
}
+DWORD apr_wait_for_single_object(HANDLE handle, apr_interval_time_t timeout)
+{
+ apr_interval_time_t t = timeout;
+ DWORD res;
+ DWORD timeout_ms = 0;
+
+ do {
+ if (t < 0) {
+ timeout_ms = INFINITE;
+ }
+ else if (t > 0) {
+ /* Given timeout is 64bit usecs whereas Windows timeouts are
+ * 32bit msecs and below INFINITE (2^32 - 1), so we may need
+ * multiple timed out waits...
+ */
+ if (t > apr_time_from_msec(INFINITE - 1)) {
+ timeout_ms = INFINITE - 1;
+ t -= apr_time_from_msec(INFINITE - 1);
+ }
+ else {
+ timeout_ms = (DWORD)apr_time_as_msec(t);
+ t = 0;
+ }
+ }
+ res = WaitForSingleObject(handle, timeout_ms);
+ } while (res == WAIT_TIMEOUT && t > 0);
+
+ return res;
+}
+
/* Declared in include/arch/win32/apr_dbg_win32_handles.h
*/
APR_DECLARE_NONSTD(HANDLE) apr_dbg_log(char* fn, HANDLE ha, char* fl, int ln,