3 This file is part of libmspcore
4 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
11 #include "semaphore.h"
12 #include "../time/timestamp.h"
13 #include "../time/units.h"
14 #include "../time/utils.h"
18 Semaphore::Semaphore():
25 Semaphore::Semaphore(Mutex &m):
33 int Semaphore::signal()
38 int ret=!ReleaseSemaphore(sem, 1, 0);
40 unsigned old_count=count;
42 while(count==old_count)
49 int Semaphore::broadcast()
53 int ret=!ReleaseSemaphore(sem, count, 0);
67 DWORD ret=WaitForSingleObject(sem, INFINITE);
71 return ret==WAIT_OBJECT_0;
75 int Semaphore::wait(const Time::TimeDelta &d)
78 Time::TimeStamp ts=Time::now()+d;
81 timeout.tv_sec=ts.raw()/1000000;
82 timeout.tv_nsec=(ts.raw()%1000000)*1000;
84 int r=pthread_cond_timedwait(&sem, &mutex->mutex, &timeout);
93 DWORD ret=WaitForSingleObject(sem, (DWORD)(d/Time::usec));
96 return ret==WAIT_OBJECT_0;
100 Semaphore::~Semaphore()
107 pthread_cond_destroy(&sem);
111 void Semaphore::init()
115 sem=CreateSemaphore(0, 0, 32, 0);
117 pthread_cond_init(&sem, 0);