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