-
Notifications
You must be signed in to change notification settings - Fork 0
/
SemaphoreWindows.cpp
63 lines (51 loc) · 1.5 KB
/
SemaphoreWindows.cpp
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
#include "Help.h"
#include "Semaphore.h"
#include <windows.h>
#include <iostream>
class _Semaphore {
HANDLE semaphore;
public:
_Semaphore(const char* name, bool create, bool unlink, bool state)
: name(name), unlink(unlink) {
if (unlink) {
sem_unlink(name);
}
// XXX
// - how to unlink a windows semaphore?
// - how do we fail if the semaphore does not exists?
// - how do we set the initial state?
// XXX
// - can we use a simple const char* or do we need a L"" ?
semaphore = OpenSemaphore(
SEMAPHORE_ALL_ACCESS, // Request full access
FALSE, // Handle not inheritable
name); // Name of semaphore
if (semaphore == NULL) {
TRACE("%s open failed; Probably CLAVM is not running\n", name);
exit(1);
}
}
_Semaphore() {
// XXX
// - check result
CloseHandle(semaphore);
}
bool wait(double timeout) {
// XXX
// - check result
// - use timeout
WaitForSingleObject(semaphore, INFINITE);
return true;
}
void post() {
// XXX
// - what do the arguments mean?
// - check result
ReleaseSemaphore(semaphore, 1, NULL);
}
};
Semaphore::Semaphore(const char* name, bool create, bool unlink, bool state)
: implementation(new _Semaphore(name, create, unlink, state)) {}
Semaphore::~Semaphore() { delete implementation; }
bool Semaphore::wait(double timeout) { return implementation->wait(timeout); }
void Semaphore::post() { implementation->post(); }