-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemaphoreMacOS.cpp
More file actions
67 lines (56 loc) · 1.46 KB
/
SemaphoreMacOS.cpp
File metadata and controls
67 lines (56 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
66
67
#include <semaphore.h>
#include <stdlib.h>
#include <unistd.h>
#include "Help.h"
#include "Semaphore.h"
class _Semaphore {
const char* name;
bool unlink;
sem_t* semaphore;
public:
_Semaphore(const char* name, bool create, bool state)
: name(name), unlink(create) {
if (unlink) {
sem_unlink(name);
}
if (create) {
semaphore = sem_open(name, O_CREAT, 0666, state ? 1 : 0);
} else {
semaphore = sem_open(name, 0);
}
if (semaphore == SEM_FAILED) {
TRACE("%s open failed; Probably CLAVM is not running\n", name);
exit(1);
}
}
_Semaphore() {
sem_close(semaphore);
if (unlink) {
sem_unlink(name);
}
sem_unlink(name);
}
bool wait(double timeout) {
if (timeout == 0.0) {
sem_wait(semaphore);
return true;
}
for (int i = 0; i < 10; i++) {
if (0 == sem_trywait(semaphore)) {
return true;
}
usleep(static_cast<int>(timeout / 10000000));
}
return false;
}
bool trywait() {
return 0 == sem_trywait(semaphore);
}
void post() { sem_post(semaphore); }
};
Semaphore::Semaphore(const char* name, bool create, bool state)
: implementation(new _Semaphore(name, create, state)) {}
Semaphore::~Semaphore() { delete implementation; }
bool Semaphore::wait(double timeout) { return implementation->wait(timeout); }
bool Semaphore::trywait() { return implementation->trywait(); }
void Semaphore::post() { implementation->post(); }