-
Notifications
You must be signed in to change notification settings - Fork 1
/
poll.c
117 lines (102 loc) · 2.51 KB
/
poll.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
#include "poll.h"
#include <errno.h>
#include <malloc.h>
#include <sys/epoll.h>
#include <unistd.h>
#define EPOLL_MAX_EVENTS 64
struct poll {
int epoll_fd;
};
struct poll* poll_create() {
int epoll_fd = epoll_create1(0);
if (epoll_fd < 0) {
return NULL;
}
struct poll* p = malloc(sizeof(struct poll));
p->epoll_fd = epoll_fd;
return p;
}
void poll_destroy(struct poll* p) {
close(p->epoll_fd);
free(p);
}
struct poll_task {
void* data;
bool one_shot;
poll_callback callback;
};
int poll_submit_event(
struct poll* p,
int fd,
void* data,
uint32_t base_events,
bool one_shot,
bool edge_triggered,
poll_callback callback) {
/*
* FIXME:
* since we only free the task when it's completed, submitting another task before the first one is completed
* on the same fd results in `task` being leaked.
*/
struct poll_task* task = malloc(sizeof(struct poll_task));
task->data = data;
task->one_shot = one_shot;
task->callback = callback;
struct epoll_event event;
event.data.ptr = task;
event.events = base_events;
if (one_shot) {
event.events |= EPOLLONESHOT;
}
if (edge_triggered) {
event.events |= EPOLLET;
}
// try `mod` first, then `add` if `mod` fails
if (epoll_ctl(p->epoll_fd, EPOLL_CTL_MOD, fd, &event) < 0) {
if (errno != ENOENT) {
free(task);
return -1;
}
if (epoll_ctl(p->epoll_fd, EPOLL_CTL_ADD, fd, &event) < 0) {
free(task);
return -1;
}
}
return 0;
}
int poll_wait_for_readability(
struct poll* p,
int fd,
void* data,
bool one_shot,
bool edge_triggered,
poll_callback callback) {
return poll_submit_event(p, fd, data, EPOLLIN, one_shot, edge_triggered, callback);
}
int poll_wait_for_writability(
struct poll* p,
int fd,
void* data,
bool one_shot,
bool edge_triggered,
poll_callback callback) {
return poll_submit_event(p, fd, data, EPOLLOUT, one_shot, edge_triggered, callback);
}
int poll_run(struct poll* p) {
struct epoll_event events[EPOLL_MAX_EVENTS];
while (1) {
int num_events = epoll_wait(p->epoll_fd, events, EPOLL_MAX_EVENTS, -1);
if (num_events < 0) {
return num_events;
}
for (int i = 0; i < num_events; i++) {
struct poll_task* task = events[i].data.ptr;
task->callback(p, task->data);
// If it's one-shot, this task will not be used again.
// Otherwise, subsequent notifications will return the same task pointer.
if (task->one_shot) {
free(task);
}
}
}
}