-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathepoll.c
129 lines (109 loc) · 3.44 KB
/
epoll.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
127
128
129
// Copyright (c) 2018-2020 The EFramework Project
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "poll.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/epoll.h>
typedef struct epoll_event epoll_event_t;
typedef struct _ef_epoll {
ef_poll_t poll;
int epfd;
int cap;
epoll_event_t events[0];
} ef_epoll_t;
static int ef_epoll_associate(ef_poll_t *p, int fd, int events, void *ptr, int fired)
{
ef_epoll_t *ep;
epoll_event_t *e;
/*
* epoll will not auto dissociate fd after event fired
*/
if (fired) {
return 0;
}
ep = (ef_epoll_t *)p;
e = &ep->events[0];
e->events = events;
e->data.ptr = ptr;
return epoll_ctl(ep->epfd, EPOLL_CTL_ADD, fd, e);
}
static int ef_epoll_dissociate(ef_poll_t *p, int fd, int fired, int onclose)
{
ef_epoll_t *ep = (ef_epoll_t *)p;
epoll_event_t *e = &ep->events[0];
return epoll_ctl(ep->epfd, EPOLL_CTL_DEL, fd, e);
}
static int ef_epoll_unset(ef_poll_t *p, int fd, int events)
{
return 0;
}
static int ef_epoll_wait(ef_poll_t *p, ef_event_t *evts, int count, int millisecs)
{
int ret, idx;
ef_epoll_t *ep = (ef_epoll_t *)p;
if (count > ep->cap) {
count = ep->cap;
}
ret = epoll_wait(ep->epfd, &ep->events[0], count, millisecs);
if (ret <= 0) {
return ret;
}
for (idx = 0; idx < ret; ++idx) {
evts[idx].events = ep->events[idx].events;
evts[idx].ptr = ep->events[idx].data.ptr;
}
return ret;
}
static int ef_epoll_free(ef_poll_t *p)
{
ef_epoll_t *ep = (ef_epoll_t *)p;
close(ep->epfd);
free(ep);
return 0;
}
static ef_poll_t *ef_epoll_create(int cap)
{
ef_epoll_t *ep;
size_t size = sizeof(ef_epoll_t);
/*
* event buffer at least 128
*/
if (cap < 128) {
cap = 128;
}
size += sizeof(epoll_event_t) * cap;
ep = (ef_epoll_t *)malloc(size);
if (!ep) {
return NULL;
}
ep->epfd = epoll_create1(EPOLL_CLOEXEC);
if (ep->epfd < 0) {
free(ep);
return NULL;
}
ep->poll.associate = ef_epoll_associate;
ep->poll.dissociate = ef_epoll_dissociate;
ep->poll.unset = ef_epoll_unset;
ep->poll.wait = ef_epoll_wait;
ep->poll.free = ef_epoll_free;
ep->cap = cap;
return &ep->poll;
}
create_func_t ef_create_poll = ef_epoll_create;