]> git.tdb.fi Git - libs/core.git/blob - source/core/semaphore.cpp
7006eafc51ae5e8c43d938c9f8d04824fbc5e03c
[libs/core.git] / source / core / semaphore.cpp
1 /*
2 This file is part of libmspframework
3 Copyright © 2006  Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
5 */
6 #include <sys/time.h>
7 #include <errno.h>
8 #include "semaphore.h"
9 #include "../time/timestamp.h"
10 #include "../time/units.h"
11 #include "../time/utils.h"
12
13 namespace Msp {
14
15 int Semaphore::wait(const Time::TimeDelta &d)
16 {
17 #ifndef WIN32
18         Time::TimeStamp ts=Time::now()+d;
19
20         timespec timeout;
21         timeout.tv_sec=ts.raw()/1000000;
22         timeout.tv_nsec=(ts.raw()%1000000)*1000;
23
24         MutexLock l(mutex);
25         int r=pthread_cond_timedwait(&sem, &mutex.mutex, &timeout);
26         if(r==ETIMEDOUT)
27                 return 1;
28         else if(r)
29                 return -1;
30         return 0;
31 #else
32         return WaitForSingleObject(sem, (DWORD)(d/Time::usec))==WAIT_OBJECT_0;
33 #endif
34 }
35
36 } // namespace Msp