]> pd.if.org Git - pdclib/blobdiff - platform/win32/functions/threads/mtx_unlock.c
win32: initial pass at thread support
[pdclib] / platform / win32 / functions / threads / mtx_unlock.c
diff --git a/platform/win32/functions/threads/mtx_unlock.c b/platform/win32/functions/threads/mtx_unlock.c
new file mode 100644 (file)
index 0000000..27b7aef
--- /dev/null
@@ -0,0 +1,39 @@
+#ifndef REGTEST\r
+#include <threads.h>\r
+#include <windows.h>\r
+\r
+extern void _PDCLIB_w32errno( void );\r
+int mtx_unlock(mtx_t *mtx)\r
+{\r
+    if(mtx->_NestCount) {\r
+        mtx->_NestCount--;\r
+        return thrd_success;\r
+    }\r
+\r
+    mtx->_ThreadId = 0;\r
+\r
+    DWORD res = InterlockedDecrement(&mtx->_State);\r
+    if(res == (DWORD) -1) {\r
+        // We reset the state to -1; success!\r
+        return thrd_success;\r
+    }\r
+\r
+    DWORD rv = SetEvent(mtx->_WaitEvHandle);\r
+    if(rv == 0) {\r
+        _PDCLIB_w32errno();\r
+        return thrd_error;\r
+    }\r
+\r
+    return thrd_success;\r
+}\r
+#endif\r
+\r
+#ifdef TEST\r
+#include <_PDCLIB_test.h>\r
+\r
+int main( void )\r
+{\r
+    return TEST_RESULTS;\r
+}\r
+\r
+#endif
\ No newline at end of file