… | |
… | |
57 | # endif |
57 | # endif |
58 | # ifndef EV_USE_MONOTONIC |
58 | # ifndef EV_USE_MONOTONIC |
59 | # define EV_USE_MONOTONIC 1 |
59 | # define EV_USE_MONOTONIC 1 |
60 | # endif |
60 | # endif |
61 | # endif |
61 | # endif |
|
|
62 | # elif !defined(EV_USE_CLOCK_SYSCALL) |
|
|
63 | # define EV_USE_CLOCK_SYSCALL 0 |
62 | # endif |
64 | # endif |
63 | |
65 | |
64 | # if HAVE_CLOCK_GETTIME |
66 | # if HAVE_CLOCK_GETTIME |
65 | # ifndef EV_USE_MONOTONIC |
67 | # ifndef EV_USE_MONOTONIC |
66 | # define EV_USE_MONOTONIC 1 |
68 | # define EV_USE_MONOTONIC 1 |
… | |
… | |
131 | # else |
133 | # else |
132 | # define EV_USE_INOTIFY 0 |
134 | # define EV_USE_INOTIFY 0 |
133 | # endif |
135 | # endif |
134 | # endif |
136 | # endif |
135 | |
137 | |
|
|
138 | # ifndef EV_USE_SIGNALFD |
|
|
139 | # if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H |
|
|
140 | # define EV_USE_SIGNALFD 1 |
|
|
141 | # else |
|
|
142 | # define EV_USE_SIGNALFD 0 |
|
|
143 | # endif |
|
|
144 | # endif |
|
|
145 | |
136 | # ifndef EV_USE_EVENTFD |
146 | # ifndef EV_USE_EVENTFD |
137 | # if HAVE_EVENTFD |
147 | # if HAVE_EVENTFD |
138 | # define EV_USE_EVENTFD 1 |
148 | # define EV_USE_EVENTFD 1 |
139 | # else |
149 | # else |
140 | # define EV_USE_EVENTFD 0 |
150 | # define EV_USE_EVENTFD 0 |
… | |
… | |
176 | # endif |
186 | # endif |
177 | #endif |
187 | #endif |
178 | |
188 | |
179 | /* this block tries to deduce configuration from header-defined symbols and defaults */ |
189 | /* this block tries to deduce configuration from header-defined symbols and defaults */ |
180 | |
190 | |
|
|
191 | /* try to deduce the maximum number of signals on this platform */ |
|
|
192 | #if defined (EV_NSIG) |
|
|
193 | /* use what's provided */ |
|
|
194 | #elif defined (NSIG) |
|
|
195 | # define EV_NSIG (NSIG) |
|
|
196 | #elif defined(_NSIG) |
|
|
197 | # define EV_NSIG (_NSIG) |
|
|
198 | #elif defined (SIGMAX) |
|
|
199 | # define EV_NSIG (SIGMAX+1) |
|
|
200 | #elif defined (SIG_MAX) |
|
|
201 | # define EV_NSIG (SIG_MAX+1) |
|
|
202 | #elif defined (_SIG_MAX) |
|
|
203 | # define EV_NSIG (_SIG_MAX+1) |
|
|
204 | #elif defined (MAXSIG) |
|
|
205 | # define EV_NSIG (MAXSIG+1) |
|
|
206 | #elif defined (MAX_SIG) |
|
|
207 | # define EV_NSIG (MAX_SIG+1) |
|
|
208 | #elif defined (SIGARRAYSIZE) |
|
|
209 | # define EV_NSIG SIGARRAYSIZE /* Assume ary[SIGARRAYSIZE] */ |
|
|
210 | #elif defined (_sys_nsig) |
|
|
211 | # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */ |
|
|
212 | #else |
|
|
213 | # error "unable to find value for NSIG, please report" |
|
|
214 | /* to make it compile regardless, just remove the above line */ |
|
|
215 | # define EV_NSIG 65 |
|
|
216 | #endif |
|
|
217 | |
181 | #ifndef EV_USE_CLOCK_SYSCALL |
218 | #ifndef EV_USE_CLOCK_SYSCALL |
182 | # if __linux && __GLIBC__ >= 2 |
219 | # if __linux && __GLIBC__ >= 2 |
183 | # define EV_USE_CLOCK_SYSCALL 1 |
220 | # define EV_USE_CLOCK_SYSCALL 1 |
184 | # else |
221 | # else |
185 | # define EV_USE_CLOCK_SYSCALL 0 |
222 | # define EV_USE_CLOCK_SYSCALL 0 |
… | |
… | |
264 | # else |
301 | # else |
265 | # define EV_USE_EVENTFD 0 |
302 | # define EV_USE_EVENTFD 0 |
266 | # endif |
303 | # endif |
267 | #endif |
304 | #endif |
268 | |
305 | |
|
|
306 | #ifndef EV_USE_SIGNALFD |
|
|
307 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9)) |
|
|
308 | # define EV_USE_SIGNALFD 1 |
|
|
309 | # else |
|
|
310 | # define EV_USE_SIGNALFD 0 |
|
|
311 | # endif |
|
|
312 | #endif |
|
|
313 | |
269 | #if 0 /* debugging */ |
314 | #if 0 /* debugging */ |
270 | # define EV_VERIFY 3 |
315 | # define EV_VERIFY 3 |
271 | # define EV_USE_4HEAP 1 |
316 | # define EV_USE_4HEAP 1 |
272 | # define EV_HEAP_CACHE_AT 1 |
317 | # define EV_HEAP_CACHE_AT 1 |
273 | #endif |
318 | #endif |
… | |
… | |
280 | # define EV_USE_4HEAP !EV_MINIMAL |
325 | # define EV_USE_4HEAP !EV_MINIMAL |
281 | #endif |
326 | #endif |
282 | |
327 | |
283 | #ifndef EV_HEAP_CACHE_AT |
328 | #ifndef EV_HEAP_CACHE_AT |
284 | # define EV_HEAP_CACHE_AT !EV_MINIMAL |
329 | # define EV_HEAP_CACHE_AT !EV_MINIMAL |
|
|
330 | #endif |
|
|
331 | |
|
|
332 | /* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ |
|
|
333 | /* which makes programs even slower. might work on other unices, too. */ |
|
|
334 | #if EV_USE_CLOCK_SYSCALL |
|
|
335 | # include <syscall.h> |
|
|
336 | # ifdef SYS_clock_gettime |
|
|
337 | # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
|
|
338 | # undef EV_USE_MONOTONIC |
|
|
339 | # define EV_USE_MONOTONIC 1 |
|
|
340 | # else |
|
|
341 | # undef EV_USE_CLOCK_SYSCALL |
|
|
342 | # define EV_USE_CLOCK_SYSCALL 0 |
|
|
343 | # endif |
285 | #endif |
344 | #endif |
286 | |
345 | |
287 | /* this block fixes any misconfiguration where we know we run into trouble otherwise */ |
346 | /* this block fixes any misconfiguration where we know we run into trouble otherwise */ |
288 | |
347 | |
289 | #ifndef CLOCK_MONOTONIC |
348 | #ifndef CLOCK_MONOTONIC |
… | |
… | |
320 | |
379 | |
321 | #if EV_SELECT_IS_WINSOCKET |
380 | #if EV_SELECT_IS_WINSOCKET |
322 | # include <winsock.h> |
381 | # include <winsock.h> |
323 | #endif |
382 | #endif |
324 | |
383 | |
325 | /* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */ |
|
|
326 | /* which makes programs even slower. might work on other unices, too. */ |
|
|
327 | #if EV_USE_CLOCK_SYSCALL |
|
|
328 | # include <syscall.h> |
|
|
329 | # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) |
|
|
330 | # undef EV_USE_MONOTONIC |
|
|
331 | # define EV_USE_MONOTONIC 1 |
|
|
332 | #endif |
|
|
333 | |
|
|
334 | #if EV_USE_EVENTFD |
384 | #if EV_USE_EVENTFD |
335 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
385 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
336 | # include <stdint.h> |
386 | # include <stdint.h> |
|
|
387 | # ifndef EFD_NONBLOCK |
|
|
388 | # define EFD_NONBLOCK O_NONBLOCK |
|
|
389 | # endif |
|
|
390 | # ifndef EFD_CLOEXEC |
|
|
391 | # ifdef O_CLOEXEC |
|
|
392 | # define EFD_CLOEXEC O_CLOEXEC |
|
|
393 | # else |
|
|
394 | # define EFD_CLOEXEC 02000000 |
|
|
395 | # endif |
|
|
396 | # endif |
337 | # ifdef __cplusplus |
397 | # ifdef __cplusplus |
338 | extern "C" { |
398 | extern "C" { |
339 | # endif |
399 | # endif |
340 | int eventfd (unsigned int initval, int flags); |
400 | int eventfd (unsigned int initval, int flags); |
341 | # ifdef __cplusplus |
401 | # ifdef __cplusplus |
342 | } |
402 | } |
343 | # endif |
403 | # endif |
|
|
404 | #endif |
|
|
405 | |
|
|
406 | #if EV_USE_SIGNALFD |
|
|
407 | # include <sys/signalfd.h> |
344 | #endif |
408 | #endif |
345 | |
409 | |
346 | /**/ |
410 | /**/ |
347 | |
411 | |
348 | #if EV_VERIFY >= 3 |
412 | #if EV_VERIFY >= 3 |
… | |
… | |
384 | # define inline_speed static noinline |
448 | # define inline_speed static noinline |
385 | #else |
449 | #else |
386 | # define inline_speed static inline |
450 | # define inline_speed static inline |
387 | #endif |
451 | #endif |
388 | |
452 | |
389 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
453 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
454 | |
|
|
455 | #if EV_MINPRI == EV_MAXPRI |
|
|
456 | # define ABSPRI(w) (((W)w), 0) |
|
|
457 | #else |
390 | #define ABSPRI(w) (((W)w)->priority - EV_MINPRI) |
458 | # define ABSPRI(w) (((W)w)->priority - EV_MINPRI) |
|
|
459 | #endif |
391 | |
460 | |
392 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
461 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
393 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
462 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
394 | |
463 | |
395 | typedef ev_watcher *W; |
464 | typedef ev_watcher *W; |
… | |
… | |
407 | |
476 | |
408 | #if EV_USE_MONOTONIC |
477 | #if EV_USE_MONOTONIC |
409 | static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
478 | static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
410 | #endif |
479 | #endif |
411 | |
480 | |
|
|
481 | #ifndef EV_FD_TO_WIN32_HANDLE |
|
|
482 | # define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd) |
|
|
483 | #endif |
|
|
484 | #ifndef EV_WIN32_HANDLE_TO_FD |
|
|
485 | # define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (fd, 0) |
|
|
486 | #endif |
|
|
487 | #ifndef EV_WIN32_CLOSE_FD |
|
|
488 | # define EV_WIN32_CLOSE_FD(fd) close (fd) |
|
|
489 | #endif |
|
|
490 | |
412 | #ifdef _WIN32 |
491 | #ifdef _WIN32 |
413 | # include "ev_win32.c" |
492 | # include "ev_win32.c" |
414 | #endif |
493 | #endif |
415 | |
494 | |
416 | /*****************************************************************************/ |
495 | /*****************************************************************************/ |
… | |
… | |
478 | #define ev_malloc(size) ev_realloc (0, (size)) |
557 | #define ev_malloc(size) ev_realloc (0, (size)) |
479 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
558 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
480 | |
559 | |
481 | /*****************************************************************************/ |
560 | /*****************************************************************************/ |
482 | |
561 | |
|
|
562 | /* set in reify when reification needed */ |
|
|
563 | #define EV_ANFD_REIFY 1 |
|
|
564 | |
|
|
565 | /* file descriptor info structure */ |
483 | typedef struct |
566 | typedef struct |
484 | { |
567 | { |
485 | WL head; |
568 | WL head; |
486 | unsigned char events; |
569 | unsigned char events; /* the events watched for */ |
487 | unsigned char reify; |
570 | unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */ |
488 | unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ |
571 | unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ |
489 | unsigned char unused; |
572 | unsigned char unused; |
490 | #if EV_USE_EPOLL |
573 | #if EV_USE_EPOLL |
491 | unsigned int egen; /* generation counter to counter epoll bugs */ |
574 | unsigned int egen; /* generation counter to counter epoll bugs */ |
492 | #endif |
575 | #endif |
493 | #if EV_SELECT_IS_WINSOCKET |
576 | #if EV_SELECT_IS_WINSOCKET |
494 | SOCKET handle; |
577 | SOCKET handle; |
495 | #endif |
578 | #endif |
496 | } ANFD; |
579 | } ANFD; |
497 | |
580 | |
|
|
581 | /* stores the pending event set for a given watcher */ |
498 | typedef struct |
582 | typedef struct |
499 | { |
583 | { |
500 | W w; |
584 | W w; |
501 | int events; |
585 | int events; /* the pending event set for the given watcher */ |
502 | } ANPENDING; |
586 | } ANPENDING; |
503 | |
587 | |
504 | #if EV_USE_INOTIFY |
588 | #if EV_USE_INOTIFY |
505 | /* hash table entry per inotify-id */ |
589 | /* hash table entry per inotify-id */ |
506 | typedef struct |
590 | typedef struct |
… | |
… | |
509 | } ANFS; |
593 | } ANFS; |
510 | #endif |
594 | #endif |
511 | |
595 | |
512 | /* Heap Entry */ |
596 | /* Heap Entry */ |
513 | #if EV_HEAP_CACHE_AT |
597 | #if EV_HEAP_CACHE_AT |
|
|
598 | /* a heap element */ |
514 | typedef struct { |
599 | typedef struct { |
515 | ev_tstamp at; |
600 | ev_tstamp at; |
516 | WT w; |
601 | WT w; |
517 | } ANHE; |
602 | } ANHE; |
518 | |
603 | |
519 | #define ANHE_w(he) (he).w /* access watcher, read-write */ |
604 | #define ANHE_w(he) (he).w /* access watcher, read-write */ |
520 | #define ANHE_at(he) (he).at /* access cached at, read-only */ |
605 | #define ANHE_at(he) (he).at /* access cached at, read-only */ |
521 | #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */ |
606 | #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */ |
522 | #else |
607 | #else |
|
|
608 | /* a heap element */ |
523 | typedef WT ANHE; |
609 | typedef WT ANHE; |
524 | |
610 | |
525 | #define ANHE_w(he) (he) |
611 | #define ANHE_w(he) (he) |
526 | #define ANHE_at(he) (he)->at |
612 | #define ANHE_at(he) (he)->at |
527 | #define ANHE_at_cache(he) |
613 | #define ANHE_at_cache(he) |
… | |
… | |
551 | |
637 | |
552 | static int ev_default_loop_ptr; |
638 | static int ev_default_loop_ptr; |
553 | |
639 | |
554 | #endif |
640 | #endif |
555 | |
641 | |
|
|
642 | #if EV_MINIMAL < 2 |
|
|
643 | # define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A) |
|
|
644 | # define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A) |
|
|
645 | # define EV_INVOKE_PENDING invoke_cb (EV_A) |
|
|
646 | #else |
|
|
647 | # define EV_RELEASE_CB (void)0 |
|
|
648 | # define EV_ACQUIRE_CB (void)0 |
|
|
649 | # define EV_INVOKE_PENDING ev_invoke_pending (EV_A) |
|
|
650 | #endif |
|
|
651 | |
|
|
652 | #define EVUNLOOP_RECURSE 0x80 |
|
|
653 | |
556 | /*****************************************************************************/ |
654 | /*****************************************************************************/ |
557 | |
655 | |
|
|
656 | #ifndef EV_HAVE_EV_TIME |
558 | ev_tstamp |
657 | ev_tstamp |
559 | ev_time (void) |
658 | ev_time (void) |
560 | { |
659 | { |
561 | #if EV_USE_REALTIME |
660 | #if EV_USE_REALTIME |
562 | if (expect_true (have_realtime)) |
661 | if (expect_true (have_realtime)) |
… | |
… | |
569 | |
668 | |
570 | struct timeval tv; |
669 | struct timeval tv; |
571 | gettimeofday (&tv, 0); |
670 | gettimeofday (&tv, 0); |
572 | return tv.tv_sec + tv.tv_usec * 1e-6; |
671 | return tv.tv_sec + tv.tv_usec * 1e-6; |
573 | } |
672 | } |
|
|
673 | #endif |
574 | |
674 | |
575 | inline_size ev_tstamp |
675 | inline_size ev_tstamp |
576 | get_clock (void) |
676 | get_clock (void) |
577 | { |
677 | { |
578 | #if EV_USE_MONOTONIC |
678 | #if EV_USE_MONOTONIC |
… | |
… | |
614 | |
714 | |
615 | tv.tv_sec = (time_t)delay; |
715 | tv.tv_sec = (time_t)delay; |
616 | tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); |
716 | tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); |
617 | |
717 | |
618 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
718 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
619 | /* somehting nto guaranteed by newer posix versions, but guaranteed */ |
719 | /* something not guaranteed by newer posix versions, but guaranteed */ |
620 | /* by older ones */ |
720 | /* by older ones */ |
621 | select (0, 0, 0, 0, &tv); |
721 | select (0, 0, 0, 0, &tv); |
622 | #endif |
722 | #endif |
623 | } |
723 | } |
624 | } |
724 | } |
625 | |
725 | |
626 | /*****************************************************************************/ |
726 | /*****************************************************************************/ |
627 | |
727 | |
628 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
728 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
629 | |
729 | |
|
|
730 | /* find a suitable new size for the given array, */ |
|
|
731 | /* hopefully by rounding to a ncie-to-malloc size */ |
630 | inline_size int |
732 | inline_size int |
631 | array_nextsize (int elem, int cur, int cnt) |
733 | array_nextsize (int elem, int cur, int cnt) |
632 | { |
734 | { |
633 | int ncur = cur + 1; |
735 | int ncur = cur + 1; |
634 | |
736 | |
… | |
… | |
680 | #define array_free(stem, idx) \ |
782 | #define array_free(stem, idx) \ |
681 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0 |
783 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0 |
682 | |
784 | |
683 | /*****************************************************************************/ |
785 | /*****************************************************************************/ |
684 | |
786 | |
|
|
787 | /* dummy callback for pending events */ |
|
|
788 | static void noinline |
|
|
789 | pendingcb (EV_P_ ev_prepare *w, int revents) |
|
|
790 | { |
|
|
791 | } |
|
|
792 | |
685 | void noinline |
793 | void noinline |
686 | ev_feed_event (EV_P_ void *w, int revents) |
794 | ev_feed_event (EV_P_ void *w, int revents) |
687 | { |
795 | { |
688 | W w_ = (W)w; |
796 | W w_ = (W)w; |
689 | int pri = ABSPRI (w_); |
797 | int pri = ABSPRI (w_); |
… | |
… | |
724 | } |
832 | } |
725 | |
833 | |
726 | /*****************************************************************************/ |
834 | /*****************************************************************************/ |
727 | |
835 | |
728 | inline_speed void |
836 | inline_speed void |
729 | fd_event (EV_P_ int fd, int revents) |
837 | fd_event_nc (EV_P_ int fd, int revents) |
730 | { |
838 | { |
731 | ANFD *anfd = anfds + fd; |
839 | ANFD *anfd = anfds + fd; |
732 | ev_io *w; |
840 | ev_io *w; |
733 | |
841 | |
734 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
842 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
… | |
… | |
738 | if (ev) |
846 | if (ev) |
739 | ev_feed_event (EV_A_ (W)w, ev); |
847 | ev_feed_event (EV_A_ (W)w, ev); |
740 | } |
848 | } |
741 | } |
849 | } |
742 | |
850 | |
|
|
851 | /* do not submit kernel events for fds that have reify set */ |
|
|
852 | /* because that means they changed while we were polling for new events */ |
|
|
853 | inline_speed void |
|
|
854 | fd_event (EV_P_ int fd, int revents) |
|
|
855 | { |
|
|
856 | ANFD *anfd = anfds + fd; |
|
|
857 | |
|
|
858 | if (expect_true (!anfd->reify)) |
|
|
859 | fd_event_nc (EV_A_ fd, revents); |
|
|
860 | } |
|
|
861 | |
743 | void |
862 | void |
744 | ev_feed_fd_event (EV_P_ int fd, int revents) |
863 | ev_feed_fd_event (EV_P_ int fd, int revents) |
745 | { |
864 | { |
746 | if (fd >= 0 && fd < anfdmax) |
865 | if (fd >= 0 && fd < anfdmax) |
747 | fd_event (EV_A_ fd, revents); |
866 | fd_event_nc (EV_A_ fd, revents); |
748 | } |
867 | } |
749 | |
868 | |
|
|
869 | /* make sure the external fd watch events are in-sync */ |
|
|
870 | /* with the kernel/libev internal state */ |
750 | inline_size void |
871 | inline_size void |
751 | fd_reify (EV_P) |
872 | fd_reify (EV_P) |
752 | { |
873 | { |
753 | int i; |
874 | int i; |
754 | |
875 | |
… | |
… | |
765 | |
886 | |
766 | #if EV_SELECT_IS_WINSOCKET |
887 | #if EV_SELECT_IS_WINSOCKET |
767 | if (events) |
888 | if (events) |
768 | { |
889 | { |
769 | unsigned long arg; |
890 | unsigned long arg; |
770 | #ifdef EV_FD_TO_WIN32_HANDLE |
|
|
771 | anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); |
891 | anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); |
772 | #else |
|
|
773 | anfd->handle = _get_osfhandle (fd); |
|
|
774 | #endif |
|
|
775 | assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); |
892 | assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); |
776 | } |
893 | } |
777 | #endif |
894 | #endif |
778 | |
895 | |
779 | { |
896 | { |
… | |
… | |
789 | } |
906 | } |
790 | |
907 | |
791 | fdchangecnt = 0; |
908 | fdchangecnt = 0; |
792 | } |
909 | } |
793 | |
910 | |
|
|
911 | /* something about the given fd changed */ |
794 | inline_size void |
912 | inline_size void |
795 | fd_change (EV_P_ int fd, int flags) |
913 | fd_change (EV_P_ int fd, int flags) |
796 | { |
914 | { |
797 | unsigned char reify = anfds [fd].reify; |
915 | unsigned char reify = anfds [fd].reify; |
798 | anfds [fd].reify |= flags; |
916 | anfds [fd].reify |= flags; |
… | |
… | |
803 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
921 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
804 | fdchanges [fdchangecnt - 1] = fd; |
922 | fdchanges [fdchangecnt - 1] = fd; |
805 | } |
923 | } |
806 | } |
924 | } |
807 | |
925 | |
|
|
926 | /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ |
808 | inline_speed void |
927 | inline_speed void |
809 | fd_kill (EV_P_ int fd) |
928 | fd_kill (EV_P_ int fd) |
810 | { |
929 | { |
811 | ev_io *w; |
930 | ev_io *w; |
812 | |
931 | |
… | |
… | |
815 | ev_io_stop (EV_A_ w); |
934 | ev_io_stop (EV_A_ w); |
816 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
935 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
817 | } |
936 | } |
818 | } |
937 | } |
819 | |
938 | |
|
|
939 | /* check whether the given fd is atcually valid, for error recovery */ |
820 | inline_size int |
940 | inline_size int |
821 | fd_valid (int fd) |
941 | fd_valid (int fd) |
822 | { |
942 | { |
823 | #ifdef _WIN32 |
943 | #ifdef _WIN32 |
824 | return _get_osfhandle (fd) != -1; |
944 | return _get_osfhandle (fd) != -1; |
… | |
… | |
847 | |
967 | |
848 | for (fd = anfdmax; fd--; ) |
968 | for (fd = anfdmax; fd--; ) |
849 | if (anfds [fd].events) |
969 | if (anfds [fd].events) |
850 | { |
970 | { |
851 | fd_kill (EV_A_ fd); |
971 | fd_kill (EV_A_ fd); |
852 | return; |
972 | break; |
853 | } |
973 | } |
854 | } |
974 | } |
855 | |
975 | |
856 | /* usually called after fork if backend needs to re-arm all fds from scratch */ |
976 | /* usually called after fork if backend needs to re-arm all fds from scratch */ |
857 | static void noinline |
977 | static void noinline |
… | |
… | |
862 | for (fd = 0; fd < anfdmax; ++fd) |
982 | for (fd = 0; fd < anfdmax; ++fd) |
863 | if (anfds [fd].events) |
983 | if (anfds [fd].events) |
864 | { |
984 | { |
865 | anfds [fd].events = 0; |
985 | anfds [fd].events = 0; |
866 | anfds [fd].emask = 0; |
986 | anfds [fd].emask = 0; |
867 | fd_change (EV_A_ fd, EV__IOFDSET | 1); |
987 | fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY); |
868 | } |
988 | } |
869 | } |
989 | } |
870 | |
990 | |
871 | /*****************************************************************************/ |
991 | /*****************************************************************************/ |
872 | |
992 | |
… | |
… | |
947 | |
1067 | |
948 | for (;;) |
1068 | for (;;) |
949 | { |
1069 | { |
950 | int c = k << 1; |
1070 | int c = k << 1; |
951 | |
1071 | |
952 | if (c > N + HEAP0 - 1) |
1072 | if (c >= N + HEAP0) |
953 | break; |
1073 | break; |
954 | |
1074 | |
955 | c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1]) |
1075 | c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1]) |
956 | ? 1 : 0; |
1076 | ? 1 : 0; |
957 | |
1077 | |
… | |
… | |
989 | |
1109 | |
990 | heap [k] = he; |
1110 | heap [k] = he; |
991 | ev_active (ANHE_w (he)) = k; |
1111 | ev_active (ANHE_w (he)) = k; |
992 | } |
1112 | } |
993 | |
1113 | |
|
|
1114 | /* move an element suitably so it is in a correct place */ |
994 | inline_size void |
1115 | inline_size void |
995 | adjustheap (ANHE *heap, int N, int k) |
1116 | adjustheap (ANHE *heap, int N, int k) |
996 | { |
1117 | { |
997 | if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) |
1118 | if (k > HEAP0 && ANHE_at (heap [k]) <= ANHE_at (heap [HPARENT (k)])) |
998 | upheap (heap, k); |
1119 | upheap (heap, k); |
999 | else |
1120 | else |
1000 | downheap (heap, N, k); |
1121 | downheap (heap, N, k); |
1001 | } |
1122 | } |
1002 | |
1123 | |
… | |
… | |
1012 | upheap (heap, i + HEAP0); |
1133 | upheap (heap, i + HEAP0); |
1013 | } |
1134 | } |
1014 | |
1135 | |
1015 | /*****************************************************************************/ |
1136 | /*****************************************************************************/ |
1016 | |
1137 | |
|
|
1138 | /* associate signal watchers to a signal signal */ |
1017 | typedef struct |
1139 | typedef struct |
1018 | { |
1140 | { |
|
|
1141 | EV_ATOMIC_T pending; |
|
|
1142 | #if EV_MULTIPLICITY |
|
|
1143 | EV_P; |
|
|
1144 | #endif |
1019 | WL head; |
1145 | WL head; |
1020 | EV_ATOMIC_T gotsig; |
|
|
1021 | } ANSIG; |
1146 | } ANSIG; |
1022 | |
1147 | |
1023 | static ANSIG *signals; |
1148 | static ANSIG signals [EV_NSIG - 1]; |
1024 | static int signalmax; |
|
|
1025 | |
|
|
1026 | static EV_ATOMIC_T gotsig; |
|
|
1027 | |
1149 | |
1028 | /*****************************************************************************/ |
1150 | /*****************************************************************************/ |
1029 | |
1151 | |
|
|
1152 | /* used to prepare libev internal fd's */ |
|
|
1153 | /* this is not fork-safe */ |
1030 | inline_speed void |
1154 | inline_speed void |
1031 | fd_intern (int fd) |
1155 | fd_intern (int fd) |
1032 | { |
1156 | { |
1033 | #ifdef _WIN32 |
1157 | #ifdef _WIN32 |
1034 | unsigned long arg = 1; |
1158 | unsigned long arg = 1; |
… | |
… | |
1040 | } |
1164 | } |
1041 | |
1165 | |
1042 | static void noinline |
1166 | static void noinline |
1043 | evpipe_init (EV_P) |
1167 | evpipe_init (EV_P) |
1044 | { |
1168 | { |
1045 | if (!ev_is_active (&pipeev)) |
1169 | if (!ev_is_active (&pipe_w)) |
1046 | { |
1170 | { |
1047 | #if EV_USE_EVENTFD |
1171 | #if EV_USE_EVENTFD |
|
|
1172 | evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); |
|
|
1173 | if (evfd < 0 && errno == EINVAL) |
1048 | if ((evfd = eventfd (0, 0)) >= 0) |
1174 | evfd = eventfd (0, 0); |
|
|
1175 | |
|
|
1176 | if (evfd >= 0) |
1049 | { |
1177 | { |
1050 | evpipe [0] = -1; |
1178 | evpipe [0] = -1; |
1051 | fd_intern (evfd); |
1179 | fd_intern (evfd); /* doing it twice doesn't hurt */ |
1052 | ev_io_set (&pipeev, evfd, EV_READ); |
1180 | ev_io_set (&pipe_w, evfd, EV_READ); |
1053 | } |
1181 | } |
1054 | else |
1182 | else |
1055 | #endif |
1183 | #endif |
1056 | { |
1184 | { |
1057 | while (pipe (evpipe)) |
1185 | while (pipe (evpipe)) |
1058 | ev_syserr ("(libev) error creating signal/async pipe"); |
1186 | ev_syserr ("(libev) error creating signal/async pipe"); |
1059 | |
1187 | |
1060 | fd_intern (evpipe [0]); |
1188 | fd_intern (evpipe [0]); |
1061 | fd_intern (evpipe [1]); |
1189 | fd_intern (evpipe [1]); |
1062 | ev_io_set (&pipeev, evpipe [0], EV_READ); |
1190 | ev_io_set (&pipe_w, evpipe [0], EV_READ); |
1063 | } |
1191 | } |
1064 | |
1192 | |
1065 | ev_io_start (EV_A_ &pipeev); |
1193 | ev_io_start (EV_A_ &pipe_w); |
1066 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1194 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1067 | } |
1195 | } |
1068 | } |
1196 | } |
1069 | |
1197 | |
1070 | inline_size void |
1198 | inline_size void |
… | |
… | |
1088 | |
1216 | |
1089 | errno = old_errno; |
1217 | errno = old_errno; |
1090 | } |
1218 | } |
1091 | } |
1219 | } |
1092 | |
1220 | |
|
|
1221 | /* called whenever the libev signal pipe */ |
|
|
1222 | /* got some events (signal, async) */ |
1093 | static void |
1223 | static void |
1094 | pipecb (EV_P_ ev_io *iow, int revents) |
1224 | pipecb (EV_P_ ev_io *iow, int revents) |
1095 | { |
1225 | { |
|
|
1226 | int i; |
|
|
1227 | |
1096 | #if EV_USE_EVENTFD |
1228 | #if EV_USE_EVENTFD |
1097 | if (evfd >= 0) |
1229 | if (evfd >= 0) |
1098 | { |
1230 | { |
1099 | uint64_t counter; |
1231 | uint64_t counter; |
1100 | read (evfd, &counter, sizeof (uint64_t)); |
1232 | read (evfd, &counter, sizeof (uint64_t)); |
… | |
… | |
1104 | { |
1236 | { |
1105 | char dummy; |
1237 | char dummy; |
1106 | read (evpipe [0], &dummy, 1); |
1238 | read (evpipe [0], &dummy, 1); |
1107 | } |
1239 | } |
1108 | |
1240 | |
1109 | if (gotsig && ev_is_default_loop (EV_A)) |
1241 | if (sig_pending) |
1110 | { |
1242 | { |
1111 | int signum; |
1243 | sig_pending = 0; |
1112 | gotsig = 0; |
|
|
1113 | |
1244 | |
1114 | for (signum = signalmax; signum--; ) |
1245 | for (i = EV_NSIG - 1; i--; ) |
1115 | if (signals [signum].gotsig) |
1246 | if (expect_false (signals [i].pending)) |
1116 | ev_feed_signal_event (EV_A_ signum + 1); |
1247 | ev_feed_signal_event (EV_A_ i + 1); |
1117 | } |
1248 | } |
1118 | |
1249 | |
1119 | #if EV_ASYNC_ENABLE |
1250 | #if EV_ASYNC_ENABLE |
1120 | if (gotasync) |
1251 | if (async_pending) |
1121 | { |
1252 | { |
1122 | int i; |
1253 | async_pending = 0; |
1123 | gotasync = 0; |
|
|
1124 | |
1254 | |
1125 | for (i = asynccnt; i--; ) |
1255 | for (i = asynccnt; i--; ) |
1126 | if (asyncs [i]->sent) |
1256 | if (asyncs [i]->sent) |
1127 | { |
1257 | { |
1128 | asyncs [i]->sent = 0; |
1258 | asyncs [i]->sent = 0; |
… | |
… | |
1136 | |
1266 | |
1137 | static void |
1267 | static void |
1138 | ev_sighandler (int signum) |
1268 | ev_sighandler (int signum) |
1139 | { |
1269 | { |
1140 | #if EV_MULTIPLICITY |
1270 | #if EV_MULTIPLICITY |
1141 | struct ev_loop *loop = &default_loop_struct; |
1271 | EV_P = signals [signum - 1].loop; |
1142 | #endif |
1272 | #endif |
1143 | |
1273 | |
1144 | #if _WIN32 |
1274 | #if _WIN32 |
1145 | signal (signum, ev_sighandler); |
1275 | signal (signum, ev_sighandler); |
1146 | #endif |
1276 | #endif |
1147 | |
1277 | |
1148 | signals [signum - 1].gotsig = 1; |
1278 | signals [signum - 1].pending = 1; |
1149 | evpipe_write (EV_A_ &gotsig); |
1279 | evpipe_write (EV_A_ &sig_pending); |
1150 | } |
1280 | } |
1151 | |
1281 | |
1152 | void noinline |
1282 | void noinline |
1153 | ev_feed_signal_event (EV_P_ int signum) |
1283 | ev_feed_signal_event (EV_P_ int signum) |
1154 | { |
1284 | { |
1155 | WL w; |
1285 | WL w; |
1156 | |
1286 | |
|
|
1287 | if (expect_false (signum <= 0 || signum > EV_NSIG)) |
|
|
1288 | return; |
|
|
1289 | |
|
|
1290 | --signum; |
|
|
1291 | |
1157 | #if EV_MULTIPLICITY |
1292 | #if EV_MULTIPLICITY |
1158 | assert (("libev: feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr)); |
1293 | /* it is permissible to try to feed a signal to the wrong loop */ |
1159 | #endif |
1294 | /* or, likely more useful, feeding a signal nobody is waiting for */ |
1160 | |
1295 | |
1161 | --signum; |
1296 | if (expect_false (signals [signum].loop != EV_A)) |
1162 | |
|
|
1163 | if (signum < 0 || signum >= signalmax) |
|
|
1164 | return; |
1297 | return; |
|
|
1298 | #endif |
1165 | |
1299 | |
1166 | signals [signum].gotsig = 0; |
1300 | signals [signum].pending = 0; |
1167 | |
1301 | |
1168 | for (w = signals [signum].head; w; w = w->next) |
1302 | for (w = signals [signum].head; w; w = w->next) |
1169 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
1303 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
1170 | } |
1304 | } |
1171 | |
1305 | |
|
|
1306 | #if EV_USE_SIGNALFD |
|
|
1307 | static void |
|
|
1308 | sigfdcb (EV_P_ ev_io *iow, int revents) |
|
|
1309 | { |
|
|
1310 | struct signalfd_siginfo si[2], *sip; /* these structs are big */ |
|
|
1311 | |
|
|
1312 | for (;;) |
|
|
1313 | { |
|
|
1314 | ssize_t res = read (sigfd, si, sizeof (si)); |
|
|
1315 | |
|
|
1316 | /* not ISO-C, as res might be -1, but works with SuS */ |
|
|
1317 | for (sip = si; (char *)sip < (char *)si + res; ++sip) |
|
|
1318 | ev_feed_signal_event (EV_A_ sip->ssi_signo); |
|
|
1319 | |
|
|
1320 | if (res < (ssize_t)sizeof (si)) |
|
|
1321 | break; |
|
|
1322 | } |
|
|
1323 | } |
|
|
1324 | #endif |
|
|
1325 | |
1172 | /*****************************************************************************/ |
1326 | /*****************************************************************************/ |
1173 | |
1327 | |
1174 | static WL childs [EV_PID_HASHSIZE]; |
1328 | static WL childs [EV_PID_HASHSIZE]; |
1175 | |
1329 | |
1176 | #ifndef _WIN32 |
1330 | #ifndef _WIN32 |
… | |
… | |
1179 | |
1333 | |
1180 | #ifndef WIFCONTINUED |
1334 | #ifndef WIFCONTINUED |
1181 | # define WIFCONTINUED(status) 0 |
1335 | # define WIFCONTINUED(status) 0 |
1182 | #endif |
1336 | #endif |
1183 | |
1337 | |
|
|
1338 | /* handle a single child status event */ |
1184 | inline_speed void |
1339 | inline_speed void |
1185 | child_reap (EV_P_ int chain, int pid, int status) |
1340 | child_reap (EV_P_ int chain, int pid, int status) |
1186 | { |
1341 | { |
1187 | ev_child *w; |
1342 | ev_child *w; |
1188 | int traced = WIFSTOPPED (status) || WIFCONTINUED (status); |
1343 | int traced = WIFSTOPPED (status) || WIFCONTINUED (status); |
… | |
… | |
1202 | |
1357 | |
1203 | #ifndef WCONTINUED |
1358 | #ifndef WCONTINUED |
1204 | # define WCONTINUED 0 |
1359 | # define WCONTINUED 0 |
1205 | #endif |
1360 | #endif |
1206 | |
1361 | |
|
|
1362 | /* called on sigchld etc., calls waitpid */ |
1207 | static void |
1363 | static void |
1208 | childcb (EV_P_ ev_signal *sw, int revents) |
1364 | childcb (EV_P_ ev_signal *sw, int revents) |
1209 | { |
1365 | { |
1210 | int pid, status; |
1366 | int pid, status; |
1211 | |
1367 | |
… | |
… | |
1318 | ev_backend (EV_P) |
1474 | ev_backend (EV_P) |
1319 | { |
1475 | { |
1320 | return backend; |
1476 | return backend; |
1321 | } |
1477 | } |
1322 | |
1478 | |
|
|
1479 | #if EV_MINIMAL < 2 |
1323 | unsigned int |
1480 | unsigned int |
1324 | ev_loop_count (EV_P) |
1481 | ev_loop_count (EV_P) |
1325 | { |
1482 | { |
1326 | return loop_count; |
1483 | return loop_count; |
1327 | } |
1484 | } |
1328 | |
1485 | |
|
|
1486 | unsigned int |
|
|
1487 | ev_loop_depth (EV_P) |
|
|
1488 | { |
|
|
1489 | return loop_depth; |
|
|
1490 | } |
|
|
1491 | |
1329 | void |
1492 | void |
1330 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) |
1493 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) |
1331 | { |
1494 | { |
1332 | io_blocktime = interval; |
1495 | io_blocktime = interval; |
1333 | } |
1496 | } |
… | |
… | |
1336 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
1499 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
1337 | { |
1500 | { |
1338 | timeout_blocktime = interval; |
1501 | timeout_blocktime = interval; |
1339 | } |
1502 | } |
1340 | |
1503 | |
|
|
1504 | void |
|
|
1505 | ev_set_userdata (EV_P_ void *data) |
|
|
1506 | { |
|
|
1507 | userdata = data; |
|
|
1508 | } |
|
|
1509 | |
|
|
1510 | void * |
|
|
1511 | ev_userdata (EV_P) |
|
|
1512 | { |
|
|
1513 | return userdata; |
|
|
1514 | } |
|
|
1515 | |
|
|
1516 | void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
|
|
1517 | { |
|
|
1518 | invoke_cb = invoke_pending_cb; |
|
|
1519 | } |
|
|
1520 | |
|
|
1521 | void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
|
|
1522 | { |
|
|
1523 | release_cb = release; |
|
|
1524 | acquire_cb = acquire; |
|
|
1525 | } |
|
|
1526 | #endif |
|
|
1527 | |
|
|
1528 | /* initialise a loop structure, must be zero-initialised */ |
1341 | static void noinline |
1529 | static void noinline |
1342 | loop_init (EV_P_ unsigned int flags) |
1530 | loop_init (EV_P_ unsigned int flags) |
1343 | { |
1531 | { |
1344 | if (!backend) |
1532 | if (!backend) |
1345 | { |
1533 | { |
… | |
… | |
1361 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
1549 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
1362 | have_monotonic = 1; |
1550 | have_monotonic = 1; |
1363 | } |
1551 | } |
1364 | #endif |
1552 | #endif |
1365 | |
1553 | |
|
|
1554 | /* pid check not overridable via env */ |
|
|
1555 | #ifndef _WIN32 |
|
|
1556 | if (flags & EVFLAG_FORKCHECK) |
|
|
1557 | curpid = getpid (); |
|
|
1558 | #endif |
|
|
1559 | |
|
|
1560 | if (!(flags & EVFLAG_NOENV) |
|
|
1561 | && !enable_secure () |
|
|
1562 | && getenv ("LIBEV_FLAGS")) |
|
|
1563 | flags = atoi (getenv ("LIBEV_FLAGS")); |
|
|
1564 | |
1366 | ev_rt_now = ev_time (); |
1565 | ev_rt_now = ev_time (); |
1367 | mn_now = get_clock (); |
1566 | mn_now = get_clock (); |
1368 | now_floor = mn_now; |
1567 | now_floor = mn_now; |
1369 | rtmn_diff = ev_rt_now - mn_now; |
1568 | rtmn_diff = ev_rt_now - mn_now; |
|
|
1569 | #if EV_MINIMAL < 2 |
|
|
1570 | invoke_cb = ev_invoke_pending; |
|
|
1571 | #endif |
1370 | |
1572 | |
1371 | io_blocktime = 0.; |
1573 | io_blocktime = 0.; |
1372 | timeout_blocktime = 0.; |
1574 | timeout_blocktime = 0.; |
1373 | backend = 0; |
1575 | backend = 0; |
1374 | backend_fd = -1; |
1576 | backend_fd = -1; |
1375 | gotasync = 0; |
1577 | sig_pending = 0; |
|
|
1578 | #if EV_ASYNC_ENABLE |
|
|
1579 | async_pending = 0; |
|
|
1580 | #endif |
1376 | #if EV_USE_INOTIFY |
1581 | #if EV_USE_INOTIFY |
1377 | fs_fd = -2; |
1582 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1378 | #endif |
1583 | #endif |
1379 | |
1584 | #if EV_USE_SIGNALFD |
1380 | /* pid check not overridable via env */ |
1585 | sigfd = flags & EVFLAG_NOSIGFD ? -1 : -2; |
1381 | #ifndef _WIN32 |
|
|
1382 | if (flags & EVFLAG_FORKCHECK) |
|
|
1383 | curpid = getpid (); |
|
|
1384 | #endif |
1586 | #endif |
1385 | |
|
|
1386 | if (!(flags & EVFLAG_NOENV) |
|
|
1387 | && !enable_secure () |
|
|
1388 | && getenv ("LIBEV_FLAGS")) |
|
|
1389 | flags = atoi (getenv ("LIBEV_FLAGS")); |
|
|
1390 | |
1587 | |
1391 | if (!(flags & 0x0000ffffU)) |
1588 | if (!(flags & 0x0000ffffU)) |
1392 | flags |= ev_recommended_backends (); |
1589 | flags |= ev_recommended_backends (); |
1393 | |
1590 | |
1394 | #if EV_USE_PORT |
1591 | #if EV_USE_PORT |
… | |
… | |
1405 | #endif |
1602 | #endif |
1406 | #if EV_USE_SELECT |
1603 | #if EV_USE_SELECT |
1407 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
1604 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
1408 | #endif |
1605 | #endif |
1409 | |
1606 | |
|
|
1607 | ev_prepare_init (&pending_w, pendingcb); |
|
|
1608 | |
1410 | ev_init (&pipeev, pipecb); |
1609 | ev_init (&pipe_w, pipecb); |
1411 | ev_set_priority (&pipeev, EV_MAXPRI); |
1610 | ev_set_priority (&pipe_w, EV_MAXPRI); |
1412 | } |
1611 | } |
1413 | } |
1612 | } |
1414 | |
1613 | |
|
|
1614 | /* free up a loop structure */ |
1415 | static void noinline |
1615 | static void noinline |
1416 | loop_destroy (EV_P) |
1616 | loop_destroy (EV_P) |
1417 | { |
1617 | { |
1418 | int i; |
1618 | int i; |
1419 | |
1619 | |
1420 | if (ev_is_active (&pipeev)) |
1620 | if (ev_is_active (&pipe_w)) |
1421 | { |
1621 | { |
1422 | ev_ref (EV_A); /* signal watcher */ |
1622 | /*ev_ref (EV_A);*/ |
1423 | ev_io_stop (EV_A_ &pipeev); |
1623 | /*ev_io_stop (EV_A_ &pipe_w);*/ |
1424 | |
1624 | |
1425 | #if EV_USE_EVENTFD |
1625 | #if EV_USE_EVENTFD |
1426 | if (evfd >= 0) |
1626 | if (evfd >= 0) |
1427 | close (evfd); |
1627 | close (evfd); |
1428 | #endif |
1628 | #endif |
1429 | |
1629 | |
1430 | if (evpipe [0] >= 0) |
1630 | if (evpipe [0] >= 0) |
1431 | { |
1631 | { |
1432 | close (evpipe [0]); |
1632 | EV_WIN32_CLOSE_FD (evpipe [0]); |
1433 | close (evpipe [1]); |
1633 | EV_WIN32_CLOSE_FD (evpipe [1]); |
1434 | } |
1634 | } |
1435 | } |
1635 | } |
|
|
1636 | |
|
|
1637 | #if EV_USE_SIGNALFD |
|
|
1638 | if (ev_is_active (&sigfd_w)) |
|
|
1639 | { |
|
|
1640 | /*ev_ref (EV_A);*/ |
|
|
1641 | /*ev_io_stop (EV_A_ &sigfd_w);*/ |
|
|
1642 | |
|
|
1643 | close (sigfd); |
|
|
1644 | } |
|
|
1645 | #endif |
1436 | |
1646 | |
1437 | #if EV_USE_INOTIFY |
1647 | #if EV_USE_INOTIFY |
1438 | if (fs_fd >= 0) |
1648 | if (fs_fd >= 0) |
1439 | close (fs_fd); |
1649 | close (fs_fd); |
1440 | #endif |
1650 | #endif |
… | |
… | |
1464 | #if EV_IDLE_ENABLE |
1674 | #if EV_IDLE_ENABLE |
1465 | array_free (idle, [i]); |
1675 | array_free (idle, [i]); |
1466 | #endif |
1676 | #endif |
1467 | } |
1677 | } |
1468 | |
1678 | |
1469 | ev_free (anfds); anfdmax = 0; |
1679 | ev_free (anfds); anfds = 0; anfdmax = 0; |
1470 | |
1680 | |
1471 | /* have to use the microsoft-never-gets-it-right macro */ |
1681 | /* have to use the microsoft-never-gets-it-right macro */ |
1472 | array_free (rfeed, EMPTY); |
1682 | array_free (rfeed, EMPTY); |
1473 | array_free (fdchange, EMPTY); |
1683 | array_free (fdchange, EMPTY); |
1474 | array_free (timer, EMPTY); |
1684 | array_free (timer, EMPTY); |
… | |
… | |
1505 | #endif |
1715 | #endif |
1506 | #if EV_USE_INOTIFY |
1716 | #if EV_USE_INOTIFY |
1507 | infy_fork (EV_A); |
1717 | infy_fork (EV_A); |
1508 | #endif |
1718 | #endif |
1509 | |
1719 | |
1510 | if (ev_is_active (&pipeev)) |
1720 | if (ev_is_active (&pipe_w)) |
1511 | { |
1721 | { |
1512 | /* this "locks" the handlers against writing to the pipe */ |
1722 | /* this "locks" the handlers against writing to the pipe */ |
1513 | /* while we modify the fd vars */ |
1723 | /* while we modify the fd vars */ |
1514 | gotsig = 1; |
1724 | sig_pending = 1; |
1515 | #if EV_ASYNC_ENABLE |
1725 | #if EV_ASYNC_ENABLE |
1516 | gotasync = 1; |
1726 | async_pending = 1; |
1517 | #endif |
1727 | #endif |
1518 | |
1728 | |
1519 | ev_ref (EV_A); |
1729 | ev_ref (EV_A); |
1520 | ev_io_stop (EV_A_ &pipeev); |
1730 | ev_io_stop (EV_A_ &pipe_w); |
1521 | |
1731 | |
1522 | #if EV_USE_EVENTFD |
1732 | #if EV_USE_EVENTFD |
1523 | if (evfd >= 0) |
1733 | if (evfd >= 0) |
1524 | close (evfd); |
1734 | close (evfd); |
1525 | #endif |
1735 | #endif |
1526 | |
1736 | |
1527 | if (evpipe [0] >= 0) |
1737 | if (evpipe [0] >= 0) |
1528 | { |
1738 | { |
1529 | close (evpipe [0]); |
1739 | EV_WIN32_CLOSE_FD (evpipe [0]); |
1530 | close (evpipe [1]); |
1740 | EV_WIN32_CLOSE_FD (evpipe [1]); |
1531 | } |
1741 | } |
1532 | |
1742 | |
1533 | evpipe_init (EV_A); |
1743 | evpipe_init (EV_A); |
1534 | /* now iterate over everything, in case we missed something */ |
1744 | /* now iterate over everything, in case we missed something */ |
1535 | pipecb (EV_A_ &pipeev, EV_READ); |
1745 | pipecb (EV_A_ &pipe_w, EV_READ); |
1536 | } |
1746 | } |
1537 | |
1747 | |
1538 | postfork = 0; |
1748 | postfork = 0; |
1539 | } |
1749 | } |
1540 | |
1750 | |
1541 | #if EV_MULTIPLICITY |
1751 | #if EV_MULTIPLICITY |
1542 | |
1752 | |
1543 | struct ev_loop * |
1753 | struct ev_loop * |
1544 | ev_loop_new (unsigned int flags) |
1754 | ev_loop_new (unsigned int flags) |
1545 | { |
1755 | { |
1546 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
1756 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
1547 | |
1757 | |
1548 | memset (loop, 0, sizeof (struct ev_loop)); |
1758 | memset (EV_A, 0, sizeof (struct ev_loop)); |
1549 | |
|
|
1550 | loop_init (EV_A_ flags); |
1759 | loop_init (EV_A_ flags); |
1551 | |
1760 | |
1552 | if (ev_backend (EV_A)) |
1761 | if (ev_backend (EV_A)) |
1553 | return loop; |
1762 | return EV_A; |
1554 | |
1763 | |
1555 | return 0; |
1764 | return 0; |
1556 | } |
1765 | } |
1557 | |
1766 | |
1558 | void |
1767 | void |
… | |
… | |
1565 | void |
1774 | void |
1566 | ev_loop_fork (EV_P) |
1775 | ev_loop_fork (EV_P) |
1567 | { |
1776 | { |
1568 | postfork = 1; /* must be in line with ev_default_fork */ |
1777 | postfork = 1; /* must be in line with ev_default_fork */ |
1569 | } |
1778 | } |
|
|
1779 | #endif /* multiplicity */ |
1570 | |
1780 | |
1571 | #if EV_VERIFY |
1781 | #if EV_VERIFY |
1572 | static void noinline |
1782 | static void noinline |
1573 | verify_watcher (EV_P_ W w) |
1783 | verify_watcher (EV_P_ W w) |
1574 | { |
1784 | { |
… | |
… | |
1602 | verify_watcher (EV_A_ ws [cnt]); |
1812 | verify_watcher (EV_A_ ws [cnt]); |
1603 | } |
1813 | } |
1604 | } |
1814 | } |
1605 | #endif |
1815 | #endif |
1606 | |
1816 | |
|
|
1817 | #if EV_MINIMAL < 2 |
1607 | void |
1818 | void |
1608 | ev_loop_verify (EV_P) |
1819 | ev_loop_verify (EV_P) |
1609 | { |
1820 | { |
1610 | #if EV_VERIFY |
1821 | #if EV_VERIFY |
1611 | int i; |
1822 | int i; |
… | |
… | |
1660 | assert (checkmax >= checkcnt); |
1871 | assert (checkmax >= checkcnt); |
1661 | array_verify (EV_A_ (W *)checks, checkcnt); |
1872 | array_verify (EV_A_ (W *)checks, checkcnt); |
1662 | |
1873 | |
1663 | # if 0 |
1874 | # if 0 |
1664 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
1875 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
1665 | for (signum = signalmax; signum--; ) if (signals [signum].gotsig) |
1876 | for (signum = EV_NSIG; signum--; ) if (signals [signum].pending) |
1666 | # endif |
1877 | # endif |
1667 | #endif |
1878 | #endif |
1668 | } |
1879 | } |
1669 | |
1880 | #endif |
1670 | #endif /* multiplicity */ |
|
|
1671 | |
1881 | |
1672 | #if EV_MULTIPLICITY |
1882 | #if EV_MULTIPLICITY |
1673 | struct ev_loop * |
1883 | struct ev_loop * |
1674 | ev_default_loop_init (unsigned int flags) |
1884 | ev_default_loop_init (unsigned int flags) |
1675 | #else |
1885 | #else |
… | |
… | |
1678 | #endif |
1888 | #endif |
1679 | { |
1889 | { |
1680 | if (!ev_default_loop_ptr) |
1890 | if (!ev_default_loop_ptr) |
1681 | { |
1891 | { |
1682 | #if EV_MULTIPLICITY |
1892 | #if EV_MULTIPLICITY |
1683 | struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct; |
1893 | EV_P = ev_default_loop_ptr = &default_loop_struct; |
1684 | #else |
1894 | #else |
1685 | ev_default_loop_ptr = 1; |
1895 | ev_default_loop_ptr = 1; |
1686 | #endif |
1896 | #endif |
1687 | |
1897 | |
1688 | loop_init (EV_A_ flags); |
1898 | loop_init (EV_A_ flags); |
… | |
… | |
1705 | |
1915 | |
1706 | void |
1916 | void |
1707 | ev_default_destroy (void) |
1917 | ev_default_destroy (void) |
1708 | { |
1918 | { |
1709 | #if EV_MULTIPLICITY |
1919 | #if EV_MULTIPLICITY |
1710 | struct ev_loop *loop = ev_default_loop_ptr; |
1920 | EV_P = ev_default_loop_ptr; |
1711 | #endif |
1921 | #endif |
1712 | |
1922 | |
1713 | ev_default_loop_ptr = 0; |
1923 | ev_default_loop_ptr = 0; |
1714 | |
1924 | |
1715 | #ifndef _WIN32 |
1925 | #ifndef _WIN32 |
… | |
… | |
1722 | |
1932 | |
1723 | void |
1933 | void |
1724 | ev_default_fork (void) |
1934 | ev_default_fork (void) |
1725 | { |
1935 | { |
1726 | #if EV_MULTIPLICITY |
1936 | #if EV_MULTIPLICITY |
1727 | struct ev_loop *loop = ev_default_loop_ptr; |
1937 | EV_P = ev_default_loop_ptr; |
1728 | #endif |
1938 | #endif |
1729 | |
1939 | |
1730 | postfork = 1; /* must be in line with ev_loop_fork */ |
1940 | postfork = 1; /* must be in line with ev_loop_fork */ |
1731 | } |
1941 | } |
1732 | |
1942 | |
… | |
… | |
1736 | ev_invoke (EV_P_ void *w, int revents) |
1946 | ev_invoke (EV_P_ void *w, int revents) |
1737 | { |
1947 | { |
1738 | EV_CB_INVOKE ((W)w, revents); |
1948 | EV_CB_INVOKE ((W)w, revents); |
1739 | } |
1949 | } |
1740 | |
1950 | |
1741 | inline_speed void |
1951 | unsigned int |
1742 | call_pending (EV_P) |
1952 | ev_pending_count (EV_P) |
|
|
1953 | { |
|
|
1954 | int pri; |
|
|
1955 | unsigned int count = 0; |
|
|
1956 | |
|
|
1957 | for (pri = NUMPRI; pri--; ) |
|
|
1958 | count += pendingcnt [pri]; |
|
|
1959 | |
|
|
1960 | return count; |
|
|
1961 | } |
|
|
1962 | |
|
|
1963 | void noinline |
|
|
1964 | ev_invoke_pending (EV_P) |
1743 | { |
1965 | { |
1744 | int pri; |
1966 | int pri; |
1745 | |
1967 | |
1746 | for (pri = NUMPRI; pri--; ) |
1968 | for (pri = NUMPRI; pri--; ) |
1747 | while (pendingcnt [pri]) |
1969 | while (pendingcnt [pri]) |
1748 | { |
1970 | { |
1749 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1971 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1750 | |
1972 | |
1751 | if (expect_true (p->w)) |
|
|
1752 | { |
|
|
1753 | /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ |
1973 | /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ |
|
|
1974 | /* ^ this is no longer true, as pending_w could be here */ |
1754 | |
1975 | |
1755 | p->w->pending = 0; |
1976 | p->w->pending = 0; |
1756 | EV_CB_INVOKE (p->w, p->events); |
1977 | EV_CB_INVOKE (p->w, p->events); |
1757 | EV_FREQUENT_CHECK; |
1978 | EV_FREQUENT_CHECK; |
1758 | } |
|
|
1759 | } |
1979 | } |
1760 | } |
1980 | } |
1761 | |
1981 | |
1762 | #if EV_IDLE_ENABLE |
1982 | #if EV_IDLE_ENABLE |
|
|
1983 | /* make idle watchers pending. this handles the "call-idle */ |
|
|
1984 | /* only when higher priorities are idle" logic */ |
1763 | inline_size void |
1985 | inline_size void |
1764 | idle_reify (EV_P) |
1986 | idle_reify (EV_P) |
1765 | { |
1987 | { |
1766 | if (expect_false (idleall)) |
1988 | if (expect_false (idleall)) |
1767 | { |
1989 | { |
… | |
… | |
1780 | } |
2002 | } |
1781 | } |
2003 | } |
1782 | } |
2004 | } |
1783 | #endif |
2005 | #endif |
1784 | |
2006 | |
|
|
2007 | /* make timers pending */ |
1785 | inline_size void |
2008 | inline_size void |
1786 | timers_reify (EV_P) |
2009 | timers_reify (EV_P) |
1787 | { |
2010 | { |
1788 | EV_FREQUENT_CHECK; |
2011 | EV_FREQUENT_CHECK; |
1789 | |
2012 | |
… | |
… | |
1818 | feed_reverse_done (EV_A_ EV_TIMEOUT); |
2041 | feed_reverse_done (EV_A_ EV_TIMEOUT); |
1819 | } |
2042 | } |
1820 | } |
2043 | } |
1821 | |
2044 | |
1822 | #if EV_PERIODIC_ENABLE |
2045 | #if EV_PERIODIC_ENABLE |
|
|
2046 | /* make periodics pending */ |
1823 | inline_size void |
2047 | inline_size void |
1824 | periodics_reify (EV_P) |
2048 | periodics_reify (EV_P) |
1825 | { |
2049 | { |
1826 | EV_FREQUENT_CHECK; |
2050 | EV_FREQUENT_CHECK; |
1827 | |
2051 | |
… | |
… | |
1874 | |
2098 | |
1875 | feed_reverse_done (EV_A_ EV_PERIODIC); |
2099 | feed_reverse_done (EV_A_ EV_PERIODIC); |
1876 | } |
2100 | } |
1877 | } |
2101 | } |
1878 | |
2102 | |
|
|
2103 | /* simply recalculate all periodics */ |
|
|
2104 | /* TODO: maybe ensure that at leats one event happens when jumping forward? */ |
1879 | static void noinline |
2105 | static void noinline |
1880 | periodics_reschedule (EV_P) |
2106 | periodics_reschedule (EV_P) |
1881 | { |
2107 | { |
1882 | int i; |
2108 | int i; |
1883 | |
2109 | |
… | |
… | |
1896 | |
2122 | |
1897 | reheap (periodics, periodiccnt); |
2123 | reheap (periodics, periodiccnt); |
1898 | } |
2124 | } |
1899 | #endif |
2125 | #endif |
1900 | |
2126 | |
|
|
2127 | /* adjust all timers by a given offset */ |
1901 | static void noinline |
2128 | static void noinline |
1902 | timers_reschedule (EV_P_ ev_tstamp adjust) |
2129 | timers_reschedule (EV_P_ ev_tstamp adjust) |
1903 | { |
2130 | { |
1904 | int i; |
2131 | int i; |
1905 | |
2132 | |
… | |
… | |
1909 | ANHE_w (*he)->at += adjust; |
2136 | ANHE_w (*he)->at += adjust; |
1910 | ANHE_at_cache (*he); |
2137 | ANHE_at_cache (*he); |
1911 | } |
2138 | } |
1912 | } |
2139 | } |
1913 | |
2140 | |
|
|
2141 | /* fetch new monotonic and realtime times from the kernel */ |
|
|
2142 | /* also detetc if there was a timejump, and act accordingly */ |
1914 | inline_speed void |
2143 | inline_speed void |
1915 | time_update (EV_P_ ev_tstamp max_block) |
2144 | time_update (EV_P_ ev_tstamp max_block) |
1916 | { |
2145 | { |
1917 | int i; |
|
|
1918 | |
|
|
1919 | #if EV_USE_MONOTONIC |
2146 | #if EV_USE_MONOTONIC |
1920 | if (expect_true (have_monotonic)) |
2147 | if (expect_true (have_monotonic)) |
1921 | { |
2148 | { |
|
|
2149 | int i; |
1922 | ev_tstamp odiff = rtmn_diff; |
2150 | ev_tstamp odiff = rtmn_diff; |
1923 | |
2151 | |
1924 | mn_now = get_clock (); |
2152 | mn_now = get_clock (); |
1925 | |
2153 | |
1926 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
2154 | /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ |
… | |
… | |
1976 | |
2204 | |
1977 | mn_now = ev_rt_now; |
2205 | mn_now = ev_rt_now; |
1978 | } |
2206 | } |
1979 | } |
2207 | } |
1980 | |
2208 | |
1981 | static int loop_done; |
|
|
1982 | |
|
|
1983 | void |
2209 | void |
1984 | ev_loop (EV_P_ int flags) |
2210 | ev_loop (EV_P_ int flags) |
1985 | { |
2211 | { |
|
|
2212 | #if EV_MINIMAL < 2 |
|
|
2213 | ++loop_depth; |
|
|
2214 | #endif |
|
|
2215 | |
|
|
2216 | assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE)); |
|
|
2217 | |
1986 | loop_done = EVUNLOOP_CANCEL; |
2218 | loop_done = EVUNLOOP_CANCEL; |
1987 | |
2219 | |
1988 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
2220 | EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */ |
1989 | |
2221 | |
1990 | do |
2222 | do |
1991 | { |
2223 | { |
1992 | #if EV_VERIFY >= 2 |
2224 | #if EV_VERIFY >= 2 |
1993 | ev_loop_verify (EV_A); |
2225 | ev_loop_verify (EV_A); |
… | |
… | |
2006 | /* we might have forked, so queue fork handlers */ |
2238 | /* we might have forked, so queue fork handlers */ |
2007 | if (expect_false (postfork)) |
2239 | if (expect_false (postfork)) |
2008 | if (forkcnt) |
2240 | if (forkcnt) |
2009 | { |
2241 | { |
2010 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
2242 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
2011 | call_pending (EV_A); |
2243 | EV_INVOKE_PENDING; |
2012 | } |
2244 | } |
2013 | #endif |
2245 | #endif |
2014 | |
2246 | |
2015 | /* queue prepare watchers (and execute them) */ |
2247 | /* queue prepare watchers (and execute them) */ |
2016 | if (expect_false (preparecnt)) |
2248 | if (expect_false (preparecnt)) |
2017 | { |
2249 | { |
2018 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
2250 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
2019 | call_pending (EV_A); |
2251 | EV_INVOKE_PENDING; |
2020 | } |
2252 | } |
|
|
2253 | |
|
|
2254 | if (expect_false (loop_done)) |
|
|
2255 | break; |
2021 | |
2256 | |
2022 | /* we might have forked, so reify kernel state if necessary */ |
2257 | /* we might have forked, so reify kernel state if necessary */ |
2023 | if (expect_false (postfork)) |
2258 | if (expect_false (postfork)) |
2024 | loop_fork (EV_A); |
2259 | loop_fork (EV_A); |
2025 | |
2260 | |
… | |
… | |
2031 | ev_tstamp waittime = 0.; |
2266 | ev_tstamp waittime = 0.; |
2032 | ev_tstamp sleeptime = 0.; |
2267 | ev_tstamp sleeptime = 0.; |
2033 | |
2268 | |
2034 | if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) |
2269 | if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) |
2035 | { |
2270 | { |
|
|
2271 | /* remember old timestamp for io_blocktime calculation */ |
|
|
2272 | ev_tstamp prev_mn_now = mn_now; |
|
|
2273 | |
2036 | /* update time to cancel out callback processing overhead */ |
2274 | /* update time to cancel out callback processing overhead */ |
2037 | time_update (EV_A_ 1e100); |
2275 | time_update (EV_A_ 1e100); |
2038 | |
2276 | |
2039 | waittime = MAX_BLOCKTIME; |
2277 | waittime = MAX_BLOCKTIME; |
2040 | |
2278 | |
… | |
… | |
2050 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; |
2288 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; |
2051 | if (waittime > to) waittime = to; |
2289 | if (waittime > to) waittime = to; |
2052 | } |
2290 | } |
2053 | #endif |
2291 | #endif |
2054 | |
2292 | |
|
|
2293 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2055 | if (expect_false (waittime < timeout_blocktime)) |
2294 | if (expect_false (waittime < timeout_blocktime)) |
2056 | waittime = timeout_blocktime; |
2295 | waittime = timeout_blocktime; |
2057 | |
2296 | |
2058 | sleeptime = waittime - backend_fudge; |
2297 | /* extra check because io_blocktime is commonly 0 */ |
2059 | |
|
|
2060 | if (expect_true (sleeptime > io_blocktime)) |
2298 | if (expect_false (io_blocktime)) |
2061 | sleeptime = io_blocktime; |
|
|
2062 | |
|
|
2063 | if (sleeptime) |
|
|
2064 | { |
2299 | { |
|
|
2300 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
|
|
2301 | |
|
|
2302 | if (sleeptime > waittime - backend_fudge) |
|
|
2303 | sleeptime = waittime - backend_fudge; |
|
|
2304 | |
|
|
2305 | if (expect_true (sleeptime > 0.)) |
|
|
2306 | { |
2065 | ev_sleep (sleeptime); |
2307 | ev_sleep (sleeptime); |
2066 | waittime -= sleeptime; |
2308 | waittime -= sleeptime; |
|
|
2309 | } |
2067 | } |
2310 | } |
2068 | } |
2311 | } |
2069 | |
2312 | |
|
|
2313 | #if EV_MINIMAL < 2 |
2070 | ++loop_count; |
2314 | ++loop_count; |
|
|
2315 | #endif |
|
|
2316 | assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */ |
2071 | backend_poll (EV_A_ waittime); |
2317 | backend_poll (EV_A_ waittime); |
|
|
2318 | assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */ |
2072 | |
2319 | |
2073 | /* update ev_rt_now, do magic */ |
2320 | /* update ev_rt_now, do magic */ |
2074 | time_update (EV_A_ waittime + sleeptime); |
2321 | time_update (EV_A_ waittime + sleeptime); |
2075 | } |
2322 | } |
2076 | |
2323 | |
… | |
… | |
2087 | |
2334 | |
2088 | /* queue check watchers, to be executed first */ |
2335 | /* queue check watchers, to be executed first */ |
2089 | if (expect_false (checkcnt)) |
2336 | if (expect_false (checkcnt)) |
2090 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
2337 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
2091 | |
2338 | |
2092 | call_pending (EV_A); |
2339 | EV_INVOKE_PENDING; |
2093 | } |
2340 | } |
2094 | while (expect_true ( |
2341 | while (expect_true ( |
2095 | activecnt |
2342 | activecnt |
2096 | && !loop_done |
2343 | && !loop_done |
2097 | && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) |
2344 | && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) |
2098 | )); |
2345 | )); |
2099 | |
2346 | |
2100 | if (loop_done == EVUNLOOP_ONE) |
2347 | if (loop_done == EVUNLOOP_ONE) |
2101 | loop_done = EVUNLOOP_CANCEL; |
2348 | loop_done = EVUNLOOP_CANCEL; |
|
|
2349 | |
|
|
2350 | #if EV_MINIMAL < 2 |
|
|
2351 | --loop_depth; |
|
|
2352 | #endif |
2102 | } |
2353 | } |
2103 | |
2354 | |
2104 | void |
2355 | void |
2105 | ev_unloop (EV_P_ int how) |
2356 | ev_unloop (EV_P_ int how) |
2106 | { |
2357 | { |
… | |
… | |
2137 | ev_tstamp mn_prev = mn_now; |
2388 | ev_tstamp mn_prev = mn_now; |
2138 | |
2389 | |
2139 | ev_now_update (EV_A); |
2390 | ev_now_update (EV_A); |
2140 | timers_reschedule (EV_A_ mn_now - mn_prev); |
2391 | timers_reschedule (EV_A_ mn_now - mn_prev); |
2141 | #if EV_PERIODIC_ENABLE |
2392 | #if EV_PERIODIC_ENABLE |
|
|
2393 | /* TODO: really do this? */ |
2142 | periodics_reschedule (EV_A); |
2394 | periodics_reschedule (EV_A); |
2143 | #endif |
2395 | #endif |
2144 | } |
2396 | } |
2145 | |
2397 | |
2146 | /*****************************************************************************/ |
2398 | /*****************************************************************************/ |
|
|
2399 | /* singly-linked list management, used when the expected list length is short */ |
2147 | |
2400 | |
2148 | inline_size void |
2401 | inline_size void |
2149 | wlist_add (WL *head, WL elem) |
2402 | wlist_add (WL *head, WL elem) |
2150 | { |
2403 | { |
2151 | elem->next = *head; |
2404 | elem->next = *head; |
… | |
… | |
2155 | inline_size void |
2408 | inline_size void |
2156 | wlist_del (WL *head, WL elem) |
2409 | wlist_del (WL *head, WL elem) |
2157 | { |
2410 | { |
2158 | while (*head) |
2411 | while (*head) |
2159 | { |
2412 | { |
2160 | if (*head == elem) |
2413 | if (expect_true (*head == elem)) |
2161 | { |
2414 | { |
2162 | *head = elem->next; |
2415 | *head = elem->next; |
2163 | return; |
2416 | break; |
2164 | } |
2417 | } |
2165 | |
2418 | |
2166 | head = &(*head)->next; |
2419 | head = &(*head)->next; |
2167 | } |
2420 | } |
2168 | } |
2421 | } |
2169 | |
2422 | |
|
|
2423 | /* internal, faster, version of ev_clear_pending */ |
2170 | inline_speed void |
2424 | inline_speed void |
2171 | clear_pending (EV_P_ W w) |
2425 | clear_pending (EV_P_ W w) |
2172 | { |
2426 | { |
2173 | if (w->pending) |
2427 | if (w->pending) |
2174 | { |
2428 | { |
2175 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
2429 | pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w; |
2176 | w->pending = 0; |
2430 | w->pending = 0; |
2177 | } |
2431 | } |
2178 | } |
2432 | } |
2179 | |
2433 | |
2180 | int |
2434 | int |
… | |
… | |
2184 | int pending = w_->pending; |
2438 | int pending = w_->pending; |
2185 | |
2439 | |
2186 | if (expect_true (pending)) |
2440 | if (expect_true (pending)) |
2187 | { |
2441 | { |
2188 | ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; |
2442 | ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; |
|
|
2443 | p->w = (W)&pending_w; |
2189 | w_->pending = 0; |
2444 | w_->pending = 0; |
2190 | p->w = 0; |
|
|
2191 | return p->events; |
2445 | return p->events; |
2192 | } |
2446 | } |
2193 | else |
2447 | else |
2194 | return 0; |
2448 | return 0; |
2195 | } |
2449 | } |
2196 | |
2450 | |
2197 | inline_size void |
2451 | inline_size void |
2198 | pri_adjust (EV_P_ W w) |
2452 | pri_adjust (EV_P_ W w) |
2199 | { |
2453 | { |
2200 | int pri = w->priority; |
2454 | int pri = ev_priority (w); |
2201 | pri = pri < EV_MINPRI ? EV_MINPRI : pri; |
2455 | pri = pri < EV_MINPRI ? EV_MINPRI : pri; |
2202 | pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; |
2456 | pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; |
2203 | w->priority = pri; |
2457 | ev_set_priority (w, pri); |
2204 | } |
2458 | } |
2205 | |
2459 | |
2206 | inline_speed void |
2460 | inline_speed void |
2207 | ev_start (EV_P_ W w, int active) |
2461 | ev_start (EV_P_ W w, int active) |
2208 | { |
2462 | { |
… | |
… | |
2235 | |
2489 | |
2236 | ev_start (EV_A_ (W)w, 1); |
2490 | ev_start (EV_A_ (W)w, 1); |
2237 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
2491 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
2238 | wlist_add (&anfds[fd].head, (WL)w); |
2492 | wlist_add (&anfds[fd].head, (WL)w); |
2239 | |
2493 | |
2240 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | 1); |
2494 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); |
2241 | w->events &= ~EV__IOFDSET; |
2495 | w->events &= ~EV__IOFDSET; |
2242 | |
2496 | |
2243 | EV_FREQUENT_CHECK; |
2497 | EV_FREQUENT_CHECK; |
2244 | } |
2498 | } |
2245 | |
2499 | |
… | |
… | |
2339 | } |
2593 | } |
2340 | |
2594 | |
2341 | EV_FREQUENT_CHECK; |
2595 | EV_FREQUENT_CHECK; |
2342 | } |
2596 | } |
2343 | |
2597 | |
|
|
2598 | ev_tstamp |
|
|
2599 | ev_timer_remaining (EV_P_ ev_timer *w) |
|
|
2600 | { |
|
|
2601 | return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); |
|
|
2602 | } |
|
|
2603 | |
2344 | #if EV_PERIODIC_ENABLE |
2604 | #if EV_PERIODIC_ENABLE |
2345 | void noinline |
2605 | void noinline |
2346 | ev_periodic_start (EV_P_ ev_periodic *w) |
2606 | ev_periodic_start (EV_P_ ev_periodic *w) |
2347 | { |
2607 | { |
2348 | if (expect_false (ev_is_active (w))) |
2608 | if (expect_false (ev_is_active (w))) |
… | |
… | |
2415 | #endif |
2675 | #endif |
2416 | |
2676 | |
2417 | void noinline |
2677 | void noinline |
2418 | ev_signal_start (EV_P_ ev_signal *w) |
2678 | ev_signal_start (EV_P_ ev_signal *w) |
2419 | { |
2679 | { |
2420 | #if EV_MULTIPLICITY |
|
|
2421 | assert (("libev: signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
|
|
2422 | #endif |
|
|
2423 | if (expect_false (ev_is_active (w))) |
2680 | if (expect_false (ev_is_active (w))) |
2424 | return; |
2681 | return; |
2425 | |
2682 | |
2426 | assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0)); |
2683 | assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); |
2427 | |
2684 | |
2428 | evpipe_init (EV_A); |
2685 | #if EV_MULTIPLICITY |
|
|
2686 | assert (("libev: a signal must not be attached to two different loops", |
|
|
2687 | !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop)); |
2429 | |
2688 | |
2430 | EV_FREQUENT_CHECK; |
2689 | signals [w->signum - 1].loop = EV_A; |
|
|
2690 | #endif |
2431 | |
2691 | |
|
|
2692 | EV_FREQUENT_CHECK; |
|
|
2693 | |
|
|
2694 | #if EV_USE_SIGNALFD |
|
|
2695 | if (sigfd == -2) |
2432 | { |
2696 | { |
2433 | #ifndef _WIN32 |
2697 | sigfd = signalfd (-1, &sigfd_set, SFD_NONBLOCK | SFD_CLOEXEC); |
2434 | sigset_t full, prev; |
2698 | if (sigfd < 0 && errno == EINVAL) |
2435 | sigfillset (&full); |
2699 | sigfd = signalfd (-1, &sigfd_set, 0); /* retry without flags */ |
2436 | sigprocmask (SIG_SETMASK, &full, &prev); |
|
|
2437 | #endif |
|
|
2438 | |
2700 | |
2439 | array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero); |
2701 | if (sigfd >= 0) |
|
|
2702 | { |
|
|
2703 | fd_intern (sigfd); /* doing it twice will not hurt */ |
2440 | |
2704 | |
2441 | #ifndef _WIN32 |
2705 | sigemptyset (&sigfd_set); |
2442 | sigprocmask (SIG_SETMASK, &prev, 0); |
2706 | |
2443 | #endif |
2707 | ev_io_init (&sigfd_w, sigfdcb, sigfd, EV_READ); |
|
|
2708 | ev_set_priority (&sigfd_w, EV_MAXPRI); |
|
|
2709 | ev_io_start (EV_A_ &sigfd_w); |
|
|
2710 | ev_unref (EV_A); /* signalfd watcher should not keep loop alive */ |
|
|
2711 | } |
2444 | } |
2712 | } |
|
|
2713 | |
|
|
2714 | if (sigfd >= 0) |
|
|
2715 | { |
|
|
2716 | /* TODO: check .head */ |
|
|
2717 | sigaddset (&sigfd_set, w->signum); |
|
|
2718 | sigprocmask (SIG_BLOCK, &sigfd_set, 0); |
|
|
2719 | |
|
|
2720 | signalfd (sigfd, &sigfd_set, 0); |
|
|
2721 | } |
|
|
2722 | #endif |
2445 | |
2723 | |
2446 | ev_start (EV_A_ (W)w, 1); |
2724 | ev_start (EV_A_ (W)w, 1); |
2447 | wlist_add (&signals [w->signum - 1].head, (WL)w); |
2725 | wlist_add (&signals [w->signum - 1].head, (WL)w); |
2448 | |
2726 | |
2449 | if (!((WL)w)->next) |
2727 | if (!((WL)w)->next) |
|
|
2728 | # if EV_USE_SIGNALFD |
|
|
2729 | if (sigfd < 0) /*TODO*/ |
|
|
2730 | # endif |
2450 | { |
2731 | { |
2451 | #if _WIN32 |
2732 | # if _WIN32 |
2452 | signal (w->signum, ev_sighandler); |
2733 | signal (w->signum, ev_sighandler); |
2453 | #else |
2734 | # else |
2454 | struct sigaction sa; |
2735 | struct sigaction sa; |
|
|
2736 | |
|
|
2737 | evpipe_init (EV_A); |
|
|
2738 | |
2455 | sa.sa_handler = ev_sighandler; |
2739 | sa.sa_handler = ev_sighandler; |
2456 | sigfillset (&sa.sa_mask); |
2740 | sigfillset (&sa.sa_mask); |
2457 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2741 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2458 | sigaction (w->signum, &sa, 0); |
2742 | sigaction (w->signum, &sa, 0); |
|
|
2743 | |
|
|
2744 | sigemptyset (&sa.sa_mask); |
|
|
2745 | sigaddset (&sa.sa_mask, w->signum); |
|
|
2746 | sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); |
2459 | #endif |
2747 | #endif |
2460 | } |
2748 | } |
2461 | |
2749 | |
2462 | EV_FREQUENT_CHECK; |
2750 | EV_FREQUENT_CHECK; |
2463 | } |
2751 | } |
2464 | |
2752 | |
2465 | void noinline |
2753 | void noinline |
… | |
… | |
2473 | |
2761 | |
2474 | wlist_del (&signals [w->signum - 1].head, (WL)w); |
2762 | wlist_del (&signals [w->signum - 1].head, (WL)w); |
2475 | ev_stop (EV_A_ (W)w); |
2763 | ev_stop (EV_A_ (W)w); |
2476 | |
2764 | |
2477 | if (!signals [w->signum - 1].head) |
2765 | if (!signals [w->signum - 1].head) |
|
|
2766 | { |
|
|
2767 | #if EV_MULTIPLICITY |
|
|
2768 | signals [w->signum - 1].loop = 0; /* unattach from signal */ |
|
|
2769 | #endif |
|
|
2770 | #if EV_USE_SIGNALFD |
|
|
2771 | if (sigfd >= 0) |
|
|
2772 | { |
|
|
2773 | sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D |
|
|
2774 | sigdelset (&sigfd_set, w->signum); |
|
|
2775 | signalfd (sigfd, &sigfd_set, 0); |
|
|
2776 | sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D |
|
|
2777 | /*TODO: maybe unblock signal? */ |
|
|
2778 | } |
|
|
2779 | else |
|
|
2780 | #endif |
2478 | signal (w->signum, SIG_DFL); |
2781 | signal (w->signum, SIG_DFL); |
|
|
2782 | } |
2479 | |
2783 | |
2480 | EV_FREQUENT_CHECK; |
2784 | EV_FREQUENT_CHECK; |
2481 | } |
2785 | } |
2482 | |
2786 | |
2483 | void |
2787 | void |
… | |
… | |
2563 | } |
2867 | } |
2564 | } |
2868 | } |
2565 | |
2869 | |
2566 | if (w->wd >= 0) |
2870 | if (w->wd >= 0) |
2567 | { |
2871 | { |
|
|
2872 | struct statfs sfs; |
|
|
2873 | |
2568 | wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); |
2874 | wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); |
2569 | |
2875 | |
2570 | /* now local changes will be tracked by inotify, but remote changes won't */ |
2876 | /* now local changes will be tracked by inotify, but remote changes won't */ |
2571 | /* unless the filesystem it known to be local, we therefore still poll */ |
2877 | /* unless the filesystem it known to be local, we therefore still poll */ |
2572 | /* also do poll on <2.6.25, but with normal frequency */ |
2878 | /* also do poll on <2.6.25, but with normal frequency */ |
2573 | struct statfs sfs; |
|
|
2574 | |
2879 | |
2575 | if (fs_2625 && !statfs (w->path, &sfs)) |
2880 | if (fs_2625 && !statfs (w->path, &sfs)) |
2576 | if (sfs.f_type == 0x1373 /* devfs */ |
2881 | if (sfs.f_type == 0x1373 /* devfs */ |
2577 | || sfs.f_type == 0xEF53 /* ext2/3 */ |
2882 | || sfs.f_type == 0xEF53 /* ext2/3 */ |
2578 | || sfs.f_type == 0x3153464a /* jfs */ |
2883 | || sfs.f_type == 0x3153464a /* jfs */ |
… | |
… | |
2963 | embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) |
3268 | embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) |
2964 | { |
3269 | { |
2965 | ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare)); |
3270 | ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare)); |
2966 | |
3271 | |
2967 | { |
3272 | { |
2968 | struct ev_loop *loop = w->other; |
3273 | EV_P = w->other; |
2969 | |
3274 | |
2970 | while (fdchangecnt) |
3275 | while (fdchangecnt) |
2971 | { |
3276 | { |
2972 | fd_reify (EV_A); |
3277 | fd_reify (EV_A); |
2973 | ev_loop (EV_A_ EVLOOP_NONBLOCK); |
3278 | ev_loop (EV_A_ EVLOOP_NONBLOCK); |
… | |
… | |
2981 | ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork)); |
3286 | ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork)); |
2982 | |
3287 | |
2983 | ev_embed_stop (EV_A_ w); |
3288 | ev_embed_stop (EV_A_ w); |
2984 | |
3289 | |
2985 | { |
3290 | { |
2986 | struct ev_loop *loop = w->other; |
3291 | EV_P = w->other; |
2987 | |
3292 | |
2988 | ev_loop_fork (EV_A); |
3293 | ev_loop_fork (EV_A); |
2989 | ev_loop (EV_A_ EVLOOP_NONBLOCK); |
3294 | ev_loop (EV_A_ EVLOOP_NONBLOCK); |
2990 | } |
3295 | } |
2991 | |
3296 | |
… | |
… | |
3005 | { |
3310 | { |
3006 | if (expect_false (ev_is_active (w))) |
3311 | if (expect_false (ev_is_active (w))) |
3007 | return; |
3312 | return; |
3008 | |
3313 | |
3009 | { |
3314 | { |
3010 | struct ev_loop *loop = w->other; |
3315 | EV_P = w->other; |
3011 | assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); |
3316 | assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); |
3012 | ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ); |
3317 | ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ); |
3013 | } |
3318 | } |
3014 | |
3319 | |
3015 | EV_FREQUENT_CHECK; |
3320 | EV_FREQUENT_CHECK; |
… | |
… | |
3127 | |
3432 | |
3128 | void |
3433 | void |
3129 | ev_async_send (EV_P_ ev_async *w) |
3434 | ev_async_send (EV_P_ ev_async *w) |
3130 | { |
3435 | { |
3131 | w->sent = 1; |
3436 | w->sent = 1; |
3132 | evpipe_write (EV_A_ &gotasync); |
3437 | evpipe_write (EV_A_ &async_pending); |
3133 | } |
3438 | } |
3134 | #endif |
3439 | #endif |
3135 | |
3440 | |
3136 | /*****************************************************************************/ |
3441 | /*****************************************************************************/ |
3137 | |
3442 | |
… | |
… | |
3201 | } |
3506 | } |
3202 | } |
3507 | } |
3203 | |
3508 | |
3204 | /*****************************************************************************/ |
3509 | /*****************************************************************************/ |
3205 | |
3510 | |
3206 | #if 0 |
3511 | #if EV_WALK_ENABLE |
3207 | void |
3512 | void |
3208 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3513 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3209 | { |
3514 | { |
3210 | int i, j; |
3515 | int i, j; |
3211 | ev_watcher_list *wl, *wn; |
3516 | ev_watcher_list *wl, *wn; |
… | |
… | |
3227 | #if EV_USE_INOTIFY |
3532 | #if EV_USE_INOTIFY |
3228 | if (ev_cb ((ev_io *)wl) == infy_cb) |
3533 | if (ev_cb ((ev_io *)wl) == infy_cb) |
3229 | ; |
3534 | ; |
3230 | else |
3535 | else |
3231 | #endif |
3536 | #endif |
3232 | if ((ev_io *)wl != &pipeev) |
3537 | if ((ev_io *)wl != &pipe_w) |
3233 | if (types & EV_IO) |
3538 | if (types & EV_IO) |
3234 | cb (EV_A_ EV_IO, wl); |
3539 | cb (EV_A_ EV_IO, wl); |
3235 | |
3540 | |
3236 | wl = wn; |
3541 | wl = wn; |
3237 | } |
3542 | } |
… | |
… | |
3286 | if (types & EV_CHECK) |
3591 | if (types & EV_CHECK) |
3287 | for (i = checkcnt; i--; ) |
3592 | for (i = checkcnt; i--; ) |
3288 | cb (EV_A_ EV_CHECK, checks [i]); |
3593 | cb (EV_A_ EV_CHECK, checks [i]); |
3289 | |
3594 | |
3290 | if (types & EV_SIGNAL) |
3595 | if (types & EV_SIGNAL) |
3291 | for (i = 0; i < signalmax; ++i) |
3596 | for (i = 0; i < EV_NSIG - 1; ++i) |
3292 | for (wl = signals [i].head; wl; ) |
3597 | for (wl = signals [i].head; wl; ) |
3293 | { |
3598 | { |
3294 | wn = wl->next; |
3599 | wn = wl->next; |
3295 | cb (EV_A_ EV_SIGNAL, wl); |
3600 | cb (EV_A_ EV_SIGNAL, wl); |
3296 | wl = wn; |
3601 | wl = wn; |