… | |
… | |
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 | |
… | |
… | |
457 | |
468 | |
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; |
|
|
473 | |
|
|
474 | #ifndef WIN32 |
462 | |
475 | |
463 | #ifndef WCONTINUED |
476 | #ifndef WCONTINUED |
464 | # define WCONTINUED 0 |
477 | # define WCONTINUED 0 |
465 | #endif |
478 | #endif |
466 | |
479 | |
… | |
… | |
472 | |
485 | |
473 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
486 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
474 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
487 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
475 | if (w->pid == pid || !w->pid) |
488 | if (w->pid == pid || !w->pid) |
476 | { |
489 | { |
|
|
490 | w->rpid = pid; |
477 | w->status = status; |
491 | w->rstatus = status; |
478 | event ((W)w, EV_CHILD); |
492 | event ((W)w, EV_CHILD); |
479 | } |
493 | } |
480 | } |
494 | } |
481 | |
495 | |
482 | /*****************************************************************************/ |
496 | #endif |
483 | |
497 | |
|
|
498 | /*****************************************************************************/ |
|
|
499 | |
|
|
500 | #if EV_USE_KQUEUE |
|
|
501 | # include "ev_kqueue.c" |
|
|
502 | #endif |
484 | #if EV_USE_EPOLL |
503 | #if EV_USE_EPOLL |
485 | # include "ev_epoll.c" |
504 | # include "ev_epoll.c" |
486 | #endif |
505 | #endif |
487 | #if EV_USE_POLL |
506 | #if EV_USE_POLL |
488 | # include "ev_poll.c" |
507 | # include "ev_poll.c" |
… | |
… | |
536 | methods = atoi (getenv ("LIBEV_METHODS")); |
555 | methods = atoi (getenv ("LIBEV_METHODS")); |
537 | else |
556 | else |
538 | methods = EVMETHOD_ANY; |
557 | methods = EVMETHOD_ANY; |
539 | |
558 | |
540 | ev_method = 0; |
559 | ev_method = 0; |
|
|
560 | #if EV_USE_KQUEUE |
|
|
561 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
|
|
562 | #endif |
541 | #if EV_USE_EPOLL |
563 | #if EV_USE_EPOLL |
542 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
564 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
543 | #endif |
565 | #endif |
544 | #if EV_USE_POLL |
566 | #if EV_USE_POLL |
545 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
567 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
… | |
… | |
551 | if (ev_method) |
573 | if (ev_method) |
552 | { |
574 | { |
553 | ev_watcher_init (&sigev, sigcb); |
575 | ev_watcher_init (&sigev, sigcb); |
554 | siginit (); |
576 | siginit (); |
555 | |
577 | |
|
|
578 | #ifndef WIN32 |
556 | ev_signal_init (&childev, childcb, SIGCHLD); |
579 | ev_signal_init (&childev, childcb, SIGCHLD); |
557 | ev_signal_start (&childev); |
580 | ev_signal_start (&childev); |
|
|
581 | #endif |
558 | } |
582 | } |
559 | } |
583 | } |
560 | |
584 | |
561 | return ev_method; |
585 | return ev_method; |
562 | } |
586 | } |
… | |
… | |
593 | /*****************************************************************************/ |
617 | /*****************************************************************************/ |
594 | |
618 | |
595 | static void |
619 | static void |
596 | call_pending (void) |
620 | call_pending (void) |
597 | { |
621 | { |
|
|
622 | int pri; |
|
|
623 | |
|
|
624 | for (pri = NUMPRI; pri--; ) |
598 | while (pendingcnt) |
625 | while (pendingcnt [pri]) |
599 | { |
626 | { |
600 | ANPENDING *p = pendings + --pendingcnt; |
627 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
601 | |
628 | |
602 | if (p->w) |
629 | if (p->w) |
603 | { |
630 | { |
604 | p->w->pending = 0; |
631 | p->w->pending = 0; |
605 | p->w->cb (p->w, p->events); |
632 | p->w->cb (p->w, p->events); |
606 | } |
633 | } |
607 | } |
634 | } |
608 | } |
635 | } |
609 | |
636 | |
610 | static void |
637 | static void |
611 | timers_reify (void) |
638 | timers_reify (void) |
612 | { |
639 | { |
… | |
… | |
844 | static void |
871 | static void |
845 | ev_clear_pending (W w) |
872 | ev_clear_pending (W w) |
846 | { |
873 | { |
847 | if (w->pending) |
874 | if (w->pending) |
848 | { |
875 | { |
849 | pendings [w->pending - 1].w = 0; |
876 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
850 | w->pending = 0; |
877 | w->pending = 0; |
851 | } |
878 | } |
852 | } |
879 | } |
853 | |
880 | |
854 | static void |
881 | static void |
855 | ev_start (W w, int active) |
882 | ev_start (W w, int active) |
856 | { |
883 | { |
|
|
884 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
885 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
886 | |
857 | w->active = active; |
887 | w->active = active; |
858 | } |
888 | } |
859 | |
889 | |
860 | static void |
890 | static void |
861 | ev_stop (W w) |
891 | ev_stop (W w) |