X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=platform%2Fwin32%2Ffunctions%2Fthreads%2Fmtx_lock.c;fp=platform%2Fwin32%2Ffunctions%2Fthreads%2Fmtx_lock.c;h=70af8ae35bd7cd65c7785527544d7cf7778e2776;hb=639bad513ab9399ed9a8c588a476a10dbe6c9478;hp=0000000000000000000000000000000000000000;hpb=008908f7a61acf9df1248c005378a3c4b4393547;p=pdclib diff --git a/platform/win32/functions/threads/mtx_lock.c b/platform/win32/functions/threads/mtx_lock.c new file mode 100644 index 0000000..70af8ae --- /dev/null +++ b/platform/win32/functions/threads/mtx_lock.c @@ -0,0 +1,40 @@ +#ifndef REGTEST +#include +#include + +int mtx_lock(mtx_t *mtx) +{ + DWORD myId = GetCurrentThreadId(); + + if(mtx->_ThreadId == myId) { + mtx->_NestCount++; + return thrd_success; + } + + DWORD res = InterlockedIncrement(&mtx->_State); + if(res == 0) { + mtx->_ThreadId = myId; + return thrd_success; + } + + // If that increment didn't leave the state == 0, then we have contention + // -> block on the wait event handle + DWORD rv = WaitForSingleObject(mtx->_WaitEvHandle, INFINITE); + if(rv != WAIT_OBJECT_0) + return thrd_error; + + // We now own the mutex - so set it up for our use + mtx->_ThreadId = myId; + return thrd_success; +} +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + return TEST_RESULTS; +} + +#endif \ No newline at end of file