X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fsemaphore.h;h=c55553a6f31d9ffd02b23040621ebb87337feff0;hp=1cf3cd6ad11b44402eacca11d415d9df60941916;hb=521cf1db00f8ce2d9f9494dca503d6c17d89ac2f;hpb=80bbee2f401b4af71cb1b80508bdb0d2bb61fa40 diff --git a/source/core/semaphore.h b/source/core/semaphore.h index 1cf3cd6..c55553a 100644 --- a/source/core/semaphore.h +++ b/source/core/semaphore.h @@ -1,10 +1,11 @@ -/* +/* $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" @@ -15,28 +16,36 @@ namespace Msp { class Semaphore { public: -#ifndef WIN32 - 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); } + Semaphore(); + Semaphore(Mutex &); + int signal(); + int broadcast(); + int wait(); 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); } -#endif + Mutex &get_mutex() { return *mutex; } + ~Semaphore(); private: -#ifndef WIN32 - Mutex mutex; -#endif + 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