]> git.tdb.fi Git - libs/core.git/commitdiff
Semaphore is now win32-compatible, I hope
authorMikko Rasa <tdb@tdb.fi>
Tue, 5 Sep 2006 12:24:46 +0000 (12:24 +0000)
committerMikko Rasa <tdb@tdb.fi>
Tue, 5 Sep 2006 12:24:46 +0000 (12:24 +0000)
source/core/semaphore.cpp
source/core/semaphore.h
source/core/types.h
source/time/timedelta.h

index 9139ce1429b8e3ab484c38256925c7d1619ca83d..06777fb984872f9444dc4c3146208c3e50643ae3 100644 (file)
@@ -11,9 +11,9 @@ Distributed under the LGPL
 
 namespace Msp {
 
 
 namespace Msp {
 
-#ifndef WIN32
 int Semaphore::wait(const Time::TimeDelta &d)
 {
 int Semaphore::wait(const Time::TimeDelta &d)
 {
+#ifndef WIN32
        Time::TimeStamp ts=Time::now()+d;
 
        timespec timeout;
        Time::TimeStamp ts=Time::now()+d;
 
        timespec timeout;
@@ -21,13 +21,15 @@ int Semaphore::wait(const Time::TimeDelta &d)
        timeout.tv_nsec=(ts.raw()%1000000)*1000;
 
        MutexLock l(mutex);
        timeout.tv_nsec=(ts.raw()%1000000)*1000;
 
        MutexLock l(mutex);
-       int r=pthread_cond_timedwait(&cond, &mutex.mutex, &timeout);
+       int r=pthread_cond_timedwait(&sem, &mutex.mutex, &timeout);
        if(r==ETIMEDOUT)
                return 1;
        else if(r)
                return -1;
        return 0;
        if(r==ETIMEDOUT)
                return 1;
        else if(r)
                return -1;
        return 0;
-}
+#else
+       return WaitForSingleObject(sem, (DWORD)(d/Time::usec))==WAIT_OBJECT_0;
 #endif
 #endif
+}
 
 } // namespace Msp
 
 } // namespace Msp
index 75c51e6a424716deee151e3bedfd7eb80342e006..164131acaaa8c19d5f94b9139be12fe9e05117a6 100644 (file)
@@ -6,8 +6,8 @@ Distributed under the LGPL
 #ifndef MSP_FRAMEWORK_SEMAPHORE_H_
 #define MSP_FRAMEWORK_SEMAPHORE_H_
 
 #ifndef MSP_FRAMEWORK_SEMAPHORE_H_
 #define MSP_FRAMEWORK_SEMAPHORE_H_
 
-#include <pthread.h>
 #include "mutex.h"
 #include "mutex.h"
+#include "types.h"
 #include "../time/timedelta.h"
 
 namespace Msp {
 #include "../time/timedelta.h"
 
 namespace Msp {
@@ -16,17 +16,24 @@ class Semaphore
 {
 public:
 #ifndef WIN32
 {
 public:
 #ifndef WIN32
-       Semaphore()        { pthread_cond_init(&cond, 0); }
+       Semaphore()        { pthread_cond_init(&sem, 0); }
        //Mutex &get_mutex() { return mutex; }
        //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); }
+       int   signal()     { MutexLock l(mutex); return pthread_cond_signal(&sem); }
+       int   broadcast()  { MutexLock l(mutex); return pthread_cond_broadcast(&sem); }
+       int   wait()       { mutex.lock(); return pthread_cond_wait(&sem, &mutex.mutex); }
        int   wait(const Time::TimeDelta &);
        int   wait(const Time::TimeDelta &);
-       ~Semaphore()       { pthread_cond_destroy(&cond); }
+       ~Semaphore()       { pthread_cond_destroy(&sem); }
+#else
+       Semaphore()        { sem=CreateSemaphore(0, 0, 32, 0); }
+       int   signal()     { return !ReleaseSemaphore(sem, 1, 0); }
+       int   broadcast()  { return !ReleaseSemaphore(sem, 32, 0); }
+       int   wait()       { return WaitForSingleObject(sem, INFINITE)==WAIT_OBJECT_0; }
+       int   wait(const Time::TimeDelta &);
+       ~Semaphore()       { CloseHandle(sem); }
 #endif
 private:
        Mutex mutex;
 #endif
 private:
        Mutex mutex;
-       pthread_cond_t cond;
+       SemaphoreHandle sem;
 };
 
 }
 };
 
 }
index 729668978d1323aa7d9924eb3f04a3d30b2adb70..bf0831379704e9d4d51de787c0fbc4d4093b2306 100644 (file)
@@ -17,9 +17,11 @@ namespace Msp {
 #ifdef WIN32
 typedef HANDLE ThreadHandle;
 typedef HANDLE MutexHandle;
 #ifdef WIN32
 typedef HANDLE ThreadHandle;
 typedef HANDLE MutexHandle;
+typedef HANDLE SemaphoreHandle;
 #else
 typedef pthread_t ThreadHandle;
 typedef pthread_mutex_t MutexHandle;
 #else
 typedef pthread_t ThreadHandle;
 typedef pthread_mutex_t MutexHandle;
+typedef pthread_cond_t SemaphoreHandle;
 #endif
 
 } // namespace Msp
 #endif
 
 } // namespace Msp
index 694136ec8e78477a0acbcb82227c607824491b67..96dc7604f48e3b8ccca9ace1eeeae8cdc7fcff99 100644 (file)
@@ -37,11 +37,13 @@ public:
        */
        int64_t raw() const { return usec; }
 
        */
        int64_t raw() const { return usec; }
 
+#ifndef WIN32
        /**
        Fills in a timespec struct.  To get a meaningful scalar value from the
        TimeDelta, divide with one of the values in units.h.
        */
        void fill_timespec(timespec &ts) const { ts.tv_sec=usec/1000000; ts.tv_nsec=(usec%1000000)*1000; }
        /**
        Fills in a timespec struct.  To get a meaningful scalar value from the
        TimeDelta, divide with one of the values in units.h.
        */
        void fill_timespec(timespec &ts) const { ts.tv_sec=usec/1000000; ts.tv_nsec=(usec%1000000)*1000; }
+#endif
 
        TimeDelta operator+(const TimeDelta &t) const  { return TimeDelta(usec+t.usec); }
        TimeDelta &operator+=(const TimeDelta &t)      { usec+=t.usec; return *this; }
 
        TimeDelta operator+(const TimeDelta &t) const  { return TimeDelta(usec+t.usec); }
        TimeDelta &operator+=(const TimeDelta &t)      { usec+=t.usec; return *this; }