… | |
… | |
448 | # ifdef __cplusplus |
448 | # ifdef __cplusplus |
449 | } |
449 | } |
450 | # endif |
450 | # endif |
451 | #endif |
451 | #endif |
452 | |
452 | |
453 | |
|
|
454 | /**/ |
453 | /**/ |
455 | |
454 | |
456 | #if EV_VERIFY >= 3 |
455 | #if EV_VERIFY >= 3 |
457 | # define EV_FREQUENT_CHECK ev_verify (EV_A) |
456 | # define EV_FREQUENT_CHECK ev_verify (EV_A) |
458 | #else |
457 | #else |
… | |
… | |
470 | #define TIME_EPSILON 0.0001220703125 /* 1/8192 */ |
469 | #define TIME_EPSILON 0.0001220703125 /* 1/8192 */ |
471 | |
470 | |
472 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
471 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
473 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
472 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
474 | |
473 | |
|
|
474 | #define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0) |
|
|
475 | #define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0) |
|
|
476 | |
475 | #if __GNUC__ >= 4 |
477 | #if __GNUC__ >= 4 |
476 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
478 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
477 | # define noinline __attribute__ ((noinline)) |
479 | # define noinline __attribute__ ((noinline)) |
478 | #else |
480 | #else |
479 | # define expect(expr,value) (expr) |
481 | # define expect(expr,value) (expr) |
… | |
… | |
511 | #define ev_active(w) ((W)(w))->active |
513 | #define ev_active(w) ((W)(w))->active |
512 | #define ev_at(w) ((WT)(w))->at |
514 | #define ev_at(w) ((WT)(w))->at |
513 | |
515 | |
514 | #if EV_USE_REALTIME |
516 | #if EV_USE_REALTIME |
515 | /* sig_atomic_t is used to avoid per-thread variables or locking but still */ |
517 | /* sig_atomic_t is used to avoid per-thread variables or locking but still */ |
516 | /* giving it a reasonably high chance of working on typical architetcures */ |
518 | /* giving it a reasonably high chance of working on typical architectures */ |
517 | static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */ |
519 | static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */ |
518 | #endif |
520 | #endif |
519 | |
521 | |
520 | #if EV_USE_MONOTONIC |
522 | #if EV_USE_MONOTONIC |
521 | static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
523 | static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
… | |
… | |
769 | if (delay > 0.) |
771 | if (delay > 0.) |
770 | { |
772 | { |
771 | #if EV_USE_NANOSLEEP |
773 | #if EV_USE_NANOSLEEP |
772 | struct timespec ts; |
774 | struct timespec ts; |
773 | |
775 | |
774 | ts.tv_sec = (time_t)delay; |
776 | EV_TS_SET (ts, delay); |
775 | ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9); |
|
|
776 | |
|
|
777 | nanosleep (&ts, 0); |
777 | nanosleep (&ts, 0); |
778 | #elif defined(_WIN32) |
778 | #elif defined(_WIN32) |
779 | Sleep ((unsigned long)(delay * 1e3)); |
779 | Sleep ((unsigned long)(delay * 1e3)); |
780 | #else |
780 | #else |
781 | struct timeval tv; |
781 | struct timeval tv; |
782 | |
782 | |
783 | tv.tv_sec = (time_t)delay; |
|
|
784 | tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); |
|
|
785 | |
|
|
786 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
783 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
787 | /* something not guaranteed by newer posix versions, but guaranteed */ |
784 | /* something not guaranteed by newer posix versions, but guaranteed */ |
788 | /* by older ones */ |
785 | /* by older ones */ |
|
|
786 | EV_TV_SET (tv, delay); |
789 | select (0, 0, 0, 0, &tv); |
787 | select (0, 0, 0, 0, &tv); |
790 | #endif |
788 | #endif |
791 | } |
789 | } |
792 | } |
790 | } |
793 | |
791 | |
794 | /*****************************************************************************/ |
792 | /*****************************************************************************/ |
795 | |
793 | |
796 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
794 | #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ |
797 | |
795 | |
798 | /* find a suitable new size for the given array, */ |
796 | /* find a suitable new size for the given array, */ |
799 | /* hopefully by rounding to a ncie-to-malloc size */ |
797 | /* hopefully by rounding to a nice-to-malloc size */ |
800 | inline_size int |
798 | inline_size int |
801 | array_nextsize (int elem, int cur, int cnt) |
799 | array_nextsize (int elem, int cur, int cnt) |
802 | { |
800 | { |
803 | int ncur = cur + 1; |
801 | int ncur = cur + 1; |
804 | |
802 | |
… | |
… | |
945 | { |
943 | { |
946 | int fd = fdchanges [i]; |
944 | int fd = fdchanges [i]; |
947 | ANFD *anfd = anfds + fd; |
945 | ANFD *anfd = anfds + fd; |
948 | ev_io *w; |
946 | ev_io *w; |
949 | |
947 | |
950 | unsigned char events = 0; |
948 | unsigned char o_events = anfd->events; |
|
|
949 | unsigned char o_reify = anfd->reify; |
951 | |
950 | |
952 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
951 | anfd->reify = 0; |
953 | events |= (unsigned char)w->events; |
|
|
954 | |
952 | |
955 | #if EV_SELECT_IS_WINSOCKET |
953 | #if EV_SELECT_IS_WINSOCKET |
956 | if (events) |
954 | if (o_reify & EV__IOFDSET) |
957 | { |
955 | { |
958 | unsigned long arg; |
956 | unsigned long arg; |
959 | anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); |
957 | anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); |
960 | assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); |
958 | assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); |
961 | } |
959 | } |
962 | #endif |
960 | #endif |
963 | |
961 | |
|
|
962 | /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */ |
964 | { |
963 | { |
965 | unsigned char o_events = anfd->events; |
|
|
966 | unsigned char o_reify = anfd->reify; |
|
|
967 | |
|
|
968 | anfd->reify = 0; |
|
|
969 | anfd->events = events; |
964 | anfd->events = 0; |
970 | |
965 | |
971 | if (o_events != events || o_reify & EV__IOFDSET) |
966 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
|
|
967 | anfd->events |= (unsigned char)w->events; |
|
|
968 | |
|
|
969 | if (o_events != anfd->events) |
|
|
970 | o_reify = EV__IOFDSET; /* actually |= */ |
|
|
971 | } |
|
|
972 | |
|
|
973 | if (o_reify & EV__IOFDSET) |
972 | backend_modify (EV_A_ fd, o_events, events); |
974 | backend_modify (EV_A_ fd, o_events, anfd->events); |
973 | } |
|
|
974 | } |
975 | } |
975 | |
976 | |
976 | fdchangecnt = 0; |
977 | fdchangecnt = 0; |
977 | } |
978 | } |
978 | |
979 | |
… | |
… | |
1071 | } |
1072 | } |
1072 | |
1073 | |
1073 | /*****************************************************************************/ |
1074 | /*****************************************************************************/ |
1074 | |
1075 | |
1075 | /* |
1076 | /* |
1076 | * the heap functions want a real array index. array index 0 uis guaranteed to not |
1077 | * the heap functions want a real array index. array index 0 is guaranteed to not |
1077 | * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives |
1078 | * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives |
1078 | * the branching factor of the d-tree. |
1079 | * the branching factor of the d-tree. |
1079 | */ |
1080 | */ |
1080 | |
1081 | |
1081 | /* |
1082 | /* |
… | |
… | |
1281 | uint64_t counter = 1; |
1282 | uint64_t counter = 1; |
1282 | write (evfd, &counter, sizeof (uint64_t)); |
1283 | write (evfd, &counter, sizeof (uint64_t)); |
1283 | } |
1284 | } |
1284 | else |
1285 | else |
1285 | #endif |
1286 | #endif |
|
|
1287 | /* win32 people keep sending patches that change this write() to send() */ |
|
|
1288 | /* and then run away. but send() is wrong, it wants a socket handle on win32 */ |
|
|
1289 | /* so when you think this write should be a send instead, please find out */ |
|
|
1290 | /* where your send() is from - it's definitely not the microsoft send, and */ |
|
|
1291 | /* tell me. thank you. */ |
1286 | write (evpipe [1], &dummy, 1); |
1292 | write (evpipe [1], &dummy, 1); |
1287 | |
1293 | |
1288 | errno = old_errno; |
1294 | errno = old_errno; |
1289 | } |
1295 | } |
1290 | } |
1296 | } |
… | |
… | |
1304 | } |
1310 | } |
1305 | else |
1311 | else |
1306 | #endif |
1312 | #endif |
1307 | { |
1313 | { |
1308 | char dummy; |
1314 | char dummy; |
|
|
1315 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1309 | read (evpipe [0], &dummy, 1); |
1316 | read (evpipe [0], &dummy, 1); |
1310 | } |
1317 | } |
1311 | |
1318 | |
1312 | if (sig_pending) |
1319 | if (sig_pending) |
1313 | { |
1320 | { |
… | |
… | |
2179 | feed_reverse_done (EV_A_ EV_PERIODIC); |
2186 | feed_reverse_done (EV_A_ EV_PERIODIC); |
2180 | } |
2187 | } |
2181 | } |
2188 | } |
2182 | |
2189 | |
2183 | /* simply recalculate all periodics */ |
2190 | /* simply recalculate all periodics */ |
2184 | /* TODO: maybe ensure that at leats one event happens when jumping forward? */ |
2191 | /* TODO: maybe ensure that at least one event happens when jumping forward? */ |
2185 | static void noinline |
2192 | static void noinline |
2186 | periodics_reschedule (EV_P) |
2193 | periodics_reschedule (EV_P) |
2187 | { |
2194 | { |
2188 | int i; |
2195 | int i; |
2189 | |
2196 | |
… | |
… | |
2593 | EV_FREQUENT_CHECK; |
2600 | EV_FREQUENT_CHECK; |
2594 | |
2601 | |
2595 | wlist_del (&anfds[w->fd].head, (WL)w); |
2602 | wlist_del (&anfds[w->fd].head, (WL)w); |
2596 | ev_stop (EV_A_ (W)w); |
2603 | ev_stop (EV_A_ (W)w); |
2597 | |
2604 | |
2598 | fd_change (EV_A_ w->fd, 1); |
2605 | fd_change (EV_A_ w->fd, EV_ANFD_REIFY); |
2599 | |
2606 | |
2600 | EV_FREQUENT_CHECK; |
2607 | EV_FREQUENT_CHECK; |
2601 | } |
2608 | } |
2602 | |
2609 | |
2603 | void noinline |
2610 | void noinline |