-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheventfd.cpp
39 lines (34 loc) · 881 Bytes
/
eventfd.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
#include "eventfd.hpp"
#include <sys/eventfd.h>
#include <cstdlib>
#include <cassert>
#include <unistd.h>
#include <iostream>
#include <cstring>
using namespace std;
namespace mutils {
eventfd::eventfd(bool block_on_first_wait)
:fd(::eventfd((block_on_first_wait ? 0 : 1),0)){}
eventfd::~eventfd(){
close(fd);
}
void eventfd::wait(){
buf_t buf{0};
auto result = read(fd,&buf,sizeof(buf));
assert(result != -1);
if (result == -1) {std::cerr << std::strerror(errno) << std::endl; throw result;}
assert(result == sizeof(buf));
}
void eventfd::clear(){
notify();
wait();
}
void eventfd::notify(){
buf_t buf{1};
auto result = write(fd,&buf,sizeof(buf));
assert(result != -1);
if (result == -1) {std::cerr << std::strerror(errno) << std::endl; throw result;}
assert(result == sizeof(buf));
}
int eventfd::underlying_fd() const { return fd;}
}