Fix race condition in empth_sleep() for Windows
Calculation of sleep duration tried to protect against integer underflow, but the protection was racy. Rewrite the whole thing.
This commit is contained in:
parent
9ee9eb3234
commit
3ce0cea94c
1 changed files with 12 additions and 11 deletions
|
@ -609,23 +609,24 @@ empth_wakeup(empth_t *pThread)
|
||||||
int
|
int
|
||||||
empth_sleep(time_t until)
|
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);
|
empth_t *pThread = TlsGetValue(dwTLSIndex);
|
||||||
int iReturn = 0;
|
DWORD result;
|
||||||
|
|
||||||
|
loc_BlockThisThread();
|
||||||
|
|
||||||
do {
|
do {
|
||||||
loc_BlockThisThread();
|
now = time(NULL);
|
||||||
|
lSec = until >= now ? until - now : 0;
|
||||||
loc_debug("going to sleep %ld sec", lSec);
|
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) !=
|
loc_debug("sleep done. Waiting to run.");
|
||||||
WAIT_TIMEOUT)
|
loc_RunThisThread(NULL);
|
||||||
iReturn = -1;
|
|
||||||
|
|
||||||
loc_debug("sleep done. Waiting to run.");
|
return result == WAIT_TIMEOUT ? 0 : -1;
|
||||||
loc_RunThisThread(NULL);
|
|
||||||
} while (!iReturn && ((lSec = until - time(0)) > 0));
|
|
||||||
|
|
||||||
return iReturn;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/************************
|
/************************
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue