]> git.tdb.fi Git - libs/core.git/blob - source/core/semaphore.cpp
Semaphore is now win32-compatible, I hope
[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/utils.h"
11
12 namespace Msp {
13
14 int Semaphore::wait(const Time::TimeDelta &d)
15 {
16 #ifndef WIN32
17         Time::TimeStamp ts=Time::now()+d;
18
19         timespec timeout;
20         timeout.tv_sec=ts.raw()/1000000;
21         timeout.tv_nsec=(ts.raw()%1000000)*1000;
22
23         MutexLock l(mutex);
24         int r=pthread_cond_timedwait(&sem, &mutex.mutex, &timeout);
25         if(r==ETIMEDOUT)
26                 return 1;
27         else if(r)
28                 return -1;
29         return 0;
30 #else
31         return WaitForSingleObject(sem, (DWORD)(d/Time::usec))==WAIT_OBJECT_0;
32 #endif
33 }
34
35 } // namespace Msp