3 This file is part of libmspcore
4 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #include "../time/timestamp.h"
11 #include "../time/units.h"
12 #include "../time/utils.h"
16 Semaphore::Semaphore():
23 Semaphore::Semaphore(Mutex &m):
31 int Semaphore::signal()
36 int ret=!ReleaseSemaphore(sem, 1, 0);
38 unsigned old_count=count;
40 while(count==old_count)
47 int Semaphore::broadcast()
51 int ret=!ReleaseSemaphore(sem, count, 0);
65 DWORD ret=WaitForSingleObject(sem, INFINITE);
69 return ret==WAIT_OBJECT_0;
73 int Semaphore::wait(const Time::TimeDelta &d)
76 Time::TimeStamp ts=Time::now()+d;
79 timeout.tv_sec=ts.raw()/1000000;
80 timeout.tv_nsec=(ts.raw()%1000000)*1000;
82 int r=pthread_cond_timedwait(&sem, &mutex->mutex, &timeout);
91 DWORD ret=WaitForSingleObject(sem, (DWORD)(d/Time::usec));
94 return ret==WAIT_OBJECT_0;
98 Semaphore::~Semaphore()
105 pthread_cond_destroy(&sem);
109 void Semaphore::init()
113 sem=CreateSemaphore(0, 0, 32, 0);
115 pthread_cond_init(&sem, 0);