]> git.tdb.fi Git - libs/core.git/blob - source/core/unix/semaphore.cpp
6a4bacf9defddb911244ba44ced8fd82121e6d97
[libs/core.git] / source / core / unix / semaphore.cpp
1 #include <sys/time.h>
2 #include <cerrno>
3 #include <msp/core/systemerror.h>
4 #include <msp/time/rawtime_private.h>
5 #include <msp/time/timestamp.h>
6 #include <msp/time/utils.h>
7 #include "mutex.h"
8 #include "mutex_private.h"
9 #include "semaphore.h"
10
11 namespace Msp {
12
13 struct Semaphore::Private
14 {
15         Mutex mutex;
16         pthread_cond_t cond;
17         unsigned limit = 1;
18         unsigned count = 0;
19 };
20
21
22 Semaphore::Semaphore(unsigned limit):
23         priv(new Private)
24 {
25         pthread_cond_init(&priv->cond, 0);
26         priv->limit = limit;
27 }
28
29 Semaphore::~Semaphore()
30 {
31         pthread_cond_destroy(&priv->cond);
32         delete priv;
33 }
34
35 void Semaphore::signal()
36 {
37         MutexLock mlock(priv->mutex);
38         if(priv->count<priv->limit)
39                 ++priv->count;
40         if(int err = pthread_cond_signal(&priv->cond))
41                 throw system_error("pthread_cond_signal", err);
42 }
43
44 void Semaphore::wait()
45
46         MutexLock mlock(priv->mutex);
47         while(!priv->count)
48                 if(int err = pthread_cond_wait(&priv->cond, &priv->mutex.priv->mutex))
49                         throw system_error("pthread_cond_wait", err);
50         --priv->count;
51 }
52
53 bool Semaphore::wait(const Time::TimeDelta &d)
54 {
55         timespec timeout = Time::rawtime_to_timespec((Time::now()+d).raw());
56
57         int err = pthread_cond_timedwait(&priv->cond, &priv->mutex.priv->mutex, &timeout);
58         if(err && err!=ETIMEDOUT)
59                 throw system_error("pthread_cond_timedwait", err);
60         return err==0;
61 }
62
63 } // namespace Msp