-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathCountingSemaphore.posix.cpp
More file actions
92 lines (80 loc) · 1.79 KB
/
CountingSemaphore.posix.cpp
File metadata and controls
92 lines (80 loc) · 1.79 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <hxcpp.h>
#include <semaphore.h>
#include <sys/time.h>
#include <memory>
#include <cerrno>
#include <hx/thread/CountingSemaphore.hpp>
struct hx::thread::CountingSemaphore_obj::Impl
{
sem_t semaphore;
static void finalise(hx::Object* obj)
{
auto impl = std::unique_ptr<Impl>(reinterpret_cast<hx::thread::CountingSemaphore_obj*>(obj)->impl);
sem_destroy(&impl->semaphore);
}
};
hx::thread::CountingSemaphore_obj::CountingSemaphore_obj(int value) : impl(new Impl())
{
if (0 != (sem_init(&impl->semaphore, false, value)))
{
hx::Throw(HX_CSTRING("Failed to create semaphore"));
}
hx::GCSetFinalizer(this, Impl::finalise);
}
void hx::thread::CountingSemaphore_obj::acquire()
{
hx::EnterGCFreeZone();
if (0 != sem_wait(&impl->semaphore))
{
hx::ExitGCFreeZone();
hx::Throw(HX_CSTRING("Failed to wait on semaphore"));
}
hx::ExitGCFreeZone();
}
void hx::thread::CountingSemaphore_obj::release()
{
if (0 != sem_post(&impl->semaphore))
{
hx::Throw(HX_CSTRING("Failed to release semaphore"));
}
}
bool hx::thread::CountingSemaphore_obj::tryAcquire(Null<double> timeout)
{
if (0 == timeout.Default(0))
{
return 0 == sem_trywait(&impl->semaphore);
}
else
{
hx::EnterGCFreeZone();
struct timeval tv;
struct timespec t;
auto delta = timeout.value;
int idelta = (int)delta, idelta2;
delta -= idelta;
delta *= 1.0e9;
gettimeofday(&tv, NULL);
delta += tv.tv_usec * 1000.0;
idelta2 = (int)(delta / 1e9);
delta -= idelta2 * 1e9;
t.tv_sec = tv.tv_sec + idelta + idelta2;
t.tv_nsec = (long)delta;
if (0 == sem_timedwait(&impl->semaphore, &t))
{
hx::ExitGCFreeZone();
return true;
}
else
{
hx::ExitGCFreeZone();
if (errno == ETIMEDOUT)
{
return false;
}
else
{
return hx::Throw(HX_CSTRING("Failed to wait for semaphore"));
}
}
}
}