… | |
… | |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
31 | #if EV_USE_CONFIG_H |
31 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
32 | # include "config.h" |
33 | #endif |
33 | #endif |
34 | |
34 | |
35 | #include <math.h> |
35 | #include <math.h> |
36 | #include <stdlib.h> |
36 | #include <stdlib.h> |
… | |
… | |
42 | #include <stdio.h> |
42 | #include <stdio.h> |
43 | |
43 | |
44 | #include <assert.h> |
44 | #include <assert.h> |
45 | #include <errno.h> |
45 | #include <errno.h> |
46 | #include <sys/types.h> |
46 | #include <sys/types.h> |
|
|
47 | #ifndef WIN32 |
47 | #include <sys/wait.h> |
48 | # include <sys/wait.h> |
|
|
49 | #endif |
48 | #include <sys/time.h> |
50 | #include <sys/time.h> |
49 | #include <time.h> |
51 | #include <time.h> |
50 | |
52 | |
51 | /**/ |
53 | /**/ |
52 | |
54 | |
… | |
… | |
56 | |
58 | |
57 | #ifndef EV_USE_SELECT |
59 | #ifndef EV_USE_SELECT |
58 | # define EV_USE_SELECT 1 |
60 | # define EV_USE_SELECT 1 |
59 | #endif |
61 | #endif |
60 | |
62 | |
61 | #ifndef EV_USE_POLL |
63 | #ifndef EV_USEV_POLL |
62 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
64 | # define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ |
63 | #endif |
65 | #endif |
64 | |
66 | |
65 | #ifndef EV_USE_EPOLL |
67 | #ifndef EV_USE_EPOLL |
66 | # define EV_USE_EPOLL 0 |
68 | # define EV_USE_EPOLL 0 |
|
|
69 | #endif |
|
|
70 | |
|
|
71 | #ifndef EV_USE_KQUEUE |
|
|
72 | # define EV_USE_KQUEUE 0 |
67 | #endif |
73 | #endif |
68 | |
74 | |
69 | #ifndef EV_USE_REALTIME |
75 | #ifndef EV_USE_REALTIME |
70 | # define EV_USE_REALTIME 1 |
76 | # define EV_USE_REALTIME 1 |
71 | #endif |
77 | #endif |
… | |
… | |
100 | #endif |
106 | #endif |
101 | |
107 | |
102 | #define expect_false(expr) expect ((expr) != 0, 0) |
108 | #define expect_false(expr) expect ((expr) != 0, 0) |
103 | #define expect_true(expr) expect ((expr) != 0, 1) |
109 | #define expect_true(expr) expect ((expr) != 0, 1) |
104 | |
110 | |
|
|
111 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
112 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
|
|
113 | |
105 | typedef struct ev_watcher *W; |
114 | typedef struct ev_watcher *W; |
106 | typedef struct ev_watcher_list *WL; |
115 | typedef struct ev_watcher_list *WL; |
107 | typedef struct ev_watcher_time *WT; |
116 | typedef struct ev_watcher_time *WT; |
108 | |
117 | |
109 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
|
|
110 | ev_tstamp ev_now; |
|
|
111 | int ev_method; |
|
|
112 | |
|
|
113 | static int have_monotonic; /* runtime */ |
|
|
114 | |
|
|
115 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
|
|
116 | static void (*method_modify)(int fd, int oev, int nev); |
|
|
117 | static void (*method_poll)(ev_tstamp timeout); |
|
|
118 | |
|
|
119 | /*****************************************************************************/ |
118 | /*****************************************************************************/ |
120 | |
119 | |
121 | ev_tstamp |
120 | typedef struct |
|
|
121 | { |
|
|
122 | struct ev_watcher_list *head; |
|
|
123 | unsigned char events; |
|
|
124 | unsigned char reify; |
|
|
125 | } ANFD; |
|
|
126 | |
|
|
127 | typedef struct |
|
|
128 | { |
|
|
129 | W w; |
|
|
130 | int events; |
|
|
131 | } ANPENDING; |
|
|
132 | |
|
|
133 | #ifdef EV_MULTIPLICITY |
|
|
134 | struct ev_loop |
|
|
135 | { |
|
|
136 | # define VAR(name,decl) decl |
|
|
137 | # include "ev_vars.h" |
|
|
138 | }; |
|
|
139 | #else |
|
|
140 | # define VAR(name,decl) static decl |
|
|
141 | # include "ev_vars.h" |
|
|
142 | #endif |
|
|
143 | #undef VAR |
|
|
144 | |
|
|
145 | /*****************************************************************************/ |
|
|
146 | |
|
|
147 | inline ev_tstamp |
122 | ev_time (void) |
148 | ev_time (void) |
123 | { |
149 | { |
124 | #if EV_USE_REALTIME |
150 | #if EV_USE_REALTIME |
125 | struct timespec ts; |
151 | struct timespec ts; |
126 | clock_gettime (CLOCK_REALTIME, &ts); |
152 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
130 | gettimeofday (&tv, 0); |
156 | gettimeofday (&tv, 0); |
131 | return tv.tv_sec + tv.tv_usec * 1e-6; |
157 | return tv.tv_sec + tv.tv_usec * 1e-6; |
132 | #endif |
158 | #endif |
133 | } |
159 | } |
134 | |
160 | |
135 | static ev_tstamp |
161 | inline ev_tstamp |
136 | get_clock (void) |
162 | get_clock (void) |
137 | { |
163 | { |
138 | #if EV_USE_MONOTONIC |
164 | #if EV_USE_MONOTONIC |
139 | if (expect_true (have_monotonic)) |
165 | if (expect_true (have_monotonic)) |
140 | { |
166 | { |
… | |
… | |
143 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
169 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
144 | } |
170 | } |
145 | #endif |
171 | #endif |
146 | |
172 | |
147 | return ev_time (); |
173 | return ev_time (); |
|
|
174 | } |
|
|
175 | |
|
|
176 | ev_tstamp |
|
|
177 | ev_now (EV_P) |
|
|
178 | { |
|
|
179 | return rt_now; |
148 | } |
180 | } |
149 | |
181 | |
150 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
182 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
151 | |
183 | |
152 | #define array_needsize(base,cur,cnt,init) \ |
184 | #define array_needsize(base,cur,cnt,init) \ |
… | |
… | |
164 | cur = newcnt; \ |
196 | cur = newcnt; \ |
165 | } |
197 | } |
166 | |
198 | |
167 | /*****************************************************************************/ |
199 | /*****************************************************************************/ |
168 | |
200 | |
169 | typedef struct |
|
|
170 | { |
|
|
171 | struct ev_io *head; |
|
|
172 | unsigned char events; |
|
|
173 | unsigned char reify; |
|
|
174 | } ANFD; |
|
|
175 | |
|
|
176 | static ANFD *anfds; |
|
|
177 | static int anfdmax; |
|
|
178 | |
|
|
179 | static void |
201 | static void |
180 | anfds_init (ANFD *base, int count) |
202 | anfds_init (ANFD *base, int count) |
181 | { |
203 | { |
182 | while (count--) |
204 | while (count--) |
183 | { |
205 | { |
… | |
… | |
187 | |
209 | |
188 | ++base; |
210 | ++base; |
189 | } |
211 | } |
190 | } |
212 | } |
191 | |
213 | |
192 | typedef struct |
|
|
193 | { |
|
|
194 | W w; |
|
|
195 | int events; |
|
|
196 | } ANPENDING; |
|
|
197 | |
|
|
198 | static ANPENDING *pendings; |
|
|
199 | static int pendingmax, pendingcnt; |
|
|
200 | |
|
|
201 | static void |
214 | static void |
202 | event (W w, int events) |
215 | event (EV_P_ W w, int events) |
203 | { |
216 | { |
204 | if (w->pending) |
217 | if (w->pending) |
205 | { |
218 | { |
206 | pendings [w->pending - 1].events |= events; |
219 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
207 | return; |
220 | return; |
208 | } |
221 | } |
209 | |
222 | |
210 | w->pending = ++pendingcnt; |
223 | w->pending = ++pendingcnt [ABSPRI (w)]; |
211 | array_needsize (pendings, pendingmax, pendingcnt, ); |
224 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
212 | pendings [pendingcnt - 1].w = w; |
225 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
213 | pendings [pendingcnt - 1].events = events; |
226 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
214 | } |
227 | } |
215 | |
228 | |
216 | static void |
229 | static void |
217 | queue_events (W *events, int eventcnt, int type) |
230 | queue_events (EV_P_ W *events, int eventcnt, int type) |
218 | { |
231 | { |
219 | int i; |
232 | int i; |
220 | |
233 | |
221 | for (i = 0; i < eventcnt; ++i) |
234 | for (i = 0; i < eventcnt; ++i) |
222 | event (events [i], type); |
235 | event (EV_A_ events [i], type); |
223 | } |
236 | } |
224 | |
237 | |
225 | static void |
238 | static void |
226 | fd_event (int fd, int events) |
239 | fd_event (EV_P_ int fd, int events) |
227 | { |
240 | { |
228 | ANFD *anfd = anfds + fd; |
241 | ANFD *anfd = anfds + fd; |
229 | struct ev_io *w; |
242 | struct ev_io *w; |
230 | |
243 | |
231 | for (w = anfd->head; w; w = w->next) |
244 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
232 | { |
245 | { |
233 | int ev = w->events & events; |
246 | int ev = w->events & events; |
234 | |
247 | |
235 | if (ev) |
248 | if (ev) |
236 | event ((W)w, ev); |
249 | event (EV_A_ (W)w, ev); |
237 | } |
250 | } |
238 | } |
251 | } |
239 | |
252 | |
240 | /*****************************************************************************/ |
253 | /*****************************************************************************/ |
241 | |
254 | |
242 | static int *fdchanges; |
|
|
243 | static int fdchangemax, fdchangecnt; |
|
|
244 | |
|
|
245 | static void |
255 | static void |
246 | fd_reify (void) |
256 | fd_reify (EV_P) |
247 | { |
257 | { |
248 | int i; |
258 | int i; |
249 | |
259 | |
250 | for (i = 0; i < fdchangecnt; ++i) |
260 | for (i = 0; i < fdchangecnt; ++i) |
251 | { |
261 | { |
… | |
… | |
253 | ANFD *anfd = anfds + fd; |
263 | ANFD *anfd = anfds + fd; |
254 | struct ev_io *w; |
264 | struct ev_io *w; |
255 | |
265 | |
256 | int events = 0; |
266 | int events = 0; |
257 | |
267 | |
258 | for (w = anfd->head; w; w = w->next) |
268 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
259 | events |= w->events; |
269 | events |= w->events; |
260 | |
270 | |
261 | anfd->reify = 0; |
271 | anfd->reify = 0; |
262 | |
272 | |
263 | if (anfd->events != events) |
273 | if (anfd->events != events) |
264 | { |
274 | { |
265 | method_modify (fd, anfd->events, events); |
275 | method_modify (EV_A_ fd, anfd->events, events); |
266 | anfd->events = events; |
276 | anfd->events = events; |
267 | } |
277 | } |
268 | } |
278 | } |
269 | |
279 | |
270 | fdchangecnt = 0; |
280 | fdchangecnt = 0; |
271 | } |
281 | } |
272 | |
282 | |
273 | static void |
283 | static void |
274 | fd_change (int fd) |
284 | fd_change (EV_P_ int fd) |
275 | { |
285 | { |
276 | if (anfds [fd].reify || fdchangecnt < 0) |
286 | if (anfds [fd].reify || fdchangecnt < 0) |
277 | return; |
287 | return; |
278 | |
288 | |
279 | anfds [fd].reify = 1; |
289 | anfds [fd].reify = 1; |
… | |
… | |
282 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
292 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
283 | fdchanges [fdchangecnt - 1] = fd; |
293 | fdchanges [fdchangecnt - 1] = fd; |
284 | } |
294 | } |
285 | |
295 | |
286 | static void |
296 | static void |
287 | fd_kill (int fd) |
297 | fd_kill (EV_P_ int fd) |
288 | { |
298 | { |
289 | struct ev_io *w; |
299 | struct ev_io *w; |
290 | |
300 | |
291 | printf ("killing fd %d\n", fd);//D |
|
|
292 | while ((w = anfds [fd].head)) |
301 | while ((w = (struct ev_io *)anfds [fd].head)) |
293 | { |
302 | { |
294 | ev_io_stop (w); |
303 | ev_io_stop (EV_A_ w); |
295 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
304 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
296 | } |
305 | } |
297 | } |
306 | } |
298 | |
307 | |
299 | /* called on EBADF to verify fds */ |
308 | /* called on EBADF to verify fds */ |
300 | static void |
309 | static void |
301 | fd_ebadf (void) |
310 | fd_ebadf (EV_P) |
302 | { |
311 | { |
303 | int fd; |
312 | int fd; |
304 | |
313 | |
305 | for (fd = 0; fd < anfdmax; ++fd) |
314 | for (fd = 0; fd < anfdmax; ++fd) |
306 | if (anfds [fd].events) |
315 | if (anfds [fd].events) |
307 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
316 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
308 | fd_kill (fd); |
317 | fd_kill (EV_A_ fd); |
309 | } |
318 | } |
310 | |
319 | |
311 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
320 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
312 | static void |
321 | static void |
313 | fd_enomem (void) |
322 | fd_enomem (EV_P) |
314 | { |
323 | { |
315 | int fd = anfdmax; |
324 | int fd = anfdmax; |
316 | |
325 | |
317 | while (fd--) |
326 | while (fd--) |
318 | if (anfds [fd].events) |
327 | if (anfds [fd].events) |
319 | { |
328 | { |
320 | close (fd); |
329 | close (fd); |
321 | fd_kill (fd); |
330 | fd_kill (EV_A_ fd); |
322 | return; |
331 | return; |
323 | } |
332 | } |
324 | } |
333 | } |
325 | |
334 | |
326 | /*****************************************************************************/ |
335 | /*****************************************************************************/ |
327 | |
|
|
328 | static struct ev_timer **timers; |
|
|
329 | static int timermax, timercnt; |
|
|
330 | |
|
|
331 | static struct ev_periodic **periodics; |
|
|
332 | static int periodicmax, periodiccnt; |
|
|
333 | |
336 | |
334 | static void |
337 | static void |
335 | upheap (WT *timers, int k) |
338 | upheap (WT *timers, int k) |
336 | { |
339 | { |
337 | WT w = timers [k]; |
340 | WT w = timers [k]; |
… | |
… | |
374 | |
377 | |
375 | /*****************************************************************************/ |
378 | /*****************************************************************************/ |
376 | |
379 | |
377 | typedef struct |
380 | typedef struct |
378 | { |
381 | { |
379 | struct ev_signal *head; |
382 | struct ev_watcher_list *head; |
380 | sig_atomic_t volatile gotsig; |
383 | sig_atomic_t volatile gotsig; |
381 | } ANSIG; |
384 | } ANSIG; |
382 | |
385 | |
383 | static ANSIG *signals; |
386 | static ANSIG *signals; |
384 | static int signalmax; |
387 | static int signalmax; |
385 | |
388 | |
386 | static int sigpipe [2]; |
389 | static int sigpipe [2]; |
387 | static sig_atomic_t volatile gotsig; |
390 | static sig_atomic_t volatile gotsig; |
388 | static struct ev_io sigev; |
|
|
389 | |
391 | |
390 | static void |
392 | static void |
391 | signals_init (ANSIG *base, int count) |
393 | signals_init (ANSIG *base, int count) |
392 | { |
394 | { |
393 | while (count--) |
395 | while (count--) |
… | |
… | |
404 | { |
406 | { |
405 | signals [signum - 1].gotsig = 1; |
407 | signals [signum - 1].gotsig = 1; |
406 | |
408 | |
407 | if (!gotsig) |
409 | if (!gotsig) |
408 | { |
410 | { |
|
|
411 | int old_errno = errno; |
409 | gotsig = 1; |
412 | gotsig = 1; |
410 | write (sigpipe [1], &signum, 1); |
413 | write (sigpipe [1], &signum, 1); |
|
|
414 | errno = old_errno; |
411 | } |
415 | } |
412 | } |
416 | } |
413 | |
417 | |
414 | static void |
418 | static void |
415 | sigcb (struct ev_io *iow, int revents) |
419 | sigcb (EV_P_ struct ev_io *iow, int revents) |
416 | { |
420 | { |
417 | struct ev_signal *w; |
421 | struct ev_watcher_list *w; |
418 | int signum; |
422 | int signum; |
419 | |
423 | |
420 | read (sigpipe [0], &revents, 1); |
424 | read (sigpipe [0], &revents, 1); |
421 | gotsig = 0; |
425 | gotsig = 0; |
422 | |
426 | |
… | |
… | |
424 | if (signals [signum].gotsig) |
428 | if (signals [signum].gotsig) |
425 | { |
429 | { |
426 | signals [signum].gotsig = 0; |
430 | signals [signum].gotsig = 0; |
427 | |
431 | |
428 | for (w = signals [signum].head; w; w = w->next) |
432 | for (w = signals [signum].head; w; w = w->next) |
429 | event ((W)w, EV_SIGNAL); |
433 | event (EV_A_ (W)w, EV_SIGNAL); |
430 | } |
434 | } |
431 | } |
435 | } |
432 | |
436 | |
433 | static void |
437 | static void |
434 | siginit (void) |
438 | siginit (EV_P) |
435 | { |
439 | { |
|
|
440 | #ifndef WIN32 |
436 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
441 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
437 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
442 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
438 | |
443 | |
439 | /* rather than sort out wether we really need nb, set it */ |
444 | /* rather than sort out wether we really need nb, set it */ |
440 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
445 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
441 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
446 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
447 | #endif |
442 | |
448 | |
443 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
449 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
444 | ev_io_start (&sigev); |
450 | ev_io_start (&sigev); |
|
|
451 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
445 | } |
452 | } |
446 | |
453 | |
447 | /*****************************************************************************/ |
454 | /*****************************************************************************/ |
448 | |
455 | |
449 | static struct ev_idle **idles; |
456 | #ifndef WIN32 |
450 | static int idlemax, idlecnt; |
|
|
451 | |
|
|
452 | static struct ev_prepare **prepares; |
|
|
453 | static int preparemax, preparecnt; |
|
|
454 | |
|
|
455 | static struct ev_check **checks; |
|
|
456 | static int checkmax, checkcnt; |
|
|
457 | |
|
|
458 | /*****************************************************************************/ |
|
|
459 | |
|
|
460 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
461 | static struct ev_signal childev; |
|
|
462 | |
457 | |
463 | #ifndef WCONTINUED |
458 | #ifndef WCONTINUED |
464 | # define WCONTINUED 0 |
459 | # define WCONTINUED 0 |
465 | #endif |
460 | #endif |
466 | |
461 | |
467 | static void |
462 | static void |
468 | childcb (struct ev_signal *sw, int revents) |
463 | child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) |
469 | { |
464 | { |
470 | struct ev_child *w; |
465 | struct ev_child *w; |
|
|
466 | |
|
|
467 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
|
|
468 | if (w->pid == pid || !w->pid) |
|
|
469 | { |
|
|
470 | w->priority = sw->priority; /* need to do it *now* */ |
|
|
471 | w->rpid = pid; |
|
|
472 | w->rstatus = status; |
|
|
473 | event (EV_A_ (W)w, EV_CHILD); |
|
|
474 | } |
|
|
475 | } |
|
|
476 | |
|
|
477 | static void |
|
|
478 | childcb (EV_P_ struct ev_signal *sw, int revents) |
|
|
479 | { |
471 | int pid, status; |
480 | int pid, status; |
472 | |
481 | |
473 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
482 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
474 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
483 | { |
475 | if (w->pid == pid || !w->pid) |
484 | /* make sure we are called again until all childs have been reaped */ |
476 | { |
485 | event (EV_A_ (W)sw, EV_SIGNAL); |
477 | w->status = status; |
486 | |
478 | event ((W)w, EV_CHILD); |
487 | child_reap (EV_A_ sw, pid, pid, status); |
479 | } |
488 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
|
|
489 | } |
480 | } |
490 | } |
|
|
491 | |
|
|
492 | #endif |
481 | |
493 | |
482 | /*****************************************************************************/ |
494 | /*****************************************************************************/ |
483 | |
495 | |
|
|
496 | #if EV_USE_KQUEUE |
|
|
497 | # include "ev_kqueue.c" |
|
|
498 | #endif |
484 | #if EV_USE_EPOLL |
499 | #if EV_USE_EPOLL |
485 | # include "ev_epoll.c" |
500 | # include "ev_epoll.c" |
486 | #endif |
501 | #endif |
487 | #if EV_USE_POLL |
502 | #if EV_USEV_POLL |
488 | # include "ev_poll.c" |
503 | # include "ev_poll.c" |
489 | #endif |
504 | #endif |
490 | #if EV_USE_SELECT |
505 | #if EV_USE_SELECT |
491 | # include "ev_select.c" |
506 | # include "ev_select.c" |
492 | #endif |
507 | #endif |
… | |
… | |
501 | ev_version_minor (void) |
516 | ev_version_minor (void) |
502 | { |
517 | { |
503 | return EV_VERSION_MINOR; |
518 | return EV_VERSION_MINOR; |
504 | } |
519 | } |
505 | |
520 | |
506 | /* return true if we are running with elevated privileges and ignore env variables */ |
521 | /* return true if we are running with elevated privileges and should ignore env variables */ |
507 | static int |
522 | static int |
508 | enable_secure () |
523 | enable_secure (void) |
509 | { |
524 | { |
|
|
525 | #ifdef WIN32 |
|
|
526 | return 0; |
|
|
527 | #else |
510 | return getuid () != geteuid () |
528 | return getuid () != geteuid () |
511 | || getgid () != getegid (); |
529 | || getgid () != getegid (); |
|
|
530 | #endif |
512 | } |
531 | } |
513 | |
532 | |
|
|
533 | int |
|
|
534 | ev_method (EV_P) |
|
|
535 | { |
|
|
536 | return method; |
|
|
537 | } |
|
|
538 | |
|
|
539 | int |
514 | int ev_init (int methods) |
540 | ev_init (EV_P_ int methods) |
515 | { |
541 | { |
|
|
542 | #ifdef EV_MULTIPLICITY |
|
|
543 | memset (loop, 0, sizeof (struct ev_loop)); |
|
|
544 | #endif |
|
|
545 | |
516 | if (!ev_method) |
546 | if (!method) |
517 | { |
547 | { |
518 | #if EV_USE_MONOTONIC |
548 | #if EV_USE_MONOTONIC |
519 | { |
549 | { |
520 | struct timespec ts; |
550 | struct timespec ts; |
521 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
551 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
522 | have_monotonic = 1; |
552 | have_monotonic = 1; |
523 | } |
553 | } |
524 | #endif |
554 | #endif |
525 | |
555 | |
526 | ev_now = ev_time (); |
556 | rt_now = ev_time (); |
527 | now = get_clock (); |
557 | mn_now = get_clock (); |
528 | now_floor = now; |
558 | now_floor = mn_now; |
529 | diff = ev_now - now; |
559 | diff = rt_now - mn_now; |
530 | |
560 | |
531 | if (pipe (sigpipe)) |
561 | if (pipe (sigpipe)) |
532 | return 0; |
562 | return 0; |
533 | |
563 | |
534 | if (methods == EVMETHOD_AUTO) |
564 | if (methods == EVMETHOD_AUTO) |
535 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
565 | if (!enable_secure () && getenv ("LIBmethodS")) |
536 | methods = atoi (getenv ("LIBEV_METHODS")); |
566 | methods = atoi (getenv ("LIBmethodS")); |
537 | else |
567 | else |
538 | methods = EVMETHOD_ANY; |
568 | methods = EVMETHOD_ANY; |
539 | |
569 | |
540 | ev_method = 0; |
570 | method = 0; |
|
|
571 | #if EV_USE_KQUEUE |
|
|
572 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
|
|
573 | #endif |
541 | #if EV_USE_EPOLL |
574 | #if EV_USE_EPOLL |
542 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
575 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
543 | #endif |
576 | #endif |
544 | #if EV_USE_POLL |
577 | #if EV_USEV_POLL |
545 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
578 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
546 | #endif |
579 | #endif |
547 | #if EV_USE_SELECT |
580 | #if EV_USE_SELECT |
548 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
581 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
549 | #endif |
582 | #endif |
550 | |
583 | |
551 | if (ev_method) |
584 | if (method) |
552 | { |
585 | { |
553 | ev_watcher_init (&sigev, sigcb); |
586 | ev_watcher_init (&sigev, sigcb); |
|
|
587 | ev_set_priority (&sigev, EV_MAXPRI); |
554 | siginit (); |
588 | siginit (EV_A); |
555 | |
589 | |
|
|
590 | #ifndef WIN32 |
556 | ev_signal_init (&childev, childcb, SIGCHLD); |
591 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
592 | ev_set_priority (&childev, EV_MAXPRI); |
557 | ev_signal_start (&childev); |
593 | ev_signal_start (EV_A_ &childev); |
|
|
594 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
|
|
595 | #endif |
558 | } |
596 | } |
559 | } |
597 | } |
560 | |
598 | |
561 | return ev_method; |
599 | return method; |
562 | } |
600 | } |
563 | |
601 | |
564 | /*****************************************************************************/ |
602 | /*****************************************************************************/ |
565 | |
603 | |
566 | void |
604 | void |
… | |
… | |
577 | |
615 | |
578 | void |
616 | void |
579 | ev_fork_child (void) |
617 | ev_fork_child (void) |
580 | { |
618 | { |
581 | #if EV_USE_EPOLL |
619 | #if EV_USE_EPOLL |
582 | if (ev_method == EVMETHOD_EPOLL) |
620 | if (method == EVMETHOD_EPOLL) |
583 | epoll_postfork_child (); |
621 | epoll_postfork_child (); |
584 | #endif |
622 | #endif |
585 | |
623 | |
586 | ev_io_stop (&sigev); |
624 | ev_io_stop (&sigev); |
587 | close (sigpipe [0]); |
625 | close (sigpipe [0]); |
… | |
… | |
591 | } |
629 | } |
592 | |
630 | |
593 | /*****************************************************************************/ |
631 | /*****************************************************************************/ |
594 | |
632 | |
595 | static void |
633 | static void |
596 | call_pending (void) |
634 | call_pending (EV_P) |
597 | { |
635 | { |
|
|
636 | int pri; |
|
|
637 | |
|
|
638 | for (pri = NUMPRI; pri--; ) |
598 | while (pendingcnt) |
639 | while (pendingcnt [pri]) |
599 | { |
640 | { |
600 | ANPENDING *p = pendings + --pendingcnt; |
641 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
601 | |
642 | |
602 | if (p->w) |
643 | if (p->w) |
603 | { |
644 | { |
604 | p->w->pending = 0; |
645 | p->w->pending = 0; |
605 | p->w->cb (p->w, p->events); |
646 | p->w->cb (EV_A_ p->w, p->events); |
606 | } |
647 | } |
607 | } |
648 | } |
608 | } |
649 | } |
609 | |
650 | |
610 | static void |
651 | static void |
611 | timers_reify (void) |
652 | timers_reify (EV_P) |
612 | { |
653 | { |
613 | while (timercnt && timers [0]->at <= now) |
654 | while (timercnt && timers [0]->at <= mn_now) |
614 | { |
655 | { |
615 | struct ev_timer *w = timers [0]; |
656 | struct ev_timer *w = timers [0]; |
616 | |
657 | |
617 | /* first reschedule or stop timer */ |
658 | /* first reschedule or stop timer */ |
618 | if (w->repeat) |
659 | if (w->repeat) |
619 | { |
660 | { |
620 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
661 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
621 | w->at = now + w->repeat; |
662 | w->at = mn_now + w->repeat; |
622 | downheap ((WT *)timers, timercnt, 0); |
663 | downheap ((WT *)timers, timercnt, 0); |
623 | } |
664 | } |
624 | else |
665 | else |
625 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
666 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
626 | |
667 | |
627 | event ((W)w, EV_TIMEOUT); |
668 | event ((W)w, EV_TIMEOUT); |
628 | } |
669 | } |
629 | } |
670 | } |
630 | |
671 | |
631 | static void |
672 | static void |
632 | periodics_reify (void) |
673 | periodics_reify (EV_P) |
633 | { |
674 | { |
634 | while (periodiccnt && periodics [0]->at <= ev_now) |
675 | while (periodiccnt && periodics [0]->at <= rt_now) |
635 | { |
676 | { |
636 | struct ev_periodic *w = periodics [0]; |
677 | struct ev_periodic *w = periodics [0]; |
637 | |
678 | |
638 | /* first reschedule or stop timer */ |
679 | /* first reschedule or stop timer */ |
639 | if (w->interval) |
680 | if (w->interval) |
640 | { |
681 | { |
641 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
682 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
642 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
683 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
643 | downheap ((WT *)periodics, periodiccnt, 0); |
684 | downheap ((WT *)periodics, periodiccnt, 0); |
644 | } |
685 | } |
645 | else |
686 | else |
646 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
687 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
647 | |
688 | |
648 | event ((W)w, EV_PERIODIC); |
689 | event (EV_A_ (W)w, EV_PERIODIC); |
649 | } |
690 | } |
650 | } |
691 | } |
651 | |
692 | |
652 | static void |
693 | static void |
653 | periodics_reschedule (ev_tstamp diff) |
694 | periodics_reschedule (EV_P_ ev_tstamp diff) |
654 | { |
695 | { |
655 | int i; |
696 | int i; |
656 | |
697 | |
657 | /* adjust periodics after time jump */ |
698 | /* adjust periodics after time jump */ |
658 | for (i = 0; i < periodiccnt; ++i) |
699 | for (i = 0; i < periodiccnt; ++i) |
659 | { |
700 | { |
660 | struct ev_periodic *w = periodics [i]; |
701 | struct ev_periodic *w = periodics [i]; |
661 | |
702 | |
662 | if (w->interval) |
703 | if (w->interval) |
663 | { |
704 | { |
664 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
705 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
665 | |
706 | |
666 | if (fabs (diff) >= 1e-4) |
707 | if (fabs (diff) >= 1e-4) |
667 | { |
708 | { |
668 | ev_periodic_stop (w); |
709 | ev_periodic_stop (EV_A_ w); |
669 | ev_periodic_start (w); |
710 | ev_periodic_start (EV_A_ w); |
670 | |
711 | |
671 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
712 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
672 | } |
713 | } |
673 | } |
714 | } |
674 | } |
715 | } |
675 | } |
716 | } |
676 | |
717 | |
677 | static int |
718 | inline int |
678 | time_update_monotonic (void) |
719 | time_update_monotonic (EV_P) |
679 | { |
720 | { |
680 | now = get_clock (); |
721 | mn_now = get_clock (); |
681 | |
722 | |
682 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
723 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
683 | { |
724 | { |
684 | ev_now = now + diff; |
725 | rt_now = mn_now + diff; |
685 | return 0; |
726 | return 0; |
686 | } |
727 | } |
687 | else |
728 | else |
688 | { |
729 | { |
689 | now_floor = now; |
730 | now_floor = mn_now; |
690 | ev_now = ev_time (); |
731 | rt_now = ev_time (); |
691 | return 1; |
732 | return 1; |
692 | } |
733 | } |
693 | } |
734 | } |
694 | |
735 | |
695 | static void |
736 | static void |
696 | time_update (void) |
737 | time_update (EV_P) |
697 | { |
738 | { |
698 | int i; |
739 | int i; |
699 | |
740 | |
700 | #if EV_USE_MONOTONIC |
741 | #if EV_USE_MONOTONIC |
701 | if (expect_true (have_monotonic)) |
742 | if (expect_true (have_monotonic)) |
702 | { |
743 | { |
703 | if (time_update_monotonic ()) |
744 | if (time_update_monotonic (EV_A)) |
704 | { |
745 | { |
705 | ev_tstamp odiff = diff; |
746 | ev_tstamp odiff = diff; |
706 | |
747 | |
707 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
748 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
708 | { |
749 | { |
709 | diff = ev_now - now; |
750 | diff = rt_now - mn_now; |
710 | |
751 | |
711 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
752 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
712 | return; /* all is well */ |
753 | return; /* all is well */ |
713 | |
754 | |
714 | ev_now = ev_time (); |
755 | rt_now = ev_time (); |
715 | now = get_clock (); |
756 | mn_now = get_clock (); |
716 | now_floor = now; |
757 | now_floor = mn_now; |
717 | } |
758 | } |
718 | |
759 | |
719 | periodics_reschedule (diff - odiff); |
760 | periodics_reschedule (EV_A_ diff - odiff); |
720 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
761 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
721 | } |
762 | } |
722 | } |
763 | } |
723 | else |
764 | else |
724 | #endif |
765 | #endif |
725 | { |
766 | { |
726 | ev_now = ev_time (); |
767 | rt_now = ev_time (); |
727 | |
768 | |
728 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
769 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
729 | { |
770 | { |
730 | periodics_reschedule (ev_now - now); |
771 | periodics_reschedule (EV_A_ rt_now - mn_now); |
731 | |
772 | |
732 | /* adjust timers. this is easy, as the offset is the same for all */ |
773 | /* adjust timers. this is easy, as the offset is the same for all */ |
733 | for (i = 0; i < timercnt; ++i) |
774 | for (i = 0; i < timercnt; ++i) |
734 | timers [i]->at += diff; |
775 | timers [i]->at += diff; |
735 | } |
776 | } |
736 | |
777 | |
737 | now = ev_now; |
778 | mn_now = rt_now; |
738 | } |
779 | } |
739 | } |
780 | } |
740 | |
781 | |
741 | int ev_loop_done; |
782 | void |
|
|
783 | ev_ref (EV_P) |
|
|
784 | { |
|
|
785 | ++activecnt; |
|
|
786 | } |
742 | |
787 | |
|
|
788 | void |
|
|
789 | ev_unref (EV_P) |
|
|
790 | { |
|
|
791 | --activecnt; |
|
|
792 | } |
|
|
793 | |
|
|
794 | static int loop_done; |
|
|
795 | |
|
|
796 | void |
743 | void ev_loop (int flags) |
797 | ev_loop (EV_P_ int flags) |
744 | { |
798 | { |
745 | double block; |
799 | double block; |
746 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
800 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
747 | |
801 | |
748 | do |
802 | do |
749 | { |
803 | { |
750 | /* queue check watchers (and execute them) */ |
804 | /* queue check watchers (and execute them) */ |
751 | if (expect_false (preparecnt)) |
805 | if (expect_false (preparecnt)) |
752 | { |
806 | { |
753 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
807 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
754 | call_pending (); |
808 | call_pending (EV_A); |
755 | } |
809 | } |
756 | |
810 | |
757 | /* update fd-related kernel structures */ |
811 | /* update fd-related kernel structures */ |
758 | fd_reify (); |
812 | fd_reify (EV_A); |
759 | |
813 | |
760 | /* calculate blocking time */ |
814 | /* calculate blocking time */ |
761 | |
815 | |
762 | /* we only need this for !monotonic clockor timers, but as we basically |
816 | /* we only need this for !monotonic clockor timers, but as we basically |
763 | always have timers, we just calculate it always */ |
817 | always have timers, we just calculate it always */ |
764 | #if EV_USE_MONOTONIC |
818 | #if EV_USE_MONOTONIC |
765 | if (expect_true (have_monotonic)) |
819 | if (expect_true (have_monotonic)) |
766 | time_update_monotonic (); |
820 | time_update_monotonic (EV_A); |
767 | else |
821 | else |
768 | #endif |
822 | #endif |
769 | { |
823 | { |
770 | ev_now = ev_time (); |
824 | rt_now = ev_time (); |
771 | now = ev_now; |
825 | mn_now = rt_now; |
772 | } |
826 | } |
773 | |
827 | |
774 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
828 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
775 | block = 0.; |
829 | block = 0.; |
776 | else |
830 | else |
777 | { |
831 | { |
778 | block = MAX_BLOCKTIME; |
832 | block = MAX_BLOCKTIME; |
779 | |
833 | |
780 | if (timercnt) |
834 | if (timercnt) |
781 | { |
835 | { |
782 | ev_tstamp to = timers [0]->at - now + method_fudge; |
836 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
783 | if (block > to) block = to; |
837 | if (block > to) block = to; |
784 | } |
838 | } |
785 | |
839 | |
786 | if (periodiccnt) |
840 | if (periodiccnt) |
787 | { |
841 | { |
788 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
842 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
789 | if (block > to) block = to; |
843 | if (block > to) block = to; |
790 | } |
844 | } |
791 | |
845 | |
792 | if (block < 0.) block = 0.; |
846 | if (block < 0.) block = 0.; |
793 | } |
847 | } |
794 | |
848 | |
795 | method_poll (block); |
849 | method_poll (EV_A_ block); |
796 | |
850 | |
797 | /* update ev_now, do magic */ |
851 | /* update rt_now, do magic */ |
798 | time_update (); |
852 | time_update (EV_A); |
799 | |
853 | |
800 | /* queue pending timers and reschedule them */ |
854 | /* queue pending timers and reschedule them */ |
801 | timers_reify (); /* relative timers called last */ |
855 | timers_reify (EV_A); /* relative timers called last */ |
802 | periodics_reify (); /* absolute timers called first */ |
856 | periodics_reify (EV_A); /* absolute timers called first */ |
803 | |
857 | |
804 | /* queue idle watchers unless io or timers are pending */ |
858 | /* queue idle watchers unless io or timers are pending */ |
805 | if (!pendingcnt) |
859 | if (!pendingcnt) |
806 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
860 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
807 | |
861 | |
808 | /* queue check watchers, to be executed first */ |
862 | /* queue check watchers, to be executed first */ |
809 | if (checkcnt) |
863 | if (checkcnt) |
810 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
864 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
811 | |
865 | |
812 | call_pending (); |
866 | call_pending (EV_A); |
813 | } |
867 | } |
814 | while (!ev_loop_done); |
868 | while (activecnt && !loop_done); |
815 | |
869 | |
816 | if (ev_loop_done != 2) |
870 | if (loop_done != 2) |
817 | ev_loop_done = 0; |
871 | loop_done = 0; |
|
|
872 | } |
|
|
873 | |
|
|
874 | void |
|
|
875 | ev_unloop (EV_P_ int how) |
|
|
876 | { |
|
|
877 | loop_done = how; |
818 | } |
878 | } |
819 | |
879 | |
820 | /*****************************************************************************/ |
880 | /*****************************************************************************/ |
821 | |
881 | |
822 | static void |
882 | inline void |
823 | wlist_add (WL *head, WL elem) |
883 | wlist_add (WL *head, WL elem) |
824 | { |
884 | { |
825 | elem->next = *head; |
885 | elem->next = *head; |
826 | *head = elem; |
886 | *head = elem; |
827 | } |
887 | } |
828 | |
888 | |
829 | static void |
889 | inline void |
830 | wlist_del (WL *head, WL elem) |
890 | wlist_del (WL *head, WL elem) |
831 | { |
891 | { |
832 | while (*head) |
892 | while (*head) |
833 | { |
893 | { |
834 | if (*head == elem) |
894 | if (*head == elem) |
… | |
… | |
839 | |
899 | |
840 | head = &(*head)->next; |
900 | head = &(*head)->next; |
841 | } |
901 | } |
842 | } |
902 | } |
843 | |
903 | |
844 | static void |
904 | inline void |
845 | ev_clear_pending (W w) |
905 | ev_clear_pending (EV_P_ W w) |
846 | { |
906 | { |
847 | if (w->pending) |
907 | if (w->pending) |
848 | { |
908 | { |
849 | pendings [w->pending - 1].w = 0; |
909 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
850 | w->pending = 0; |
910 | w->pending = 0; |
851 | } |
911 | } |
852 | } |
912 | } |
853 | |
913 | |
854 | static void |
914 | inline void |
855 | ev_start (W w, int active) |
915 | ev_start (EV_P_ W w, int active) |
856 | { |
916 | { |
|
|
917 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
918 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
919 | |
857 | w->active = active; |
920 | w->active = active; |
|
|
921 | ev_ref (EV_A); |
858 | } |
922 | } |
859 | |
923 | |
860 | static void |
924 | inline void |
861 | ev_stop (W w) |
925 | ev_stop (EV_P_ W w) |
862 | { |
926 | { |
|
|
927 | ev_unref (EV_A); |
863 | w->active = 0; |
928 | w->active = 0; |
864 | } |
929 | } |
865 | |
930 | |
866 | /*****************************************************************************/ |
931 | /*****************************************************************************/ |
867 | |
932 | |
868 | void |
933 | void |
869 | ev_io_start (struct ev_io *w) |
934 | ev_io_start (EV_P_ struct ev_io *w) |
870 | { |
935 | { |
871 | int fd = w->fd; |
936 | int fd = w->fd; |
872 | |
937 | |
873 | if (ev_is_active (w)) |
938 | if (ev_is_active (w)) |
874 | return; |
939 | return; |
875 | |
940 | |
876 | assert (("ev_io_start called with negative fd", fd >= 0)); |
941 | assert (("ev_io_start called with negative fd", fd >= 0)); |
877 | |
942 | |
878 | ev_start ((W)w, 1); |
943 | ev_start (EV_A_ (W)w, 1); |
879 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
944 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
880 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
945 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
881 | |
946 | |
882 | fd_change (fd); |
947 | fd_change (EV_A_ fd); |
883 | } |
948 | } |
884 | |
949 | |
885 | void |
950 | void |
886 | ev_io_stop (struct ev_io *w) |
951 | ev_io_stop (EV_P_ struct ev_io *w) |
887 | { |
952 | { |
888 | ev_clear_pending ((W)w); |
953 | ev_clear_pending (EV_A_ (W)w); |
889 | if (!ev_is_active (w)) |
954 | if (!ev_is_active (w)) |
890 | return; |
955 | return; |
891 | |
956 | |
892 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
957 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
893 | ev_stop ((W)w); |
958 | ev_stop (EV_A_ (W)w); |
894 | |
959 | |
895 | fd_change (w->fd); |
960 | fd_change (EV_A_ w->fd); |
896 | } |
961 | } |
897 | |
962 | |
898 | void |
963 | void |
899 | ev_timer_start (struct ev_timer *w) |
964 | ev_timer_start (EV_P_ struct ev_timer *w) |
900 | { |
965 | { |
901 | if (ev_is_active (w)) |
966 | if (ev_is_active (w)) |
902 | return; |
967 | return; |
903 | |
968 | |
904 | w->at += now; |
969 | w->at += mn_now; |
905 | |
970 | |
906 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
971 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
907 | |
972 | |
908 | ev_start ((W)w, ++timercnt); |
973 | ev_start (EV_A_ (W)w, ++timercnt); |
909 | array_needsize (timers, timermax, timercnt, ); |
974 | array_needsize (timers, timermax, timercnt, ); |
910 | timers [timercnt - 1] = w; |
975 | timers [timercnt - 1] = w; |
911 | upheap ((WT *)timers, timercnt - 1); |
976 | upheap ((WT *)timers, timercnt - 1); |
912 | } |
977 | } |
913 | |
978 | |
914 | void |
979 | void |
915 | ev_timer_stop (struct ev_timer *w) |
980 | ev_timer_stop (EV_P_ struct ev_timer *w) |
916 | { |
981 | { |
917 | ev_clear_pending ((W)w); |
982 | ev_clear_pending (EV_A_ (W)w); |
918 | if (!ev_is_active (w)) |
983 | if (!ev_is_active (w)) |
919 | return; |
984 | return; |
920 | |
985 | |
921 | if (w->active < timercnt--) |
986 | if (w->active < timercnt--) |
922 | { |
987 | { |
… | |
… | |
924 | downheap ((WT *)timers, timercnt, w->active - 1); |
989 | downheap ((WT *)timers, timercnt, w->active - 1); |
925 | } |
990 | } |
926 | |
991 | |
927 | w->at = w->repeat; |
992 | w->at = w->repeat; |
928 | |
993 | |
929 | ev_stop ((W)w); |
994 | ev_stop (EV_A_ (W)w); |
930 | } |
995 | } |
931 | |
996 | |
932 | void |
997 | void |
933 | ev_timer_again (struct ev_timer *w) |
998 | ev_timer_again (EV_P_ struct ev_timer *w) |
934 | { |
999 | { |
935 | if (ev_is_active (w)) |
1000 | if (ev_is_active (w)) |
936 | { |
1001 | { |
937 | if (w->repeat) |
1002 | if (w->repeat) |
938 | { |
1003 | { |
939 | w->at = now + w->repeat; |
1004 | w->at = mn_now + w->repeat; |
940 | downheap ((WT *)timers, timercnt, w->active - 1); |
1005 | downheap ((WT *)timers, timercnt, w->active - 1); |
941 | } |
1006 | } |
942 | else |
1007 | else |
943 | ev_timer_stop (w); |
1008 | ev_timer_stop (EV_A_ w); |
944 | } |
1009 | } |
945 | else if (w->repeat) |
1010 | else if (w->repeat) |
946 | ev_timer_start (w); |
1011 | ev_timer_start (EV_A_ w); |
947 | } |
1012 | } |
948 | |
1013 | |
949 | void |
1014 | void |
950 | ev_periodic_start (struct ev_periodic *w) |
1015 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
951 | { |
1016 | { |
952 | if (ev_is_active (w)) |
1017 | if (ev_is_active (w)) |
953 | return; |
1018 | return; |
954 | |
1019 | |
955 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1020 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
956 | |
1021 | |
957 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1022 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
958 | if (w->interval) |
1023 | if (w->interval) |
959 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
1024 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
960 | |
1025 | |
961 | ev_start ((W)w, ++periodiccnt); |
1026 | ev_start (EV_A_ (W)w, ++periodiccnt); |
962 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1027 | array_needsize (periodics, periodicmax, periodiccnt, ); |
963 | periodics [periodiccnt - 1] = w; |
1028 | periodics [periodiccnt - 1] = w; |
964 | upheap ((WT *)periodics, periodiccnt - 1); |
1029 | upheap ((WT *)periodics, periodiccnt - 1); |
965 | } |
1030 | } |
966 | |
1031 | |
967 | void |
1032 | void |
968 | ev_periodic_stop (struct ev_periodic *w) |
1033 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
969 | { |
1034 | { |
970 | ev_clear_pending ((W)w); |
1035 | ev_clear_pending (EV_A_ (W)w); |
971 | if (!ev_is_active (w)) |
1036 | if (!ev_is_active (w)) |
972 | return; |
1037 | return; |
973 | |
1038 | |
974 | if (w->active < periodiccnt--) |
1039 | if (w->active < periodiccnt--) |
975 | { |
1040 | { |
976 | periodics [w->active - 1] = periodics [periodiccnt]; |
1041 | periodics [w->active - 1] = periodics [periodiccnt]; |
977 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1042 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
978 | } |
1043 | } |
979 | |
1044 | |
980 | ev_stop ((W)w); |
1045 | ev_stop (EV_A_ (W)w); |
981 | } |
1046 | } |
982 | |
1047 | |
|
|
1048 | #ifndef SA_RESTART |
|
|
1049 | # define SA_RESTART 0 |
|
|
1050 | #endif |
|
|
1051 | |
983 | void |
1052 | void |
984 | ev_signal_start (struct ev_signal *w) |
1053 | ev_signal_start (EV_P_ struct ev_signal *w) |
985 | { |
1054 | { |
986 | if (ev_is_active (w)) |
1055 | if (ev_is_active (w)) |
987 | return; |
1056 | return; |
988 | |
1057 | |
989 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1058 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
990 | |
1059 | |
991 | ev_start ((W)w, 1); |
1060 | ev_start (EV_A_ (W)w, 1); |
992 | array_needsize (signals, signalmax, w->signum, signals_init); |
1061 | array_needsize (signals, signalmax, w->signum, signals_init); |
993 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1062 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
994 | |
1063 | |
995 | if (!w->next) |
1064 | if (!w->next) |
996 | { |
1065 | { |
997 | struct sigaction sa; |
1066 | struct sigaction sa; |
998 | sa.sa_handler = sighandler; |
1067 | sa.sa_handler = sighandler; |
999 | sigfillset (&sa.sa_mask); |
1068 | sigfillset (&sa.sa_mask); |
1000 | sa.sa_flags = 0; |
1069 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1001 | sigaction (w->signum, &sa, 0); |
1070 | sigaction (w->signum, &sa, 0); |
1002 | } |
1071 | } |
1003 | } |
1072 | } |
1004 | |
1073 | |
1005 | void |
1074 | void |
1006 | ev_signal_stop (struct ev_signal *w) |
1075 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1007 | { |
1076 | { |
1008 | ev_clear_pending ((W)w); |
1077 | ev_clear_pending (EV_A_ (W)w); |
1009 | if (!ev_is_active (w)) |
1078 | if (!ev_is_active (w)) |
1010 | return; |
1079 | return; |
1011 | |
1080 | |
1012 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1081 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1013 | ev_stop ((W)w); |
1082 | ev_stop (EV_A_ (W)w); |
1014 | |
1083 | |
1015 | if (!signals [w->signum - 1].head) |
1084 | if (!signals [w->signum - 1].head) |
1016 | signal (w->signum, SIG_DFL); |
1085 | signal (w->signum, SIG_DFL); |
1017 | } |
1086 | } |
1018 | |
1087 | |
1019 | void |
1088 | void |
1020 | ev_idle_start (struct ev_idle *w) |
1089 | ev_idle_start (EV_P_ struct ev_idle *w) |
1021 | { |
1090 | { |
1022 | if (ev_is_active (w)) |
1091 | if (ev_is_active (w)) |
1023 | return; |
1092 | return; |
1024 | |
1093 | |
1025 | ev_start ((W)w, ++idlecnt); |
1094 | ev_start (EV_A_ (W)w, ++idlecnt); |
1026 | array_needsize (idles, idlemax, idlecnt, ); |
1095 | array_needsize (idles, idlemax, idlecnt, ); |
1027 | idles [idlecnt - 1] = w; |
1096 | idles [idlecnt - 1] = w; |
1028 | } |
1097 | } |
1029 | |
1098 | |
1030 | void |
1099 | void |
1031 | ev_idle_stop (struct ev_idle *w) |
1100 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1032 | { |
1101 | { |
1033 | ev_clear_pending ((W)w); |
1102 | ev_clear_pending (EV_A_ (W)w); |
1034 | if (ev_is_active (w)) |
1103 | if (ev_is_active (w)) |
1035 | return; |
1104 | return; |
1036 | |
1105 | |
1037 | idles [w->active - 1] = idles [--idlecnt]; |
1106 | idles [w->active - 1] = idles [--idlecnt]; |
1038 | ev_stop ((W)w); |
1107 | ev_stop (EV_A_ (W)w); |
1039 | } |
1108 | } |
1040 | |
1109 | |
1041 | void |
1110 | void |
1042 | ev_prepare_start (struct ev_prepare *w) |
1111 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1043 | { |
1112 | { |
1044 | if (ev_is_active (w)) |
1113 | if (ev_is_active (w)) |
1045 | return; |
1114 | return; |
1046 | |
1115 | |
1047 | ev_start ((W)w, ++preparecnt); |
1116 | ev_start (EV_A_ (W)w, ++preparecnt); |
1048 | array_needsize (prepares, preparemax, preparecnt, ); |
1117 | array_needsize (prepares, preparemax, preparecnt, ); |
1049 | prepares [preparecnt - 1] = w; |
1118 | prepares [preparecnt - 1] = w; |
1050 | } |
1119 | } |
1051 | |
1120 | |
1052 | void |
1121 | void |
1053 | ev_prepare_stop (struct ev_prepare *w) |
1122 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1054 | { |
1123 | { |
1055 | ev_clear_pending ((W)w); |
1124 | ev_clear_pending (EV_A_ (W)w); |
1056 | if (ev_is_active (w)) |
1125 | if (ev_is_active (w)) |
1057 | return; |
1126 | return; |
1058 | |
1127 | |
1059 | prepares [w->active - 1] = prepares [--preparecnt]; |
1128 | prepares [w->active - 1] = prepares [--preparecnt]; |
1060 | ev_stop ((W)w); |
1129 | ev_stop (EV_A_ (W)w); |
1061 | } |
1130 | } |
1062 | |
1131 | |
1063 | void |
1132 | void |
1064 | ev_check_start (struct ev_check *w) |
1133 | ev_check_start (EV_P_ struct ev_check *w) |
1065 | { |
1134 | { |
1066 | if (ev_is_active (w)) |
1135 | if (ev_is_active (w)) |
1067 | return; |
1136 | return; |
1068 | |
1137 | |
1069 | ev_start ((W)w, ++checkcnt); |
1138 | ev_start (EV_A_ (W)w, ++checkcnt); |
1070 | array_needsize (checks, checkmax, checkcnt, ); |
1139 | array_needsize (checks, checkmax, checkcnt, ); |
1071 | checks [checkcnt - 1] = w; |
1140 | checks [checkcnt - 1] = w; |
1072 | } |
1141 | } |
1073 | |
1142 | |
1074 | void |
1143 | void |
1075 | ev_check_stop (struct ev_check *w) |
1144 | ev_check_stop (EV_P_ struct ev_check *w) |
1076 | { |
1145 | { |
1077 | ev_clear_pending ((W)w); |
1146 | ev_clear_pending (EV_A_ (W)w); |
1078 | if (ev_is_active (w)) |
1147 | if (ev_is_active (w)) |
1079 | return; |
1148 | return; |
1080 | |
1149 | |
1081 | checks [w->active - 1] = checks [--checkcnt]; |
1150 | checks [w->active - 1] = checks [--checkcnt]; |
1082 | ev_stop ((W)w); |
1151 | ev_stop (EV_A_ (W)w); |
1083 | } |
1152 | } |
1084 | |
1153 | |
1085 | void |
1154 | void |
1086 | ev_child_start (struct ev_child *w) |
1155 | ev_child_start (EV_P_ struct ev_child *w) |
1087 | { |
1156 | { |
1088 | if (ev_is_active (w)) |
1157 | if (ev_is_active (w)) |
1089 | return; |
1158 | return; |
1090 | |
1159 | |
1091 | ev_start ((W)w, 1); |
1160 | ev_start (EV_A_ (W)w, 1); |
1092 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1161 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1093 | } |
1162 | } |
1094 | |
1163 | |
1095 | void |
1164 | void |
1096 | ev_child_stop (struct ev_child *w) |
1165 | ev_child_stop (EV_P_ struct ev_child *w) |
1097 | { |
1166 | { |
1098 | ev_clear_pending ((W)w); |
1167 | ev_clear_pending (EV_A_ (W)w); |
1099 | if (ev_is_active (w)) |
1168 | if (ev_is_active (w)) |
1100 | return; |
1169 | return; |
1101 | |
1170 | |
1102 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1171 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1103 | ev_stop ((W)w); |
1172 | ev_stop (EV_A_ (W)w); |
1104 | } |
1173 | } |
1105 | |
1174 | |
1106 | /*****************************************************************************/ |
1175 | /*****************************************************************************/ |
1107 | |
1176 | |
1108 | struct ev_once |
1177 | struct ev_once |
… | |
… | |
1112 | void (*cb)(int revents, void *arg); |
1181 | void (*cb)(int revents, void *arg); |
1113 | void *arg; |
1182 | void *arg; |
1114 | }; |
1183 | }; |
1115 | |
1184 | |
1116 | static void |
1185 | static void |
1117 | once_cb (struct ev_once *once, int revents) |
1186 | once_cb (EV_P_ struct ev_once *once, int revents) |
1118 | { |
1187 | { |
1119 | void (*cb)(int revents, void *arg) = once->cb; |
1188 | void (*cb)(int revents, void *arg) = once->cb; |
1120 | void *arg = once->arg; |
1189 | void *arg = once->arg; |
1121 | |
1190 | |
1122 | ev_io_stop (&once->io); |
1191 | ev_io_stop (EV_A_ &once->io); |
1123 | ev_timer_stop (&once->to); |
1192 | ev_timer_stop (EV_A_ &once->to); |
1124 | free (once); |
1193 | free (once); |
1125 | |
1194 | |
1126 | cb (revents, arg); |
1195 | cb (revents, arg); |
1127 | } |
1196 | } |
1128 | |
1197 | |
1129 | static void |
1198 | static void |
1130 | once_cb_io (struct ev_io *w, int revents) |
1199 | once_cb_io (EV_P_ struct ev_io *w, int revents) |
1131 | { |
1200 | { |
1132 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1201 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1133 | } |
1202 | } |
1134 | |
1203 | |
1135 | static void |
1204 | static void |
1136 | once_cb_to (struct ev_timer *w, int revents) |
1205 | once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1137 | { |
1206 | { |
1138 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1207 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1139 | } |
1208 | } |
1140 | |
1209 | |
1141 | void |
1210 | void |
1142 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1211 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1143 | { |
1212 | { |
1144 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1213 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1145 | |
1214 | |
1146 | if (!once) |
1215 | if (!once) |
1147 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1216 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
… | |
… | |
1152 | |
1221 | |
1153 | ev_watcher_init (&once->io, once_cb_io); |
1222 | ev_watcher_init (&once->io, once_cb_io); |
1154 | if (fd >= 0) |
1223 | if (fd >= 0) |
1155 | { |
1224 | { |
1156 | ev_io_set (&once->io, fd, events); |
1225 | ev_io_set (&once->io, fd, events); |
1157 | ev_io_start (&once->io); |
1226 | ev_io_start (EV_A_ &once->io); |
1158 | } |
1227 | } |
1159 | |
1228 | |
1160 | ev_watcher_init (&once->to, once_cb_to); |
1229 | ev_watcher_init (&once->to, once_cb_to); |
1161 | if (timeout >= 0.) |
1230 | if (timeout >= 0.) |
1162 | { |
1231 | { |
1163 | ev_timer_set (&once->to, timeout, 0.); |
1232 | ev_timer_set (&once->to, timeout, 0.); |
1164 | ev_timer_start (&once->to); |
1233 | ev_timer_start (EV_A_ &once->to); |
1165 | } |
1234 | } |
1166 | } |
1235 | } |
1167 | } |
1236 | } |
1168 | |
1237 | |
1169 | /*****************************************************************************/ |
1238 | /*****************************************************************************/ |