-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_darwin.cpp
42 lines (38 loc) · 982 Bytes
/
event_darwin.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
#include "event.h"
#include <sys/event.h>
#include <cstring>
#include <iostream>
event::event(int fd)
: fd_(fd)
{
events_ = new struct kevent[2];
data_ = kqueue();
monitor_ = new struct kevent;
struct kevent &monitor = *static_cast<struct kevent*>(monitor_);
EV_SET(&monitor, fd_, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
}
event::~event()
{
if (events_)
{
delete[] static_cast<struct kevent*>(events_);
events_ = 0;
}
if (monitor_)
{
delete static_cast<struct kevent*>(monitor_);
monitor_ = 0;
}
}
bool event::wait(uint32_t timeout_usec)
{
struct kevent *monitor = static_cast<struct kevent*>(monitor_);
struct kevent *events = static_cast<struct kevent*>(events_);
int event_count = kevent(data_, monitor, 1, events, 1, 0);
if (event_count < 0)
{
std::cerr << "Error waiting for events: " << strerror(errno) << std::endl;
return false;
}
return true;
}