X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fsemaphore.h;h=e6c09c54437e57ed3ae0cf8903a30c920c976760;hb=d5dd704b2576f878809e87dbb8ff8591b9bdbce4;hp=a7cdff7371d4f301468ff42da669cea6daa5db26;hpb=ff52b5032a3f040de5d3a48f953f2943d7a223b3;p=libs%2Fcore.git diff --git a/source/core/semaphore.h b/source/core/semaphore.h index a7cdff7..e6c09c5 100644 --- a/source/core/semaphore.h +++ b/source/core/semaphore.h @@ -1,10 +1,12 @@ -/* -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 "mutex.h" #include "types.h" @@ -14,29 +16,39 @@ namespace Msp { class Semaphore { -public: -#ifndef WIN32 - Semaphore() { pthread_cond_init(&sem, 0); } - //Mutex &get_mutex() { return 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(&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); } +private: + Mutex *mutex; + bool own_mutex; + SemaphoreHandle sem; +#ifdef WIN32 + unsigned count; #endif + +public: + Semaphore(); + Semaphore(Mutex &); private: + void init(); +public: + ~Semaphore(); + + int signal(); + int broadcast(); + int wait(); + int wait(const Time::TimeDelta &); + Mutex &get_mutex() { return *mutex; } +}; + #ifndef WIN32 - Mutex mutex; +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 - SemaphoreHandle sem; -}; }