]> pd.if.org Git - pdclib/blobdiff - platform/win32/functions/threads/mtx_trylock.c
win32: mutex rewrite. new form is much simpler
[pdclib] / platform / win32 / functions / threads / mtx_trylock.c
diff --git a/platform/win32/functions/threads/mtx_trylock.c b/platform/win32/functions/threads/mtx_trylock.c
new file mode 100644 (file)
index 0000000..ebc5e85
--- /dev/null
@@ -0,0 +1,33 @@
+#ifndef REGTEST\r
+#include <threads.h>\r
+#include <windows.h>\r
+\r
+int mtx_trylock(mtx_t *mtx)\r
+{\r
+    DWORD myId = GetCurrentThreadId();\r
+\r
+    if(mtx->_ThreadId == myId) {\r
+        mtx->_NestCount++;\r
+        return thrd_success;\r
+    }\r
+\r
+    if(mtx->_ThreadId != 0)\r
+        return thrd_busy;\r
+\r
+    LONG prev = InterlockedCompareExchange(&mtx->_ThreadId, myId, 0);\r
+    if(prev == 0)\r
+        return thrd_success;\r
+    else\r
+        return thrd_busy;\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