-
Notifications
You must be signed in to change notification settings - Fork 2
/
Event.hpp
46 lines (45 loc) · 1.04 KB
/
Event.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
46
#pragma once
#include <sys/eventfd.h>
#include <poll.h>
#include <unistd.h>
class Event {
friend class MultiEventWait;
private:
int fd_;
public:
Event(uint64_t initial = 0) {
fd_ = eventfd(initial, EFD_NONBLOCK);
}
Event(const Event ©from) = delete;
Event(Event &&movefrom) {
this->fd_ = movefrom.fd_;
movefrom.fd_ = -1;
}
~Event() {
if (fd_<0) return;
close(fd_);
fd_ = -1;
}
uint64_t wait(int timeout_ms = -1, bool poll_only = false) {
struct pollfd pfd;
int64_t val = 0;
pfd.fd = this->fd_;
pfd.events = POLLIN;
pfd.revents = 0;
//do {
poll(&pfd, 1, timeout_ms);
//} while(!(pfd.revents & POLLIN));
if (!poll_only) {
if (pfd.revents & POLLIN) {
read(this->fd_, &val, sizeof val);
}
}
return val;
}
void signal(uint64_t val = 1) {
write(this->fd_, &val, sizeof val);
}
int fd() const {
return fd_;
}
};