]> git.tdb.fi Git - libs/core.git/blob - source/core/semaphore.h
Assimilate exceptions and RefPtr from mspmisc
[libs/core.git] / source / core / semaphore.h
1 /* $Id$
2
3 This file is part of libmspcore
4 Copyright © 2006  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7 #ifndef MSP_CORE_SEMAPHORE_H_
8 #define MSP_CORE_SEMAPHORE_H_
9
10 #include "mutex.h"
11 #include "types.h"
12 #include "../time/timedelta.h"
13
14 namespace Msp {
15
16 class Semaphore
17 {
18 public:
19         Semaphore();
20         Semaphore(Mutex &);
21         int   signal();
22         int   broadcast();
23         int   wait();
24         int   wait(const Time::TimeDelta &);
25         Mutex &get_mutex() { return *mutex; }
26         ~Semaphore();
27 private:
28         Mutex *mutex;
29         bool  own_mutex;
30         SemaphoreHandle sem;
31 #ifdef WIN32
32         unsigned count;
33 #endif
34
35         void init();
36 };
37
38 #ifndef WIN32
39 inline int Semaphore::signal()
40 { return pthread_cond_signal(&sem); }
41
42 inline int Semaphore::broadcast()
43 { return pthread_cond_broadcast(&sem); }
44
45 inline int Semaphore::wait()
46 { return pthread_cond_wait(&sem, &mutex->mutex); }
47 #endif
48
49 }
50
51 #endif