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 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 | * |
… | |
… | |
68 | /* |
68 | /* |
69 | * we handle EPOLL_CTL_DEL by ignoring it here |
69 | * we handle EPOLL_CTL_DEL by ignoring it here |
70 | * on the assumption that the fd is gone anyways |
70 | * on the assumption that the fd is gone anyways |
71 | * if that is wrong, we have to handle the spurious |
71 | * if that is wrong, we have to handle the spurious |
72 | * event in epoll_poll. |
72 | * event in epoll_poll. |
73 | * the fd is later added, we try to ADD it, and, if that |
73 | * if the fd is added again, we try to ADD it, and, if that |
74 | * fails, we assume it still has the same eventmask. |
74 | * fails, we assume it still has the same eventmask. |
75 | */ |
75 | */ |
76 | if (!nev) |
76 | if (!nev) |
77 | return; |
77 | return; |
78 | |
78 | |
79 | oldmask = anfds [fd].emask; |
79 | oldmask = anfds [fd].emask; |
80 | anfds [fd].emask = nev; |
80 | anfds [fd].emask = nev; |
81 | |
81 | |
82 | /* store the generation counter in the upper 32 bits */ |
82 | /* store the generation counter in the upper 32 bits, the fd in the lower 32 bits */ |
83 | ev.data.u64 = fd | ((uint64_t)++anfds [fd].egen << 32); |
83 | ev.data.u64 = (uint64_t)(uint32_t)fd |
|
|
84 | | ((uint64_t)(uint32_t)++anfds [fd].egen << 32); |
84 | ev.events = (nev & EV_READ ? EPOLLIN : 0) |
85 | ev.events = (nev & EV_READ ? EPOLLIN : 0) |
85 | | (nev & EV_WRITE ? EPOLLOUT : 0); |
86 | | (nev & EV_WRITE ? EPOLLOUT : 0); |
86 | |
87 | |
87 | if (expect_true (!epoll_ctl (backend_fd, oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd, &ev))) |
88 | if (expect_true (!epoll_ctl (backend_fd, oev ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd, &ev))) |
88 | return; |
89 | return; |
… | |
… | |
116 | |
117 | |
117 | static void |
118 | static void |
118 | epoll_poll (EV_P_ ev_tstamp timeout) |
119 | epoll_poll (EV_P_ ev_tstamp timeout) |
119 | { |
120 | { |
120 | int i; |
121 | int i; |
|
|
122 | int eventcnt; |
|
|
123 | |
|
|
124 | /* epoll wait times cannot be larger than (LONG_MAX - 999UL) / HZ msecs, which is below */ |
|
|
125 | /* the default libev max wait time, however. */ |
|
|
126 | EV_RELEASE_CB; |
121 | int eventcnt = epoll_wait (backend_fd, epoll_events, epoll_eventmax, (int)ceil (timeout * 1000.)); |
127 | eventcnt = epoll_wait (backend_fd, epoll_events, epoll_eventmax, (int)ceil (timeout * 1000.)); |
|
|
128 | EV_ACQUIRE_CB; |
122 | |
129 | |
123 | if (expect_false (eventcnt < 0)) |
130 | if (expect_false (eventcnt < 0)) |
124 | { |
131 | { |
125 | if (errno != EINTR) |
132 | if (errno != EINTR) |
126 | syserr ("(libev) epoll_wait"); |
133 | ev_syserr ("(libev) epoll_wait"); |
127 | |
134 | |
128 | return; |
135 | return; |
129 | } |
136 | } |
130 | |
137 | |
131 | for (i = 0; i < eventcnt; ++i) |
138 | for (i = 0; i < eventcnt; ++i) |
… | |
… | |
136 | int want = anfds [fd].events; |
143 | int want = anfds [fd].events; |
137 | int got = (ev->events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0) |
144 | int got = (ev->events & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0) |
138 | | (ev->events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0); |
145 | | (ev->events & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0); |
139 | |
146 | |
140 | /* check for spurious notification */ |
147 | /* check for spurious notification */ |
141 | if (expect_false (anfds [fd].egen != (unsigned char)(ev->data.u64 >> 32))) |
148 | if (expect_false ((uint32_t)anfds [fd].egen != (uint32_t)(ev->data.u64 >> 32))) |
142 | { |
149 | { |
143 | /* recreate kernel state */ |
150 | /* recreate kernel state */ |
144 | postfork = 1; |
151 | postfork = 1; |
145 | continue; |
152 | continue; |
146 | } |
153 | } |
… | |
… | |
174 | } |
181 | } |
175 | |
182 | |
176 | int inline_size |
183 | int inline_size |
177 | epoll_init (EV_P_ int flags) |
184 | epoll_init (EV_P_ int flags) |
178 | { |
185 | { |
|
|
186 | #ifdef EPOLL_CLOEXEC |
|
|
187 | backend_fd = epoll_create1 (EPOLL_CLOEXEC); |
|
|
188 | |
|
|
189 | if (backend_fd <= 0) |
|
|
190 | #endif |
179 | backend_fd = epoll_create (256); |
191 | backend_fd = epoll_create (256); |
180 | |
192 | |
181 | if (backend_fd < 0) |
193 | if (backend_fd < 0) |
182 | return 0; |
194 | return 0; |
183 | |
195 | |
184 | fcntl (backend_fd, F_SETFD, FD_CLOEXEC); |
196 | fcntl (backend_fd, F_SETFD, FD_CLOEXEC); |
… | |
… | |
203 | epoll_fork (EV_P) |
215 | epoll_fork (EV_P) |
204 | { |
216 | { |
205 | close (backend_fd); |
217 | close (backend_fd); |
206 | |
218 | |
207 | while ((backend_fd = epoll_create (256)) < 0) |
219 | while ((backend_fd = epoll_create (256)) < 0) |
208 | syserr ("(libev) epoll_create"); |
220 | ev_syserr ("(libev) epoll_create"); |
209 | |
221 | |
210 | fcntl (backend_fd, F_SETFD, FD_CLOEXEC); |
222 | fcntl (backend_fd, F_SETFD, FD_CLOEXEC); |
211 | |
223 | |
212 | fd_rearm_all (EV_A); |
224 | fd_rearm_all (EV_A); |
213 | } |
225 | } |