X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=platform%2Fwin32%2Ffunctions%2Fthreads%2Fmtx_timedlock.c;fp=platform%2Fwin32%2Ffunctions%2Fthreads%2Fmtx_timedlock.c;h=a0ddb2b1ae8d9898f8aecbae85d5cf7af3c6ce37;hp=79c96e9f07c3dbdc621156d87953ce93c54eee3c;hb=abc15df6b9fae3374d24c7cf5c3ab94c605b2a6d;hpb=8894c921674bb116d0a7b8f23a55311e7a768019 diff --git a/platform/win32/functions/threads/mtx_timedlock.c b/platform/win32/functions/threads/mtx_timedlock.c index 79c96e9..a0ddb2b 100644 --- a/platform/win32/functions/threads/mtx_timedlock.c +++ b/platform/win32/functions/threads/mtx_timedlock.c @@ -1,58 +1,58 @@ -#ifndef REGTEST -#include -#include -#include - -int mtx_timedlock(mtx_t *_PDCLIB_restrict mtx, - const struct timespec *_PDCLIB_restrict until) -{ - DWORD myId = GetCurrentThreadId(); - - if(mtx->_ThreadId == (long) myId) { - mtx->_NestCount++; - return thrd_success; - } - - - for(;;) { - LONG prev = InterlockedCompareExchange(&mtx->_ThreadId, myId, 0); - if(prev == 0) - return thrd_success; - - struct timespec now; - int32_t msToWait = 0; - if(timespec_get(&now, TIME_UTC) != TIME_UTC) { - // timespec_get must work! - return thrd_error; - } else { - int64_t deltaSec = (int64_t)until->tv_sec - now.tv_sec; - long deltaNsec = (long)until->tv_nsec - now.tv_nsec; - - if(INT32_MAX / 2000U < deltaSec) { - // Risk of overflow - do a shorter timeout on this iteration - msToWait = INT32_MAX / 2; - } else { - msToWait = deltaSec * 1000 + deltaNsec / 1000; - } - } - - if(msToWait < 0) { - return thrd_timeout; - } - - DWORD rv = WaitForSingleObject(mtx->_WaitEvHandle, msToWait); - if(rv != WAIT_OBJECT_0 && rv != WAIT_TIMEOUT) - return thrd_error; - } -} -#endif - -#ifdef TEST -#include "_PDCLIB_test.h" - -int main( void ) -{ - return TEST_RESULTS; -} - +#ifndef REGTEST +#include +#include +#include + +int mtx_timedlock(mtx_t *_PDCLIB_restrict mtx, + const struct timespec *_PDCLIB_restrict until) +{ + DWORD myId = GetCurrentThreadId(); + + if(mtx->_ThreadId == (long) myId) { + mtx->_NestCount++; + return thrd_success; + } + + + for(;;) { + LONG prev = InterlockedCompareExchange(&mtx->_ThreadId, myId, 0); + if(prev == 0) + return thrd_success; + + struct timespec now; + int32_t msToWait = 0; + if(timespec_get(&now, TIME_UTC) != TIME_UTC) { + // timespec_get must work! + return thrd_error; + } else { + int64_t deltaSec = (int64_t)until->tv_sec - now.tv_sec; + long deltaNsec = (long)until->tv_nsec - now.tv_nsec; + + if(INT32_MAX / 2000U < deltaSec) { + // Risk of overflow - do a shorter timeout on this iteration + msToWait = INT32_MAX / 2; + } else { + msToWait = deltaSec * 1000 + deltaNsec / 1000; + } + } + + if(msToWait < 0) { + return thrd_timeout; + } + + DWORD rv = WaitForSingleObject(mtx->_WaitEvHandle, msToWait); + if(rv != WAIT_OBJECT_0 && rv != WAIT_TIMEOUT) + return thrd_error; + } +} +#endif + +#ifdef TEST +#include "_PDCLIB_test.h" + +int main( void ) +{ + return TEST_RESULTS; +} + #endif \ No newline at end of file