X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;ds=sidebyside;f=platform%2Fwin32%2Ffunctions%2Fthreads%2Fmtx_trylock.c;fp=platform%2Fwin32%2Ffunctions%2Fthreads%2Fmtx_trylock.c;h=ebc5e85c3c3fbf806203756533b55c938947ad15;hb=a9f7a6b87674b6f413d4a9d0a258dd5f5c2df2b4;hp=0000000000000000000000000000000000000000;hpb=c436b5ffeb0f2108ce8cfd483f860125fccad79f;p=pdclib.old diff --git a/platform/win32/functions/threads/mtx_trylock.c b/platform/win32/functions/threads/mtx_trylock.c new file mode 100644 index 0000000..ebc5e85 --- /dev/null +++ b/platform/win32/functions/threads/mtx_trylock.c @@ -0,0 +1,33 @@ +#ifndef REGTEST +#include +#include + +int mtx_trylock(mtx_t *mtx) +{ + DWORD myId = GetCurrentThreadId(); + + if(mtx->_ThreadId == myId) { + mtx->_NestCount++; + return thrd_success; + } + + if(mtx->_ThreadId != 0) + return thrd_busy; + + LONG prev = InterlockedCompareExchange(&mtx->_ThreadId, myId, 0); + if(prev == 0) + return thrd_success; + else + return thrd_busy; +} +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + return TEST_RESULTS; +} + +#endif \ No newline at end of file