]> git.tdb.fi Git - libs/core.git/blob - source/core/windows/semaphore.cpp
Avoid leaking external references through default c'tors and d'tors
[libs/core.git] / source / core / windows / semaphore.cpp
1 #include <windows.h>
2 #include <msp/core/systemerror.h>
3 #include <msp/time/units.h>
4 #include "semaphore.h"
5
6 namespace Msp {
7
8 struct Semaphore::Private
9 {
10         HANDLE handle;
11 };
12
13
14 Semaphore::Semaphore(unsigned limit):
15         priv(new Private)
16 {
17         priv->handle = CreateSemaphore(0, 0, limit, 0);
18 }
19
20 Semaphore::~Semaphore()
21 {
22         CloseHandle(priv->handle);
23         delete priv;
24 }
25
26 void Semaphore::signal()
27 {
28         if(!ReleaseSemaphore(priv->handle, 1, 0))
29                 throw system_error("ReleaseSemaphore");
30 }
31
32 void Semaphore::wait()
33
34         DWORD ret = WaitForSingleObject(priv->handle, INFINITE);
35         if(ret==WAIT_FAILED)
36                 throw system_error("WaitForSingleObject");
37 }
38
39 bool Semaphore::wait(const Time::TimeDelta &d)
40 {
41         DWORD ret = WaitForSingleObject(priv->handle, (DWORD)(d/Time::usec));
42         if(ret==WAIT_FAILED)
43                 throw system_error("WaitForSingleObject");
44         return ret==WAIT_OBJECT_0;
45 }
46
47 } // namespace Msp