… | |
… | |
54 | |
54 | |
55 | #endif |
55 | #endif |
56 | |
56 | |
57 | #include <math.h> |
57 | #include <math.h> |
58 | #include <stdlib.h> |
58 | #include <stdlib.h> |
59 | #include <unistd.h> |
|
|
60 | #include <fcntl.h> |
59 | #include <fcntl.h> |
61 | #include <signal.h> |
|
|
62 | #include <stddef.h> |
60 | #include <stddef.h> |
63 | |
61 | |
64 | #include <stdio.h> |
62 | #include <stdio.h> |
65 | |
63 | |
66 | #include <assert.h> |
64 | #include <assert.h> |
67 | #include <errno.h> |
65 | #include <errno.h> |
68 | #include <sys/types.h> |
66 | #include <sys/types.h> |
|
|
67 | #include <time.h> |
|
|
68 | |
|
|
69 | #include <signal.h> |
|
|
70 | |
69 | #ifndef WIN32 |
71 | #ifndef WIN32 |
|
|
72 | # include <unistd.h> |
|
|
73 | # include <sys/time.h> |
70 | # include <sys/wait.h> |
74 | # include <sys/wait.h> |
71 | #endif |
75 | #endif |
72 | #include <sys/time.h> |
|
|
73 | #include <time.h> |
|
|
74 | |
|
|
75 | /**/ |
76 | /**/ |
76 | |
77 | |
77 | #ifndef EV_USE_MONOTONIC |
78 | #ifndef EV_USE_MONOTONIC |
78 | # define EV_USE_MONOTONIC 1 |
79 | # define EV_USE_MONOTONIC 1 |
79 | #endif |
80 | #endif |
… | |
… | |
94 | # define EV_USE_KQUEUE 0 |
95 | # define EV_USE_KQUEUE 0 |
95 | #endif |
96 | #endif |
96 | |
97 | |
97 | #ifndef EV_USE_WIN32 |
98 | #ifndef EV_USE_WIN32 |
98 | # ifdef WIN32 |
99 | # ifdef WIN32 |
|
|
100 | # define EV_USE_WIN32 0 /* it does not exist, use select */ |
|
|
101 | # undef EV_USE_SELECT |
99 | # define EV_USE_WIN32 1 |
102 | # define EV_USE_SELECT 1 |
100 | # else |
103 | # else |
101 | # define EV_USE_WIN32 0 |
104 | # define EV_USE_WIN32 0 |
102 | # endif |
105 | # endif |
103 | #endif |
106 | #endif |
104 | |
107 | |
… | |
… | |
145 | typedef struct ev_watcher_list *WL; |
148 | typedef struct ev_watcher_list *WL; |
146 | typedef struct ev_watcher_time *WT; |
149 | typedef struct ev_watcher_time *WT; |
147 | |
150 | |
148 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
151 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
149 | |
152 | |
150 | #if WIN32 |
153 | #include "ev_win32.c" |
151 | /* note: the comment below could not be substantiated, but what would I care */ |
|
|
152 | /* MSDN says this is required to handle SIGFPE */ |
|
|
153 | volatile double SIGFPE_REQ = 0.0f; |
|
|
154 | #endif |
|
|
155 | |
154 | |
156 | /*****************************************************************************/ |
155 | /*****************************************************************************/ |
157 | |
156 | |
158 | static void (*syserr_cb)(void); |
157 | static void (*syserr_cb)(const char *msg); |
159 | |
158 | |
160 | void ev_set_syserr_cb (void (*cb)(void)) |
159 | void ev_set_syserr_cb (void (*cb)(const char *msg)) |
161 | { |
160 | { |
162 | syserr_cb = cb; |
161 | syserr_cb = cb; |
163 | } |
162 | } |
164 | |
163 | |
165 | static void |
164 | static void |
166 | syserr (void) |
165 | syserr (const char *msg) |
167 | { |
166 | { |
|
|
167 | if (!msg) |
|
|
168 | msg = "(libev) system error"; |
|
|
169 | |
168 | if (syserr_cb) |
170 | if (syserr_cb) |
169 | syserr_cb (); |
171 | syserr_cb (msg); |
170 | else |
172 | else |
171 | { |
173 | { |
172 | perror ("libev"); |
174 | perror (msg); |
173 | abort (); |
175 | abort (); |
174 | } |
176 | } |
175 | } |
177 | } |
176 | |
178 | |
177 | static void *(*alloc)(void *ptr, long size); |
179 | static void *(*alloc)(void *ptr, long size); |
… | |
… | |
213 | int events; |
215 | int events; |
214 | } ANPENDING; |
216 | } ANPENDING; |
215 | |
217 | |
216 | #if EV_MULTIPLICITY |
218 | #if EV_MULTIPLICITY |
217 | |
219 | |
218 | struct ev_loop |
220 | struct ev_loop |
219 | { |
221 | { |
220 | # define VAR(name,decl) decl; |
222 | #define VAR(name,decl) decl; |
221 | # include "ev_vars.h" |
223 | #include "ev_vars.h" |
222 | }; |
|
|
223 | # undef VAR |
224 | #undef VAR |
|
|
225 | }; |
224 | # include "ev_wrap.h" |
226 | #include "ev_wrap.h" |
|
|
227 | |
|
|
228 | struct ev_loop default_loop_struct; |
|
|
229 | static struct ev_loop *default_loop; |
225 | |
230 | |
226 | #else |
231 | #else |
227 | |
232 | |
228 | # define VAR(name,decl) static decl; |
233 | #define VAR(name,decl) static decl; |
229 | # include "ev_vars.h" |
234 | #include "ev_vars.h" |
230 | # undef VAR |
235 | #undef VAR |
|
|
236 | |
|
|
237 | static int default_loop; |
231 | |
238 | |
232 | #endif |
239 | #endif |
233 | |
240 | |
234 | /*****************************************************************************/ |
241 | /*****************************************************************************/ |
235 | |
242 | |
… | |
… | |
266 | ev_now (EV_P) |
273 | ev_now (EV_P) |
267 | { |
274 | { |
268 | return rt_now; |
275 | return rt_now; |
269 | } |
276 | } |
270 | |
277 | |
271 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
278 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
272 | |
279 | |
273 | #define array_needsize(base,cur,cnt,init) \ |
280 | #define array_needsize(type,base,cur,cnt,init) \ |
274 | if (expect_false ((cnt) > cur)) \ |
281 | if (expect_false ((cnt) > cur)) \ |
275 | { \ |
282 | { \ |
276 | int newcnt = cur; \ |
283 | int newcnt = cur; \ |
277 | do \ |
284 | do \ |
278 | { \ |
285 | { \ |
279 | newcnt = array_roundsize (base, newcnt << 1); \ |
286 | newcnt = array_roundsize (type, newcnt << 1); \ |
280 | } \ |
287 | } \ |
281 | while ((cnt) > newcnt); \ |
288 | while ((cnt) > newcnt); \ |
282 | \ |
289 | \ |
283 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
290 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
284 | init (base + cur, newcnt - cur); \ |
291 | init (base + cur, newcnt - cur); \ |
285 | cur = newcnt; \ |
292 | cur = newcnt; \ |
286 | } |
293 | } |
287 | |
294 | |
288 | #define array_slim(stem) \ |
295 | #define array_slim(type,stem) \ |
289 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
296 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
290 | { \ |
297 | { \ |
291 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
298 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
292 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
299 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
293 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
300 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
294 | } |
301 | } |
|
|
302 | |
|
|
303 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
|
|
304 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
|
|
305 | #define array_free_microshit(stem) \ |
|
|
306 | ev_free (stem ## s); stem ## cnt = stem ## max = 0; |
295 | |
307 | |
296 | #define array_free(stem, idx) \ |
308 | #define array_free(stem, idx) \ |
297 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
309 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
298 | |
310 | |
299 | /*****************************************************************************/ |
311 | /*****************************************************************************/ |
… | |
… | |
309 | |
321 | |
310 | ++base; |
322 | ++base; |
311 | } |
323 | } |
312 | } |
324 | } |
313 | |
325 | |
314 | static void |
326 | void |
315 | event (EV_P_ W w, int events) |
327 | ev_feed_event (EV_P_ void *w, int revents) |
316 | { |
328 | { |
|
|
329 | W w_ = (W)w; |
|
|
330 | |
317 | if (w->pending) |
331 | if (w_->pending) |
318 | { |
332 | { |
319 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
333 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
320 | return; |
334 | return; |
321 | } |
335 | } |
322 | |
336 | |
323 | w->pending = ++pendingcnt [ABSPRI (w)]; |
337 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
324 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
338 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
325 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
339 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
326 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
340 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
327 | } |
341 | } |
328 | |
342 | |
329 | static void |
343 | static void |
330 | queue_events (EV_P_ W *events, int eventcnt, int type) |
344 | queue_events (EV_P_ W *events, int eventcnt, int type) |
331 | { |
345 | { |
332 | int i; |
346 | int i; |
333 | |
347 | |
334 | for (i = 0; i < eventcnt; ++i) |
348 | for (i = 0; i < eventcnt; ++i) |
335 | event (EV_A_ events [i], type); |
349 | ev_feed_event (EV_A_ events [i], type); |
336 | } |
350 | } |
337 | |
351 | |
338 | static void |
352 | inline void |
339 | fd_event (EV_P_ int fd, int events) |
353 | fd_event (EV_P_ int fd, int revents) |
340 | { |
354 | { |
341 | ANFD *anfd = anfds + fd; |
355 | ANFD *anfd = anfds + fd; |
342 | struct ev_io *w; |
356 | struct ev_io *w; |
343 | |
357 | |
344 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
358 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
345 | { |
359 | { |
346 | int ev = w->events & events; |
360 | int ev = w->events & revents; |
347 | |
361 | |
348 | if (ev) |
362 | if (ev) |
349 | event (EV_A_ (W)w, ev); |
363 | ev_feed_event (EV_A_ (W)w, ev); |
350 | } |
364 | } |
|
|
365 | } |
|
|
366 | |
|
|
367 | void |
|
|
368 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
369 | { |
|
|
370 | fd_event (EV_A_ fd, revents); |
351 | } |
371 | } |
352 | |
372 | |
353 | /*****************************************************************************/ |
373 | /*****************************************************************************/ |
354 | |
374 | |
355 | static void |
375 | static void |
… | |
… | |
378 | } |
398 | } |
379 | |
399 | |
380 | static void |
400 | static void |
381 | fd_change (EV_P_ int fd) |
401 | fd_change (EV_P_ int fd) |
382 | { |
402 | { |
383 | if (anfds [fd].reify || fdchangecnt < 0) |
403 | if (anfds [fd].reify) |
384 | return; |
404 | return; |
385 | |
405 | |
386 | anfds [fd].reify = 1; |
406 | anfds [fd].reify = 1; |
387 | |
407 | |
388 | ++fdchangecnt; |
408 | ++fdchangecnt; |
389 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
409 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
390 | fdchanges [fdchangecnt - 1] = fd; |
410 | fdchanges [fdchangecnt - 1] = fd; |
391 | } |
411 | } |
392 | |
412 | |
393 | static void |
413 | static void |
394 | fd_kill (EV_P_ int fd) |
414 | fd_kill (EV_P_ int fd) |
… | |
… | |
396 | struct ev_io *w; |
416 | struct ev_io *w; |
397 | |
417 | |
398 | while ((w = (struct ev_io *)anfds [fd].head)) |
418 | while ((w = (struct ev_io *)anfds [fd].head)) |
399 | { |
419 | { |
400 | ev_io_stop (EV_A_ w); |
420 | ev_io_stop (EV_A_ w); |
401 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
421 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
402 | } |
422 | } |
|
|
423 | } |
|
|
424 | |
|
|
425 | static int |
|
|
426 | fd_valid (int fd) |
|
|
427 | { |
|
|
428 | #ifdef WIN32 |
|
|
429 | return !!win32_get_osfhandle (fd); |
|
|
430 | #else |
|
|
431 | return fcntl (fd, F_GETFD) != -1; |
|
|
432 | #endif |
403 | } |
433 | } |
404 | |
434 | |
405 | /* called on EBADF to verify fds */ |
435 | /* called on EBADF to verify fds */ |
406 | static void |
436 | static void |
407 | fd_ebadf (EV_P) |
437 | fd_ebadf (EV_P) |
408 | { |
438 | { |
409 | int fd; |
439 | int fd; |
410 | |
440 | |
411 | for (fd = 0; fd < anfdmax; ++fd) |
441 | for (fd = 0; fd < anfdmax; ++fd) |
412 | if (anfds [fd].events) |
442 | if (anfds [fd].events) |
413 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
443 | if (!fd_valid (fd) == -1 && errno == EBADF) |
414 | fd_kill (EV_A_ fd); |
444 | fd_kill (EV_A_ fd); |
415 | } |
445 | } |
416 | |
446 | |
417 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
447 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
418 | static void |
448 | static void |
… | |
… | |
426 | fd_kill (EV_A_ fd); |
456 | fd_kill (EV_A_ fd); |
427 | return; |
457 | return; |
428 | } |
458 | } |
429 | } |
459 | } |
430 | |
460 | |
431 | /* susually called after fork if method needs to re-arm all fds from scratch */ |
461 | /* usually called after fork if method needs to re-arm all fds from scratch */ |
432 | static void |
462 | static void |
433 | fd_rearm_all (EV_P) |
463 | fd_rearm_all (EV_P) |
434 | { |
464 | { |
435 | int fd; |
465 | int fd; |
436 | |
466 | |
… | |
… | |
524 | |
554 | |
525 | if (!gotsig) |
555 | if (!gotsig) |
526 | { |
556 | { |
527 | int old_errno = errno; |
557 | int old_errno = errno; |
528 | gotsig = 1; |
558 | gotsig = 1; |
|
|
559 | #ifdef WIN32 |
|
|
560 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
561 | #else |
529 | write (sigpipe [1], &signum, 1); |
562 | write (sigpipe [1], &signum, 1); |
|
|
563 | #endif |
530 | errno = old_errno; |
564 | errno = old_errno; |
531 | } |
565 | } |
532 | } |
566 | } |
533 | |
567 | |
|
|
568 | void |
|
|
569 | ev_feed_signal_event (EV_P_ int signum) |
|
|
570 | { |
|
|
571 | WL w; |
|
|
572 | |
|
|
573 | #if EV_MULTIPLICITY |
|
|
574 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
|
|
575 | #endif |
|
|
576 | |
|
|
577 | --signum; |
|
|
578 | |
|
|
579 | if (signum < 0 || signum >= signalmax) |
|
|
580 | return; |
|
|
581 | |
|
|
582 | signals [signum].gotsig = 0; |
|
|
583 | |
|
|
584 | for (w = signals [signum].head; w; w = w->next) |
|
|
585 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
|
|
586 | } |
|
|
587 | |
534 | static void |
588 | static void |
535 | sigcb (EV_P_ struct ev_io *iow, int revents) |
589 | sigcb (EV_P_ struct ev_io *iow, int revents) |
536 | { |
590 | { |
537 | WL w; |
|
|
538 | int signum; |
591 | int signum; |
539 | |
592 | |
|
|
593 | #ifdef WIN32 |
|
|
594 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
595 | #else |
540 | read (sigpipe [0], &revents, 1); |
596 | read (sigpipe [0], &revents, 1); |
|
|
597 | #endif |
541 | gotsig = 0; |
598 | gotsig = 0; |
542 | |
599 | |
543 | for (signum = signalmax; signum--; ) |
600 | for (signum = signalmax; signum--; ) |
544 | if (signals [signum].gotsig) |
601 | if (signals [signum].gotsig) |
545 | { |
602 | ev_feed_signal_event (EV_A_ signum + 1); |
546 | signals [signum].gotsig = 0; |
|
|
547 | |
|
|
548 | for (w = signals [signum].head; w; w = w->next) |
|
|
549 | event (EV_A_ (W)w, EV_SIGNAL); |
|
|
550 | } |
|
|
551 | } |
603 | } |
552 | |
604 | |
553 | static void |
605 | static void |
554 | siginit (EV_P) |
606 | siginit (EV_P) |
555 | { |
607 | { |
… | |
… | |
567 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
619 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
568 | } |
620 | } |
569 | |
621 | |
570 | /*****************************************************************************/ |
622 | /*****************************************************************************/ |
571 | |
623 | |
|
|
624 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
625 | |
572 | #ifndef WIN32 |
626 | #ifndef WIN32 |
573 | |
627 | |
574 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
575 | static struct ev_signal childev; |
628 | static struct ev_signal childev; |
576 | |
629 | |
577 | #ifndef WCONTINUED |
630 | #ifndef WCONTINUED |
578 | # define WCONTINUED 0 |
631 | # define WCONTINUED 0 |
579 | #endif |
632 | #endif |
… | |
… | |
587 | if (w->pid == pid || !w->pid) |
640 | if (w->pid == pid || !w->pid) |
588 | { |
641 | { |
589 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
642 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
590 | w->rpid = pid; |
643 | w->rpid = pid; |
591 | w->rstatus = status; |
644 | w->rstatus = status; |
592 | event (EV_A_ (W)w, EV_CHILD); |
645 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
593 | } |
646 | } |
594 | } |
647 | } |
595 | |
648 | |
596 | static void |
649 | static void |
597 | childcb (EV_P_ struct ev_signal *sw, int revents) |
650 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
599 | int pid, status; |
652 | int pid, status; |
600 | |
653 | |
601 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
654 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
602 | { |
655 | { |
603 | /* make sure we are called again until all childs have been reaped */ |
656 | /* make sure we are called again until all childs have been reaped */ |
604 | event (EV_A_ (W)sw, EV_SIGNAL); |
657 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
605 | |
658 | |
606 | child_reap (EV_A_ sw, pid, pid, status); |
659 | child_reap (EV_A_ sw, pid, pid, status); |
607 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
660 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
608 | } |
661 | } |
609 | } |
662 | } |
… | |
… | |
693 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
746 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
694 | #endif |
747 | #endif |
695 | #if EV_USE_SELECT |
748 | #if EV_USE_SELECT |
696 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
749 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
697 | #endif |
750 | #endif |
|
|
751 | |
|
|
752 | ev_watcher_init (&sigev, sigcb); |
|
|
753 | ev_set_priority (&sigev, EV_MAXPRI); |
698 | } |
754 | } |
699 | } |
755 | } |
700 | |
756 | |
701 | void |
757 | void |
702 | loop_destroy (EV_P) |
758 | loop_destroy (EV_P) |
… | |
… | |
720 | #endif |
776 | #endif |
721 | |
777 | |
722 | for (i = NUMPRI; i--; ) |
778 | for (i = NUMPRI; i--; ) |
723 | array_free (pending, [i]); |
779 | array_free (pending, [i]); |
724 | |
780 | |
|
|
781 | /* have to use the microsoft-never-gets-it-right macro */ |
725 | array_free (fdchange, ); |
782 | array_free_microshit (fdchange); |
726 | array_free (timer, ); |
783 | array_free_microshit (timer); |
727 | array_free (periodic, ); |
784 | array_free_microshit (periodic); |
728 | array_free (idle, ); |
785 | array_free_microshit (idle); |
729 | array_free (prepare, ); |
786 | array_free_microshit (prepare); |
730 | array_free (check, ); |
787 | array_free_microshit (check); |
731 | |
788 | |
732 | method = 0; |
789 | method = 0; |
733 | /*TODO*/ |
|
|
734 | } |
790 | } |
735 | |
791 | |
736 | void |
792 | static void |
737 | loop_fork (EV_P) |
793 | loop_fork (EV_P) |
738 | { |
794 | { |
739 | /*TODO*/ |
|
|
740 | #if EV_USE_EPOLL |
795 | #if EV_USE_EPOLL |
741 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
796 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
742 | #endif |
797 | #endif |
743 | #if EV_USE_KQUEUE |
798 | #if EV_USE_KQUEUE |
744 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
799 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
745 | #endif |
800 | #endif |
|
|
801 | |
|
|
802 | if (ev_is_active (&sigev)) |
|
|
803 | { |
|
|
804 | /* default loop */ |
|
|
805 | |
|
|
806 | ev_ref (EV_A); |
|
|
807 | ev_io_stop (EV_A_ &sigev); |
|
|
808 | close (sigpipe [0]); |
|
|
809 | close (sigpipe [1]); |
|
|
810 | |
|
|
811 | while (pipe (sigpipe)) |
|
|
812 | syserr ("(libev) error creating pipe"); |
|
|
813 | |
|
|
814 | siginit (EV_A); |
|
|
815 | } |
|
|
816 | |
|
|
817 | postfork = 0; |
746 | } |
818 | } |
747 | |
819 | |
748 | #if EV_MULTIPLICITY |
820 | #if EV_MULTIPLICITY |
749 | struct ev_loop * |
821 | struct ev_loop * |
750 | ev_loop_new (int methods) |
822 | ev_loop_new (int methods) |
… | |
… | |
769 | } |
841 | } |
770 | |
842 | |
771 | void |
843 | void |
772 | ev_loop_fork (EV_P) |
844 | ev_loop_fork (EV_P) |
773 | { |
845 | { |
774 | loop_fork (EV_A); |
846 | postfork = 1; |
775 | } |
847 | } |
776 | |
848 | |
777 | #endif |
849 | #endif |
778 | |
850 | |
779 | #if EV_MULTIPLICITY |
851 | #if EV_MULTIPLICITY |
780 | struct ev_loop default_loop_struct; |
|
|
781 | static struct ev_loop *default_loop; |
|
|
782 | |
|
|
783 | struct ev_loop * |
852 | struct ev_loop * |
784 | #else |
853 | #else |
785 | static int default_loop; |
|
|
786 | |
|
|
787 | int |
854 | int |
788 | #endif |
855 | #endif |
789 | ev_default_loop (int methods) |
856 | ev_default_loop (int methods) |
790 | { |
857 | { |
791 | if (sigpipe [0] == sigpipe [1]) |
858 | if (sigpipe [0] == sigpipe [1]) |
… | |
… | |
802 | |
869 | |
803 | loop_init (EV_A_ methods); |
870 | loop_init (EV_A_ methods); |
804 | |
871 | |
805 | if (ev_method (EV_A)) |
872 | if (ev_method (EV_A)) |
806 | { |
873 | { |
807 | ev_watcher_init (&sigev, sigcb); |
|
|
808 | ev_set_priority (&sigev, EV_MAXPRI); |
|
|
809 | siginit (EV_A); |
874 | siginit (EV_A); |
810 | |
875 | |
811 | #ifndef WIN32 |
876 | #ifndef WIN32 |
812 | ev_signal_init (&childev, childcb, SIGCHLD); |
877 | ev_signal_init (&childev, childcb, SIGCHLD); |
813 | ev_set_priority (&childev, EV_MAXPRI); |
878 | ev_set_priority (&childev, EV_MAXPRI); |
… | |
… | |
827 | { |
892 | { |
828 | #if EV_MULTIPLICITY |
893 | #if EV_MULTIPLICITY |
829 | struct ev_loop *loop = default_loop; |
894 | struct ev_loop *loop = default_loop; |
830 | #endif |
895 | #endif |
831 | |
896 | |
|
|
897 | #ifndef WIN32 |
832 | ev_ref (EV_A); /* child watcher */ |
898 | ev_ref (EV_A); /* child watcher */ |
833 | ev_signal_stop (EV_A_ &childev); |
899 | ev_signal_stop (EV_A_ &childev); |
|
|
900 | #endif |
834 | |
901 | |
835 | ev_ref (EV_A); /* signal watcher */ |
902 | ev_ref (EV_A); /* signal watcher */ |
836 | ev_io_stop (EV_A_ &sigev); |
903 | ev_io_stop (EV_A_ &sigev); |
837 | |
904 | |
838 | close (sigpipe [0]); sigpipe [0] = 0; |
905 | close (sigpipe [0]); sigpipe [0] = 0; |
… | |
… | |
846 | { |
913 | { |
847 | #if EV_MULTIPLICITY |
914 | #if EV_MULTIPLICITY |
848 | struct ev_loop *loop = default_loop; |
915 | struct ev_loop *loop = default_loop; |
849 | #endif |
916 | #endif |
850 | |
917 | |
851 | loop_fork (EV_A); |
918 | if (method) |
852 | |
919 | postfork = 1; |
853 | ev_io_stop (EV_A_ &sigev); |
|
|
854 | close (sigpipe [0]); |
|
|
855 | close (sigpipe [1]); |
|
|
856 | pipe (sigpipe); |
|
|
857 | |
|
|
858 | ev_ref (EV_A); /* signal watcher */ |
|
|
859 | siginit (EV_A); |
|
|
860 | } |
920 | } |
861 | |
921 | |
862 | /*****************************************************************************/ |
922 | /*****************************************************************************/ |
|
|
923 | |
|
|
924 | static int |
|
|
925 | any_pending (EV_P) |
|
|
926 | { |
|
|
927 | int pri; |
|
|
928 | |
|
|
929 | for (pri = NUMPRI; pri--; ) |
|
|
930 | if (pendingcnt [pri]) |
|
|
931 | return 1; |
|
|
932 | |
|
|
933 | return 0; |
|
|
934 | } |
863 | |
935 | |
864 | static void |
936 | static void |
865 | call_pending (EV_P) |
937 | call_pending (EV_P) |
866 | { |
938 | { |
867 | int pri; |
939 | int pri; |
… | |
… | |
896 | downheap ((WT *)timers, timercnt, 0); |
968 | downheap ((WT *)timers, timercnt, 0); |
897 | } |
969 | } |
898 | else |
970 | else |
899 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
971 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
900 | |
972 | |
901 | event (EV_A_ (W)w, EV_TIMEOUT); |
973 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
902 | } |
974 | } |
903 | } |
975 | } |
904 | |
976 | |
905 | static void |
977 | static void |
906 | periodics_reify (EV_P) |
978 | periodics_reify (EV_P) |
… | |
… | |
910 | struct ev_periodic *w = periodics [0]; |
982 | struct ev_periodic *w = periodics [0]; |
911 | |
983 | |
912 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
984 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
913 | |
985 | |
914 | /* first reschedule or stop timer */ |
986 | /* first reschedule or stop timer */ |
|
|
987 | if (w->reschedule_cb) |
|
|
988 | { |
|
|
989 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
|
|
990 | |
|
|
991 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
|
|
992 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
993 | } |
915 | if (w->interval) |
994 | else if (w->interval) |
916 | { |
995 | { |
917 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
996 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
918 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
997 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
919 | downheap ((WT *)periodics, periodiccnt, 0); |
998 | downheap ((WT *)periodics, periodiccnt, 0); |
920 | } |
999 | } |
921 | else |
1000 | else |
922 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1001 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
923 | |
1002 | |
924 | event (EV_A_ (W)w, EV_PERIODIC); |
1003 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
925 | } |
1004 | } |
926 | } |
1005 | } |
927 | |
1006 | |
928 | static void |
1007 | static void |
929 | periodics_reschedule (EV_P) |
1008 | periodics_reschedule (EV_P) |
… | |
… | |
933 | /* adjust periodics after time jump */ |
1012 | /* adjust periodics after time jump */ |
934 | for (i = 0; i < periodiccnt; ++i) |
1013 | for (i = 0; i < periodiccnt; ++i) |
935 | { |
1014 | { |
936 | struct ev_periodic *w = periodics [i]; |
1015 | struct ev_periodic *w = periodics [i]; |
937 | |
1016 | |
|
|
1017 | if (w->reschedule_cb) |
|
|
1018 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
938 | if (w->interval) |
1019 | else if (w->interval) |
939 | { |
|
|
940 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1020 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
941 | |
|
|
942 | if (fabs (diff) >= 1e-4) |
|
|
943 | { |
|
|
944 | ev_periodic_stop (EV_A_ w); |
|
|
945 | ev_periodic_start (EV_A_ w); |
|
|
946 | |
|
|
947 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
948 | } |
|
|
949 | } |
|
|
950 | } |
1021 | } |
|
|
1022 | |
|
|
1023 | /* now rebuild the heap */ |
|
|
1024 | for (i = periodiccnt >> 1; i--; ) |
|
|
1025 | downheap ((WT *)periodics, periodiccnt, i); |
951 | } |
1026 | } |
952 | |
1027 | |
953 | inline int |
1028 | inline int |
954 | time_update_monotonic (EV_P) |
1029 | time_update_monotonic (EV_P) |
955 | { |
1030 | { |
… | |
… | |
1042 | { |
1117 | { |
1043 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1118 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1044 | call_pending (EV_A); |
1119 | call_pending (EV_A); |
1045 | } |
1120 | } |
1046 | |
1121 | |
|
|
1122 | /* we might have forked, so reify kernel state if necessary */ |
|
|
1123 | if (expect_false (postfork)) |
|
|
1124 | loop_fork (EV_A); |
|
|
1125 | |
1047 | /* update fd-related kernel structures */ |
1126 | /* update fd-related kernel structures */ |
1048 | fd_reify (EV_A); |
1127 | fd_reify (EV_A); |
1049 | |
1128 | |
1050 | /* calculate blocking time */ |
1129 | /* calculate blocking time */ |
1051 | |
1130 | |
1052 | /* we only need this for !monotonic clockor timers, but as we basically |
1131 | /* we only need this for !monotonic clock or timers, but as we basically |
1053 | always have timers, we just calculate it always */ |
1132 | always have timers, we just calculate it always */ |
1054 | #if EV_USE_MONOTONIC |
1133 | #if EV_USE_MONOTONIC |
1055 | if (expect_true (have_monotonic)) |
1134 | if (expect_true (have_monotonic)) |
1056 | time_update_monotonic (EV_A); |
1135 | time_update_monotonic (EV_A); |
1057 | else |
1136 | else |
… | |
… | |
1090 | /* queue pending timers and reschedule them */ |
1169 | /* queue pending timers and reschedule them */ |
1091 | timers_reify (EV_A); /* relative timers called last */ |
1170 | timers_reify (EV_A); /* relative timers called last */ |
1092 | periodics_reify (EV_A); /* absolute timers called first */ |
1171 | periodics_reify (EV_A); /* absolute timers called first */ |
1093 | |
1172 | |
1094 | /* queue idle watchers unless io or timers are pending */ |
1173 | /* queue idle watchers unless io or timers are pending */ |
1095 | if (!pendingcnt) |
1174 | if (idlecnt && !any_pending (EV_A)) |
1096 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1175 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1097 | |
1176 | |
1098 | /* queue check watchers, to be executed first */ |
1177 | /* queue check watchers, to be executed first */ |
1099 | if (checkcnt) |
1178 | if (checkcnt) |
1100 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1179 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1175 | return; |
1254 | return; |
1176 | |
1255 | |
1177 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1256 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1178 | |
1257 | |
1179 | ev_start (EV_A_ (W)w, 1); |
1258 | ev_start (EV_A_ (W)w, 1); |
1180 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1259 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1181 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1260 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1182 | |
1261 | |
1183 | fd_change (EV_A_ fd); |
1262 | fd_change (EV_A_ fd); |
1184 | } |
1263 | } |
1185 | |
1264 | |
… | |
… | |
1205 | ((WT)w)->at += mn_now; |
1284 | ((WT)w)->at += mn_now; |
1206 | |
1285 | |
1207 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1286 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1208 | |
1287 | |
1209 | ev_start (EV_A_ (W)w, ++timercnt); |
1288 | ev_start (EV_A_ (W)w, ++timercnt); |
1210 | array_needsize (timers, timermax, timercnt, ); |
1289 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1211 | timers [timercnt - 1] = w; |
1290 | timers [timercnt - 1] = w; |
1212 | upheap ((WT *)timers, timercnt - 1); |
1291 | upheap ((WT *)timers, timercnt - 1); |
1213 | |
1292 | |
1214 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1293 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1215 | } |
1294 | } |
… | |
… | |
1255 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1334 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1256 | { |
1335 | { |
1257 | if (ev_is_active (w)) |
1336 | if (ev_is_active (w)) |
1258 | return; |
1337 | return; |
1259 | |
1338 | |
|
|
1339 | if (w->reschedule_cb) |
|
|
1340 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
|
|
1341 | else if (w->interval) |
|
|
1342 | { |
1260 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1343 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1261 | |
|
|
1262 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1344 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1263 | if (w->interval) |
|
|
1264 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1345 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1346 | } |
1265 | |
1347 | |
1266 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1348 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1267 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1349 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1268 | periodics [periodiccnt - 1] = w; |
1350 | periodics [periodiccnt - 1] = w; |
1269 | upheap ((WT *)periodics, periodiccnt - 1); |
1351 | upheap ((WT *)periodics, periodiccnt - 1); |
1270 | |
1352 | |
1271 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1353 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1272 | } |
1354 | } |
… | |
… | |
1288 | |
1370 | |
1289 | ev_stop (EV_A_ (W)w); |
1371 | ev_stop (EV_A_ (W)w); |
1290 | } |
1372 | } |
1291 | |
1373 | |
1292 | void |
1374 | void |
|
|
1375 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1376 | { |
|
|
1377 | ev_periodic_stop (EV_A_ w); |
|
|
1378 | ev_periodic_start (EV_A_ w); |
|
|
1379 | } |
|
|
1380 | |
|
|
1381 | void |
1293 | ev_idle_start (EV_P_ struct ev_idle *w) |
1382 | ev_idle_start (EV_P_ struct ev_idle *w) |
1294 | { |
1383 | { |
1295 | if (ev_is_active (w)) |
1384 | if (ev_is_active (w)) |
1296 | return; |
1385 | return; |
1297 | |
1386 | |
1298 | ev_start (EV_A_ (W)w, ++idlecnt); |
1387 | ev_start (EV_A_ (W)w, ++idlecnt); |
1299 | array_needsize (idles, idlemax, idlecnt, ); |
1388 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1300 | idles [idlecnt - 1] = w; |
1389 | idles [idlecnt - 1] = w; |
1301 | } |
1390 | } |
1302 | |
1391 | |
1303 | void |
1392 | void |
1304 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1393 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1316 | { |
1405 | { |
1317 | if (ev_is_active (w)) |
1406 | if (ev_is_active (w)) |
1318 | return; |
1407 | return; |
1319 | |
1408 | |
1320 | ev_start (EV_A_ (W)w, ++preparecnt); |
1409 | ev_start (EV_A_ (W)w, ++preparecnt); |
1321 | array_needsize (prepares, preparemax, preparecnt, ); |
1410 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1322 | prepares [preparecnt - 1] = w; |
1411 | prepares [preparecnt - 1] = w; |
1323 | } |
1412 | } |
1324 | |
1413 | |
1325 | void |
1414 | void |
1326 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1415 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1338 | { |
1427 | { |
1339 | if (ev_is_active (w)) |
1428 | if (ev_is_active (w)) |
1340 | return; |
1429 | return; |
1341 | |
1430 | |
1342 | ev_start (EV_A_ (W)w, ++checkcnt); |
1431 | ev_start (EV_A_ (W)w, ++checkcnt); |
1343 | array_needsize (checks, checkmax, checkcnt, ); |
1432 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1344 | checks [checkcnt - 1] = w; |
1433 | checks [checkcnt - 1] = w; |
1345 | } |
1434 | } |
1346 | |
1435 | |
1347 | void |
1436 | void |
1348 | ev_check_stop (EV_P_ struct ev_check *w) |
1437 | ev_check_stop (EV_P_ struct ev_check *w) |
… | |
… | |
1369 | return; |
1458 | return; |
1370 | |
1459 | |
1371 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1460 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1372 | |
1461 | |
1373 | ev_start (EV_A_ (W)w, 1); |
1462 | ev_start (EV_A_ (W)w, 1); |
1374 | array_needsize (signals, signalmax, w->signum, signals_init); |
1463 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1375 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1464 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1376 | |
1465 | |
1377 | if (!((WL)w)->next) |
1466 | if (!((WL)w)->next) |
1378 | { |
1467 | { |
1379 | #if WIN32 |
1468 | #if WIN32 |
… | |
… | |
1462 | } |
1551 | } |
1463 | |
1552 | |
1464 | void |
1553 | void |
1465 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1554 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1466 | { |
1555 | { |
1467 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1556 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1468 | |
1557 | |
1469 | if (!once) |
1558 | if (!once) |
1470 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1559 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1471 | else |
1560 | else |
1472 | { |
1561 | { |