… | |
… | |
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 | { |
… | |
… | |
404 | { |
413 | { |
405 | signals [signum - 1].gotsig = 1; |
414 | signals [signum - 1].gotsig = 1; |
406 | |
415 | |
407 | if (!gotsig) |
416 | if (!gotsig) |
408 | { |
417 | { |
|
|
418 | int old_errno = errno; |
409 | gotsig = 1; |
419 | gotsig = 1; |
410 | write (sigpipe [1], &signum, 1); |
420 | write (sigpipe [1], &signum, 1); |
|
|
421 | errno = old_errno; |
411 | } |
422 | } |
412 | } |
423 | } |
413 | |
424 | |
414 | static void |
425 | static void |
415 | sigcb (struct ev_io *iow, int revents) |
426 | sigcb (struct ev_io *iow, int revents) |
… | |
… | |
431 | } |
442 | } |
432 | |
443 | |
433 | static void |
444 | static void |
434 | siginit (void) |
445 | siginit (void) |
435 | { |
446 | { |
|
|
447 | #ifndef WIN32 |
436 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
448 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
437 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
449 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
438 | |
450 | |
439 | /* rather than sort out wether we really need nb, set it */ |
451 | /* rather than sort out wether we really need nb, set it */ |
440 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
452 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
441 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
453 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
454 | #endif |
442 | |
455 | |
443 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
456 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
444 | ev_io_start (&sigev); |
457 | ev_io_start (&sigev); |
445 | } |
458 | } |
446 | |
459 | |
… | |
… | |
458 | /*****************************************************************************/ |
471 | /*****************************************************************************/ |
459 | |
472 | |
460 | static struct ev_child *childs [PID_HASHSIZE]; |
473 | static struct ev_child *childs [PID_HASHSIZE]; |
461 | static struct ev_signal childev; |
474 | static struct ev_signal childev; |
462 | |
475 | |
|
|
476 | #ifndef WIN32 |
|
|
477 | |
463 | #ifndef WCONTINUED |
478 | #ifndef WCONTINUED |
464 | # define WCONTINUED 0 |
479 | # define WCONTINUED 0 |
465 | #endif |
480 | #endif |
466 | |
481 | |
467 | static void |
482 | static void |
|
|
483 | child_reap (struct ev_signal *sw, int chain, int pid, int status) |
|
|
484 | { |
|
|
485 | struct ev_child *w; |
|
|
486 | |
|
|
487 | for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
488 | if (w->pid == pid || !w->pid) |
|
|
489 | { |
|
|
490 | w->priority = sw->priority; /* need to do it *now* */ |
|
|
491 | w->rpid = pid; |
|
|
492 | w->rstatus = status; |
|
|
493 | printf ("rpid %p %d %d\n", w, pid, w->pid);//D |
|
|
494 | event ((W)w, EV_CHILD); |
|
|
495 | } |
|
|
496 | } |
|
|
497 | |
|
|
498 | static void |
468 | childcb (struct ev_signal *sw, int revents) |
499 | childcb (struct ev_signal *sw, int revents) |
469 | { |
500 | { |
470 | struct ev_child *w; |
|
|
471 | int pid, status; |
501 | int pid, status; |
472 | |
502 | |
|
|
503 | printf ("chld %x\n", revents);//D |
473 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
504 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
474 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
505 | { |
475 | if (w->pid == pid || !w->pid) |
506 | /* make sure we are called again until all childs have been reaped */ |
476 | { |
|
|
477 | w->status = status; |
|
|
478 | event ((W)w, EV_CHILD); |
507 | event ((W)sw, EV_SIGNAL); |
479 | } |
508 | |
|
|
509 | child_reap (sw, pid, pid, status); |
|
|
510 | child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
|
|
511 | } |
480 | } |
512 | } |
|
|
513 | |
|
|
514 | #endif |
481 | |
515 | |
482 | /*****************************************************************************/ |
516 | /*****************************************************************************/ |
483 | |
517 | |
|
|
518 | #if EV_USE_KQUEUE |
|
|
519 | # include "ev_kqueue.c" |
|
|
520 | #endif |
484 | #if EV_USE_EPOLL |
521 | #if EV_USE_EPOLL |
485 | # include "ev_epoll.c" |
522 | # include "ev_epoll.c" |
486 | #endif |
523 | #endif |
487 | #if EV_USE_POLL |
524 | #if EV_USE_POLL |
488 | # include "ev_poll.c" |
525 | # include "ev_poll.c" |
… | |
… | |
536 | methods = atoi (getenv ("LIBEV_METHODS")); |
573 | methods = atoi (getenv ("LIBEV_METHODS")); |
537 | else |
574 | else |
538 | methods = EVMETHOD_ANY; |
575 | methods = EVMETHOD_ANY; |
539 | |
576 | |
540 | ev_method = 0; |
577 | ev_method = 0; |
|
|
578 | #if EV_USE_KQUEUE |
|
|
579 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
|
|
580 | #endif |
541 | #if EV_USE_EPOLL |
581 | #if EV_USE_EPOLL |
542 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
582 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
543 | #endif |
583 | #endif |
544 | #if EV_USE_POLL |
584 | #if EV_USE_POLL |
545 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
585 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
… | |
… | |
549 | #endif |
589 | #endif |
550 | |
590 | |
551 | if (ev_method) |
591 | if (ev_method) |
552 | { |
592 | { |
553 | ev_watcher_init (&sigev, sigcb); |
593 | ev_watcher_init (&sigev, sigcb); |
|
|
594 | ev_set_priority (&sigev, EV_MAXPRI); |
554 | siginit (); |
595 | siginit (); |
555 | |
596 | |
|
|
597 | #ifndef WIN32 |
556 | ev_signal_init (&childev, childcb, SIGCHLD); |
598 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
599 | ev_set_priority (&childev, EV_MAXPRI); |
557 | ev_signal_start (&childev); |
600 | ev_signal_start (&childev); |
|
|
601 | #endif |
558 | } |
602 | } |
559 | } |
603 | } |
560 | |
604 | |
561 | return ev_method; |
605 | return ev_method; |
562 | } |
606 | } |
… | |
… | |
593 | /*****************************************************************************/ |
637 | /*****************************************************************************/ |
594 | |
638 | |
595 | static void |
639 | static void |
596 | call_pending (void) |
640 | call_pending (void) |
597 | { |
641 | { |
|
|
642 | int pri; |
|
|
643 | |
|
|
644 | for (pri = NUMPRI; pri--; ) |
598 | while (pendingcnt) |
645 | while (pendingcnt [pri]) |
599 | { |
646 | { |
600 | ANPENDING *p = pendings + --pendingcnt; |
647 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
601 | |
648 | |
602 | if (p->w) |
649 | if (p->w) |
603 | { |
650 | { |
604 | p->w->pending = 0; |
651 | p->w->pending = 0; |
605 | p->w->cb (p->w, p->events); |
652 | p->w->cb (p->w, p->events); |
606 | } |
653 | } |
607 | } |
654 | } |
608 | } |
655 | } |
609 | |
656 | |
610 | static void |
657 | static void |
611 | timers_reify (void) |
658 | timers_reify (void) |
612 | { |
659 | { |
… | |
… | |
844 | static void |
891 | static void |
845 | ev_clear_pending (W w) |
892 | ev_clear_pending (W w) |
846 | { |
893 | { |
847 | if (w->pending) |
894 | if (w->pending) |
848 | { |
895 | { |
849 | pendings [w->pending - 1].w = 0; |
896 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
850 | w->pending = 0; |
897 | w->pending = 0; |
851 | } |
898 | } |
852 | } |
899 | } |
853 | |
900 | |
854 | static void |
901 | static void |
855 | ev_start (W w, int active) |
902 | ev_start (W w, int active) |
856 | { |
903 | { |
|
|
904 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
905 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
906 | |
857 | w->active = active; |
907 | w->active = active; |
858 | } |
908 | } |
859 | |
909 | |
860 | static void |
910 | static void |
861 | ev_stop (W w) |
911 | ev_stop (W w) |
… | |
… | |
978 | } |
1028 | } |
979 | |
1029 | |
980 | ev_stop ((W)w); |
1030 | ev_stop ((W)w); |
981 | } |
1031 | } |
982 | |
1032 | |
|
|
1033 | #ifndef SA_RESTART |
|
|
1034 | # define SA_RESTART 0 |
|
|
1035 | #endif |
|
|
1036 | |
983 | void |
1037 | void |
984 | ev_signal_start (struct ev_signal *w) |
1038 | ev_signal_start (struct ev_signal *w) |
985 | { |
1039 | { |
986 | if (ev_is_active (w)) |
1040 | if (ev_is_active (w)) |
987 | return; |
1041 | return; |
… | |
… | |
995 | if (!w->next) |
1049 | if (!w->next) |
996 | { |
1050 | { |
997 | struct sigaction sa; |
1051 | struct sigaction sa; |
998 | sa.sa_handler = sighandler; |
1052 | sa.sa_handler = sighandler; |
999 | sigfillset (&sa.sa_mask); |
1053 | sigfillset (&sa.sa_mask); |
1000 | sa.sa_flags = 0; |
1054 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1001 | sigaction (w->signum, &sa, 0); |
1055 | sigaction (w->signum, &sa, 0); |
1002 | } |
1056 | } |
1003 | } |
1057 | } |
1004 | |
1058 | |
1005 | void |
1059 | void |