forked from HaxeFoundation/hxcpp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountingSemaphore.win32.cpp
More file actions
65 lines (54 loc) · 1.46 KB
/
CountingSemaphore.win32.cpp
File metadata and controls
65 lines (54 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <hxcpp.h>
#include <windows.h>
#include <memory>
#include <hx/thread/CountingSemaphore.hpp>
struct hx::thread::CountingSemaphore_obj::Impl
{
HANDLE semaphore;
static void finalise(hx::Object* obj)
{
auto impl = std::unique_ptr<Impl>(reinterpret_cast<hx::thread::CountingSemaphore_obj*>(obj)->impl);
CloseHandle(impl->semaphore);
}
};
hx::thread::CountingSemaphore_obj::CountingSemaphore_obj(int value) : impl(new Impl())
{
if (nullptr == (impl->semaphore = CreateSemaphoreW(nullptr, value, 0x7FFFFFF, nullptr)))
{
hx::Throw(HX_CSTRING("Failed to create semaphore"));
}
hx::GCSetFinalizer(this, Impl::finalise);
}
void hx::thread::CountingSemaphore_obj::acquire()
{
hx::EnterGCFreeZone();
if (NO_ERROR != WaitForSingleObject(impl->semaphore, INFINITE))
{
hx::ExitGCFreeZone();
hx::Throw(HX_CSTRING("Failed to wait for semaphore"));
}
hx::ExitGCFreeZone();
}
void hx::thread::CountingSemaphore_obj::release()
{
if (false == ReleaseSemaphore(impl->semaphore, 1, nullptr))
{
hx::Throw(HX_CSTRING("Failed to release semaphore"));
}
}
bool hx::thread::CountingSemaphore_obj::tryAcquire(Null<double> timeout)
{
hx::EnterGCFreeZone();
switch (WaitForSingleObject(impl->semaphore, static_cast<DWORD>(timeout.Default(0) * 1000)))
{
case NO_ERROR:
hx::ExitGCFreeZone();
return true;
case WAIT_TIMEOUT:
hx::ExitGCFreeZone();
return false;
default:
hx::ExitGCFreeZone();
return hx::Throw(HX_CSTRING("Failed to wait for semaphore"));
}
}