… | |
… | |
269 | #endif |
269 | #endif |
270 | |
270 | |
271 | #if EV_USE_EVENTFD |
271 | #if EV_USE_EVENTFD |
272 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
272 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
273 | # include <stdint.h> |
273 | # include <stdint.h> |
|
|
274 | # ifdef __cplusplus |
|
|
275 | extern "C" { |
|
|
276 | # endif |
274 | int eventfd (unsigned int initval, int flags); |
277 | int eventfd (unsigned int initval, int flags); |
|
|
278 | # ifdef __cplusplus |
|
|
279 | } |
|
|
280 | # endif |
275 | #endif |
281 | #endif |
276 | |
282 | |
277 | /**/ |
283 | /**/ |
278 | |
284 | |
279 | /* |
285 | /* |
… | |
… | |
294 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
300 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
295 | # define noinline __attribute__ ((noinline)) |
301 | # define noinline __attribute__ ((noinline)) |
296 | #else |
302 | #else |
297 | # define expect(expr,value) (expr) |
303 | # define expect(expr,value) (expr) |
298 | # define noinline |
304 | # define noinline |
299 | # if __STDC_VERSION__ < 199901L |
305 | # if __STDC_VERSION__ < 199901L && __GNUC__ < 2 |
300 | # define inline |
306 | # define inline |
301 | # endif |
307 | # endif |
302 | #endif |
308 | #endif |
303 | |
309 | |
304 | #define expect_false(expr) expect ((expr) != 0, 0) |
310 | #define expect_false(expr) expect ((expr) != 0, 0) |
… | |
… | |
319 | |
325 | |
320 | typedef ev_watcher *W; |
326 | typedef ev_watcher *W; |
321 | typedef ev_watcher_list *WL; |
327 | typedef ev_watcher_list *WL; |
322 | typedef ev_watcher_time *WT; |
328 | typedef ev_watcher_time *WT; |
323 | |
329 | |
|
|
330 | #define ev_active(w) ((W)(w))->active |
|
|
331 | #define ev_at(w) ((WT)(w))->at |
|
|
332 | |
324 | #if EV_USE_MONOTONIC |
333 | #if EV_USE_MONOTONIC |
325 | /* sig_atomic_t is used to avoid per-thread variables or locking but still */ |
334 | /* sig_atomic_t is used to avoid per-thread variables or locking but still */ |
326 | /* giving it a reasonably high chance of working on typical architetcures */ |
335 | /* giving it a reasonably high chance of working on typical architetcures */ |
327 | static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
336 | static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
328 | #endif |
337 | #endif |
… | |
… | |
354 | perror (msg); |
363 | perror (msg); |
355 | abort (); |
364 | abort (); |
356 | } |
365 | } |
357 | } |
366 | } |
358 | |
367 | |
|
|
368 | static void * |
|
|
369 | ev_realloc_emul (void *ptr, long size) |
|
|
370 | { |
|
|
371 | /* some systems, notably openbsd and darwin, fail to properly |
|
|
372 | * implement realloc (x, 0) (as required by both ansi c-98 and |
|
|
373 | * the single unix specification, so work around them here. |
|
|
374 | */ |
|
|
375 | |
|
|
376 | if (size) |
|
|
377 | return realloc (ptr, size); |
|
|
378 | |
|
|
379 | free (ptr); |
|
|
380 | return 0; |
|
|
381 | } |
|
|
382 | |
359 | static void *(*alloc)(void *ptr, long size); |
383 | static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; |
360 | |
384 | |
361 | void |
385 | void |
362 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
386 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
363 | { |
387 | { |
364 | alloc = cb; |
388 | alloc = cb; |
365 | } |
389 | } |
366 | |
390 | |
367 | inline_speed void * |
391 | inline_speed void * |
368 | ev_realloc (void *ptr, long size) |
392 | ev_realloc (void *ptr, long size) |
369 | { |
393 | { |
370 | ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); |
394 | ptr = alloc (ptr, size); |
371 | |
395 | |
372 | if (!ptr && size) |
396 | if (!ptr && size) |
373 | { |
397 | { |
374 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
398 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
375 | abort (); |
399 | abort (); |
… | |
… | |
398 | W w; |
422 | W w; |
399 | int events; |
423 | int events; |
400 | } ANPENDING; |
424 | } ANPENDING; |
401 | |
425 | |
402 | #if EV_USE_INOTIFY |
426 | #if EV_USE_INOTIFY |
|
|
427 | /* hash table entry per inotify-id */ |
403 | typedef struct |
428 | typedef struct |
404 | { |
429 | { |
405 | WL head; |
430 | WL head; |
406 | } ANFS; |
431 | } ANFS; |
|
|
432 | #endif |
|
|
433 | |
|
|
434 | /* Heap Entry */ |
|
|
435 | #if EV_HEAP_CACHE_AT |
|
|
436 | typedef struct { |
|
|
437 | WT w; |
|
|
438 | ev_tstamp at; |
|
|
439 | } ANHE; |
|
|
440 | |
|
|
441 | #define ANHE_w(he) (he) /* access watcher, read-write */ |
|
|
442 | #define ANHE_at(he) (he)->at /* acces cahced at, read-only */ |
|
|
443 | #define ANHE_at_set(he) (he)->at = (he)->w->at /* update at from watcher */ |
|
|
444 | #else |
|
|
445 | typedef WT ANHE; |
|
|
446 | |
|
|
447 | #define ANHE_w(he) (he) |
|
|
448 | #define ANHE_at(he) (he)->at |
|
|
449 | #define ANHE_at_set(he) |
407 | #endif |
450 | #endif |
408 | |
451 | |
409 | #if EV_MULTIPLICITY |
452 | #if EV_MULTIPLICITY |
410 | |
453 | |
411 | struct ev_loop |
454 | struct ev_loop |
… | |
… | |
496 | } |
539 | } |
497 | } |
540 | } |
498 | |
541 | |
499 | /*****************************************************************************/ |
542 | /*****************************************************************************/ |
500 | |
543 | |
|
|
544 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
|
|
545 | |
501 | int inline_size |
546 | int inline_size |
502 | array_nextsize (int elem, int cur, int cnt) |
547 | array_nextsize (int elem, int cur, int cnt) |
503 | { |
548 | { |
504 | int ncur = cur + 1; |
549 | int ncur = cur + 1; |
505 | |
550 | |
506 | do |
551 | do |
507 | ncur <<= 1; |
552 | ncur <<= 1; |
508 | while (cnt > ncur); |
553 | while (cnt > ncur); |
509 | |
554 | |
510 | /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */ |
555 | /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */ |
511 | if (elem * ncur > 4096) |
556 | if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4) |
512 | { |
557 | { |
513 | ncur *= elem; |
558 | ncur *= elem; |
514 | ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; |
559 | ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1); |
515 | ncur = ncur - sizeof (void *) * 4; |
560 | ncur = ncur - sizeof (void *) * 4; |
516 | ncur /= elem; |
561 | ncur /= elem; |
517 | } |
562 | } |
518 | |
563 | |
519 | return ncur; |
564 | return ncur; |
… | |
… | |
733 | } |
778 | } |
734 | } |
779 | } |
735 | |
780 | |
736 | /*****************************************************************************/ |
781 | /*****************************************************************************/ |
737 | |
782 | |
|
|
783 | /* |
|
|
784 | * the heap functions want a real array index. array index 0 uis guaranteed to not |
|
|
785 | * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives |
|
|
786 | * the branching factor of the d-tree. |
|
|
787 | */ |
|
|
788 | |
|
|
789 | /* |
|
|
790 | * at the moment we allow libev the luxury of two heaps, |
|
|
791 | * a small-code-size 2-heap one and a ~1.5kb larger 4-heap |
|
|
792 | * which is more cache-efficient. |
|
|
793 | * the difference is about 5% with 50000+ watchers. |
|
|
794 | */ |
|
|
795 | #define EV_USE_4HEAP !EV_MINIMAL |
|
|
796 | #if EV_USE_4HEAP |
|
|
797 | |
|
|
798 | #define DHEAP 4 |
|
|
799 | #define HEAP0 (DHEAP - 1) /* index of first element in heap */ |
|
|
800 | |
|
|
801 | /* towards the root */ |
738 | void inline_speed |
802 | void inline_speed |
739 | upheap (WT *heap, int k) |
803 | upheap (ANHE *heap, int k) |
740 | { |
804 | { |
741 | WT w = heap [k]; |
805 | ANHE he = heap [k]; |
742 | |
806 | |
743 | while (k) |
807 | for (;;) |
744 | { |
808 | { |
745 | int p = (k - 1) >> 1; |
809 | int p = ((k - HEAP0 - 1) / DHEAP) + HEAP0; |
746 | |
810 | |
747 | if (heap [p]->at <= w->at) |
811 | if (p == k || ANHE_at (heap [p]) <= ANHE_at (he)) |
748 | break; |
812 | break; |
749 | |
813 | |
750 | heap [k] = heap [p]; |
814 | heap [k] = heap [p]; |
751 | ((W)heap [k])->active = k + 1; |
815 | ev_active (ANHE_w (heap [k])) = k; |
752 | k = p; |
816 | k = p; |
753 | } |
817 | } |
754 | |
818 | |
|
|
819 | ev_active (ANHE_w (he)) = k; |
|
|
820 | heap [k] = he; |
|
|
821 | } |
|
|
822 | |
|
|
823 | /* away from the root */ |
|
|
824 | void inline_speed |
|
|
825 | downheap (ANHE *heap, int N, int k) |
|
|
826 | { |
|
|
827 | ANHE he = heap [k]; |
|
|
828 | ANHE *E = heap + N + HEAP0; |
|
|
829 | |
|
|
830 | for (;;) |
|
|
831 | { |
|
|
832 | ev_tstamp minat; |
|
|
833 | ANHE *minpos; |
|
|
834 | ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0; |
|
|
835 | |
|
|
836 | // find minimum child |
|
|
837 | if (expect_true (pos + DHEAP - 1 < E)) |
|
|
838 | { |
|
|
839 | /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
|
|
840 | if (ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos)); |
|
|
841 | if (ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos)); |
|
|
842 | if (ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos)); |
|
|
843 | } |
|
|
844 | else if (pos < E) |
|
|
845 | { |
|
|
846 | /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos)); |
|
|
847 | if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos)); |
|
|
848 | if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos)); |
|
|
849 | if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos)); |
|
|
850 | } |
|
|
851 | else |
|
|
852 | break; |
|
|
853 | |
|
|
854 | if (ANHE_at (he) <= minat) |
|
|
855 | break; |
|
|
856 | |
|
|
857 | ev_active (ANHE_w (*minpos)) = k; |
|
|
858 | heap [k] = *minpos; |
|
|
859 | |
|
|
860 | k = minpos - heap; |
|
|
861 | } |
|
|
862 | |
|
|
863 | ev_active (ANHE_w (he)) = k; |
|
|
864 | heap [k] = he; |
|
|
865 | } |
|
|
866 | |
|
|
867 | #else // 4HEAP |
|
|
868 | |
|
|
869 | #define HEAP0 1 |
|
|
870 | |
|
|
871 | /* towards the root */ |
|
|
872 | void inline_speed |
|
|
873 | upheap (ANHE *heap, int k) |
|
|
874 | { |
|
|
875 | ANHE he = heap [k]; |
|
|
876 | |
|
|
877 | for (;;) |
|
|
878 | { |
|
|
879 | int p = k >> 1; |
|
|
880 | |
|
|
881 | /* maybe we could use a dummy element at heap [0]? */ |
|
|
882 | if (!p || ANHE_at (heap [p]) <= ANHE_at (he)) |
|
|
883 | break; |
|
|
884 | |
|
|
885 | heap [k] = heap [p]; |
|
|
886 | ev_active (ANHE_w (heap [k])) = k; |
|
|
887 | k = p; |
|
|
888 | } |
|
|
889 | |
755 | heap [k] = w; |
890 | heap [k] = w; |
756 | ((W)heap [k])->active = k + 1; |
891 | ev_active (ANHE_w (heap [k])) = k; |
757 | } |
892 | } |
758 | |
893 | |
|
|
894 | /* away from the root */ |
759 | void inline_speed |
895 | void inline_speed |
760 | downheap (WT *heap, int N, int k) |
896 | downheap (ANHE *heap, int N, int k) |
761 | { |
897 | { |
762 | WT w = heap [k]; |
898 | ANHE he = heap [k]; |
763 | |
899 | |
764 | for (;;) |
900 | for (;;) |
765 | { |
901 | { |
766 | int c = (k << 1) + 1; |
902 | int c = k << 1; |
767 | |
903 | |
768 | if (c >= N) |
904 | if (c > N) |
769 | break; |
905 | break; |
770 | |
906 | |
771 | c += c + 1 < N && heap [c]->at > heap [c + 1]->at |
907 | c += c + 1 < N && ANHE_at (heap [c]) > ANHE_at (heap [c + 1]) |
772 | ? 1 : 0; |
908 | ? 1 : 0; |
773 | |
909 | |
774 | if (w->at <= heap [c]->at) |
910 | if (w->at <= ANHE_at (heap [c])) |
775 | break; |
911 | break; |
776 | |
912 | |
777 | heap [k] = heap [c]; |
913 | heap [k] = heap [c]; |
778 | ((W)heap [k])->active = k + 1; |
914 | ev_active (ANHE_w (heap [k])) = k; |
779 | |
915 | |
780 | k = c; |
916 | k = c; |
781 | } |
917 | } |
782 | |
918 | |
783 | heap [k] = w; |
919 | heap [k] = he; |
784 | ((W)heap [k])->active = k + 1; |
920 | ev_active (ANHE_w (he)) = k; |
785 | } |
921 | } |
|
|
922 | #endif |
786 | |
923 | |
787 | void inline_size |
924 | void inline_size |
788 | adjustheap (WT *heap, int N, int k) |
925 | adjustheap (ANHE *heap, int N, int k) |
789 | { |
926 | { |
790 | upheap (heap, k); |
927 | upheap (heap, k); |
791 | downheap (heap, N, k); |
928 | downheap (heap, N, k); |
792 | } |
929 | } |
793 | |
930 | |
… | |
… | |
885 | pipecb (EV_P_ ev_io *iow, int revents) |
1022 | pipecb (EV_P_ ev_io *iow, int revents) |
886 | { |
1023 | { |
887 | #if EV_USE_EVENTFD |
1024 | #if EV_USE_EVENTFD |
888 | if (evfd >= 0) |
1025 | if (evfd >= 0) |
889 | { |
1026 | { |
890 | uint64_t counter = 1; |
1027 | uint64_t counter; |
891 | read (evfd, &counter, sizeof (uint64_t)); |
1028 | read (evfd, &counter, sizeof (uint64_t)); |
892 | } |
1029 | } |
893 | else |
1030 | else |
894 | #endif |
1031 | #endif |
895 | { |
1032 | { |
… | |
… | |
1164 | if (!(flags & EVFLAG_NOENV) |
1301 | if (!(flags & EVFLAG_NOENV) |
1165 | && !enable_secure () |
1302 | && !enable_secure () |
1166 | && getenv ("LIBEV_FLAGS")) |
1303 | && getenv ("LIBEV_FLAGS")) |
1167 | flags = atoi (getenv ("LIBEV_FLAGS")); |
1304 | flags = atoi (getenv ("LIBEV_FLAGS")); |
1168 | |
1305 | |
1169 | if (!(flags & 0x0000ffffUL)) |
1306 | if (!(flags & 0x0000ffffU)) |
1170 | flags |= ev_recommended_backends (); |
1307 | flags |= ev_recommended_backends (); |
1171 | |
1308 | |
1172 | #if EV_USE_PORT |
1309 | #if EV_USE_PORT |
1173 | if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); |
1310 | if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); |
1174 | #endif |
1311 | #endif |
… | |
… | |
1262 | #endif |
1399 | #endif |
1263 | |
1400 | |
1264 | backend = 0; |
1401 | backend = 0; |
1265 | } |
1402 | } |
1266 | |
1403 | |
|
|
1404 | #if EV_USE_INOTIFY |
1267 | void inline_size infy_fork (EV_P); |
1405 | void inline_size infy_fork (EV_P); |
|
|
1406 | #endif |
1268 | |
1407 | |
1269 | void inline_size |
1408 | void inline_size |
1270 | loop_fork (EV_P) |
1409 | loop_fork (EV_P) |
1271 | { |
1410 | { |
1272 | #if EV_USE_PORT |
1411 | #if EV_USE_PORT |
… | |
… | |
1339 | void |
1478 | void |
1340 | ev_loop_fork (EV_P) |
1479 | ev_loop_fork (EV_P) |
1341 | { |
1480 | { |
1342 | postfork = 1; /* must be in line with ev_default_fork */ |
1481 | postfork = 1; /* must be in line with ev_default_fork */ |
1343 | } |
1482 | } |
1344 | |
|
|
1345 | #endif |
1483 | #endif |
1346 | |
1484 | |
1347 | #if EV_MULTIPLICITY |
1485 | #if EV_MULTIPLICITY |
1348 | struct ev_loop * |
1486 | struct ev_loop * |
1349 | ev_default_loop_init (unsigned int flags) |
1487 | ev_default_loop_init (unsigned int flags) |
… | |
… | |
1430 | EV_CB_INVOKE (p->w, p->events); |
1568 | EV_CB_INVOKE (p->w, p->events); |
1431 | } |
1569 | } |
1432 | } |
1570 | } |
1433 | } |
1571 | } |
1434 | |
1572 | |
1435 | void inline_size |
|
|
1436 | timers_reify (EV_P) |
|
|
1437 | { |
|
|
1438 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
|
|
1439 | { |
|
|
1440 | ev_timer *w = (ev_timer *)timers [0]; |
|
|
1441 | |
|
|
1442 | /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ |
|
|
1443 | |
|
|
1444 | /* first reschedule or stop timer */ |
|
|
1445 | if (w->repeat) |
|
|
1446 | { |
|
|
1447 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
|
|
1448 | |
|
|
1449 | ((WT)w)->at += w->repeat; |
|
|
1450 | if (((WT)w)->at < mn_now) |
|
|
1451 | ((WT)w)->at = mn_now; |
|
|
1452 | |
|
|
1453 | downheap (timers, timercnt, 0); |
|
|
1454 | } |
|
|
1455 | else |
|
|
1456 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
|
|
1457 | |
|
|
1458 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
|
|
1459 | } |
|
|
1460 | } |
|
|
1461 | |
|
|
1462 | #if EV_PERIODIC_ENABLE |
|
|
1463 | void inline_size |
|
|
1464 | periodics_reify (EV_P) |
|
|
1465 | { |
|
|
1466 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
|
|
1467 | { |
|
|
1468 | ev_periodic *w = (ev_periodic *)periodics [0]; |
|
|
1469 | |
|
|
1470 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
|
|
1471 | |
|
|
1472 | /* first reschedule or stop timer */ |
|
|
1473 | if (w->reschedule_cb) |
|
|
1474 | { |
|
|
1475 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON); |
|
|
1476 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
|
|
1477 | downheap (periodics, periodiccnt, 0); |
|
|
1478 | } |
|
|
1479 | else if (w->interval) |
|
|
1480 | { |
|
|
1481 | ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
1482 | if (((WT)w)->at - ev_rt_now <= TIME_EPSILON) ((WT)w)->at += w->interval; |
|
|
1483 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
|
|
1484 | downheap (periodics, periodiccnt, 0); |
|
|
1485 | } |
|
|
1486 | else |
|
|
1487 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
|
|
1488 | |
|
|
1489 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
|
|
1490 | } |
|
|
1491 | } |
|
|
1492 | |
|
|
1493 | static void noinline |
|
|
1494 | periodics_reschedule (EV_P) |
|
|
1495 | { |
|
|
1496 | int i; |
|
|
1497 | |
|
|
1498 | /* adjust periodics after time jump */ |
|
|
1499 | for (i = 0; i < periodiccnt; ++i) |
|
|
1500 | { |
|
|
1501 | ev_periodic *w = (ev_periodic *)periodics [i]; |
|
|
1502 | |
|
|
1503 | if (w->reschedule_cb) |
|
|
1504 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
|
|
1505 | else if (w->interval) |
|
|
1506 | ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
1507 | } |
|
|
1508 | |
|
|
1509 | /* now rebuild the heap */ |
|
|
1510 | for (i = periodiccnt >> 1; i--; ) |
|
|
1511 | downheap (periodics, periodiccnt, i); |
|
|
1512 | } |
|
|
1513 | #endif |
|
|
1514 | |
|
|
1515 | #if EV_IDLE_ENABLE |
1573 | #if EV_IDLE_ENABLE |
1516 | void inline_size |
1574 | void inline_size |
1517 | idle_reify (EV_P) |
1575 | idle_reify (EV_P) |
1518 | { |
1576 | { |
1519 | if (expect_false (idleall)) |
1577 | if (expect_false (idleall)) |
… | |
… | |
1530 | queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); |
1588 | queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE); |
1531 | break; |
1589 | break; |
1532 | } |
1590 | } |
1533 | } |
1591 | } |
1534 | } |
1592 | } |
|
|
1593 | } |
|
|
1594 | #endif |
|
|
1595 | |
|
|
1596 | void inline_size |
|
|
1597 | timers_reify (EV_P) |
|
|
1598 | { |
|
|
1599 | while (timercnt && ANHE_at (timers [HEAP0]) <= mn_now) |
|
|
1600 | { |
|
|
1601 | ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]); |
|
|
1602 | |
|
|
1603 | /*assert (("inactive timer on timer heap detected", ev_is_active (w)));*/ |
|
|
1604 | |
|
|
1605 | /* first reschedule or stop timer */ |
|
|
1606 | if (w->repeat) |
|
|
1607 | { |
|
|
1608 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
|
|
1609 | |
|
|
1610 | ev_at (w) += w->repeat; |
|
|
1611 | if (ev_at (w) < mn_now) |
|
|
1612 | ev_at (w) = mn_now; |
|
|
1613 | |
|
|
1614 | downheap (timers, timercnt, HEAP0); |
|
|
1615 | } |
|
|
1616 | else |
|
|
1617 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
|
|
1618 | |
|
|
1619 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
|
|
1620 | } |
|
|
1621 | } |
|
|
1622 | |
|
|
1623 | #if EV_PERIODIC_ENABLE |
|
|
1624 | void inline_size |
|
|
1625 | periodics_reify (EV_P) |
|
|
1626 | { |
|
|
1627 | while (periodiccnt && ANHE_at (periodics [HEAP0]) <= ev_rt_now) |
|
|
1628 | { |
|
|
1629 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); |
|
|
1630 | |
|
|
1631 | /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ |
|
|
1632 | |
|
|
1633 | /* first reschedule or stop timer */ |
|
|
1634 | if (w->reschedule_cb) |
|
|
1635 | { |
|
|
1636 | ev_at (w) = w->reschedule_cb (w, ev_rt_now + TIME_EPSILON); |
|
|
1637 | assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) > ev_rt_now)); |
|
|
1638 | downheap (periodics, periodiccnt, 1); |
|
|
1639 | } |
|
|
1640 | else if (w->interval) |
|
|
1641 | { |
|
|
1642 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
1643 | if (ev_at (w) - ev_rt_now <= TIME_EPSILON) ev_at (w) += w->interval; |
|
|
1644 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ev_at (w) > ev_rt_now)); |
|
|
1645 | downheap (periodics, periodiccnt, HEAP0); |
|
|
1646 | } |
|
|
1647 | else |
|
|
1648 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
|
|
1649 | |
|
|
1650 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
|
|
1651 | } |
|
|
1652 | } |
|
|
1653 | |
|
|
1654 | static void noinline |
|
|
1655 | periodics_reschedule (EV_P) |
|
|
1656 | { |
|
|
1657 | int i; |
|
|
1658 | |
|
|
1659 | /* adjust periodics after time jump */ |
|
|
1660 | for (i = HEAP0; i < periodiccnt + HEAP0; ++i) |
|
|
1661 | { |
|
|
1662 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); |
|
|
1663 | |
|
|
1664 | if (w->reschedule_cb) |
|
|
1665 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
|
|
1666 | else if (w->interval) |
|
|
1667 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
1668 | } |
|
|
1669 | |
|
|
1670 | /* now rebuild the heap, this for the 2-heap, inefficient for the 4-heap, but correct */ |
|
|
1671 | for (i = periodiccnt >> 1; --i; ) |
|
|
1672 | downheap (periodics, periodiccnt, i + HEAP0); |
1535 | } |
1673 | } |
1536 | #endif |
1674 | #endif |
1537 | |
1675 | |
1538 | void inline_speed |
1676 | void inline_speed |
1539 | time_update (EV_P_ ev_tstamp max_block) |
1677 | time_update (EV_P_ ev_tstamp max_block) |
… | |
… | |
1568 | */ |
1706 | */ |
1569 | for (i = 4; --i; ) |
1707 | for (i = 4; --i; ) |
1570 | { |
1708 | { |
1571 | rtmn_diff = ev_rt_now - mn_now; |
1709 | rtmn_diff = ev_rt_now - mn_now; |
1572 | |
1710 | |
1573 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1711 | if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)) |
1574 | return; /* all is well */ |
1712 | return; /* all is well */ |
1575 | |
1713 | |
1576 | ev_rt_now = ev_time (); |
1714 | ev_rt_now = ev_time (); |
1577 | mn_now = get_clock (); |
1715 | mn_now = get_clock (); |
1578 | now_floor = mn_now; |
1716 | now_floor = mn_now; |
… | |
… | |
1594 | #if EV_PERIODIC_ENABLE |
1732 | #if EV_PERIODIC_ENABLE |
1595 | periodics_reschedule (EV_A); |
1733 | periodics_reschedule (EV_A); |
1596 | #endif |
1734 | #endif |
1597 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
1735 | /* adjust timers. this is easy, as the offset is the same for all of them */ |
1598 | for (i = 0; i < timercnt; ++i) |
1736 | for (i = 0; i < timercnt; ++i) |
|
|
1737 | { |
|
|
1738 | ANHE *he = timers + i + HEAP0; |
1599 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1739 | ANHE_w (*he)->at += ev_rt_now - mn_now; |
|
|
1740 | ANHE_at_set (*he); |
|
|
1741 | } |
1600 | } |
1742 | } |
1601 | |
1743 | |
1602 | mn_now = ev_rt_now; |
1744 | mn_now = ev_rt_now; |
1603 | } |
1745 | } |
1604 | } |
1746 | } |
… | |
… | |
1674 | |
1816 | |
1675 | waittime = MAX_BLOCKTIME; |
1817 | waittime = MAX_BLOCKTIME; |
1676 | |
1818 | |
1677 | if (timercnt) |
1819 | if (timercnt) |
1678 | { |
1820 | { |
1679 | ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; |
1821 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; |
1680 | if (waittime > to) waittime = to; |
1822 | if (waittime > to) waittime = to; |
1681 | } |
1823 | } |
1682 | |
1824 | |
1683 | #if EV_PERIODIC_ENABLE |
1825 | #if EV_PERIODIC_ENABLE |
1684 | if (periodiccnt) |
1826 | if (periodiccnt) |
1685 | { |
1827 | { |
1686 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; |
1828 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; |
1687 | if (waittime > to) waittime = to; |
1829 | if (waittime > to) waittime = to; |
1688 | } |
1830 | } |
1689 | #endif |
1831 | #endif |
1690 | |
1832 | |
1691 | if (expect_false (waittime < timeout_blocktime)) |
1833 | if (expect_false (waittime < timeout_blocktime)) |
… | |
… | |
1857 | ev_timer_start (EV_P_ ev_timer *w) |
1999 | ev_timer_start (EV_P_ ev_timer *w) |
1858 | { |
2000 | { |
1859 | if (expect_false (ev_is_active (w))) |
2001 | if (expect_false (ev_is_active (w))) |
1860 | return; |
2002 | return; |
1861 | |
2003 | |
1862 | ((WT)w)->at += mn_now; |
2004 | ev_at (w) += mn_now; |
1863 | |
2005 | |
1864 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
2006 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1865 | |
2007 | |
1866 | ev_start (EV_A_ (W)w, ++timercnt); |
2008 | ev_start (EV_A_ (W)w, ++timercnt + HEAP0 - 1); |
1867 | array_needsize (WT, timers, timermax, timercnt, EMPTY2); |
2009 | array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2); |
1868 | timers [timercnt - 1] = (WT)w; |
2010 | ANHE_w (timers [ev_active (w)]) = (WT)w; |
1869 | upheap (timers, timercnt - 1); |
2011 | ANHE_at_set (timers [ev_active (w)]); |
|
|
2012 | upheap (timers, ev_active (w)); |
1870 | |
2013 | |
1871 | /*assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));*/ |
2014 | /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/ |
1872 | } |
2015 | } |
1873 | |
2016 | |
1874 | void noinline |
2017 | void noinline |
1875 | ev_timer_stop (EV_P_ ev_timer *w) |
2018 | ev_timer_stop (EV_P_ ev_timer *w) |
1876 | { |
2019 | { |
1877 | clear_pending (EV_A_ (W)w); |
2020 | clear_pending (EV_A_ (W)w); |
1878 | if (expect_false (!ev_is_active (w))) |
2021 | if (expect_false (!ev_is_active (w))) |
1879 | return; |
2022 | return; |
1880 | |
2023 | |
1881 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == (WT)w)); |
|
|
1882 | |
|
|
1883 | { |
2024 | { |
1884 | int active = ((W)w)->active; |
2025 | int active = ev_active (w); |
1885 | |
2026 | |
|
|
2027 | assert (("internal timer heap corruption", ANHE_w (timers [active]) == (WT)w)); |
|
|
2028 | |
1886 | if (expect_true (--active < --timercnt)) |
2029 | if (expect_true (active < timercnt + HEAP0 - 1)) |
1887 | { |
2030 | { |
1888 | timers [active] = timers [timercnt]; |
2031 | timers [active] = timers [timercnt + HEAP0 - 1]; |
1889 | adjustheap (timers, timercnt, active); |
2032 | adjustheap (timers, timercnt, active); |
1890 | } |
2033 | } |
|
|
2034 | |
|
|
2035 | --timercnt; |
1891 | } |
2036 | } |
1892 | |
2037 | |
1893 | ((WT)w)->at -= mn_now; |
2038 | ev_at (w) -= mn_now; |
1894 | |
2039 | |
1895 | ev_stop (EV_A_ (W)w); |
2040 | ev_stop (EV_A_ (W)w); |
1896 | } |
2041 | } |
1897 | |
2042 | |
1898 | void noinline |
2043 | void noinline |
… | |
… | |
1900 | { |
2045 | { |
1901 | if (ev_is_active (w)) |
2046 | if (ev_is_active (w)) |
1902 | { |
2047 | { |
1903 | if (w->repeat) |
2048 | if (w->repeat) |
1904 | { |
2049 | { |
1905 | ((WT)w)->at = mn_now + w->repeat; |
2050 | ev_at (w) = mn_now + w->repeat; |
|
|
2051 | ANHE_at_set (timers [ev_active (w)]); |
1906 | adjustheap (timers, timercnt, ((W)w)->active - 1); |
2052 | adjustheap (timers, timercnt, ev_active (w)); |
1907 | } |
2053 | } |
1908 | else |
2054 | else |
1909 | ev_timer_stop (EV_A_ w); |
2055 | ev_timer_stop (EV_A_ w); |
1910 | } |
2056 | } |
1911 | else if (w->repeat) |
2057 | else if (w->repeat) |
1912 | { |
2058 | { |
1913 | w->at = w->repeat; |
2059 | ev_at (w) = w->repeat; |
1914 | ev_timer_start (EV_A_ w); |
2060 | ev_timer_start (EV_A_ w); |
1915 | } |
2061 | } |
1916 | } |
2062 | } |
1917 | |
2063 | |
1918 | #if EV_PERIODIC_ENABLE |
2064 | #if EV_PERIODIC_ENABLE |
… | |
… | |
1921 | { |
2067 | { |
1922 | if (expect_false (ev_is_active (w))) |
2068 | if (expect_false (ev_is_active (w))) |
1923 | return; |
2069 | return; |
1924 | |
2070 | |
1925 | if (w->reschedule_cb) |
2071 | if (w->reschedule_cb) |
1926 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
2072 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
1927 | else if (w->interval) |
2073 | else if (w->interval) |
1928 | { |
2074 | { |
1929 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
2075 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1930 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
2076 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1931 | ((WT)w)->at = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
2077 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
1932 | } |
2078 | } |
1933 | else |
2079 | else |
1934 | ((WT)w)->at = w->offset; |
2080 | ev_at (w) = w->offset; |
1935 | |
2081 | |
1936 | ev_start (EV_A_ (W)w, ++periodiccnt); |
2082 | ev_start (EV_A_ (W)w, ++periodiccnt + HEAP0 - 1); |
1937 | array_needsize (WT, periodics, periodicmax, periodiccnt, EMPTY2); |
2083 | array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2); |
1938 | periodics [periodiccnt - 1] = (WT)w; |
2084 | ANHE_w (periodics [ev_active (w)]) = (WT)w; |
1939 | upheap (periodics, periodiccnt - 1); |
2085 | upheap (periodics, ev_active (w)); |
1940 | |
2086 | |
1941 | /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ |
2087 | /*assert (("internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/ |
1942 | } |
2088 | } |
1943 | |
2089 | |
1944 | void noinline |
2090 | void noinline |
1945 | ev_periodic_stop (EV_P_ ev_periodic *w) |
2091 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1946 | { |
2092 | { |
1947 | clear_pending (EV_A_ (W)w); |
2093 | clear_pending (EV_A_ (W)w); |
1948 | if (expect_false (!ev_is_active (w))) |
2094 | if (expect_false (!ev_is_active (w))) |
1949 | return; |
2095 | return; |
1950 | |
2096 | |
1951 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == (WT)w)); |
|
|
1952 | |
|
|
1953 | { |
2097 | { |
1954 | int active = ((W)w)->active; |
2098 | int active = ev_active (w); |
1955 | |
2099 | |
|
|
2100 | assert (("internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w)); |
|
|
2101 | |
1956 | if (expect_true (--active < --periodiccnt)) |
2102 | if (expect_true (active < periodiccnt + HEAP0 - 1)) |
1957 | { |
2103 | { |
1958 | periodics [active] = periodics [periodiccnt]; |
2104 | periodics [active] = periodics [periodiccnt + HEAP0 - 1]; |
1959 | adjustheap (periodics, periodiccnt, active); |
2105 | adjustheap (periodics, periodiccnt, active); |
1960 | } |
2106 | } |
|
|
2107 | |
|
|
2108 | --periodiccnt; |
1961 | } |
2109 | } |
1962 | |
2110 | |
1963 | ev_stop (EV_A_ (W)w); |
2111 | ev_stop (EV_A_ (W)w); |
1964 | } |
2112 | } |
1965 | |
2113 | |
… | |
… | |
2081 | if (w->wd < 0) |
2229 | if (w->wd < 0) |
2082 | { |
2230 | { |
2083 | ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ |
2231 | ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ |
2084 | |
2232 | |
2085 | /* monitor some parent directory for speedup hints */ |
2233 | /* monitor some parent directory for speedup hints */ |
|
|
2234 | /* note that exceeding the hardcoded limit is not a correctness issue, */ |
|
|
2235 | /* but an efficiency issue only */ |
2086 | if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) |
2236 | if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) |
2087 | { |
2237 | { |
2088 | char path [4096]; |
2238 | char path [4096]; |
2089 | strcpy (path, w->path); |
2239 | strcpy (path, w->path); |
2090 | |
2240 | |
… | |
… | |
2335 | clear_pending (EV_A_ (W)w); |
2485 | clear_pending (EV_A_ (W)w); |
2336 | if (expect_false (!ev_is_active (w))) |
2486 | if (expect_false (!ev_is_active (w))) |
2337 | return; |
2487 | return; |
2338 | |
2488 | |
2339 | { |
2489 | { |
2340 | int active = ((W)w)->active; |
2490 | int active = ev_active (w); |
2341 | |
2491 | |
2342 | idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; |
2492 | idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; |
2343 | ((W)idles [ABSPRI (w)][active - 1])->active = active; |
2493 | ev_active (idles [ABSPRI (w)][active - 1]) = active; |
2344 | |
2494 | |
2345 | ev_stop (EV_A_ (W)w); |
2495 | ev_stop (EV_A_ (W)w); |
2346 | --idleall; |
2496 | --idleall; |
2347 | } |
2497 | } |
2348 | } |
2498 | } |
… | |
… | |
2365 | clear_pending (EV_A_ (W)w); |
2515 | clear_pending (EV_A_ (W)w); |
2366 | if (expect_false (!ev_is_active (w))) |
2516 | if (expect_false (!ev_is_active (w))) |
2367 | return; |
2517 | return; |
2368 | |
2518 | |
2369 | { |
2519 | { |
2370 | int active = ((W)w)->active; |
2520 | int active = ev_active (w); |
|
|
2521 | |
2371 | prepares [active - 1] = prepares [--preparecnt]; |
2522 | prepares [active - 1] = prepares [--preparecnt]; |
2372 | ((W)prepares [active - 1])->active = active; |
2523 | ev_active (prepares [active - 1]) = active; |
2373 | } |
2524 | } |
2374 | |
2525 | |
2375 | ev_stop (EV_A_ (W)w); |
2526 | ev_stop (EV_A_ (W)w); |
2376 | } |
2527 | } |
2377 | |
2528 | |
… | |
… | |
2392 | clear_pending (EV_A_ (W)w); |
2543 | clear_pending (EV_A_ (W)w); |
2393 | if (expect_false (!ev_is_active (w))) |
2544 | if (expect_false (!ev_is_active (w))) |
2394 | return; |
2545 | return; |
2395 | |
2546 | |
2396 | { |
2547 | { |
2397 | int active = ((W)w)->active; |
2548 | int active = ev_active (w); |
|
|
2549 | |
2398 | checks [active - 1] = checks [--checkcnt]; |
2550 | checks [active - 1] = checks [--checkcnt]; |
2399 | ((W)checks [active - 1])->active = active; |
2551 | ev_active (checks [active - 1]) = active; |
2400 | } |
2552 | } |
2401 | |
2553 | |
2402 | ev_stop (EV_A_ (W)w); |
2554 | ev_stop (EV_A_ (W)w); |
2403 | } |
2555 | } |
2404 | |
2556 | |
… | |
… | |
2500 | clear_pending (EV_A_ (W)w); |
2652 | clear_pending (EV_A_ (W)w); |
2501 | if (expect_false (!ev_is_active (w))) |
2653 | if (expect_false (!ev_is_active (w))) |
2502 | return; |
2654 | return; |
2503 | |
2655 | |
2504 | { |
2656 | { |
2505 | int active = ((W)w)->active; |
2657 | int active = ev_active (w); |
|
|
2658 | |
2506 | forks [active - 1] = forks [--forkcnt]; |
2659 | forks [active - 1] = forks [--forkcnt]; |
2507 | ((W)forks [active - 1])->active = active; |
2660 | ev_active (forks [active - 1]) = active; |
2508 | } |
2661 | } |
2509 | |
2662 | |
2510 | ev_stop (EV_A_ (W)w); |
2663 | ev_stop (EV_A_ (W)w); |
2511 | } |
2664 | } |
2512 | #endif |
2665 | #endif |
… | |
… | |
2531 | clear_pending (EV_A_ (W)w); |
2684 | clear_pending (EV_A_ (W)w); |
2532 | if (expect_false (!ev_is_active (w))) |
2685 | if (expect_false (!ev_is_active (w))) |
2533 | return; |
2686 | return; |
2534 | |
2687 | |
2535 | { |
2688 | { |
2536 | int active = ((W)w)->active; |
2689 | int active = ev_active (w); |
|
|
2690 | |
2537 | asyncs [active - 1] = asyncs [--asynccnt]; |
2691 | asyncs [active - 1] = asyncs [--asynccnt]; |
2538 | ((W)asyncs [active - 1])->active = active; |
2692 | ev_active (asyncs [active - 1]) = active; |
2539 | } |
2693 | } |
2540 | |
2694 | |
2541 | ev_stop (EV_A_ (W)w); |
2695 | ev_stop (EV_A_ (W)w); |
2542 | } |
2696 | } |
2543 | |
2697 | |