6 int mtx_timedlock(mtx_t *_PDCLIB_restrict mtx,
7 const struct timespec *_PDCLIB_restrict until)
9 DWORD myId = GetCurrentThreadId();
11 if(mtx->_ThreadId == (long) myId) {
18 LONG prev = InterlockedCompareExchange(&mtx->_ThreadId, myId, 0);
24 if(timespec_get(&now, TIME_UTC) != TIME_UTC) {
25 // timespec_get must work!
28 int64_t deltaSec = (int64_t)until->tv_sec - now.tv_sec;
29 long deltaNsec = (long)until->tv_nsec - now.tv_nsec;
31 if(INT32_MAX / 2000U < deltaSec) {
32 // Risk of overflow - do a shorter timeout on this iteration
33 msToWait = INT32_MAX / 2;
35 msToWait = deltaSec * 1000 + deltaNsec / 1000;
43 DWORD rv = WaitForSingleObject(mtx->_WaitEvHandle, msToWait);
44 if(rv != WAIT_OBJECT_0 && rv != WAIT_TIMEOUT)
51 #include "_PDCLIB_test.h"