… | |
… | |
111 | #include <time.h> |
111 | #include <time.h> |
112 | |
112 | |
113 | #include <signal.h> |
113 | #include <signal.h> |
114 | |
114 | |
115 | #ifndef _WIN32 |
115 | #ifndef _WIN32 |
116 | # include <unistd.h> |
|
|
117 | # include <sys/time.h> |
116 | # include <sys/time.h> |
118 | # include <sys/wait.h> |
117 | # include <sys/wait.h> |
|
|
118 | # include <unistd.h> |
119 | #else |
119 | #else |
120 | # define WIN32_LEAN_AND_MEAN |
120 | # define WIN32_LEAN_AND_MEAN |
121 | # include <windows.h> |
121 | # include <windows.h> |
122 | # ifndef EV_SELECT_IS_WINSOCKET |
122 | # ifndef EV_SELECT_IS_WINSOCKET |
123 | # define EV_SELECT_IS_WINSOCKET 1 |
123 | # define EV_SELECT_IS_WINSOCKET 1 |
… | |
… | |
187 | # include "ev.h" |
187 | # include "ev.h" |
188 | #endif |
188 | #endif |
189 | |
189 | |
190 | #if __GNUC__ >= 3 |
190 | #if __GNUC__ >= 3 |
191 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
191 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
192 | # define inline_size static inline /* inline for codesize */ |
|
|
193 | # if EV_MINIMAL |
|
|
194 | # define noinline __attribute__ ((noinline)) |
|
|
195 | # define inline_speed static noinline |
|
|
196 | # else |
|
|
197 | # define noinline |
192 | # define inline static inline |
198 | # define inline_speed static inline |
|
|
199 | # endif |
193 | #else |
200 | #else |
194 | # define expect(expr,value) (expr) |
201 | # define expect(expr,value) (expr) |
195 | # define inline static |
202 | # define inline_speed static |
|
|
203 | # define inline_minimal static |
|
|
204 | # define noinline |
196 | #endif |
205 | #endif |
197 | |
206 | |
198 | #define expect_false(expr) expect ((expr) != 0, 0) |
207 | #define expect_false(expr) expect ((expr) != 0, 0) |
199 | #define expect_true(expr) expect ((expr) != 0, 1) |
208 | #define expect_true(expr) expect ((expr) != 0, 1) |
200 | |
209 | |
… | |
… | |
306 | |
315 | |
307 | #endif |
316 | #endif |
308 | |
317 | |
309 | /*****************************************************************************/ |
318 | /*****************************************************************************/ |
310 | |
319 | |
311 | ev_tstamp |
320 | ev_tstamp noinline |
312 | ev_time (void) |
321 | ev_time (void) |
313 | { |
322 | { |
314 | #if EV_USE_REALTIME |
323 | #if EV_USE_REALTIME |
315 | struct timespec ts; |
324 | struct timespec ts; |
316 | clock_gettime (CLOCK_REALTIME, &ts); |
325 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
320 | gettimeofday (&tv, 0); |
329 | gettimeofday (&tv, 0); |
321 | return tv.tv_sec + tv.tv_usec * 1e-6; |
330 | return tv.tv_sec + tv.tv_usec * 1e-6; |
322 | #endif |
331 | #endif |
323 | } |
332 | } |
324 | |
333 | |
325 | inline ev_tstamp |
334 | ev_tstamp inline_size |
326 | get_clock (void) |
335 | get_clock (void) |
327 | { |
336 | { |
328 | #if EV_USE_MONOTONIC |
337 | #if EV_USE_MONOTONIC |
329 | if (expect_true (have_monotonic)) |
338 | if (expect_true (have_monotonic)) |
330 | { |
339 | { |
… | |
… | |
373 | #define array_free(stem, idx) \ |
382 | #define array_free(stem, idx) \ |
374 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
383 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
375 | |
384 | |
376 | /*****************************************************************************/ |
385 | /*****************************************************************************/ |
377 | |
386 | |
378 | static void |
387 | void inline_size |
379 | anfds_init (ANFD *base, int count) |
388 | anfds_init (ANFD *base, int count) |
380 | { |
389 | { |
381 | while (count--) |
390 | while (count--) |
382 | { |
391 | { |
383 | base->head = 0; |
392 | base->head = 0; |
… | |
… | |
386 | |
395 | |
387 | ++base; |
396 | ++base; |
388 | } |
397 | } |
389 | } |
398 | } |
390 | |
399 | |
391 | void |
400 | void noinline |
392 | ev_feed_event (EV_P_ void *w, int revents) |
401 | ev_feed_event (EV_P_ void *w, int revents) |
393 | { |
402 | { |
394 | W w_ = (W)w; |
403 | W w_ = (W)w; |
395 | |
404 | |
396 | if (expect_false (w_->pending)) |
405 | if (expect_false (w_->pending)) |
… | |
… | |
412 | |
421 | |
413 | for (i = 0; i < eventcnt; ++i) |
422 | for (i = 0; i < eventcnt; ++i) |
414 | ev_feed_event (EV_A_ events [i], type); |
423 | ev_feed_event (EV_A_ events [i], type); |
415 | } |
424 | } |
416 | |
425 | |
417 | inline void |
426 | void inline_speed |
418 | fd_event (EV_P_ int fd, int revents) |
427 | fd_event (EV_P_ int fd, int revents) |
419 | { |
428 | { |
420 | ANFD *anfd = anfds + fd; |
429 | ANFD *anfd = anfds + fd; |
421 | ev_io *w; |
430 | ev_io *w; |
422 | |
431 | |
… | |
… | |
435 | fd_event (EV_A_ fd, revents); |
444 | fd_event (EV_A_ fd, revents); |
436 | } |
445 | } |
437 | |
446 | |
438 | /*****************************************************************************/ |
447 | /*****************************************************************************/ |
439 | |
448 | |
440 | inline void |
449 | void inline_size |
441 | fd_reify (EV_P) |
450 | fd_reify (EV_P) |
442 | { |
451 | { |
443 | int i; |
452 | int i; |
444 | |
453 | |
445 | for (i = 0; i < fdchangecnt; ++i) |
454 | for (i = 0; i < fdchangecnt; ++i) |
… | |
… | |
469 | } |
478 | } |
470 | |
479 | |
471 | fdchangecnt = 0; |
480 | fdchangecnt = 0; |
472 | } |
481 | } |
473 | |
482 | |
474 | static void |
483 | void inline_size |
475 | fd_change (EV_P_ int fd) |
484 | fd_change (EV_P_ int fd) |
476 | { |
485 | { |
477 | if (expect_false (anfds [fd].reify)) |
486 | if (expect_false (anfds [fd].reify)) |
478 | return; |
487 | return; |
479 | |
488 | |
… | |
… | |
482 | ++fdchangecnt; |
491 | ++fdchangecnt; |
483 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
492 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
484 | fdchanges [fdchangecnt - 1] = fd; |
493 | fdchanges [fdchangecnt - 1] = fd; |
485 | } |
494 | } |
486 | |
495 | |
487 | static void |
496 | void inline_speed |
488 | fd_kill (EV_P_ int fd) |
497 | fd_kill (EV_P_ int fd) |
489 | { |
498 | { |
490 | ev_io *w; |
499 | ev_io *w; |
491 | |
500 | |
492 | while ((w = (ev_io *)anfds [fd].head)) |
501 | while ((w = (ev_io *)anfds [fd].head)) |
… | |
… | |
494 | ev_io_stop (EV_A_ w); |
503 | ev_io_stop (EV_A_ w); |
495 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
504 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
496 | } |
505 | } |
497 | } |
506 | } |
498 | |
507 | |
499 | inline int |
508 | int inline_size |
500 | fd_valid (int fd) |
509 | fd_valid (int fd) |
501 | { |
510 | { |
502 | #ifdef _WIN32 |
511 | #ifdef _WIN32 |
503 | return _get_osfhandle (fd) != -1; |
512 | return _get_osfhandle (fd) != -1; |
504 | #else |
513 | #else |
505 | return fcntl (fd, F_GETFD) != -1; |
514 | return fcntl (fd, F_GETFD) != -1; |
506 | #endif |
515 | #endif |
507 | } |
516 | } |
508 | |
517 | |
509 | /* called on EBADF to verify fds */ |
518 | /* called on EBADF to verify fds */ |
510 | static void |
519 | static void noinline |
511 | fd_ebadf (EV_P) |
520 | fd_ebadf (EV_P) |
512 | { |
521 | { |
513 | int fd; |
522 | int fd; |
514 | |
523 | |
515 | for (fd = 0; fd < anfdmax; ++fd) |
524 | for (fd = 0; fd < anfdmax; ++fd) |
… | |
… | |
517 | if (!fd_valid (fd) == -1 && errno == EBADF) |
526 | if (!fd_valid (fd) == -1 && errno == EBADF) |
518 | fd_kill (EV_A_ fd); |
527 | fd_kill (EV_A_ fd); |
519 | } |
528 | } |
520 | |
529 | |
521 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
530 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
522 | static void |
531 | static void noinline |
523 | fd_enomem (EV_P) |
532 | fd_enomem (EV_P) |
524 | { |
533 | { |
525 | int fd; |
534 | int fd; |
526 | |
535 | |
527 | for (fd = anfdmax; fd--; ) |
536 | for (fd = anfdmax; fd--; ) |
… | |
… | |
531 | return; |
540 | return; |
532 | } |
541 | } |
533 | } |
542 | } |
534 | |
543 | |
535 | /* usually called after fork if backend needs to re-arm all fds from scratch */ |
544 | /* usually called after fork if backend needs to re-arm all fds from scratch */ |
536 | static void |
545 | static void noinline |
537 | fd_rearm_all (EV_P) |
546 | fd_rearm_all (EV_P) |
538 | { |
547 | { |
539 | int fd; |
548 | int fd; |
540 | |
549 | |
541 | /* this should be highly optimised to not do anything but set a flag */ |
550 | /* this should be highly optimised to not do anything but set a flag */ |
… | |
… | |
547 | } |
556 | } |
548 | } |
557 | } |
549 | |
558 | |
550 | /*****************************************************************************/ |
559 | /*****************************************************************************/ |
551 | |
560 | |
552 | static void |
561 | void inline_speed |
553 | upheap (WT *heap, int k) |
562 | upheap (WT *heap, int k) |
554 | { |
563 | { |
555 | WT w = heap [k]; |
564 | WT w = heap [k]; |
556 | |
565 | |
557 | while (k && heap [k >> 1]->at > w->at) |
566 | while (k && heap [k >> 1]->at > w->at) |
… | |
… | |
564 | heap [k] = w; |
573 | heap [k] = w; |
565 | ((W)heap [k])->active = k + 1; |
574 | ((W)heap [k])->active = k + 1; |
566 | |
575 | |
567 | } |
576 | } |
568 | |
577 | |
569 | static void |
578 | void inline_speed |
570 | downheap (WT *heap, int N, int k) |
579 | downheap (WT *heap, int N, int k) |
571 | { |
580 | { |
572 | WT w = heap [k]; |
581 | WT w = heap [k]; |
573 | |
582 | |
574 | while (k < (N >> 1)) |
583 | while (k < (N >> 1)) |
… | |
… | |
588 | |
597 | |
589 | heap [k] = w; |
598 | heap [k] = w; |
590 | ((W)heap [k])->active = k + 1; |
599 | ((W)heap [k])->active = k + 1; |
591 | } |
600 | } |
592 | |
601 | |
593 | inline void |
602 | void inline_size |
594 | adjustheap (WT *heap, int N, int k) |
603 | adjustheap (WT *heap, int N, int k) |
595 | { |
604 | { |
596 | upheap (heap, k); |
605 | upheap (heap, k); |
597 | downheap (heap, N, k); |
606 | downheap (heap, N, k); |
598 | } |
607 | } |
… | |
… | |
610 | |
619 | |
611 | static int sigpipe [2]; |
620 | static int sigpipe [2]; |
612 | static sig_atomic_t volatile gotsig; |
621 | static sig_atomic_t volatile gotsig; |
613 | static ev_io sigev; |
622 | static ev_io sigev; |
614 | |
623 | |
615 | static void |
624 | void inline_size |
616 | signals_init (ANSIG *base, int count) |
625 | signals_init (ANSIG *base, int count) |
617 | { |
626 | { |
618 | while (count--) |
627 | while (count--) |
619 | { |
628 | { |
620 | base->head = 0; |
629 | base->head = 0; |
… | |
… | |
640 | write (sigpipe [1], &signum, 1); |
649 | write (sigpipe [1], &signum, 1); |
641 | errno = old_errno; |
650 | errno = old_errno; |
642 | } |
651 | } |
643 | } |
652 | } |
644 | |
653 | |
645 | void |
654 | void noinline |
646 | ev_feed_signal_event (EV_P_ int signum) |
655 | ev_feed_signal_event (EV_P_ int signum) |
647 | { |
656 | { |
648 | WL w; |
657 | WL w; |
649 | |
658 | |
650 | #if EV_MULTIPLICITY |
659 | #if EV_MULTIPLICITY |
… | |
… | |
673 | for (signum = signalmax; signum--; ) |
682 | for (signum = signalmax; signum--; ) |
674 | if (signals [signum].gotsig) |
683 | if (signals [signum].gotsig) |
675 | ev_feed_signal_event (EV_A_ signum + 1); |
684 | ev_feed_signal_event (EV_A_ signum + 1); |
676 | } |
685 | } |
677 | |
686 | |
678 | static void |
687 | void inline_size |
679 | fd_intern (int fd) |
688 | fd_intern (int fd) |
680 | { |
689 | { |
681 | #ifdef _WIN32 |
690 | #ifdef _WIN32 |
682 | int arg = 1; |
691 | int arg = 1; |
683 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
692 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
… | |
… | |
685 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
694 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
686 | fcntl (fd, F_SETFL, O_NONBLOCK); |
695 | fcntl (fd, F_SETFL, O_NONBLOCK); |
687 | #endif |
696 | #endif |
688 | } |
697 | } |
689 | |
698 | |
690 | static void |
699 | static void noinline |
691 | siginit (EV_P) |
700 | siginit (EV_P) |
692 | { |
701 | { |
693 | fd_intern (sigpipe [0]); |
702 | fd_intern (sigpipe [0]); |
694 | fd_intern (sigpipe [1]); |
703 | fd_intern (sigpipe [1]); |
695 | |
704 | |
… | |
… | |
708 | |
717 | |
709 | #ifndef WCONTINUED |
718 | #ifndef WCONTINUED |
710 | # define WCONTINUED 0 |
719 | # define WCONTINUED 0 |
711 | #endif |
720 | #endif |
712 | |
721 | |
713 | static void |
722 | void inline_speed |
714 | child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) |
723 | child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) |
715 | { |
724 | { |
716 | ev_child *w; |
725 | ev_child *w; |
717 | |
726 | |
718 | for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
727 | for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
… | |
… | |
772 | { |
781 | { |
773 | return EV_VERSION_MINOR; |
782 | return EV_VERSION_MINOR; |
774 | } |
783 | } |
775 | |
784 | |
776 | /* return true if we are running with elevated privileges and should ignore env variables */ |
785 | /* return true if we are running with elevated privileges and should ignore env variables */ |
777 | static int |
786 | int inline_size |
778 | enable_secure (void) |
787 | enable_secure (void) |
779 | { |
788 | { |
780 | #ifdef _WIN32 |
789 | #ifdef _WIN32 |
781 | return 0; |
790 | return 0; |
782 | #else |
791 | #else |
… | |
… | |
904 | array_free (pending, [i]); |
913 | array_free (pending, [i]); |
905 | |
914 | |
906 | /* have to use the microsoft-never-gets-it-right macro */ |
915 | /* have to use the microsoft-never-gets-it-right macro */ |
907 | array_free (fdchange, EMPTY0); |
916 | array_free (fdchange, EMPTY0); |
908 | array_free (timer, EMPTY0); |
917 | array_free (timer, EMPTY0); |
909 | #if EV_PERIODICS |
918 | #if EV_PERIODIC_ENABLE |
910 | array_free (periodic, EMPTY0); |
919 | array_free (periodic, EMPTY0); |
911 | #endif |
920 | #endif |
912 | array_free (idle, EMPTY0); |
921 | array_free (idle, EMPTY0); |
913 | array_free (prepare, EMPTY0); |
922 | array_free (prepare, EMPTY0); |
914 | array_free (check, EMPTY0); |
923 | array_free (check, EMPTY0); |
… | |
… | |
1050 | postfork = 1; |
1059 | postfork = 1; |
1051 | } |
1060 | } |
1052 | |
1061 | |
1053 | /*****************************************************************************/ |
1062 | /*****************************************************************************/ |
1054 | |
1063 | |
1055 | static int |
1064 | int inline_size |
1056 | any_pending (EV_P) |
1065 | any_pending (EV_P) |
1057 | { |
1066 | { |
1058 | int pri; |
1067 | int pri; |
1059 | |
1068 | |
1060 | for (pri = NUMPRI; pri--; ) |
1069 | for (pri = NUMPRI; pri--; ) |
… | |
… | |
1062 | return 1; |
1071 | return 1; |
1063 | |
1072 | |
1064 | return 0; |
1073 | return 0; |
1065 | } |
1074 | } |
1066 | |
1075 | |
1067 | inline void |
1076 | void inline_speed |
1068 | call_pending (EV_P) |
1077 | call_pending (EV_P) |
1069 | { |
1078 | { |
1070 | int pri; |
1079 | int pri; |
1071 | |
1080 | |
1072 | for (pri = NUMPRI; pri--; ) |
1081 | for (pri = NUMPRI; pri--; ) |
… | |
… | |
1082 | EV_CB_INVOKE (p->w, p->events); |
1091 | EV_CB_INVOKE (p->w, p->events); |
1083 | } |
1092 | } |
1084 | } |
1093 | } |
1085 | } |
1094 | } |
1086 | |
1095 | |
1087 | inline void |
1096 | void inline_size |
1088 | timers_reify (EV_P) |
1097 | timers_reify (EV_P) |
1089 | { |
1098 | { |
1090 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1099 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1091 | { |
1100 | { |
1092 | ev_timer *w = timers [0]; |
1101 | ev_timer *w = timers [0]; |
… | |
… | |
1109 | |
1118 | |
1110 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1119 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1111 | } |
1120 | } |
1112 | } |
1121 | } |
1113 | |
1122 | |
1114 | #if EV_PERIODICS |
1123 | #if EV_PERIODIC_ENABLE |
1115 | inline void |
1124 | void inline_size |
1116 | periodics_reify (EV_P) |
1125 | periodics_reify (EV_P) |
1117 | { |
1126 | { |
1118 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1127 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1119 | { |
1128 | { |
1120 | ev_periodic *w = periodics [0]; |
1129 | ev_periodic *w = periodics [0]; |
… | |
… | |
1139 | |
1148 | |
1140 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1149 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1141 | } |
1150 | } |
1142 | } |
1151 | } |
1143 | |
1152 | |
1144 | static void |
1153 | static void noinline |
1145 | periodics_reschedule (EV_P) |
1154 | periodics_reschedule (EV_P) |
1146 | { |
1155 | { |
1147 | int i; |
1156 | int i; |
1148 | |
1157 | |
1149 | /* adjust periodics after time jump */ |
1158 | /* adjust periodics after time jump */ |
… | |
… | |
1161 | for (i = periodiccnt >> 1; i--; ) |
1170 | for (i = periodiccnt >> 1; i--; ) |
1162 | downheap ((WT *)periodics, periodiccnt, i); |
1171 | downheap ((WT *)periodics, periodiccnt, i); |
1163 | } |
1172 | } |
1164 | #endif |
1173 | #endif |
1165 | |
1174 | |
1166 | inline int |
1175 | int inline_size |
1167 | time_update_monotonic (EV_P) |
1176 | time_update_monotonic (EV_P) |
1168 | { |
1177 | { |
1169 | mn_now = get_clock (); |
1178 | mn_now = get_clock (); |
1170 | |
1179 | |
1171 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1180 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
… | |
… | |
1179 | ev_rt_now = ev_time (); |
1188 | ev_rt_now = ev_time (); |
1180 | return 1; |
1189 | return 1; |
1181 | } |
1190 | } |
1182 | } |
1191 | } |
1183 | |
1192 | |
1184 | inline void |
1193 | void inline_size |
1185 | time_update (EV_P) |
1194 | time_update (EV_P) |
1186 | { |
1195 | { |
1187 | int i; |
1196 | int i; |
1188 | |
1197 | |
1189 | #if EV_USE_MONOTONIC |
1198 | #if EV_USE_MONOTONIC |
… | |
… | |
1211 | ev_rt_now = ev_time (); |
1220 | ev_rt_now = ev_time (); |
1212 | mn_now = get_clock (); |
1221 | mn_now = get_clock (); |
1213 | now_floor = mn_now; |
1222 | now_floor = mn_now; |
1214 | } |
1223 | } |
1215 | |
1224 | |
1216 | # if EV_PERIODICS |
1225 | # if EV_PERIODIC_ENABLE |
1217 | periodics_reschedule (EV_A); |
1226 | periodics_reschedule (EV_A); |
1218 | # endif |
1227 | # endif |
1219 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1228 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1220 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1229 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1221 | } |
1230 | } |
… | |
… | |
1225 | { |
1234 | { |
1226 | ev_rt_now = ev_time (); |
1235 | ev_rt_now = ev_time (); |
1227 | |
1236 | |
1228 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1237 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1229 | { |
1238 | { |
1230 | #if EV_PERIODICS |
1239 | #if EV_PERIODIC_ENABLE |
1231 | periodics_reschedule (EV_A); |
1240 | periodics_reschedule (EV_A); |
1232 | #endif |
1241 | #endif |
1233 | |
1242 | |
1234 | /* adjust timers. this is easy, as the offset is the same for all */ |
1243 | /* adjust timers. this is easy, as the offset is the same for all */ |
1235 | for (i = 0; i < timercnt; ++i) |
1244 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
1302 | { |
1311 | { |
1303 | ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; |
1312 | ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; |
1304 | if (block > to) block = to; |
1313 | if (block > to) block = to; |
1305 | } |
1314 | } |
1306 | |
1315 | |
1307 | #if EV_PERIODICS |
1316 | #if EV_PERIODIC_ENABLE |
1308 | if (periodiccnt) |
1317 | if (periodiccnt) |
1309 | { |
1318 | { |
1310 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; |
1319 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; |
1311 | if (block > to) block = to; |
1320 | if (block > to) block = to; |
1312 | } |
1321 | } |
… | |
… | |
1321 | /* update ev_rt_now, do magic */ |
1330 | /* update ev_rt_now, do magic */ |
1322 | time_update (EV_A); |
1331 | time_update (EV_A); |
1323 | |
1332 | |
1324 | /* queue pending timers and reschedule them */ |
1333 | /* queue pending timers and reschedule them */ |
1325 | timers_reify (EV_A); /* relative timers called last */ |
1334 | timers_reify (EV_A); /* relative timers called last */ |
1326 | #if EV_PERIODICS |
1335 | #if EV_PERIODIC_ENABLE |
1327 | periodics_reify (EV_A); /* absolute timers called first */ |
1336 | periodics_reify (EV_A); /* absolute timers called first */ |
1328 | #endif |
1337 | #endif |
1329 | |
1338 | |
1330 | /* queue idle watchers unless other events are pending */ |
1339 | /* queue idle watchers unless other events are pending */ |
1331 | if (idlecnt && !any_pending (EV_A)) |
1340 | if (idlecnt && !any_pending (EV_A)) |
… | |
… | |
1351 | loop_done = how; |
1360 | loop_done = how; |
1352 | } |
1361 | } |
1353 | |
1362 | |
1354 | /*****************************************************************************/ |
1363 | /*****************************************************************************/ |
1355 | |
1364 | |
1356 | inline void |
1365 | void inline_size |
1357 | wlist_add (WL *head, WL elem) |
1366 | wlist_add (WL *head, WL elem) |
1358 | { |
1367 | { |
1359 | elem->next = *head; |
1368 | elem->next = *head; |
1360 | *head = elem; |
1369 | *head = elem; |
1361 | } |
1370 | } |
1362 | |
1371 | |
1363 | inline void |
1372 | void inline_size |
1364 | wlist_del (WL *head, WL elem) |
1373 | wlist_del (WL *head, WL elem) |
1365 | { |
1374 | { |
1366 | while (*head) |
1375 | while (*head) |
1367 | { |
1376 | { |
1368 | if (*head == elem) |
1377 | if (*head == elem) |
… | |
… | |
1373 | |
1382 | |
1374 | head = &(*head)->next; |
1383 | head = &(*head)->next; |
1375 | } |
1384 | } |
1376 | } |
1385 | } |
1377 | |
1386 | |
1378 | inline void |
1387 | void inline_speed |
1379 | ev_clear_pending (EV_P_ W w) |
1388 | ev_clear_pending (EV_P_ W w) |
1380 | { |
1389 | { |
1381 | if (w->pending) |
1390 | if (w->pending) |
1382 | { |
1391 | { |
1383 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1392 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1384 | w->pending = 0; |
1393 | w->pending = 0; |
1385 | } |
1394 | } |
1386 | } |
1395 | } |
1387 | |
1396 | |
1388 | inline void |
1397 | void inline_speed |
1389 | ev_start (EV_P_ W w, int active) |
1398 | ev_start (EV_P_ W w, int active) |
1390 | { |
1399 | { |
1391 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1400 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1392 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1401 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1393 | |
1402 | |
1394 | w->active = active; |
1403 | w->active = active; |
1395 | ev_ref (EV_A); |
1404 | ev_ref (EV_A); |
1396 | } |
1405 | } |
1397 | |
1406 | |
1398 | inline void |
1407 | void inline_size |
1399 | ev_stop (EV_P_ W w) |
1408 | ev_stop (EV_P_ W w) |
1400 | { |
1409 | { |
1401 | ev_unref (EV_A); |
1410 | ev_unref (EV_A); |
1402 | w->active = 0; |
1411 | w->active = 0; |
1403 | } |
1412 | } |
… | |
… | |
1492 | w->at = w->repeat; |
1501 | w->at = w->repeat; |
1493 | ev_timer_start (EV_A_ w); |
1502 | ev_timer_start (EV_A_ w); |
1494 | } |
1503 | } |
1495 | } |
1504 | } |
1496 | |
1505 | |
1497 | #if EV_PERIODICS |
1506 | #if EV_PERIODIC_ENABLE |
1498 | void |
1507 | void |
1499 | ev_periodic_start (EV_P_ ev_periodic *w) |
1508 | ev_periodic_start (EV_P_ ev_periodic *w) |
1500 | { |
1509 | { |
1501 | if (expect_false (ev_is_active (w))) |
1510 | if (expect_false (ev_is_active (w))) |
1502 | return; |
1511 | return; |
… | |
… | |
1695 | |
1704 | |
1696 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1705 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1697 | ev_stop (EV_A_ (W)w); |
1706 | ev_stop (EV_A_ (W)w); |
1698 | } |
1707 | } |
1699 | |
1708 | |
1700 | #if EV_MULTIPLICITY |
1709 | #if EV_EMBED_ENABLE |
1701 | void |
1710 | void noinline |
1702 | ev_embed_sweep (EV_P_ ev_embed *w) |
1711 | ev_embed_sweep (EV_P_ ev_embed *w) |
1703 | { |
1712 | { |
1704 | ev_loop (w->loop, EVLOOP_NONBLOCK); |
1713 | ev_loop (w->loop, EVLOOP_NONBLOCK); |
1705 | } |
1714 | } |
1706 | |
1715 | |
… | |
… | |
1727 | ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); |
1736 | ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); |
1728 | } |
1737 | } |
1729 | |
1738 | |
1730 | ev_set_priority (&w->io, ev_priority (w)); |
1739 | ev_set_priority (&w->io, ev_priority (w)); |
1731 | ev_io_start (EV_A_ &w->io); |
1740 | ev_io_start (EV_A_ &w->io); |
|
|
1741 | |
1732 | ev_start (EV_A_ (W)w, 1); |
1742 | ev_start (EV_A_ (W)w, 1); |
1733 | } |
1743 | } |
1734 | |
1744 | |
1735 | void |
1745 | void |
1736 | ev_embed_stop (EV_P_ ev_embed *w) |
1746 | ev_embed_stop (EV_P_ ev_embed *w) |
… | |
… | |
1738 | ev_clear_pending (EV_A_ (W)w); |
1748 | ev_clear_pending (EV_A_ (W)w); |
1739 | if (expect_false (!ev_is_active (w))) |
1749 | if (expect_false (!ev_is_active (w))) |
1740 | return; |
1750 | return; |
1741 | |
1751 | |
1742 | ev_io_stop (EV_A_ &w->io); |
1752 | ev_io_stop (EV_A_ &w->io); |
|
|
1753 | |
|
|
1754 | ev_stop (EV_A_ (W)w); |
|
|
1755 | } |
|
|
1756 | #endif |
|
|
1757 | |
|
|
1758 | #if EV_STAT_ENABLE |
|
|
1759 | |
|
|
1760 | # ifdef _WIN32 |
|
|
1761 | # define lstat(a,b) stat(a,b) |
|
|
1762 | # endif |
|
|
1763 | |
|
|
1764 | void |
|
|
1765 | ev_stat_stat (EV_P_ ev_stat *w) |
|
|
1766 | { |
|
|
1767 | if (lstat (w->path, &w->attr) < 0) |
|
|
1768 | w->attr.st_nlink = 0; |
|
|
1769 | else if (!w->attr.st_nlink) |
|
|
1770 | w->attr.st_nlink = 1; |
|
|
1771 | } |
|
|
1772 | |
|
|
1773 | static void |
|
|
1774 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
|
|
1775 | { |
|
|
1776 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
|
|
1777 | |
|
|
1778 | /* we copy this here each the time so that */ |
|
|
1779 | /* prev has the old value when the callback gets invoked */ |
|
|
1780 | w->prev = w->attr; |
|
|
1781 | ev_stat_stat (EV_A_ w); |
|
|
1782 | |
|
|
1783 | if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) |
|
|
1784 | ev_feed_event (EV_A_ w, EV_STAT); |
|
|
1785 | } |
|
|
1786 | |
|
|
1787 | void |
|
|
1788 | ev_stat_start (EV_P_ ev_stat *w) |
|
|
1789 | { |
|
|
1790 | if (expect_false (ev_is_active (w))) |
|
|
1791 | return; |
|
|
1792 | |
|
|
1793 | /* since we use memcmp, we need to clear any padding data etc. */ |
|
|
1794 | memset (&w->prev, 0, sizeof (ev_statdata)); |
|
|
1795 | memset (&w->attr, 0, sizeof (ev_statdata)); |
|
|
1796 | |
|
|
1797 | ev_stat_stat (EV_A_ w); |
|
|
1798 | |
|
|
1799 | ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval); |
|
|
1800 | ev_set_priority (&w->timer, ev_priority (w)); |
|
|
1801 | ev_timer_start (EV_A_ &w->timer); |
|
|
1802 | |
|
|
1803 | ev_start (EV_A_ (W)w, 1); |
|
|
1804 | } |
|
|
1805 | |
|
|
1806 | void |
|
|
1807 | ev_stat_stop (EV_P_ ev_stat *w) |
|
|
1808 | { |
|
|
1809 | ev_clear_pending (EV_A_ (W)w); |
|
|
1810 | if (expect_false (!ev_is_active (w))) |
|
|
1811 | return; |
|
|
1812 | |
|
|
1813 | ev_timer_stop (EV_A_ &w->timer); |
|
|
1814 | |
1743 | ev_stop (EV_A_ (W)w); |
1815 | ev_stop (EV_A_ (W)w); |
1744 | } |
1816 | } |
1745 | #endif |
1817 | #endif |
1746 | |
1818 | |
1747 | /*****************************************************************************/ |
1819 | /*****************************************************************************/ |