]> pd.if.org Git - pdclib/blob - platform/win32/functions/threads/mtx_unlock.c
win32: initial pass at thread support
[pdclib] / platform / win32 / functions / threads / mtx_unlock.c
1 #ifndef REGTEST\r
2 #include <threads.h>\r
3 #include <windows.h>\r
4 \r
5 extern void _PDCLIB_w32errno( void );\r
6 int mtx_unlock(mtx_t *mtx)\r
7 {\r
8     if(mtx->_NestCount) {\r
9         mtx->_NestCount--;\r
10         return thrd_success;\r
11     }\r
12 \r
13     mtx->_ThreadId = 0;\r
14 \r
15     DWORD res = InterlockedDecrement(&mtx->_State);\r
16     if(res == (DWORD) -1) {\r
17         // We reset the state to -1; success!\r
18         return thrd_success;\r
19     }\r
20 \r
21     DWORD rv = SetEvent(mtx->_WaitEvHandle);\r
22     if(rv == 0) {\r
23         _PDCLIB_w32errno();\r
24         return thrd_error;\r
25     }\r
26 \r
27     return thrd_success;\r
28 }\r
29 #endif\r
30 \r
31 #ifdef TEST\r
32 #include <_PDCLIB_test.h>\r
33 \r
34 int main( void )\r
35 {\r
36     return TEST_RESULTS;\r
37 }\r
38 \r
39 #endif