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>
8 #include "mutex_private.h"
13 struct Semaphore::Private
22 Semaphore::Semaphore(unsigned limit):
25 pthread_cond_init(&priv->cond, 0);
30 Semaphore::~Semaphore()
32 pthread_cond_destroy(&priv->cond);
36 void Semaphore::signal()
38 MutexLock mlock(priv->mutex);
39 if(priv->count<priv->limit)
41 if(int err = pthread_cond_signal(&priv->cond))
42 throw system_error("pthread_cond_signal", err);
45 void Semaphore::wait()
47 MutexLock mlock(priv->mutex);
49 if(int err = pthread_cond_wait(&priv->cond, &priv->mutex.priv->mutex))
50 throw system_error("pthread_cond_wait", err);
54 bool Semaphore::wait(const Time::TimeDelta &d)
56 timespec timeout = Time::rawtime_to_timespec((Time::now()+d).raw());
58 int err = pthread_cond_timedwait(&priv->cond, &priv->mutex.priv->mutex, &timeout);
59 if(err && err!=ETIMEDOUT)
60 throw system_error("pthread_cond_timedwait", err);