1 | /* |
1 | /* |
2 | * libev epoll fd activity backend |
2 | * libev epoll fd activity backend |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libev@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
… | |
… | |
47 | * b) the fact that ADD != MOD creates a lot of extra syscalls due to a) |
47 | * b) the fact that ADD != MOD creates a lot of extra syscalls due to a) |
48 | * and seems not to have any advantage. |
48 | * and seems not to have any advantage. |
49 | * c) the inability to handle fork or file descriptors (think dup) |
49 | * c) the inability to handle fork or file descriptors (think dup) |
50 | * limits the applicability over poll, so this is not a generic |
50 | * limits the applicability over poll, so this is not a generic |
51 | * poll replacement. |
51 | * poll replacement. |
|
|
52 | * d) epoll doesn't work the same as select with many file descriptors |
|
|
53 | * (such as files). while not critical, no other advanced interface |
|
|
54 | * seems to share this (rather non-unixy) limitation. |
|
|
55 | * e) epoll claims to be embeddable, but in practise you never get |
|
|
56 | * a ready event for the epoll fd. |
52 | * |
57 | * |
53 | * lots of "weird code" and complication handling in this file is due |
58 | * lots of "weird code" and complication handling in this file is due |
54 | * to these design problems with epoll, as we try very hard to avoid |
59 | * to these design problems with epoll, as we try very hard to avoid |
55 | * epoll_ctl syscalls for common usage patterns and handle the breakage |
60 | * epoll_ctl syscalls for common usage patterns and handle the breakage |
56 | * ensuing from receiving events for closed and otherwise long gone |
61 | * ensuing from receiving events for closed and otherwise long gone |
… | |
… | |
119 | epoll_poll (EV_P_ ev_tstamp timeout) |
124 | epoll_poll (EV_P_ ev_tstamp timeout) |
120 | { |
125 | { |
121 | int i; |
126 | int i; |
122 | int eventcnt; |
127 | int eventcnt; |
123 | |
128 | |
|
|
129 | /* epoll wait times cannot be larger than (LONG_MAX - 999UL) / HZ msecs, which is below */ |
|
|
130 | /* the default libev max wait time, however. */ |
124 | EV_RELEASE_CB; |
131 | EV_RELEASE_CB; |
125 | eventcnt = epoll_wait (backend_fd, epoll_events, epoll_eventmax, (int)ceil (timeout * 1000.)); |
132 | eventcnt = epoll_wait (backend_fd, epoll_events, epoll_eventmax, (int)ceil (timeout * 1000.)); |
126 | EV_ACQUIRE_CB; |
133 | EV_ACQUIRE_CB; |
127 | |
134 | |
128 | if (expect_false (eventcnt < 0)) |
135 | if (expect_false (eventcnt < 0)) |
… | |
… | |
157 | /* we received an event but are not interested in it, try mod or del */ |
164 | /* we received an event but are not interested in it, try mod or del */ |
158 | /* I don't think we ever need MOD, but let's handle it anyways */ |
165 | /* I don't think we ever need MOD, but let's handle it anyways */ |
159 | ev->events = (want & EV_READ ? EPOLLIN : 0) |
166 | ev->events = (want & EV_READ ? EPOLLIN : 0) |
160 | | (want & EV_WRITE ? EPOLLOUT : 0); |
167 | | (want & EV_WRITE ? EPOLLOUT : 0); |
161 | |
168 | |
|
|
169 | /* pre-2.6.9 kernels require a non-null pointer with EPOLL_CTL_DEL, */ |
|
|
170 | /* which is fortunately easy to do for us. */ |
162 | if (epoll_ctl (backend_fd, want ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, fd, ev)) |
171 | if (epoll_ctl (backend_fd, want ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, fd, ev)) |
163 | { |
172 | { |
164 | postfork = 1; /* an error occured, recreate kernel state */ |
173 | postfork = 1; /* an error occured, recreate kernel state */ |
165 | continue; |
174 | continue; |
166 | } |
175 | } |
… | |
… | |
179 | } |
188 | } |
180 | |
189 | |
181 | int inline_size |
190 | int inline_size |
182 | epoll_init (EV_P_ int flags) |
191 | epoll_init (EV_P_ int flags) |
183 | { |
192 | { |
|
|
193 | #ifdef EPOLL_CLOEXEC |
|
|
194 | backend_fd = epoll_create1 (EPOLL_CLOEXEC); |
|
|
195 | |
|
|
196 | if (backend_fd <= 0) |
|
|
197 | #endif |
184 | backend_fd = epoll_create (256); |
198 | backend_fd = epoll_create (256); |
185 | |
199 | |
186 | if (backend_fd < 0) |
200 | if (backend_fd < 0) |
187 | return 0; |
201 | return 0; |
188 | |
202 | |
189 | fcntl (backend_fd, F_SETFD, FD_CLOEXEC); |
203 | fcntl (backend_fd, F_SETFD, FD_CLOEXEC); |