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
68
69
70
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "native_epoll.h"
#include <cassert>
#include <cerrno>
#include <cstring>
#include <unistd.h>
#include <vespa/log/log.h>
namespace vespalib {
namespace {
uint32_t maybe(uint32_t value, bool yes) { return yes ? value : 0; }
void check(int res) {
if (res == -1) {
if (errno == ENOMEM) {
LOG_ABORT("out of memory");
}
}
}
}
Epoll::Epoll()
: _epoll_fd(epoll_create1(0))
{
assert(_epoll_fd != -1);
}
Epoll::~Epoll()
{
close(_epoll_fd);
}
void
Epoll::add(int fd, void *ctx, bool read, bool write)
{
epoll_event evt;
evt.events = maybe(EPOLLIN, read) | maybe(EPOLLOUT, write);
evt.data.ptr = ctx;
check(epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, fd, &evt));
}
void
Epoll::update(int fd, void *ctx, bool read, bool write)
{
epoll_event evt;
evt.events = maybe(EPOLLIN, read) | maybe(EPOLLOUT, write);
evt.data.ptr = ctx;
check(epoll_ctl(_epoll_fd, EPOLL_CTL_MOD, fd, &evt));
}
void
Epoll::remove(int fd)
{
epoll_event evt;
memset(&evt, 0, sizeof(evt));
check(epoll_ctl(_epoll_fd, EPOLL_CTL_DEL, fd, &evt));
}
size_t
Epoll::wait(epoll_event *events, size_t max_events, int timeout_ms)
{
int res = epoll_wait(_epoll_fd, events, max_events, timeout_ms);
return std::max(res, 0);
}
}
|