-
Notifications
You must be signed in to change notification settings - Fork 0
/
ev.c
126 lines (102 loc) · 2.02 KB
/
ev.c
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <pthread.h>
#include "driver.h"
#define EV_COUNT 64
#define EV_TIMEOUT 10
#define array_size(x) (sizeof(x) / sizeof((x)[0]))
typedef void (*ev_callback)(int rank, int events);
struct driver {
int ep;
int size;
int timeout;
ev_callback cb[0];
};
struct driver *
ev_start(int size)
{
struct driver *d;
int rc;
d = malloc(sizeof(*d) + size * sizeof(ev_callback));
if (!d)
err(1, "ev_start(malloc)");
rc = epoll_create(1);
if (rc == -1)
err(1, "epoll_create()");
d->ep = rc;
d->size = size;
d->timeout = EV_TIMEOUT;
return d;
}
void
ev_stop(struct driver *d)
{
close(d->ep);
free(d);
}
void
ev_register(struct driver *d, int fd, int rank, ev_callback cb, int events)
{
struct epoll_event ev;
int rc;
if (rank >= d->size)
errx(1, "rank %d >= size %d", rank, d->size);
d->cb[rank] = cb;
ev.events = events;
ev.data.ptr = &d->cb[rank];
rc = epoll_ctl(d->ep, EPOLL_CTL_ADD, fd, &ev);
if (rc == -1 && errno == EEXIST)
rc = epoll_ctl(d->ep, EPOLL_CTL_MOD, fd, &ev);
if (rc == -1)
err(1, "epoll_add/mod()");
}
void
ev_unregister(struct driver *d, int fd)
{
int rc;
rc = epoll_ctl(d->ep, EPOLL_CTL_DEL, fd, NULL);
if (rc == -1)
err(1, "epoll_del()");
}
void
ev_loop(struct driver *d, bool (*stop_fn)(void))
{
struct epoll_event ev[EV_COUNT];
ev_callback *cb;
int i, n, rank;
while (!stop_fn()) {
n = epoll_wait(d->ep, ev, array_size(ev), d->timeout);
if (n == 0)
continue;
if (n == -1) {
if (errno == EINTR)
continue;
err(1, "epoll_wait");
}
for (i = 0; i < n; i++) {
cb = ev[i].data.ptr;
rank = cb - d->cb;
(*cb)(rank, ev[i].events);
}
}
}
static bool
ev_poll_toggle(void)
{
static bool stop = true;
stop = !stop;
return stop;
}
void
ev_poll(struct driver *d)
{
ev_loop(d, ev_poll_toggle);
}
/*---------------------------------------------------------------------------*/
struct io_driver *io_driver;