]> git.tdb.fi Git - libs/core.git/blobdiff - source/mutex.h
Native threads for Win32
[libs/core.git] / source / mutex.h
index 0c2879e0cff6eb3301dd66c8c3c57faa3c1d0ba3..b619fcdb7c6b9127a550a296c9f4cb521d0d3ecc 100644 (file)
@@ -6,21 +6,29 @@ Distributed under the LGPL
 #ifndef MSP_FRAMEWORK_MUTEX_H_
 #define MSP_FRAMEWORK_MUTEX_H_
 
-#include <pthread.h>
 #include <msp/refcount.h>
+#include "types.h"
 
 namespace Msp {
 
 class Mutex
 {
 public:
+#ifndef WIN32
        Mutex()       { pthread_mutex_init(&mutex, 0); }
        int lock()    { return pthread_mutex_lock(&mutex); }
        int trylock() { return pthread_mutex_trylock(&mutex); }
        int unlock()  { return pthread_mutex_unlock(&mutex); }
        ~Mutex()      { pthread_mutex_destroy(&mutex); }
+#else
+       Mutex()       { mutex=CreateMutex(0, false, 0); }
+       int lock()    { return WaitForSingleObject(mutex, INFINITE)==WAIT_OBJECT_0; }
+       int trylock() { return WaitForSingleObject(mutex, 0)==WAIT_OBJECT_0; }
+       int unlock()  { return !ReleaseMutex(mutex); }
+       ~Mutex()      { CloseHandle(mutex); }
+#endif
 private:
-       pthread_mutex_t mutex;
+       MutexHandle mutex;
 
        friend class Semaphore;
 };