-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemaphore.hpp
45 lines (34 loc) · 948 Bytes
/
semaphore.hpp
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
#pragma once
#include <poll.h>
#include <semaphore.h>
#include <cstdint>
#include <cstdio>
#include <thread.hpp>
#include "bsp_time.h"
namespace System {
class Semaphore {
public:
Semaphore(uint32_t init_count) { sem_init(&this->handle_, 0, init_count); }
~Semaphore() { sem_destroy(&this->handle_); }
void Post() { sem_post(&this->handle_); }
bool Wait(uint32_t timeout = UINT32_MAX) {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
uint32_t secs = timeout / 1000;
timeout = timeout % 1000;
uint32_t add = 0;
long raw_time = timeout * 1000U * 1000U + ts.tv_nsec;
add = raw_time / (1000U * 1000U * 1000U);
ts.tv_sec += (add + secs);
ts.tv_nsec = raw_time % (1000U * 1000U * 1000U);
return sem_timedwait(&handle_, &ts) == 0;
}
uint32_t Value() {
int value = 0;
sem_getvalue(&handle_, &value);
return value;
}
private:
sem_t handle_;
};
} // namespace System