X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fsemaphore.h;h=1cf3cd6ad11b44402eacca11d415d9df60941916;hp=75c51e6a424716deee151e3bedfd7eb80342e006;hb=fe77fc6b869a71bf94d501a0762579f4ddbc5094;hpb=e1ea831a640fba534e7e42e399f04cdf681ef8d3 diff --git a/source/core/semaphore.h b/source/core/semaphore.h index 75c51e6..1cf3cd6 100644 --- a/source/core/semaphore.h +++ b/source/core/semaphore.h @@ -1,13 +1,13 @@ /* -This file is part of libmspframework +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_ -#include #include "mutex.h" +#include "types.h" #include "../time/timedelta.h" namespace Msp { @@ -16,17 +16,25 @@ 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() { pthread_cond_init(&sem, 0); } + int signal() { MutexLock l(mutex); return pthread_cond_signal(&sem); } + int broadcast() { MutexLock l(mutex); return pthread_cond_broadcast(&sem); } + int wait() { MutexLock l(mutex); 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: +#ifndef WIN32 Mutex mutex; - pthread_cond_t cond; +#endif + SemaphoreHandle sem; }; }