From c6e82eb96aba6354a35143ccae6f0ae87b4c1204 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 5 Sep 2006 12:24:46 +0000 Subject: [PATCH] Semaphore is now win32-compatible, I hope --- source/core/semaphore.cpp | 8 +++++--- source/core/semaphore.h | 21 ++++++++++++++------- source/core/types.h | 2 ++ source/time/timedelta.h | 2 ++ 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/source/core/semaphore.cpp b/source/core/semaphore.cpp index 9139ce1..06777fb 100644 --- a/source/core/semaphore.cpp +++ b/source/core/semaphore.cpp @@ -11,9 +11,9 @@ Distributed under the LGPL namespace Msp { -#ifndef WIN32 int Semaphore::wait(const Time::TimeDelta &d) { +#ifndef WIN32 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); - 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; -} +#else + return WaitForSingleObject(sem, (DWORD)(d/Time::usec))==WAIT_OBJECT_0; #endif +} } // namespace Msp diff --git a/source/core/semaphore.h b/source/core/semaphore.h index 75c51e6..164131a 100644 --- a/source/core/semaphore.h +++ b/source/core/semaphore.h @@ -6,8 +6,8 @@ Distributed under the LGPL #ifndef MSP_FRAMEWORK_SEMAPHORE_H_ #define MSP_FRAMEWORK_SEMAPHORE_H_ -#include #include "mutex.h" +#include "types.h" #include "../time/timedelta.h" namespace Msp { @@ -16,17 +16,24 @@ class Semaphore { public: #ifndef WIN32 - Semaphore() { pthread_cond_init(&cond, 0); } + Semaphore() { pthread_cond_init(&sem, 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); } + 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 &); - ~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; - pthread_cond_t cond; + SemaphoreHandle sem; }; } diff --git a/source/core/types.h b/source/core/types.h index 7296689..bf08313 100644 --- a/source/core/types.h +++ b/source/core/types.h @@ -17,9 +17,11 @@ namespace Msp { #ifdef WIN32 typedef HANDLE ThreadHandle; typedef HANDLE MutexHandle; +typedef HANDLE SemaphoreHandle; #else typedef pthread_t ThreadHandle; typedef pthread_mutex_t MutexHandle; +typedef pthread_cond_t SemaphoreHandle; #endif } // namespace Msp diff --git a/source/time/timedelta.h b/source/time/timedelta.h index 694136e..96dc760 100644 --- a/source/time/timedelta.h +++ b/source/time/timedelta.h @@ -37,11 +37,13 @@ public: */ 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; } +#endif TimeDelta operator+(const TimeDelta &t) const { return TimeDelta(usec+t.usec); } TimeDelta &operator+=(const TimeDelta &t) { usec+=t.usec; return *this; } -- 2.43.0