]> pd.if.org Git - pdclib/blob - platform/win32/functions/threads/mtx_trylock.c
dos2unix
[pdclib] / platform / win32 / functions / threads / mtx_trylock.c
1 #ifndef REGTEST
2 #include <threads.h>
3 #include <windows.h>
4
5 int mtx_trylock(mtx_t *mtx)
6 {
7     DWORD myId = GetCurrentThreadId();
8
9     if(mtx->_ThreadId == (long) myId) {
10         mtx->_NestCount++;
11         return thrd_success;
12     }
13
14     if(mtx->_ThreadId != 0)
15         return thrd_busy;
16
17     LONG prev = InterlockedCompareExchange(&mtx->_ThreadId, myId, 0);
18     if(prev == 0)
19         return thrd_success;
20     else
21         return thrd_busy;
22 }
23 #endif
24
25 #ifdef TEST
26 #include "_PDCLIB_test.h"
27
28 int main( void )
29 {
30     return TEST_RESULTS;
31 }
32
33 #endif