apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yla...@apache.org
Subject svn commit: r1792620 - in /apr/apr/trunk/locks/win32: proc_mutex.c thread_cond.c thread_mutex.c
Date Tue, 25 Apr 2017 15:09:54 GMT
Author: ylavic
Date: Tue Apr 25 15:09:54 2017
New Revision: 1792620

URL: http://svn.apache.org/viewvc?rev=1792620&view=rev
Log:
locks: Windows: work around 64bit usecs to native 32bit msecs timeouts for
apr_{proc,thread}_{mutex,cond}_timed{lock,wait}(), such that the given timeout
interval value is not truncated or switched from/to signed/unsigned.


Modified:
    apr/apr/trunk/locks/win32/proc_mutex.c
    apr/apr/trunk/locks/win32/thread_cond.c
    apr/apr/trunk/locks/win32/thread_mutex.c

Modified: apr/apr/trunk/locks/win32/proc_mutex.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/locks/win32/proc_mutex.c?rev=1792620&r1=1792619&r2=1792620&view=diff
==============================================================================
--- apr/apr/trunk/locks/win32/proc_mutex.c (original)
+++ apr/apr/trunk/locks/win32/proc_mutex.c Tue Apr 25 15:09:54 2017
@@ -167,9 +167,26 @@ APR_DECLARE(apr_status_t) apr_proc_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);
+    DWORD rv, timeout_ms = 0;
+    apr_interval_time_t t = timeout;
 
-    rv = WaitForSingleObject(mutex->handle, timeout_ms);
+    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);
 
     if (rv == WAIT_TIMEOUT) {
         return APR_TIMEUP;

Modified: apr/apr/trunk/locks/win32/thread_cond.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/locks/win32/thread_cond.c?rev=1792620&r1=1792619&r2=1792620&view=diff
==============================================================================
--- apr/apr/trunk/locks/win32/thread_cond.c (original)
+++ apr/apr/trunk/locks/win32/thread_cond.c Tue Apr 25 15:09:54 2017
@@ -63,12 +63,13 @@ APR_DECLARE(apr_status_t) apr_thread_con
 
 static APR_INLINE apr_status_t thread_cond_timedwait(apr_thread_cond_t *cond,
                                                      apr_thread_mutex_t *mutex,
-                                                     DWORD timeout_ms )
+                                                     apr_interval_time_t timeout)
 {
     DWORD res;
     apr_status_t rv;
     unsigned int wake = 0;
     unsigned long generation;
+    DWORD timeout_ms = 0;
 
     EnterCriticalSection(&cond->csection);
     cond->num_waiting++;
@@ -78,7 +79,28 @@ static APR_INLINE apr_status_t thread_co
     apr_thread_mutex_unlock(mutex);
 
     do {
-        res = WaitForSingleObject(cond->semaphore, timeout_ms);
+        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(mutex->handle, timeout_ms);
+        } while (res == WAIT_TIMEOUT && t > 0);
 
         EnterCriticalSection(&cond->csection);
 
@@ -115,15 +137,14 @@ static APR_INLINE apr_status_t thread_co
 APR_DECLARE(apr_status_t) apr_thread_cond_wait(apr_thread_cond_t *cond,
                                                apr_thread_mutex_t *mutex)
 {
-    return thread_cond_timedwait(cond, mutex, INFINITE);
+    return thread_cond_timedwait(cond, mutex, (apr_interval_time_t)-1);
 }
 
 APR_DECLARE(apr_status_t) apr_thread_cond_timedwait(apr_thread_cond_t *cond,
                                                     apr_thread_mutex_t *mutex,
                                                     apr_interval_time_t timeout)
 {
-    DWORD timeout_ms = (timeout >= 0) ? apr_time_as_msec(timeout) : INFINITE;
-    return thread_cond_timedwait(cond, mutex, timeout_ms);
+    return thread_cond_timedwait(cond, mutex, timeout);
 }
 
 APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond)

Modified: apr/apr/trunk/locks/win32/thread_mutex.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/locks/win32/thread_mutex.c?rev=1792620&r1=1792619&r2=1792620&view=diff
==============================================================================
--- apr/apr/trunk/locks/win32/thread_mutex.c (original)
+++ apr/apr/trunk/locks/win32/thread_mutex.c Tue Apr 25 15:09:54 2017
@@ -118,8 +118,27 @@ APR_DECLARE(apr_status_t) apr_thread_mut
                                                  apr_interval_time_t timeout)
 {
     if (mutex->type != thread_mutex_critical_section) {
-        DWORD rv, timeout_ms = (timeout <= 0) ? 0 : apr_time_as_msec(timeout);
-        rv = WaitForSingleObject(mutex->handle, timeout_ms);
+        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);
+
         if ((rv != WAIT_OBJECT_0) && (rv != WAIT_ABANDONED)) {
             return (rv == WAIT_TIMEOUT) ? APR_TIMEUP : apr_get_os_error();
         }



Mime
View raw message