… | |
… | |
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 | |
… | |
… | |
62 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
64 | # define EV_USE_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 |
… | |
… | |
99 | # define inline static |
105 | # define inline static |
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) |
|
|
110 | |
|
|
111 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
112 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
104 | |
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 | |
… | |
… | |
193 | { |
202 | { |
194 | W w; |
203 | W w; |
195 | int events; |
204 | int events; |
196 | } ANPENDING; |
205 | } ANPENDING; |
197 | |
206 | |
198 | static ANPENDING *pendings; |
207 | static ANPENDING *pendings [NUMPRI]; |
199 | static int pendingmax, pendingcnt; |
208 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
200 | |
209 | |
201 | static void |
210 | static void |
202 | event (W w, int events) |
211 | event (W w, int events) |
203 | { |
212 | { |
204 | if (w->pending) |
213 | if (w->pending) |
205 | { |
214 | { |
206 | pendings [w->pending - 1].events |= events; |
215 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
207 | return; |
216 | return; |
208 | } |
217 | } |
209 | |
218 | |
210 | w->pending = ++pendingcnt; |
219 | w->pending = ++pendingcnt [ABSPRI (w)]; |
211 | array_needsize (pendings, pendingmax, pendingcnt, ); |
220 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
212 | pendings [pendingcnt - 1].w = w; |
221 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
213 | pendings [pendingcnt - 1].events = events; |
222 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
214 | } |
223 | } |
215 | |
224 | |
216 | static void |
225 | static void |
217 | queue_events (W *events, int eventcnt, int type) |
226 | queue_events (W *events, int eventcnt, int type) |
218 | { |
227 | { |
… | |
… | |
431 | } |
440 | } |
432 | |
441 | |
433 | static void |
442 | static void |
434 | siginit (void) |
443 | siginit (void) |
435 | { |
444 | { |
|
|
445 | #ifndef WIN32 |
436 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
446 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
437 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
447 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
438 | |
448 | |
439 | /* rather than sort out wether we really need nb, set it */ |
449 | /* rather than sort out wether we really need nb, set it */ |
440 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
450 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
441 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
451 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
452 | #endif |
442 | |
453 | |
443 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
454 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
444 | ev_io_start (&sigev); |
455 | ev_io_start (&sigev); |
445 | } |
456 | } |
446 | |
457 | |
… | |
… | |
458 | /*****************************************************************************/ |
469 | /*****************************************************************************/ |
459 | |
470 | |
460 | static struct ev_child *childs [PID_HASHSIZE]; |
471 | static struct ev_child *childs [PID_HASHSIZE]; |
461 | static struct ev_signal childev; |
472 | static struct ev_signal childev; |
462 | |
473 | |
|
|
474 | #ifndef WIN32 |
|
|
475 | |
463 | #ifndef WCONTINUED |
476 | #ifndef WCONTINUED |
464 | # define WCONTINUED 0 |
477 | # define WCONTINUED 0 |
465 | #endif |
478 | #endif |
466 | |
479 | |
467 | static void |
480 | static void |
|
|
481 | child_reap (struct ev_signal *sw, int chain, int pid, int status) |
|
|
482 | { |
|
|
483 | struct ev_child *w; |
|
|
484 | |
|
|
485 | for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
486 | if (w->pid == pid || !w->pid) |
|
|
487 | { |
|
|
488 | w->priority = sw->priority; /* need to do it *now* */ |
|
|
489 | w->rpid = pid; |
|
|
490 | w->rstatus = status; |
|
|
491 | printf ("rpid %p %d %d\n", w, pid, w->pid);//D |
|
|
492 | event ((W)w, EV_CHILD); |
|
|
493 | } |
|
|
494 | } |
|
|
495 | |
|
|
496 | static void |
468 | childcb (struct ev_signal *sw, int revents) |
497 | childcb (struct ev_signal *sw, int revents) |
469 | { |
498 | { |
470 | struct ev_child *w; |
|
|
471 | int pid, status; |
499 | int pid, status; |
472 | |
500 | |
|
|
501 | printf ("chld %x\n", revents);//D |
473 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
502 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
474 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
503 | { |
475 | if (w->pid == pid || !w->pid) |
504 | /* make sure we are called again until all childs have been reaped */ |
476 | { |
|
|
477 | w->status = status; |
|
|
478 | event ((W)w, EV_CHILD); |
505 | event ((W)sw, EV_SIGNAL); |
479 | } |
506 | |
|
|
507 | child_reap (sw, pid, pid, status); |
|
|
508 | child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
|
|
509 | } |
480 | } |
510 | } |
|
|
511 | |
|
|
512 | #endif |
481 | |
513 | |
482 | /*****************************************************************************/ |
514 | /*****************************************************************************/ |
483 | |
515 | |
|
|
516 | #if EV_USE_KQUEUE |
|
|
517 | # include "ev_kqueue.c" |
|
|
518 | #endif |
484 | #if EV_USE_EPOLL |
519 | #if EV_USE_EPOLL |
485 | # include "ev_epoll.c" |
520 | # include "ev_epoll.c" |
486 | #endif |
521 | #endif |
487 | #if EV_USE_POLL |
522 | #if EV_USE_POLL |
488 | # include "ev_poll.c" |
523 | # include "ev_poll.c" |
… | |
… | |
536 | methods = atoi (getenv ("LIBEV_METHODS")); |
571 | methods = atoi (getenv ("LIBEV_METHODS")); |
537 | else |
572 | else |
538 | methods = EVMETHOD_ANY; |
573 | methods = EVMETHOD_ANY; |
539 | |
574 | |
540 | ev_method = 0; |
575 | ev_method = 0; |
|
|
576 | #if EV_USE_KQUEUE |
|
|
577 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
|
|
578 | #endif |
541 | #if EV_USE_EPOLL |
579 | #if EV_USE_EPOLL |
542 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
580 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
543 | #endif |
581 | #endif |
544 | #if EV_USE_POLL |
582 | #if EV_USE_POLL |
545 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
583 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
… | |
… | |
549 | #endif |
587 | #endif |
550 | |
588 | |
551 | if (ev_method) |
589 | if (ev_method) |
552 | { |
590 | { |
553 | ev_watcher_init (&sigev, sigcb); |
591 | ev_watcher_init (&sigev, sigcb); |
|
|
592 | ev_set_priority (&sigev, EV_MAXPRI); |
554 | siginit (); |
593 | siginit (); |
555 | |
594 | |
|
|
595 | #ifndef WIN32 |
556 | ev_signal_init (&childev, childcb, SIGCHLD); |
596 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
597 | ev_set_priority (&childev, EV_MAXPRI); |
557 | ev_signal_start (&childev); |
598 | ev_signal_start (&childev); |
|
|
599 | #endif |
558 | } |
600 | } |
559 | } |
601 | } |
560 | |
602 | |
561 | return ev_method; |
603 | return ev_method; |
562 | } |
604 | } |
… | |
… | |
593 | /*****************************************************************************/ |
635 | /*****************************************************************************/ |
594 | |
636 | |
595 | static void |
637 | static void |
596 | call_pending (void) |
638 | call_pending (void) |
597 | { |
639 | { |
|
|
640 | int pri; |
|
|
641 | |
|
|
642 | for (pri = NUMPRI; pri--; ) |
598 | while (pendingcnt) |
643 | while (pendingcnt [pri]) |
599 | { |
644 | { |
600 | ANPENDING *p = pendings + --pendingcnt; |
645 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
601 | |
646 | |
602 | if (p->w) |
647 | if (p->w) |
603 | { |
648 | { |
604 | p->w->pending = 0; |
649 | p->w->pending = 0; |
605 | p->w->cb (p->w, p->events); |
650 | p->w->cb (p->w, p->events); |
606 | } |
651 | } |
607 | } |
652 | } |
608 | } |
653 | } |
609 | |
654 | |
610 | static void |
655 | static void |
611 | timers_reify (void) |
656 | timers_reify (void) |
612 | { |
657 | { |
… | |
… | |
844 | static void |
889 | static void |
845 | ev_clear_pending (W w) |
890 | ev_clear_pending (W w) |
846 | { |
891 | { |
847 | if (w->pending) |
892 | if (w->pending) |
848 | { |
893 | { |
849 | pendings [w->pending - 1].w = 0; |
894 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
850 | w->pending = 0; |
895 | w->pending = 0; |
851 | } |
896 | } |
852 | } |
897 | } |
853 | |
898 | |
854 | static void |
899 | static void |
855 | ev_start (W w, int active) |
900 | ev_start (W w, int active) |
856 | { |
901 | { |
|
|
902 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
903 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
904 | |
857 | w->active = active; |
905 | w->active = active; |
858 | } |
906 | } |
859 | |
907 | |
860 | static void |
908 | static void |
861 | ev_stop (W w) |
909 | ev_stop (W w) |
… | |
… | |
978 | } |
1026 | } |
979 | |
1027 | |
980 | ev_stop ((W)w); |
1028 | ev_stop ((W)w); |
981 | } |
1029 | } |
982 | |
1030 | |
|
|
1031 | #ifndef SA_RESTART |
|
|
1032 | # define SA_RESTART 0 |
|
|
1033 | #endif |
|
|
1034 | |
983 | void |
1035 | void |
984 | ev_signal_start (struct ev_signal *w) |
1036 | ev_signal_start (struct ev_signal *w) |
985 | { |
1037 | { |
986 | if (ev_is_active (w)) |
1038 | if (ev_is_active (w)) |
987 | return; |
1039 | return; |
… | |
… | |
995 | if (!w->next) |
1047 | if (!w->next) |
996 | { |
1048 | { |
997 | struct sigaction sa; |
1049 | struct sigaction sa; |
998 | sa.sa_handler = sighandler; |
1050 | sa.sa_handler = sighandler; |
999 | sigfillset (&sa.sa_mask); |
1051 | sigfillset (&sa.sa_mask); |
1000 | sa.sa_flags = 0; |
1052 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1001 | sigaction (w->signum, &sa, 0); |
1053 | sigaction (w->signum, &sa, 0); |
1002 | } |
1054 | } |
1003 | } |
1055 | } |
1004 | |
1056 | |
1005 | void |
1057 | void |