]> git.pond.sub.org Git - empserver/commitdiff
Fix race condition in empth_sleep() for Windows
authorMarkus Armbruster <armbru@pond.sub.org>
Sun, 14 Jun 2009 16:48:38 +0000 (18:48 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Sun, 19 Jul 2009 18:11:52 +0000 (14:11 -0400)
Calculation of sleep duration tried to protect against integer
underflow, but the protection was racy.  Rewrite the whole thing.

src/lib/empthread/ntthread.c

index b20890818480776b5e978e6bce10797c0b475978..6d72ea6684f1ab6c9d3e18f9a239952c3c7fad1d 100644 (file)
@@ -609,23 +609,24 @@ empth_wakeup(empth_t *pThread)
 int
 empth_sleep(time_t until)
 {
-    long lSec = until - time(0) > 0 ? until - time(0) : 0;
+    time_t now;
+    long lSec;
     empth_t *pThread = TlsGetValue(dwTLSIndex);
-    int iReturn = 0;
+    DWORD result;
+
+    loc_BlockThisThread();
 
     do {
-       loc_BlockThisThread();
+       now = time(NULL);
+       lSec = until >= now ? until - now : 0;
        loc_debug("going to sleep %ld sec", lSec);
+       result = WaitForSingleObject(pThread->hThreadEvent, lSec * 1000L);
+    } while (result != WAIT_TIMEOUT && result != WAIT_OBJECT_0);
 
-       if (WaitForSingleObject(pThread->hThreadEvent, lSec * 1000L) !=
-           WAIT_TIMEOUT)
-           iReturn = -1;
-
-       loc_debug("sleep done. Waiting to run.");
-       loc_RunThisThread(NULL);
-    } while (!iReturn && ((lSec = until - time(0)) > 0));
+    loc_debug("sleep done. Waiting to run.");
+    loc_RunThisThread(NULL);
 
-    return iReturn;
+    return result == WAIT_TIMEOUT ? 0 : -1;
 }
 
 /************************