]> git.tdb.fi Git - libs/core.git/blob - source/core/semaphore.h
Merge branch 'strings-master'
[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
8 #ifndef MSP_CORE_SEMAPHORE_H_
9 #define MSP_CORE_SEMAPHORE_H_
10
11 #include "mutex.h"
12 #include "types.h"
13 #include "../time/timedelta.h"
14
15 namespace Msp {
16
17 class Semaphore
18 {
19 private:
20         Mutex *mutex;
21         bool  own_mutex;
22         SemaphoreHandle sem;
23 #ifdef WIN32
24         unsigned count;
25 #endif
26
27 public:
28         Semaphore();
29         Semaphore(Mutex &);
30 private:
31         void init();
32 public:
33         ~Semaphore();
34
35         int   signal();
36         int   broadcast();
37         int   wait();
38         int   wait(const Time::TimeDelta &);
39         Mutex &get_mutex() { return *mutex; }
40 };
41
42 #ifndef WIN32
43 inline int Semaphore::signal()
44 { return pthread_cond_signal(&sem); }
45
46 inline int Semaphore::broadcast()
47 { return pthread_cond_broadcast(&sem); }
48
49 inline int Semaphore::wait()
50 { return pthread_cond_wait(&sem, &mutex->mutex); }
51 #endif
52
53 }
54
55 #endif