… | |
… | |
4 | * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
10 | * 1. Redistributions of source code must retain the above copyright notice, |
11 | * this list of conditions and the following disclaimer. |
11 | * this list of conditions and the following disclaimer. |
12 | * |
12 | * |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
15 | * documentation and/or other materials provided with the distribution. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
… | |
… | |
42 | # ifdef EV_CONFIG_H |
42 | # ifdef EV_CONFIG_H |
43 | # include EV_CONFIG_H |
43 | # include EV_CONFIG_H |
44 | # else |
44 | # else |
45 | # include "config.h" |
45 | # include "config.h" |
46 | # endif |
46 | # endif |
|
|
47 | |
|
|
48 | #if HAVE_FLOOR |
|
|
49 | # ifndef EV_USE_FLOOR |
|
|
50 | # define EV_USE_FLOOR 1 |
|
|
51 | # endif |
|
|
52 | #endif |
47 | |
53 | |
48 | # if HAVE_CLOCK_SYSCALL |
54 | # if HAVE_CLOCK_SYSCALL |
49 | # ifndef EV_USE_CLOCK_SYSCALL |
55 | # ifndef EV_USE_CLOCK_SYSCALL |
50 | # define EV_USE_CLOCK_SYSCALL 1 |
56 | # define EV_USE_CLOCK_SYSCALL 1 |
51 | # ifndef EV_USE_REALTIME |
57 | # ifndef EV_USE_REALTIME |
… | |
… | |
156 | # define EV_USE_EVENTFD 0 |
162 | # define EV_USE_EVENTFD 0 |
157 | # endif |
163 | # endif |
158 | |
164 | |
159 | #endif |
165 | #endif |
160 | |
166 | |
161 | #include <math.h> |
|
|
162 | #include <stdlib.h> |
167 | #include <stdlib.h> |
163 | #include <string.h> |
168 | #include <string.h> |
164 | #include <fcntl.h> |
169 | #include <fcntl.h> |
165 | #include <stddef.h> |
170 | #include <stddef.h> |
166 | |
171 | |
… | |
… | |
232 | /* to make it compile regardless, just remove the above line, */ |
237 | /* to make it compile regardless, just remove the above line, */ |
233 | /* but consider reporting it, too! :) */ |
238 | /* but consider reporting it, too! :) */ |
234 | # define EV_NSIG 65 |
239 | # define EV_NSIG 65 |
235 | #endif |
240 | #endif |
236 | |
241 | |
|
|
242 | #ifndef EV_USE_FLOOR |
|
|
243 | # define EV_USE_FLOOR 0 |
|
|
244 | #endif |
|
|
245 | |
237 | #ifndef EV_USE_CLOCK_SYSCALL |
246 | #ifndef EV_USE_CLOCK_SYSCALL |
238 | # if __linux && __GLIBC__ >= 2 |
247 | # if __linux && __GLIBC__ >= 2 |
239 | # define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS |
248 | # define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS |
240 | # else |
249 | # else |
241 | # define EV_USE_CLOCK_SYSCALL 0 |
250 | # define EV_USE_CLOCK_SYSCALL 0 |
… | |
… | |
376 | # undef EV_USE_INOTIFY |
385 | # undef EV_USE_INOTIFY |
377 | # define EV_USE_INOTIFY 0 |
386 | # define EV_USE_INOTIFY 0 |
378 | #endif |
387 | #endif |
379 | |
388 | |
380 | #if !EV_USE_NANOSLEEP |
389 | #if !EV_USE_NANOSLEEP |
381 | # ifndef _WIN32 |
390 | /* hp-ux has it in sys/time.h, which we unconditionally include above */ |
|
|
391 | # if !defined(_WIN32) && !defined(__hpux) |
382 | # include <sys/select.h> |
392 | # include <sys/select.h> |
383 | # endif |
393 | # endif |
384 | #endif |
394 | #endif |
385 | |
395 | |
386 | #if EV_USE_INOTIFY |
396 | #if EV_USE_INOTIFY |
… | |
… | |
442 | #else |
452 | #else |
443 | # define EV_FREQUENT_CHECK do { } while (0) |
453 | # define EV_FREQUENT_CHECK do { } while (0) |
444 | #endif |
454 | #endif |
445 | |
455 | |
446 | /* |
456 | /* |
447 | * This is used to avoid floating point rounding problems. |
457 | * This is used to work around floating point rounding problems. |
448 | * It is added to ev_rt_now when scheduling periodics |
|
|
449 | * to ensure progress, time-wise, even when rounding |
|
|
450 | * errors are against us. |
|
|
451 | * This value is good at least till the year 4000. |
458 | * This value is good at least till the year 4000. |
452 | * Better solutions welcome. |
|
|
453 | */ |
459 | */ |
454 | #define TIME_EPSILON 0.0001220703125 /* 1/8192 */ |
460 | #define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */ |
|
|
461 | /*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */ |
455 | |
462 | |
456 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
463 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
457 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
464 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
458 | |
465 | |
459 | #define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) |
466 | #define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) |
460 | #define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) |
467 | #define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) |
461 | |
468 | |
462 | #if __GNUC__ >= 4 |
469 | /* the following are taken from libecb */ |
463 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
470 | /* ecb.h start */ |
464 | # define noinline __attribute__ ((noinline)) |
471 | |
|
|
472 | /* many compilers define _GNUC_ to some versions but then only implement |
|
|
473 | * what their idiot authors think are the "more important" extensions, |
|
|
474 | * causing enourmous grief in return for some better fake benchmark numbers. |
|
|
475 | * or so. |
|
|
476 | * we try to detect these and simply assume they are not gcc - if they have |
|
|
477 | * an issue with that they should have done it right in the first place. |
|
|
478 | */ |
|
|
479 | #ifndef ECB_GCC_VERSION |
|
|
480 | #if !defined(__GNUC_MINOR__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__llvm__) || defined(__clang__) |
|
|
481 | #define ECB_GCC_VERSION(major,minor) 0 |
|
|
482 | #else |
|
|
483 | #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) |
|
|
484 | #endif |
|
|
485 | #endif |
|
|
486 | |
|
|
487 | #if __cplusplus |
|
|
488 | #define ecb_inline static inline |
|
|
489 | #elif ECB_GCC_VERSION(2,5) |
|
|
490 | #define ecb_inline static __inline__ |
|
|
491 | #elif ECB_C99 |
|
|
492 | #define ecb_inline static inline |
465 | #else |
493 | #else |
466 | # define expect(expr,value) (expr) |
494 | #define ecb_inline static |
467 | # define noinline |
|
|
468 | # if __STDC_VERSION__ < 199901L && __GNUC__ < 2 |
|
|
469 | # define inline |
|
|
470 | # endif |
495 | #endif |
|
|
496 | |
|
|
497 | #ifndef ECB_MEMORY_FENCE |
|
|
498 | #if ECB_GCC_VERSION(2,5) |
|
|
499 | #if __x86 |
|
|
500 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") |
|
|
501 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
502 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE /* better be safe than sorry */ |
|
|
503 | #elif __amd64 |
|
|
504 | #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") |
|
|
505 | #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") |
|
|
506 | #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") |
|
|
507 | #endif |
471 | #endif |
508 | #endif |
|
|
509 | #endif |
472 | |
510 | |
|
|
511 | #ifndef ECB_MEMORY_FENCE |
|
|
512 | #if ECB_GCC_VERSION(4,4) |
|
|
513 | #define ECB_MEMORY_FENCE __sync_synchronize () |
|
|
514 | #define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) |
|
|
515 | #define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) |
|
|
516 | #elif _MSC_VER >= 1400 /* VC++ 2005 */ |
|
|
517 | #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) |
|
|
518 | #define ECB_MEMORY_FENCE _ReadWriteBarrier () |
|
|
519 | #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ |
|
|
520 | #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () |
|
|
521 | #elif defined(_WIN32) |
|
|
522 | #include <WinNT.h> |
|
|
523 | #define ECB_MEMORY_FENCE MemoryBarrier () |
|
|
524 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
525 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
526 | #endif |
|
|
527 | #endif |
|
|
528 | |
|
|
529 | #ifndef ECB_MEMORY_FENCE |
|
|
530 | #include <pthread.h> |
|
|
531 | |
|
|
532 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
|
|
533 | #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) |
|
|
534 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
535 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
536 | #endif |
|
|
537 | |
|
|
538 | #if ECB_GCC_VERSION(3,1) |
|
|
539 | #define ecb_attribute(attrlist) __attribute__(attrlist) |
|
|
540 | #define ecb_is_constant(expr) __builtin_constant_p (expr) |
|
|
541 | #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
542 | #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
|
|
543 | #else |
|
|
544 | #define ecb_attribute(attrlist) |
|
|
545 | #define ecb_is_constant(expr) 0 |
|
|
546 | #define ecb_expect(expr,value) (expr) |
|
|
547 | #define ecb_prefetch(addr,rw,locality) |
|
|
548 | #endif |
|
|
549 | |
|
|
550 | #define ecb_noinline ecb_attribute ((__noinline__)) |
|
|
551 | #define ecb_noreturn ecb_attribute ((__noreturn__)) |
|
|
552 | #define ecb_unused ecb_attribute ((__unused__)) |
|
|
553 | #define ecb_const ecb_attribute ((__const__)) |
|
|
554 | #define ecb_pure ecb_attribute ((__pure__)) |
|
|
555 | |
|
|
556 | #if ECB_GCC_VERSION(4,3) |
|
|
557 | #define ecb_artificial ecb_attribute ((__artificial__)) |
|
|
558 | #define ecb_hot ecb_attribute ((__hot__)) |
|
|
559 | #define ecb_cold ecb_attribute ((__cold__)) |
|
|
560 | #else |
|
|
561 | #define ecb_artificial |
|
|
562 | #define ecb_hot |
|
|
563 | #define ecb_cold |
|
|
564 | #endif |
|
|
565 | |
|
|
566 | /* put around conditional expressions if you are very sure that the */ |
|
|
567 | /* expression is mostly true or mostly false. note that these return */ |
|
|
568 | /* booleans, not the expression. */ |
473 | #define expect_false(expr) expect ((expr) != 0, 0) |
569 | #define ecb_expect_false(expr) ecb_expect (!!(expr), 0) |
474 | #define expect_true(expr) expect ((expr) != 0, 1) |
570 | #define ecb_expect_true(expr) ecb_expect (!!(expr), 1) |
|
|
571 | /* ecb.h end */ |
|
|
572 | |
|
|
573 | #define expect_false(cond) ecb_expect_false (cond) |
|
|
574 | #define expect_true(cond) ecb_expect_true (cond) |
|
|
575 | #define noinline ecb_noinline |
|
|
576 | |
475 | #define inline_size static inline |
577 | #define inline_size ecb_inline |
476 | |
578 | |
477 | #if EV_FEATURE_CODE |
579 | #if EV_FEATURE_CODE |
478 | # define inline_speed static inline |
580 | # define inline_speed ecb_inline |
479 | #else |
581 | #else |
480 | # define inline_speed static noinline |
582 | # define inline_speed static noinline |
481 | #endif |
583 | #endif |
482 | |
584 | |
483 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
585 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
… | |
… | |
522 | # include "ev_win32.c" |
624 | # include "ev_win32.c" |
523 | #endif |
625 | #endif |
524 | |
626 | |
525 | /*****************************************************************************/ |
627 | /*****************************************************************************/ |
526 | |
628 | |
|
|
629 | /* define a suitable floor function (only used by periodics atm) */ |
|
|
630 | |
|
|
631 | #if EV_USE_FLOOR |
|
|
632 | # include <math.h> |
|
|
633 | # define ev_floor(v) floor (v) |
|
|
634 | #else |
|
|
635 | |
|
|
636 | #include <float.h> |
|
|
637 | |
|
|
638 | /* a floor() replacement function, should be independent of ev_tstamp type */ |
|
|
639 | static ev_tstamp noinline |
|
|
640 | ev_floor (ev_tstamp v) |
|
|
641 | { |
|
|
642 | /* the choice of shift factor is not terribly important */ |
|
|
643 | #if FLT_RADIX != 2 /* assume FLT_RADIX == 10 */ |
|
|
644 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.; |
|
|
645 | #else |
|
|
646 | const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.; |
|
|
647 | #endif |
|
|
648 | |
|
|
649 | /* argument too large for an unsigned long? */ |
|
|
650 | if (expect_false (v >= shift)) |
|
|
651 | { |
|
|
652 | ev_tstamp f; |
|
|
653 | |
|
|
654 | if (v == v - 1.) |
|
|
655 | return v; /* very large number */ |
|
|
656 | |
|
|
657 | f = shift * ev_floor (v * (1. / shift)); |
|
|
658 | return f + ev_floor (v - f); |
|
|
659 | } |
|
|
660 | |
|
|
661 | /* special treatment for negative args? */ |
|
|
662 | if (expect_false (v < 0.)) |
|
|
663 | { |
|
|
664 | ev_tstamp f = -ev_floor (-v); |
|
|
665 | |
|
|
666 | return f - (f == v ? 0 : 1); |
|
|
667 | } |
|
|
668 | |
|
|
669 | /* fits into an unsigned long */ |
|
|
670 | return (unsigned long)v; |
|
|
671 | } |
|
|
672 | |
|
|
673 | #endif |
|
|
674 | |
|
|
675 | /*****************************************************************************/ |
|
|
676 | |
527 | #ifdef __linux |
677 | #ifdef __linux |
528 | # include <sys/utsname.h> |
678 | # include <sys/utsname.h> |
529 | #endif |
679 | #endif |
530 | |
680 | |
531 | static unsigned int noinline |
681 | static unsigned int noinline ecb_cold |
532 | ev_linux_version (void) |
682 | ev_linux_version (void) |
533 | { |
683 | { |
534 | #ifdef __linux |
684 | #ifdef __linux |
535 | unsigned int v = 0; |
685 | unsigned int v = 0; |
536 | struct utsname buf; |
686 | struct utsname buf; |
… | |
… | |
565 | } |
715 | } |
566 | |
716 | |
567 | /*****************************************************************************/ |
717 | /*****************************************************************************/ |
568 | |
718 | |
569 | #if EV_AVOID_STDIO |
719 | #if EV_AVOID_STDIO |
570 | static void noinline |
720 | static void noinline ecb_cold |
571 | ev_printerr (const char *msg) |
721 | ev_printerr (const char *msg) |
572 | { |
722 | { |
573 | write (STDERR_FILENO, msg, strlen (msg)); |
723 | write (STDERR_FILENO, msg, strlen (msg)); |
574 | } |
724 | } |
575 | #endif |
725 | #endif |
576 | |
726 | |
577 | static void (*syserr_cb)(const char *msg); |
727 | static void (*syserr_cb)(const char *msg); |
578 | |
728 | |
579 | void |
729 | void ecb_cold |
580 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
730 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
581 | { |
731 | { |
582 | syserr_cb = cb; |
732 | syserr_cb = cb; |
583 | } |
733 | } |
584 | |
734 | |
585 | static void noinline |
735 | static void noinline ecb_cold |
586 | ev_syserr (const char *msg) |
736 | ev_syserr (const char *msg) |
587 | { |
737 | { |
588 | if (!msg) |
738 | if (!msg) |
589 | msg = "(libev) system error"; |
739 | msg = "(libev) system error"; |
590 | |
740 | |
… | |
… | |
623 | #endif |
773 | #endif |
624 | } |
774 | } |
625 | |
775 | |
626 | static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; |
776 | static void *(*alloc)(void *ptr, long size) = ev_realloc_emul; |
627 | |
777 | |
628 | void |
778 | void ecb_cold |
629 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
779 | ev_set_allocator (void *(*cb)(void *ptr, long size)) |
630 | { |
780 | { |
631 | alloc = cb; |
781 | alloc = cb; |
632 | } |
782 | } |
633 | |
783 | |
… | |
… | |
815 | select (0, 0, 0, 0, &tv); |
965 | select (0, 0, 0, 0, &tv); |
816 | #endif |
966 | #endif |
817 | } |
967 | } |
818 | } |
968 | } |
819 | |
969 | |
820 | inline_speed int |
|
|
821 | ev_timeout_to_ms (ev_tstamp timeout) |
|
|
822 | { |
|
|
823 | int ms = timeout * 1000. + .999999; |
|
|
824 | |
|
|
825 | return expect_true (ms) ? ms : timeout < 1e-6 ? 0 : 1; |
|
|
826 | } |
|
|
827 | |
|
|
828 | /*****************************************************************************/ |
970 | /*****************************************************************************/ |
829 | |
971 | |
830 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
972 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
831 | |
973 | |
832 | /* find a suitable new size for the given array, */ |
974 | /* find a suitable new size for the given array, */ |
… | |
… | |
850 | } |
992 | } |
851 | |
993 | |
852 | return ncur; |
994 | return ncur; |
853 | } |
995 | } |
854 | |
996 | |
855 | static noinline void * |
997 | static void * noinline ecb_cold |
856 | array_realloc (int elem, void *base, int *cur, int cnt) |
998 | array_realloc (int elem, void *base, int *cur, int cnt) |
857 | { |
999 | { |
858 | *cur = array_nextsize (elem, *cur, cnt); |
1000 | *cur = array_nextsize (elem, *cur, cnt); |
859 | return ev_realloc (base, elem * *cur); |
1001 | return ev_realloc (base, elem * *cur); |
860 | } |
1002 | } |
… | |
… | |
863 | memset ((void *)(base), 0, sizeof (*(base)) * (count)) |
1005 | memset ((void *)(base), 0, sizeof (*(base)) * (count)) |
864 | |
1006 | |
865 | #define array_needsize(type,base,cur,cnt,init) \ |
1007 | #define array_needsize(type,base,cur,cnt,init) \ |
866 | if (expect_false ((cnt) > (cur))) \ |
1008 | if (expect_false ((cnt) > (cur))) \ |
867 | { \ |
1009 | { \ |
868 | int ocur_ = (cur); \ |
1010 | int ecb_unused ocur_ = (cur); \ |
869 | (base) = (type *)array_realloc \ |
1011 | (base) = (type *)array_realloc \ |
870 | (sizeof (type), (base), &(cur), (cnt)); \ |
1012 | (sizeof (type), (base), &(cur), (cnt)); \ |
871 | init ((base) + (ocur_), (cur) - ocur_); \ |
1013 | init ((base) + (ocur_), (cur) - ocur_); \ |
872 | } |
1014 | } |
873 | |
1015 | |
… | |
… | |
973 | inline_size void |
1115 | inline_size void |
974 | fd_reify (EV_P) |
1116 | fd_reify (EV_P) |
975 | { |
1117 | { |
976 | int i; |
1118 | int i; |
977 | |
1119 | |
|
|
1120 | #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP |
|
|
1121 | for (i = 0; i < fdchangecnt; ++i) |
|
|
1122 | { |
|
|
1123 | int fd = fdchanges [i]; |
|
|
1124 | ANFD *anfd = anfds + fd; |
|
|
1125 | |
|
|
1126 | if (anfd->reify & EV__IOFDSET && anfd->head) |
|
|
1127 | { |
|
|
1128 | SOCKET handle = EV_FD_TO_WIN32_HANDLE (fd); |
|
|
1129 | |
|
|
1130 | if (handle != anfd->handle) |
|
|
1131 | { |
|
|
1132 | unsigned long arg; |
|
|
1133 | |
|
|
1134 | assert (("libev: only socket fds supported in this configuration", ioctlsocket (handle, FIONREAD, &arg) == 0)); |
|
|
1135 | |
|
|
1136 | /* handle changed, but fd didn't - we need to do it in two steps */ |
|
|
1137 | backend_modify (EV_A_ fd, anfd->events, 0); |
|
|
1138 | anfd->events = 0; |
|
|
1139 | anfd->handle = handle; |
|
|
1140 | } |
|
|
1141 | } |
|
|
1142 | } |
|
|
1143 | #endif |
|
|
1144 | |
978 | for (i = 0; i < fdchangecnt; ++i) |
1145 | for (i = 0; i < fdchangecnt; ++i) |
979 | { |
1146 | { |
980 | int fd = fdchanges [i]; |
1147 | int fd = fdchanges [i]; |
981 | ANFD *anfd = anfds + fd; |
1148 | ANFD *anfd = anfds + fd; |
982 | ev_io *w; |
1149 | ev_io *w; |
… | |
… | |
984 | unsigned char o_events = anfd->events; |
1151 | unsigned char o_events = anfd->events; |
985 | unsigned char o_reify = anfd->reify; |
1152 | unsigned char o_reify = anfd->reify; |
986 | |
1153 | |
987 | anfd->reify = 0; |
1154 | anfd->reify = 0; |
988 | |
1155 | |
989 | #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP |
|
|
990 | if (o_reify & EV__IOFDSET) |
|
|
991 | { |
|
|
992 | unsigned long arg; |
|
|
993 | anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); |
|
|
994 | assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); |
|
|
995 | printf ("oi %d %x\n", fd, anfd->handle);//D |
|
|
996 | } |
|
|
997 | #endif |
|
|
998 | |
|
|
999 | /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */ |
1156 | /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */ |
1000 | { |
1157 | { |
1001 | anfd->events = 0; |
1158 | anfd->events = 0; |
1002 | |
1159 | |
1003 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
1160 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
… | |
… | |
1028 | fdchanges [fdchangecnt - 1] = fd; |
1185 | fdchanges [fdchangecnt - 1] = fd; |
1029 | } |
1186 | } |
1030 | } |
1187 | } |
1031 | |
1188 | |
1032 | /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ |
1189 | /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */ |
1033 | inline_speed void |
1190 | inline_speed void ecb_cold |
1034 | fd_kill (EV_P_ int fd) |
1191 | fd_kill (EV_P_ int fd) |
1035 | { |
1192 | { |
1036 | ev_io *w; |
1193 | ev_io *w; |
1037 | |
1194 | |
1038 | while ((w = (ev_io *)anfds [fd].head)) |
1195 | while ((w = (ev_io *)anfds [fd].head)) |
… | |
… | |
1041 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
1198 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
1042 | } |
1199 | } |
1043 | } |
1200 | } |
1044 | |
1201 | |
1045 | /* check whether the given fd is actually valid, for error recovery */ |
1202 | /* check whether the given fd is actually valid, for error recovery */ |
1046 | inline_size int |
1203 | inline_size int ecb_cold |
1047 | fd_valid (int fd) |
1204 | fd_valid (int fd) |
1048 | { |
1205 | { |
1049 | #ifdef _WIN32 |
1206 | #ifdef _WIN32 |
1050 | return EV_FD_TO_WIN32_HANDLE (fd) != -1; |
1207 | return EV_FD_TO_WIN32_HANDLE (fd) != -1; |
1051 | #else |
1208 | #else |
1052 | return fcntl (fd, F_GETFD) != -1; |
1209 | return fcntl (fd, F_GETFD) != -1; |
1053 | #endif |
1210 | #endif |
1054 | } |
1211 | } |
1055 | |
1212 | |
1056 | /* called on EBADF to verify fds */ |
1213 | /* called on EBADF to verify fds */ |
1057 | static void noinline |
1214 | static void noinline ecb_cold |
1058 | fd_ebadf (EV_P) |
1215 | fd_ebadf (EV_P) |
1059 | { |
1216 | { |
1060 | int fd; |
1217 | int fd; |
1061 | |
1218 | |
1062 | for (fd = 0; fd < anfdmax; ++fd) |
1219 | for (fd = 0; fd < anfdmax; ++fd) |
… | |
… | |
1064 | if (!fd_valid (fd) && errno == EBADF) |
1221 | if (!fd_valid (fd) && errno == EBADF) |
1065 | fd_kill (EV_A_ fd); |
1222 | fd_kill (EV_A_ fd); |
1066 | } |
1223 | } |
1067 | |
1224 | |
1068 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
1225 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
1069 | static void noinline |
1226 | static void noinline ecb_cold |
1070 | fd_enomem (EV_P) |
1227 | fd_enomem (EV_P) |
1071 | { |
1228 | { |
1072 | int fd; |
1229 | int fd; |
1073 | |
1230 | |
1074 | for (fd = anfdmax; fd--; ) |
1231 | for (fd = anfdmax; fd--; ) |
… | |
… | |
1269 | |
1426 | |
1270 | /*****************************************************************************/ |
1427 | /*****************************************************************************/ |
1271 | |
1428 | |
1272 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
1429 | #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE |
1273 | |
1430 | |
1274 | static void noinline |
1431 | static void noinline ecb_cold |
1275 | evpipe_init (EV_P) |
1432 | evpipe_init (EV_P) |
1276 | { |
1433 | { |
1277 | if (!ev_is_active (&pipe_w)) |
1434 | if (!ev_is_active (&pipe_w)) |
1278 | { |
1435 | { |
1279 | # if EV_USE_EVENTFD |
1436 | # if EV_USE_EVENTFD |
… | |
… | |
1301 | ev_io_start (EV_A_ &pipe_w); |
1458 | ev_io_start (EV_A_ &pipe_w); |
1302 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1459 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
1303 | } |
1460 | } |
1304 | } |
1461 | } |
1305 | |
1462 | |
1306 | inline_size void |
1463 | inline_speed void |
1307 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1464 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1308 | { |
1465 | { |
1309 | if (!*flag) |
1466 | if (expect_true (*flag)) |
|
|
1467 | return; |
|
|
1468 | |
|
|
1469 | *flag = 1; |
|
|
1470 | |
|
|
1471 | ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */ |
|
|
1472 | |
|
|
1473 | pipe_write_skipped = 1; |
|
|
1474 | |
|
|
1475 | ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */ |
|
|
1476 | |
|
|
1477 | if (pipe_write_wanted) |
1310 | { |
1478 | { |
|
|
1479 | int old_errno; |
|
|
1480 | |
|
|
1481 | pipe_write_skipped = 0; /* just an optimsiation, no fence needed */ |
|
|
1482 | |
1311 | int old_errno = errno; /* save errno because write might clobber it */ |
1483 | old_errno = errno; /* save errno because write will clobber it */ |
1312 | char dummy; |
|
|
1313 | |
|
|
1314 | *flag = 1; |
|
|
1315 | |
1484 | |
1316 | #if EV_USE_EVENTFD |
1485 | #if EV_USE_EVENTFD |
1317 | if (evfd >= 0) |
1486 | if (evfd >= 0) |
1318 | { |
1487 | { |
1319 | uint64_t counter = 1; |
1488 | uint64_t counter = 1; |
1320 | write (evfd, &counter, sizeof (uint64_t)); |
1489 | write (evfd, &counter, sizeof (uint64_t)); |
1321 | } |
1490 | } |
1322 | else |
1491 | else |
1323 | #endif |
1492 | #endif |
|
|
1493 | { |
1324 | /* win32 people keep sending patches that change this write() to send() */ |
1494 | /* win32 people keep sending patches that change this write() to send() */ |
1325 | /* and then run away. but send() is wrong, it wants a socket handle on win32 */ |
1495 | /* and then run away. but send() is wrong, it wants a socket handle on win32 */ |
1326 | /* so when you think this write should be a send instead, please find out */ |
1496 | /* so when you think this write should be a send instead, please find out */ |
1327 | /* where your send() is from - it's definitely not the microsoft send, and */ |
1497 | /* where your send() is from - it's definitely not the microsoft send, and */ |
1328 | /* tell me. thank you. */ |
1498 | /* tell me. thank you. */ |
1329 | write (evpipe [1], &dummy, 1); |
1499 | write (evpipe [1], &(evpipe [1]), 1); |
|
|
1500 | } |
1330 | |
1501 | |
1331 | errno = old_errno; |
1502 | errno = old_errno; |
1332 | } |
1503 | } |
1333 | } |
1504 | } |
1334 | |
1505 | |
… | |
… | |
1337 | static void |
1508 | static void |
1338 | pipecb (EV_P_ ev_io *iow, int revents) |
1509 | pipecb (EV_P_ ev_io *iow, int revents) |
1339 | { |
1510 | { |
1340 | int i; |
1511 | int i; |
1341 | |
1512 | |
|
|
1513 | if (revents & EV_READ) |
|
|
1514 | { |
1342 | #if EV_USE_EVENTFD |
1515 | #if EV_USE_EVENTFD |
1343 | if (evfd >= 0) |
1516 | if (evfd >= 0) |
1344 | { |
1517 | { |
1345 | uint64_t counter; |
1518 | uint64_t counter; |
1346 | read (evfd, &counter, sizeof (uint64_t)); |
1519 | read (evfd, &counter, sizeof (uint64_t)); |
1347 | } |
1520 | } |
1348 | else |
1521 | else |
1349 | #endif |
1522 | #endif |
1350 | { |
1523 | { |
1351 | char dummy; |
1524 | char dummy; |
1352 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1525 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1353 | read (evpipe [0], &dummy, 1); |
1526 | read (evpipe [0], &dummy, 1); |
|
|
1527 | } |
1354 | } |
1528 | } |
|
|
1529 | |
|
|
1530 | pipe_write_skipped = 0; |
1355 | |
1531 | |
1356 | #if EV_SIGNAL_ENABLE |
1532 | #if EV_SIGNAL_ENABLE |
1357 | if (sig_pending) |
1533 | if (sig_pending) |
1358 | { |
1534 | { |
1359 | sig_pending = 0; |
1535 | sig_pending = 0; |
1360 | |
1536 | |
1361 | for (i = EV_NSIG - 1; i--; ) |
1537 | for (i = EV_NSIG - 1; i--; ) |
1362 | if (expect_false (signals [i].pending)) |
1538 | if (expect_false (signals [i].pending)) |
1363 | ev_feed_signal_event (EV_A_ i + 1); |
1539 | ev_feed_signal_event (EV_A_ i + 1); |
… | |
… | |
1388 | EV_P = signals [signum - 1].loop; |
1564 | EV_P = signals [signum - 1].loop; |
1389 | |
1565 | |
1390 | if (!EV_A) |
1566 | if (!EV_A) |
1391 | return; |
1567 | return; |
1392 | #endif |
1568 | #endif |
|
|
1569 | |
|
|
1570 | if (!ev_active (&pipe_w)) |
|
|
1571 | return; |
1393 | |
1572 | |
1394 | signals [signum - 1].pending = 1; |
1573 | signals [signum - 1].pending = 1; |
1395 | evpipe_write (EV_A_ &sig_pending); |
1574 | evpipe_write (EV_A_ &sig_pending); |
1396 | } |
1575 | } |
1397 | |
1576 | |
… | |
… | |
1529 | #endif |
1708 | #endif |
1530 | #if EV_USE_SELECT |
1709 | #if EV_USE_SELECT |
1531 | # include "ev_select.c" |
1710 | # include "ev_select.c" |
1532 | #endif |
1711 | #endif |
1533 | |
1712 | |
1534 | int |
1713 | int ecb_cold |
1535 | ev_version_major (void) |
1714 | ev_version_major (void) |
1536 | { |
1715 | { |
1537 | return EV_VERSION_MAJOR; |
1716 | return EV_VERSION_MAJOR; |
1538 | } |
1717 | } |
1539 | |
1718 | |
1540 | int |
1719 | int ecb_cold |
1541 | ev_version_minor (void) |
1720 | ev_version_minor (void) |
1542 | { |
1721 | { |
1543 | return EV_VERSION_MINOR; |
1722 | return EV_VERSION_MINOR; |
1544 | } |
1723 | } |
1545 | |
1724 | |
1546 | /* return true if we are running with elevated privileges and should ignore env variables */ |
1725 | /* return true if we are running with elevated privileges and should ignore env variables */ |
1547 | int inline_size |
1726 | int inline_size ecb_cold |
1548 | enable_secure (void) |
1727 | enable_secure (void) |
1549 | { |
1728 | { |
1550 | #ifdef _WIN32 |
1729 | #ifdef _WIN32 |
1551 | return 0; |
1730 | return 0; |
1552 | #else |
1731 | #else |
1553 | return getuid () != geteuid () |
1732 | return getuid () != geteuid () |
1554 | || getgid () != getegid (); |
1733 | || getgid () != getegid (); |
1555 | #endif |
1734 | #endif |
1556 | } |
1735 | } |
1557 | |
1736 | |
1558 | unsigned int |
1737 | unsigned int ecb_cold |
1559 | ev_supported_backends (void) |
1738 | ev_supported_backends (void) |
1560 | { |
1739 | { |
1561 | unsigned int flags = 0; |
1740 | unsigned int flags = 0; |
1562 | |
1741 | |
1563 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
1742 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
… | |
… | |
1567 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
1746 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
1568 | |
1747 | |
1569 | return flags; |
1748 | return flags; |
1570 | } |
1749 | } |
1571 | |
1750 | |
1572 | unsigned int |
1751 | unsigned int ecb_cold |
1573 | ev_recommended_backends (void) |
1752 | ev_recommended_backends (void) |
1574 | { |
1753 | { |
1575 | unsigned int flags = ev_supported_backends (); |
1754 | unsigned int flags = ev_supported_backends (); |
1576 | |
1755 | |
1577 | #ifndef __NetBSD__ |
1756 | #ifndef __NetBSD__ |
… | |
… | |
1589 | #endif |
1768 | #endif |
1590 | |
1769 | |
1591 | return flags; |
1770 | return flags; |
1592 | } |
1771 | } |
1593 | |
1772 | |
1594 | unsigned int |
1773 | unsigned int ecb_cold |
1595 | ev_embeddable_backends (void) |
1774 | ev_embeddable_backends (void) |
1596 | { |
1775 | { |
1597 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
1776 | int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; |
1598 | |
1777 | |
1599 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
1778 | /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ |
… | |
… | |
1644 | ev_userdata (EV_P) |
1823 | ev_userdata (EV_P) |
1645 | { |
1824 | { |
1646 | return userdata; |
1825 | return userdata; |
1647 | } |
1826 | } |
1648 | |
1827 | |
|
|
1828 | void |
1649 | void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
1829 | ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
1650 | { |
1830 | { |
1651 | invoke_cb = invoke_pending_cb; |
1831 | invoke_cb = invoke_pending_cb; |
1652 | } |
1832 | } |
1653 | |
1833 | |
|
|
1834 | void |
1654 | void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
1835 | ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
1655 | { |
1836 | { |
1656 | release_cb = release; |
1837 | release_cb = release; |
1657 | acquire_cb = acquire; |
1838 | acquire_cb = acquire; |
1658 | } |
1839 | } |
1659 | #endif |
1840 | #endif |
1660 | |
1841 | |
1661 | /* initialise a loop structure, must be zero-initialised */ |
1842 | /* initialise a loop structure, must be zero-initialised */ |
1662 | static void noinline |
1843 | static void noinline ecb_cold |
1663 | loop_init (EV_P_ unsigned int flags) |
1844 | loop_init (EV_P_ unsigned int flags) |
1664 | { |
1845 | { |
1665 | if (!backend) |
1846 | if (!backend) |
1666 | { |
1847 | { |
1667 | origflags = flags; |
1848 | origflags = flags; |
… | |
… | |
1695 | if (!(flags & EVFLAG_NOENV) |
1876 | if (!(flags & EVFLAG_NOENV) |
1696 | && !enable_secure () |
1877 | && !enable_secure () |
1697 | && getenv ("LIBEV_FLAGS")) |
1878 | && getenv ("LIBEV_FLAGS")) |
1698 | flags = atoi (getenv ("LIBEV_FLAGS")); |
1879 | flags = atoi (getenv ("LIBEV_FLAGS")); |
1699 | |
1880 | |
1700 | ev_rt_now = ev_time (); |
1881 | ev_rt_now = ev_time (); |
1701 | mn_now = get_clock (); |
1882 | mn_now = get_clock (); |
1702 | now_floor = mn_now; |
1883 | now_floor = mn_now; |
1703 | rtmn_diff = ev_rt_now - mn_now; |
1884 | rtmn_diff = ev_rt_now - mn_now; |
1704 | #if EV_FEATURE_API |
1885 | #if EV_FEATURE_API |
1705 | invoke_cb = ev_invoke_pending; |
1886 | invoke_cb = ev_invoke_pending; |
1706 | #endif |
1887 | #endif |
1707 | |
1888 | |
1708 | io_blocktime = 0.; |
1889 | io_blocktime = 0.; |
1709 | timeout_blocktime = 0.; |
1890 | timeout_blocktime = 0.; |
1710 | backend = 0; |
1891 | backend = 0; |
1711 | backend_fd = -1; |
1892 | backend_fd = -1; |
1712 | sig_pending = 0; |
1893 | sig_pending = 0; |
1713 | #if EV_ASYNC_ENABLE |
1894 | #if EV_ASYNC_ENABLE |
1714 | async_pending = 0; |
1895 | async_pending = 0; |
1715 | #endif |
1896 | #endif |
|
|
1897 | pipe_write_skipped = 0; |
|
|
1898 | pipe_write_wanted = 0; |
1716 | #if EV_USE_INOTIFY |
1899 | #if EV_USE_INOTIFY |
1717 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1900 | fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; |
1718 | #endif |
1901 | #endif |
1719 | #if EV_USE_SIGNALFD |
1902 | #if EV_USE_SIGNALFD |
1720 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1903 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1721 | #endif |
1904 | #endif |
1722 | |
1905 | |
1723 | if (!(flags & EVBACKEND_MASK)) |
1906 | if (!(flags & EVBACKEND_MASK)) |
1724 | flags |= ev_recommended_backends (); |
1907 | flags |= ev_recommended_backends (); |
1725 | |
1908 | |
… | |
… | |
1750 | #endif |
1933 | #endif |
1751 | } |
1934 | } |
1752 | } |
1935 | } |
1753 | |
1936 | |
1754 | /* free up a loop structure */ |
1937 | /* free up a loop structure */ |
1755 | void |
1938 | void ecb_cold |
1756 | ev_loop_destroy (EV_P) |
1939 | ev_loop_destroy (EV_P) |
1757 | { |
1940 | { |
1758 | int i; |
1941 | int i; |
1759 | |
1942 | |
1760 | #if EV_MULTIPLICITY |
1943 | #if EV_MULTIPLICITY |
… | |
… | |
1890 | infy_fork (EV_A); |
2073 | infy_fork (EV_A); |
1891 | #endif |
2074 | #endif |
1892 | |
2075 | |
1893 | if (ev_is_active (&pipe_w)) |
2076 | if (ev_is_active (&pipe_w)) |
1894 | { |
2077 | { |
1895 | /* this "locks" the handlers against writing to the pipe */ |
2078 | /* pipe_write_wanted must be false now, so modifying fd vars should be safe */ |
1896 | /* while we modify the fd vars */ |
|
|
1897 | sig_pending = 1; |
|
|
1898 | #if EV_ASYNC_ENABLE |
|
|
1899 | async_pending = 1; |
|
|
1900 | #endif |
|
|
1901 | |
2079 | |
1902 | ev_ref (EV_A); |
2080 | ev_ref (EV_A); |
1903 | ev_io_stop (EV_A_ &pipe_w); |
2081 | ev_io_stop (EV_A_ &pipe_w); |
1904 | |
2082 | |
1905 | #if EV_USE_EVENTFD |
2083 | #if EV_USE_EVENTFD |
… | |
… | |
1923 | postfork = 0; |
2101 | postfork = 0; |
1924 | } |
2102 | } |
1925 | |
2103 | |
1926 | #if EV_MULTIPLICITY |
2104 | #if EV_MULTIPLICITY |
1927 | |
2105 | |
1928 | struct ev_loop * |
2106 | struct ev_loop * ecb_cold |
1929 | ev_loop_new (unsigned int flags) |
2107 | ev_loop_new (unsigned int flags) |
1930 | { |
2108 | { |
1931 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
2109 | EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
1932 | |
2110 | |
1933 | memset (EV_A, 0, sizeof (struct ev_loop)); |
2111 | memset (EV_A, 0, sizeof (struct ev_loop)); |
… | |
… | |
1941 | } |
2119 | } |
1942 | |
2120 | |
1943 | #endif /* multiplicity */ |
2121 | #endif /* multiplicity */ |
1944 | |
2122 | |
1945 | #if EV_VERIFY |
2123 | #if EV_VERIFY |
1946 | static void noinline |
2124 | static void noinline ecb_cold |
1947 | verify_watcher (EV_P_ W w) |
2125 | verify_watcher (EV_P_ W w) |
1948 | { |
2126 | { |
1949 | assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); |
2127 | assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); |
1950 | |
2128 | |
1951 | if (w->pending) |
2129 | if (w->pending) |
1952 | assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); |
2130 | assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w)); |
1953 | } |
2131 | } |
1954 | |
2132 | |
1955 | static void noinline |
2133 | static void noinline ecb_cold |
1956 | verify_heap (EV_P_ ANHE *heap, int N) |
2134 | verify_heap (EV_P_ ANHE *heap, int N) |
1957 | { |
2135 | { |
1958 | int i; |
2136 | int i; |
1959 | |
2137 | |
1960 | for (i = HEAP0; i < N + HEAP0; ++i) |
2138 | for (i = HEAP0; i < N + HEAP0; ++i) |
… | |
… | |
1965 | |
2143 | |
1966 | verify_watcher (EV_A_ (W)ANHE_w (heap [i])); |
2144 | verify_watcher (EV_A_ (W)ANHE_w (heap [i])); |
1967 | } |
2145 | } |
1968 | } |
2146 | } |
1969 | |
2147 | |
1970 | static void noinline |
2148 | static void noinline ecb_cold |
1971 | array_verify (EV_P_ W *ws, int cnt) |
2149 | array_verify (EV_P_ W *ws, int cnt) |
1972 | { |
2150 | { |
1973 | while (cnt--) |
2151 | while (cnt--) |
1974 | { |
2152 | { |
1975 | assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
2153 | assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1)); |
… | |
… | |
1977 | } |
2155 | } |
1978 | } |
2156 | } |
1979 | #endif |
2157 | #endif |
1980 | |
2158 | |
1981 | #if EV_FEATURE_API |
2159 | #if EV_FEATURE_API |
1982 | void |
2160 | void ecb_cold |
1983 | ev_verify (EV_P) |
2161 | ev_verify (EV_P) |
1984 | { |
2162 | { |
1985 | #if EV_VERIFY |
2163 | #if EV_VERIFY |
1986 | int i; |
2164 | int i; |
1987 | WL w; |
2165 | WL w; |
… | |
… | |
2053 | #endif |
2231 | #endif |
2054 | } |
2232 | } |
2055 | #endif |
2233 | #endif |
2056 | |
2234 | |
2057 | #if EV_MULTIPLICITY |
2235 | #if EV_MULTIPLICITY |
2058 | struct ev_loop * |
2236 | struct ev_loop * ecb_cold |
2059 | #else |
2237 | #else |
2060 | int |
2238 | int |
2061 | #endif |
2239 | #endif |
2062 | ev_default_loop (unsigned int flags) |
2240 | ev_default_loop (unsigned int flags) |
2063 | { |
2241 | { |
… | |
… | |
2191 | feed_reverse_done (EV_A_ EV_TIMER); |
2369 | feed_reverse_done (EV_A_ EV_TIMER); |
2192 | } |
2370 | } |
2193 | } |
2371 | } |
2194 | |
2372 | |
2195 | #if EV_PERIODIC_ENABLE |
2373 | #if EV_PERIODIC_ENABLE |
|
|
2374 | |
|
|
2375 | static void noinline |
|
|
2376 | periodic_recalc (EV_P_ ev_periodic *w) |
|
|
2377 | { |
|
|
2378 | ev_tstamp interval = w->interval > MIN_INTERVAL ? w->interval : MIN_INTERVAL; |
|
|
2379 | ev_tstamp at = w->offset + interval * ev_floor ((ev_rt_now - w->offset) / interval); |
|
|
2380 | |
|
|
2381 | /* the above almost always errs on the low side */ |
|
|
2382 | while (at <= ev_rt_now) |
|
|
2383 | { |
|
|
2384 | ev_tstamp nat = at + w->interval; |
|
|
2385 | |
|
|
2386 | /* when resolution fails us, we use ev_rt_now */ |
|
|
2387 | if (expect_false (nat == at)) |
|
|
2388 | { |
|
|
2389 | at = ev_rt_now; |
|
|
2390 | break; |
|
|
2391 | } |
|
|
2392 | |
|
|
2393 | at = nat; |
|
|
2394 | } |
|
|
2395 | |
|
|
2396 | ev_at (w) = at; |
|
|
2397 | } |
|
|
2398 | |
2196 | /* make periodics pending */ |
2399 | /* make periodics pending */ |
2197 | inline_size void |
2400 | inline_size void |
2198 | periodics_reify (EV_P) |
2401 | periodics_reify (EV_P) |
2199 | { |
2402 | { |
2200 | EV_FREQUENT_CHECK; |
2403 | EV_FREQUENT_CHECK; |
… | |
… | |
2219 | ANHE_at_cache (periodics [HEAP0]); |
2422 | ANHE_at_cache (periodics [HEAP0]); |
2220 | downheap (periodics, periodiccnt, HEAP0); |
2423 | downheap (periodics, periodiccnt, HEAP0); |
2221 | } |
2424 | } |
2222 | else if (w->interval) |
2425 | else if (w->interval) |
2223 | { |
2426 | { |
2224 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
2427 | periodic_recalc (EV_A_ w); |
2225 | /* if next trigger time is not sufficiently in the future, put it there */ |
|
|
2226 | /* this might happen because of floating point inexactness */ |
|
|
2227 | if (ev_at (w) - ev_rt_now < TIME_EPSILON) |
|
|
2228 | { |
|
|
2229 | ev_at (w) += w->interval; |
|
|
2230 | |
|
|
2231 | /* if interval is unreasonably low we might still have a time in the past */ |
|
|
2232 | /* so correct this. this will make the periodic very inexact, but the user */ |
|
|
2233 | /* has effectively asked to get triggered more often than possible */ |
|
|
2234 | if (ev_at (w) < ev_rt_now) |
|
|
2235 | ev_at (w) = ev_rt_now; |
|
|
2236 | } |
|
|
2237 | |
|
|
2238 | ANHE_at_cache (periodics [HEAP0]); |
2428 | ANHE_at_cache (periodics [HEAP0]); |
2239 | downheap (periodics, periodiccnt, HEAP0); |
2429 | downheap (periodics, periodiccnt, HEAP0); |
2240 | } |
2430 | } |
2241 | else |
2431 | else |
2242 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
2432 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
… | |
… | |
2250 | } |
2440 | } |
2251 | } |
2441 | } |
2252 | |
2442 | |
2253 | /* simply recalculate all periodics */ |
2443 | /* simply recalculate all periodics */ |
2254 | /* TODO: maybe ensure that at least one event happens when jumping forward? */ |
2444 | /* TODO: maybe ensure that at least one event happens when jumping forward? */ |
2255 | static void noinline |
2445 | static void noinline ecb_cold |
2256 | periodics_reschedule (EV_P) |
2446 | periodics_reschedule (EV_P) |
2257 | { |
2447 | { |
2258 | int i; |
2448 | int i; |
2259 | |
2449 | |
2260 | /* adjust periodics after time jump */ |
2450 | /* adjust periodics after time jump */ |
… | |
… | |
2263 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); |
2453 | ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); |
2264 | |
2454 | |
2265 | if (w->reschedule_cb) |
2455 | if (w->reschedule_cb) |
2266 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2456 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2267 | else if (w->interval) |
2457 | else if (w->interval) |
2268 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
2458 | periodic_recalc (EV_A_ w); |
2269 | |
2459 | |
2270 | ANHE_at_cache (periodics [i]); |
2460 | ANHE_at_cache (periodics [i]); |
2271 | } |
2461 | } |
2272 | |
2462 | |
2273 | reheap (periodics, periodiccnt); |
2463 | reheap (periodics, periodiccnt); |
2274 | } |
2464 | } |
2275 | #endif |
2465 | #endif |
2276 | |
2466 | |
2277 | /* adjust all timers by a given offset */ |
2467 | /* adjust all timers by a given offset */ |
2278 | static void noinline |
2468 | static void noinline ecb_cold |
2279 | timers_reschedule (EV_P_ ev_tstamp adjust) |
2469 | timers_reschedule (EV_P_ ev_tstamp adjust) |
2280 | { |
2470 | { |
2281 | int i; |
2471 | int i; |
2282 | |
2472 | |
2283 | for (i = 0; i < timercnt; ++i) |
2473 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
2320 | * doesn't hurt either as we only do this on time-jumps or |
2510 | * doesn't hurt either as we only do this on time-jumps or |
2321 | * in the unlikely event of having been preempted here. |
2511 | * in the unlikely event of having been preempted here. |
2322 | */ |
2512 | */ |
2323 | for (i = 4; --i; ) |
2513 | for (i = 4; --i; ) |
2324 | { |
2514 | { |
|
|
2515 | ev_tstamp diff; |
2325 | rtmn_diff = ev_rt_now - mn_now; |
2516 | rtmn_diff = ev_rt_now - mn_now; |
2326 | |
2517 | |
|
|
2518 | diff = odiff - rtmn_diff; |
|
|
2519 | |
2327 | if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)) |
2520 | if (expect_true ((diff < 0. ? -diff : diff) < MIN_TIMEJUMP)) |
2328 | return; /* all is well */ |
2521 | return; /* all is well */ |
2329 | |
2522 | |
2330 | ev_rt_now = ev_time (); |
2523 | ev_rt_now = ev_time (); |
2331 | mn_now = get_clock (); |
2524 | mn_now = get_clock (); |
2332 | now_floor = mn_now; |
2525 | now_floor = mn_now; |
… | |
… | |
2422 | ev_tstamp prev_mn_now = mn_now; |
2615 | ev_tstamp prev_mn_now = mn_now; |
2423 | |
2616 | |
2424 | /* update time to cancel out callback processing overhead */ |
2617 | /* update time to cancel out callback processing overhead */ |
2425 | time_update (EV_A_ 1e100); |
2618 | time_update (EV_A_ 1e100); |
2426 | |
2619 | |
|
|
2620 | /* from now on, we want a pipe-wake-up */ |
|
|
2621 | pipe_write_wanted = 1; |
|
|
2622 | |
|
|
2623 | ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */ |
|
|
2624 | |
2427 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt))) |
2625 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
2428 | { |
2626 | { |
2429 | waittime = MAX_BLOCKTIME; |
2627 | waittime = MAX_BLOCKTIME; |
2430 | |
2628 | |
2431 | if (timercnt) |
2629 | if (timercnt) |
2432 | { |
2630 | { |
2433 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; |
2631 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now; |
2434 | if (waittime > to) waittime = to; |
2632 | if (waittime > to) waittime = to; |
2435 | } |
2633 | } |
2436 | |
2634 | |
2437 | #if EV_PERIODIC_ENABLE |
2635 | #if EV_PERIODIC_ENABLE |
2438 | if (periodiccnt) |
2636 | if (periodiccnt) |
2439 | { |
2637 | { |
2440 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; |
2638 | ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now; |
2441 | if (waittime > to) waittime = to; |
2639 | if (waittime > to) waittime = to; |
2442 | } |
2640 | } |
2443 | #endif |
2641 | #endif |
2444 | |
2642 | |
2445 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2643 | /* don't let timeouts decrease the waittime below timeout_blocktime */ |
2446 | if (expect_false (waittime < timeout_blocktime)) |
2644 | if (expect_false (waittime < timeout_blocktime)) |
2447 | waittime = timeout_blocktime; |
2645 | waittime = timeout_blocktime; |
|
|
2646 | |
|
|
2647 | /* at this point, we NEED to wait, so we have to ensure */ |
|
|
2648 | /* to pass a minimum nonzero value to the backend */ |
|
|
2649 | if (expect_false (waittime < backend_mintime)) |
|
|
2650 | waittime = backend_mintime; |
2448 | |
2651 | |
2449 | /* extra check because io_blocktime is commonly 0 */ |
2652 | /* extra check because io_blocktime is commonly 0 */ |
2450 | if (expect_false (io_blocktime)) |
2653 | if (expect_false (io_blocktime)) |
2451 | { |
2654 | { |
2452 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
2655 | sleeptime = io_blocktime - (mn_now - prev_mn_now); |
2453 | |
2656 | |
2454 | if (sleeptime > waittime - backend_fudge) |
2657 | if (sleeptime > waittime - backend_mintime) |
2455 | sleeptime = waittime - backend_fudge; |
2658 | sleeptime = waittime - backend_mintime; |
2456 | |
2659 | |
2457 | if (expect_true (sleeptime > 0.)) |
2660 | if (expect_true (sleeptime > 0.)) |
2458 | { |
2661 | { |
2459 | ev_sleep (sleeptime); |
2662 | ev_sleep (sleeptime); |
2460 | waittime -= sleeptime; |
2663 | waittime -= sleeptime; |
… | |
… | |
2466 | ++loop_count; |
2669 | ++loop_count; |
2467 | #endif |
2670 | #endif |
2468 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2671 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2469 | backend_poll (EV_A_ waittime); |
2672 | backend_poll (EV_A_ waittime); |
2470 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
2673 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
|
|
2674 | |
|
|
2675 | pipe_write_wanted = 0; /* just an optimsiation, no fence needed */ |
|
|
2676 | |
|
|
2677 | if (pipe_write_skipped) |
|
|
2678 | { |
|
|
2679 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
|
|
2680 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
|
|
2681 | } |
|
|
2682 | |
2471 | |
2683 | |
2472 | /* update ev_rt_now, do magic */ |
2684 | /* update ev_rt_now, do magic */ |
2473 | time_update (EV_A_ waittime + sleeptime); |
2685 | time_update (EV_A_ waittime + sleeptime); |
2474 | } |
2686 | } |
2475 | |
2687 | |
… | |
… | |
2765 | if (w->reschedule_cb) |
2977 | if (w->reschedule_cb) |
2766 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2978 | ev_at (w) = w->reschedule_cb (w, ev_rt_now); |
2767 | else if (w->interval) |
2979 | else if (w->interval) |
2768 | { |
2980 | { |
2769 | assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.)); |
2981 | assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.)); |
2770 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
2982 | periodic_recalc (EV_A_ w); |
2771 | ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; |
|
|
2772 | } |
2983 | } |
2773 | else |
2984 | else |
2774 | ev_at (w) = w->offset; |
2985 | ev_at (w) = w->offset; |
2775 | |
2986 | |
2776 | EV_FREQUENT_CHECK; |
2987 | EV_FREQUENT_CHECK; |
… | |
… | |
3053 | if (!pend || pend == path) |
3264 | if (!pend || pend == path) |
3054 | break; |
3265 | break; |
3055 | |
3266 | |
3056 | *pend = 0; |
3267 | *pend = 0; |
3057 | w->wd = inotify_add_watch (fs_fd, path, mask); |
3268 | w->wd = inotify_add_watch (fs_fd, path, mask); |
3058 | } |
3269 | } |
3059 | while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); |
3270 | while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); |
3060 | } |
3271 | } |
3061 | } |
3272 | } |
3062 | |
3273 | |
3063 | if (w->wd >= 0) |
3274 | if (w->wd >= 0) |
… | |
… | |
3130 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
3341 | infy_wd (EV_A_ ev->wd, ev->wd, ev); |
3131 | ofs += sizeof (struct inotify_event) + ev->len; |
3342 | ofs += sizeof (struct inotify_event) + ev->len; |
3132 | } |
3343 | } |
3133 | } |
3344 | } |
3134 | |
3345 | |
3135 | inline_size void |
3346 | inline_size void ecb_cold |
3136 | ev_check_2625 (EV_P) |
3347 | ev_check_2625 (EV_P) |
3137 | { |
3348 | { |
3138 | /* kernels < 2.6.25 are borked |
3349 | /* kernels < 2.6.25 are borked |
3139 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
3350 | * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html |
3140 | */ |
3351 | */ |
… | |
… | |
3765 | } |
3976 | } |
3766 | |
3977 | |
3767 | /*****************************************************************************/ |
3978 | /*****************************************************************************/ |
3768 | |
3979 | |
3769 | #if EV_WALK_ENABLE |
3980 | #if EV_WALK_ENABLE |
3770 | void |
3981 | void ecb_cold |
3771 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3982 | ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) |
3772 | { |
3983 | { |
3773 | int i, j; |
3984 | int i, j; |
3774 | ev_watcher_list *wl, *wn; |
3985 | ev_watcher_list *wl, *wn; |
3775 | |
3986 | |
… | |
… | |
3819 | cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i])); |
4030 | cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i])); |
3820 | #endif |
4031 | #endif |
3821 | |
4032 | |
3822 | #if EV_IDLE_ENABLE |
4033 | #if EV_IDLE_ENABLE |
3823 | if (types & EV_IDLE) |
4034 | if (types & EV_IDLE) |
3824 | for (j = NUMPRI; i--; ) |
4035 | for (j = NUMPRI; j--; ) |
3825 | for (i = idlecnt [j]; i--; ) |
4036 | for (i = idlecnt [j]; i--; ) |
3826 | cb (EV_A_ EV_IDLE, idles [j][i]); |
4037 | cb (EV_A_ EV_IDLE, idles [j][i]); |
3827 | #endif |
4038 | #endif |
3828 | |
4039 | |
3829 | #if EV_FORK_ENABLE |
4040 | #if EV_FORK_ENABLE |