]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/semaphore.h
Assimilate exceptions and RefPtr from mspmisc
[libs/core.git] / source / core / semaphore.h
index 75c51e6a424716deee151e3bedfd7eb80342e006..c55553a6f31d9ffd02b23040621ebb87337feff0 100644 (file)
@@ -1,13 +1,14 @@
-/*
-This file is part of libmspframework
+/* $Id$
+
+This file is part of libmspcore
 Copyright © 2006  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
-#ifndef MSP_FRAMEWORK_SEMAPHORE_H_
-#define MSP_FRAMEWORK_SEMAPHORE_H_
+#ifndef MSP_CORE_SEMAPHORE_H_
+#define MSP_CORE_SEMAPHORE_H_
 
-#include <pthread.h>
 #include "mutex.h"
+#include "types.h"
 #include "../time/timedelta.h"
 
 namespace Msp {
@@ -15,20 +16,36 @@ namespace Msp {
 class Semaphore
 {
 public:
-#ifndef WIN32
-       Semaphore()        { pthread_cond_init(&cond, 0); }
-       //Mutex &get_mutex() { return mutex; }
-       int   signal()     { MutexLock l(mutex); return pthread_cond_signal(&cond); }
-       int   broadcast()  { MutexLock l(mutex); return pthread_cond_broadcast(&cond); }
-       int   wait()       { mutex.lock(); return pthread_cond_wait(&cond, &mutex.mutex); }
+       Semaphore();
+       Semaphore(Mutex &);
+       int   signal();
+       int   broadcast();
+       int   wait();
        int   wait(const Time::TimeDelta &);
-       ~Semaphore()       { pthread_cond_destroy(&cond); }
-#endif
+       Mutex &get_mutex() { return *mutex; }
+       ~Semaphore();
 private:
-       Mutex mutex;
-       pthread_cond_t cond;
+       Mutex *mutex;
+       bool  own_mutex;
+       SemaphoreHandle sem;
+#ifdef WIN32
+       unsigned count;
+#endif
+
+       void init();
 };
 
+#ifndef WIN32
+inline int Semaphore::signal()
+{ return pthread_cond_signal(&sem); }
+
+inline int Semaphore::broadcast()
+{ return pthread_cond_broadcast(&sem); }
+
+inline int Semaphore::wait()
+{ return pthread_cond_wait(&sem, &mutex->mutex); }
+#endif
+
 }
 
 #endif