-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignal.cpp
More file actions
75 lines (63 loc) · 1.68 KB
/
Copy pathsignal.cpp
File metadata and controls
75 lines (63 loc) · 1.68 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
#ifdef CBS_WIN32
void Signal::create(this Signal& self) {
self.event = ::CreateEvent(nullptr, TRUE, FALSE, nullptr);
if (self.event == nullptr) {
ctk::panic("::CreateEvent failed");
}
}
void Signal::destroy(this Signal& self) {
::CloseHandle(self.event);
}
void Signal::trigger(this Signal& self) {
::SetEvent(self.event);
}
void Signal::wait(this Signal& self) {
::WaitForSingleObject(self.event, INFINITE);
}
bool Signal::try_wait(this Signal& self) {
DWORD result = ::WaitForSingleObject(self.event, 0);
return result == WAIT_OBJECT_0;
}
void Signal::reset(this Signal& self) {
::ResetEvent(self.event);
}
#endif
#ifdef CBS_LINUX
void Signal::create(this Signal& self) {
if (::pthread_mutex_init(&self.mutex, nullptr) != 0) {
ctk::panic("::pthread_mutex_init failed");
}
if (::pthread_cond_init(&self.cond, nullptr) != 0) {
ctk::panic("::pthread_cond_init failed");
}
self.flag = false;
}
void Signal::destroy(this Signal& self) {
::pthread_mutex_destroy(&self.mutex);
::pthread_cond_destroy(&self.cond);
}
void Signal::trigger(this Signal& self) {
::pthread_mutex_lock(&self.mutex);
self.flag = true;
::pthread_cond_broadcast(&self.cond);
::pthread_mutex_unlock(&self.mutex);
}
void Signal::wait(this Signal& self) {
::pthread_mutex_lock(&self.mutex);
while (self.flag == false) {
::pthread_cond_wait(&self.cond, &self.mutex);
}
::pthread_mutex_unlock(&self.mutex);
}
bool Signal::try_wait(this Signal& self) {
::pthread_mutex_lock(&self.mutex);
bool triggered = self.flag;
::pthread_mutex_unlock(&self.mutex);
return triggered;
}
void Signal::reset(this Signal& self) {
::pthread_mutex_lock(&self.mutex);
self.flag = false;
::pthread_mutex_unlock(&self.mutex);
}
#endif