… | |
… | |
196 | # define EV_SELECT_IS_WINSOCKET 1 |
196 | # define EV_SELECT_IS_WINSOCKET 1 |
197 | # endif |
197 | # endif |
198 | # undef EV_AVOID_STDIO |
198 | # undef EV_AVOID_STDIO |
199 | #endif |
199 | #endif |
200 | |
200 | |
|
|
201 | /* OS X, in its infinite idiocy, actually HARDCODES |
|
|
202 | * a limit of 1024 into their select. Where people have brains, |
|
|
203 | * OS X engineers apparently have a vacuum. Or maybe they were |
|
|
204 | * ordered to have a vacuum, or they do anything for money. |
|
|
205 | * This might help. Or not. |
|
|
206 | */ |
|
|
207 | #define _DARWIN_UNLIMITED_SELECT 1 |
|
|
208 | |
201 | /* this block tries to deduce configuration from header-defined symbols and defaults */ |
209 | /* this block tries to deduce configuration from header-defined symbols and defaults */ |
202 | |
210 | |
203 | /* try to deduce the maximum number of signals on this platform */ |
211 | /* try to deduce the maximum number of signals on this platform */ |
204 | #if defined (EV_NSIG) |
212 | #if defined (EV_NSIG) |
205 | /* use what's provided */ |
213 | /* use what's provided */ |
… | |
… | |
440 | # ifdef __cplusplus |
448 | # ifdef __cplusplus |
441 | } |
449 | } |
442 | # endif |
450 | # endif |
443 | #endif |
451 | #endif |
444 | |
452 | |
445 | |
|
|
446 | /**/ |
453 | /**/ |
447 | |
454 | |
448 | #if EV_VERIFY >= 3 |
455 | #if EV_VERIFY >= 3 |
449 | # define EV_FREQUENT_CHECK ev_verify (EV_A) |
456 | # define EV_FREQUENT_CHECK ev_verify (EV_A) |
450 | #else |
457 | #else |
… | |
… | |
462 | #define TIME_EPSILON 0.0001220703125 /* 1/8192 */ |
469 | #define TIME_EPSILON 0.0001220703125 /* 1/8192 */ |
463 | |
470 | |
464 | #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) */ |
465 | #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) */ |
466 | |
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 | |
467 | #if __GNUC__ >= 4 |
477 | #if __GNUC__ >= 4 |
468 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
478 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
469 | # define noinline __attribute__ ((noinline)) |
479 | # define noinline __attribute__ ((noinline)) |
470 | #else |
480 | #else |
471 | # define expect(expr,value) (expr) |
481 | # define expect(expr,value) (expr) |
… | |
… | |
503 | #define ev_active(w) ((W)(w))->active |
513 | #define ev_active(w) ((W)(w))->active |
504 | #define ev_at(w) ((WT)(w))->at |
514 | #define ev_at(w) ((WT)(w))->at |
505 | |
515 | |
506 | #if EV_USE_REALTIME |
516 | #if EV_USE_REALTIME |
507 | /* 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 */ |
508 | /* giving it a reasonably high chance of working on typical architetcures */ |
518 | /* giving it a reasonably high chance of working on typical architectures */ |
509 | 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? */ |
510 | #endif |
520 | #endif |
511 | |
521 | |
512 | #if EV_USE_MONOTONIC |
522 | #if EV_USE_MONOTONIC |
513 | 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? */ |
… | |
… | |
707 | # define EV_RELEASE_CB (void)0 |
717 | # define EV_RELEASE_CB (void)0 |
708 | # define EV_ACQUIRE_CB (void)0 |
718 | # define EV_ACQUIRE_CB (void)0 |
709 | # define EV_INVOKE_PENDING ev_invoke_pending (EV_A) |
719 | # define EV_INVOKE_PENDING ev_invoke_pending (EV_A) |
710 | #endif |
720 | #endif |
711 | |
721 | |
712 | #define EVUNLOOP_RECURSE 0x80 |
722 | #define EVBREAK_RECURSE 0x80 |
713 | |
723 | |
714 | /*****************************************************************************/ |
724 | /*****************************************************************************/ |
715 | |
725 | |
716 | #ifndef EV_HAVE_EV_TIME |
726 | #ifndef EV_HAVE_EV_TIME |
717 | ev_tstamp |
727 | ev_tstamp |
… | |
… | |
761 | if (delay > 0.) |
771 | if (delay > 0.) |
762 | { |
772 | { |
763 | #if EV_USE_NANOSLEEP |
773 | #if EV_USE_NANOSLEEP |
764 | struct timespec ts; |
774 | struct timespec ts; |
765 | |
775 | |
766 | ts.tv_sec = (time_t)delay; |
776 | EV_TS_SET (ts, delay); |
767 | ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9); |
|
|
768 | |
|
|
769 | nanosleep (&ts, 0); |
777 | nanosleep (&ts, 0); |
770 | #elif defined(_WIN32) |
778 | #elif defined(_WIN32) |
771 | Sleep ((unsigned long)(delay * 1e3)); |
779 | Sleep ((unsigned long)(delay * 1e3)); |
772 | #else |
780 | #else |
773 | struct timeval tv; |
781 | struct timeval tv; |
774 | |
782 | |
775 | tv.tv_sec = (time_t)delay; |
|
|
776 | tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); |
|
|
777 | |
|
|
778 | /* 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 */ |
779 | /* something not guaranteed by newer posix versions, but guaranteed */ |
784 | /* something not guaranteed by newer posix versions, but guaranteed */ |
780 | /* by older ones */ |
785 | /* by older ones */ |
|
|
786 | EV_TV_SET (tv, delay); |
781 | select (0, 0, 0, 0, &tv); |
787 | select (0, 0, 0, 0, &tv); |
782 | #endif |
788 | #endif |
783 | } |
789 | } |
784 | } |
790 | } |
785 | |
791 | |
786 | /*****************************************************************************/ |
792 | /*****************************************************************************/ |
787 | |
793 | |
788 | #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 */ |
789 | |
795 | |
790 | /* find a suitable new size for the given array, */ |
796 | /* find a suitable new size for the given array, */ |
791 | /* hopefully by rounding to a ncie-to-malloc size */ |
797 | /* hopefully by rounding to a nice-to-malloc size */ |
792 | inline_size int |
798 | inline_size int |
793 | array_nextsize (int elem, int cur, int cnt) |
799 | array_nextsize (int elem, int cur, int cnt) |
794 | { |
800 | { |
795 | int ncur = cur + 1; |
801 | int ncur = cur + 1; |
796 | |
802 | |
… | |
… | |
937 | { |
943 | { |
938 | int fd = fdchanges [i]; |
944 | int fd = fdchanges [i]; |
939 | ANFD *anfd = anfds + fd; |
945 | ANFD *anfd = anfds + fd; |
940 | ev_io *w; |
946 | ev_io *w; |
941 | |
947 | |
942 | unsigned char events = 0; |
948 | unsigned char o_events = anfd->events; |
|
|
949 | unsigned char o_reify = anfd->reify; |
943 | |
950 | |
944 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
951 | anfd->reify = 0; |
945 | events |= (unsigned char)w->events; |
|
|
946 | |
952 | |
947 | #if EV_SELECT_IS_WINSOCKET |
953 | #if EV_SELECT_IS_WINSOCKET |
948 | if (events) |
954 | if (o_reify & EV__IOFDSET) |
949 | { |
955 | { |
950 | unsigned long arg; |
956 | unsigned long arg; |
951 | anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); |
957 | anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); |
952 | 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)); |
953 | } |
959 | } |
954 | #endif |
960 | #endif |
955 | |
961 | |
|
|
962 | /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */ |
956 | { |
963 | { |
957 | unsigned char o_events = anfd->events; |
|
|
958 | unsigned char o_reify = anfd->reify; |
|
|
959 | |
|
|
960 | anfd->reify = 0; |
|
|
961 | anfd->events = events; |
964 | anfd->events = 0; |
962 | |
965 | |
963 | 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) |
964 | backend_modify (EV_A_ fd, o_events, events); |
974 | backend_modify (EV_A_ fd, o_events, anfd->events); |
965 | } |
|
|
966 | } |
975 | } |
967 | |
976 | |
968 | fdchangecnt = 0; |
977 | fdchangecnt = 0; |
969 | } |
978 | } |
970 | |
979 | |
… | |
… | |
1063 | } |
1072 | } |
1064 | |
1073 | |
1065 | /*****************************************************************************/ |
1074 | /*****************************************************************************/ |
1066 | |
1075 | |
1067 | /* |
1076 | /* |
1068 | * 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 |
1069 | * 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 |
1070 | * the branching factor of the d-tree. |
1079 | * the branching factor of the d-tree. |
1071 | */ |
1080 | */ |
1072 | |
1081 | |
1073 | /* |
1082 | /* |
… | |
… | |
1273 | uint64_t counter = 1; |
1282 | uint64_t counter = 1; |
1274 | write (evfd, &counter, sizeof (uint64_t)); |
1283 | write (evfd, &counter, sizeof (uint64_t)); |
1275 | } |
1284 | } |
1276 | else |
1285 | else |
1277 | #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. */ |
1278 | write (evpipe [1], &dummy, 1); |
1292 | write (evpipe [1], &dummy, 1); |
1279 | |
1293 | |
1280 | errno = old_errno; |
1294 | errno = old_errno; |
1281 | } |
1295 | } |
1282 | } |
1296 | } |
… | |
… | |
1296 | } |
1310 | } |
1297 | else |
1311 | else |
1298 | #endif |
1312 | #endif |
1299 | { |
1313 | { |
1300 | char dummy; |
1314 | char dummy; |
|
|
1315 | /* see discussion in evpipe_write when you think this read should be recv in win32 */ |
1301 | read (evpipe [0], &dummy, 1); |
1316 | read (evpipe [0], &dummy, 1); |
1302 | } |
1317 | } |
1303 | |
1318 | |
1304 | if (sig_pending) |
1319 | if (sig_pending) |
1305 | { |
1320 | { |
… | |
… | |
2171 | feed_reverse_done (EV_A_ EV_PERIODIC); |
2186 | feed_reverse_done (EV_A_ EV_PERIODIC); |
2172 | } |
2187 | } |
2173 | } |
2188 | } |
2174 | |
2189 | |
2175 | /* simply recalculate all periodics */ |
2190 | /* simply recalculate all periodics */ |
2176 | /* 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? */ |
2177 | static void noinline |
2192 | static void noinline |
2178 | periodics_reschedule (EV_P) |
2193 | periodics_reschedule (EV_P) |
2179 | { |
2194 | { |
2180 | int i; |
2195 | int i; |
2181 | |
2196 | |
… | |
… | |
2277 | mn_now = ev_rt_now; |
2292 | mn_now = ev_rt_now; |
2278 | } |
2293 | } |
2279 | } |
2294 | } |
2280 | |
2295 | |
2281 | void |
2296 | void |
2282 | ev_loop (EV_P_ int flags) |
2297 | ev_run (EV_P_ int flags) |
2283 | { |
2298 | { |
2284 | #if EV_FEATURE_API |
2299 | #if EV_FEATURE_API |
2285 | ++loop_depth; |
2300 | ++loop_depth; |
2286 | #endif |
2301 | #endif |
2287 | |
2302 | |
2288 | assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE)); |
2303 | assert (("libev: ev_loop recursion during release detected", loop_done != EVBREAK_RECURSE)); |
2289 | |
2304 | |
2290 | loop_done = EVUNLOOP_CANCEL; |
2305 | loop_done = EVBREAK_CANCEL; |
2291 | |
2306 | |
2292 | EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */ |
2307 | EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */ |
2293 | |
2308 | |
2294 | do |
2309 | do |
2295 | { |
2310 | { |
… | |
… | |
2338 | /* calculate blocking time */ |
2353 | /* calculate blocking time */ |
2339 | { |
2354 | { |
2340 | ev_tstamp waittime = 0.; |
2355 | ev_tstamp waittime = 0.; |
2341 | ev_tstamp sleeptime = 0.; |
2356 | ev_tstamp sleeptime = 0.; |
2342 | |
2357 | |
|
|
2358 | /* remember old timestamp for io_blocktime calculation */ |
|
|
2359 | ev_tstamp prev_mn_now = mn_now; |
|
|
2360 | |
|
|
2361 | /* update time to cancel out callback processing overhead */ |
|
|
2362 | time_update (EV_A_ 1e100); |
|
|
2363 | |
2343 | if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) |
2364 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt))) |
2344 | { |
2365 | { |
2345 | /* remember old timestamp for io_blocktime calculation */ |
|
|
2346 | ev_tstamp prev_mn_now = mn_now; |
|
|
2347 | |
|
|
2348 | /* update time to cancel out callback processing overhead */ |
|
|
2349 | time_update (EV_A_ 1e100); |
|
|
2350 | |
|
|
2351 | waittime = MAX_BLOCKTIME; |
2366 | waittime = MAX_BLOCKTIME; |
2352 | |
2367 | |
2353 | if (timercnt) |
2368 | if (timercnt) |
2354 | { |
2369 | { |
2355 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; |
2370 | ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; |
… | |
… | |
2385 | } |
2400 | } |
2386 | |
2401 | |
2387 | #if EV_FEATURE_API |
2402 | #if EV_FEATURE_API |
2388 | ++loop_count; |
2403 | ++loop_count; |
2389 | #endif |
2404 | #endif |
2390 | assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */ |
2405 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2391 | backend_poll (EV_A_ waittime); |
2406 | backend_poll (EV_A_ waittime); |
2392 | assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */ |
2407 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
2393 | |
2408 | |
2394 | /* update ev_rt_now, do magic */ |
2409 | /* update ev_rt_now, do magic */ |
2395 | time_update (EV_A_ waittime + sleeptime); |
2410 | time_update (EV_A_ waittime + sleeptime); |
2396 | } |
2411 | } |
2397 | |
2412 | |
… | |
… | |
2415 | EV_INVOKE_PENDING; |
2430 | EV_INVOKE_PENDING; |
2416 | } |
2431 | } |
2417 | while (expect_true ( |
2432 | while (expect_true ( |
2418 | activecnt |
2433 | activecnt |
2419 | && !loop_done |
2434 | && !loop_done |
2420 | && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) |
2435 | && !(flags & (EVRUN_ONCE | EVRUN_NOWAIT)) |
2421 | )); |
2436 | )); |
2422 | |
2437 | |
2423 | if (loop_done == EVUNLOOP_ONE) |
2438 | if (loop_done == EVBREAK_ONE) |
2424 | loop_done = EVUNLOOP_CANCEL; |
2439 | loop_done = EVBREAK_CANCEL; |
2425 | |
2440 | |
2426 | #if EV_FEATURE_API |
2441 | #if EV_FEATURE_API |
2427 | --loop_depth; |
2442 | --loop_depth; |
2428 | #endif |
2443 | #endif |
2429 | } |
2444 | } |
2430 | |
2445 | |
2431 | void |
2446 | void |
2432 | ev_unloop (EV_P_ int how) |
2447 | ev_break (EV_P_ int how) |
2433 | { |
2448 | { |
2434 | loop_done = how; |
2449 | loop_done = how; |
2435 | } |
2450 | } |
2436 | |
2451 | |
2437 | void |
2452 | void |
… | |
… | |
2585 | EV_FREQUENT_CHECK; |
2600 | EV_FREQUENT_CHECK; |
2586 | |
2601 | |
2587 | wlist_del (&anfds[w->fd].head, (WL)w); |
2602 | wlist_del (&anfds[w->fd].head, (WL)w); |
2588 | ev_stop (EV_A_ (W)w); |
2603 | ev_stop (EV_A_ (W)w); |
2589 | |
2604 | |
2590 | fd_change (EV_A_ w->fd, 1); |
2605 | fd_change (EV_A_ w->fd, EV_ANFD_REIFY); |
2591 | |
2606 | |
2592 | EV_FREQUENT_CHECK; |
2607 | EV_FREQUENT_CHECK; |
2593 | } |
2608 | } |
2594 | |
2609 | |
2595 | void noinline |
2610 | void noinline |
… | |
… | |
3412 | |
3427 | |
3413 | #if EV_EMBED_ENABLE |
3428 | #if EV_EMBED_ENABLE |
3414 | void noinline |
3429 | void noinline |
3415 | ev_embed_sweep (EV_P_ ev_embed *w) |
3430 | ev_embed_sweep (EV_P_ ev_embed *w) |
3416 | { |
3431 | { |
3417 | ev_loop (w->other, EVLOOP_NONBLOCK); |
3432 | ev_run (w->other, EVRUN_NOWAIT); |
3418 | } |
3433 | } |
3419 | |
3434 | |
3420 | static void |
3435 | static void |
3421 | embed_io_cb (EV_P_ ev_io *io, int revents) |
3436 | embed_io_cb (EV_P_ ev_io *io, int revents) |
3422 | { |
3437 | { |
3423 | ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); |
3438 | ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); |
3424 | |
3439 | |
3425 | if (ev_cb (w)) |
3440 | if (ev_cb (w)) |
3426 | ev_feed_event (EV_A_ (W)w, EV_EMBED); |
3441 | ev_feed_event (EV_A_ (W)w, EV_EMBED); |
3427 | else |
3442 | else |
3428 | ev_loop (w->other, EVLOOP_NONBLOCK); |
3443 | ev_run (w->other, EVRUN_NOWAIT); |
3429 | } |
3444 | } |
3430 | |
3445 | |
3431 | static void |
3446 | static void |
3432 | embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) |
3447 | embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) |
3433 | { |
3448 | { |
… | |
… | |
3437 | EV_P = w->other; |
3452 | EV_P = w->other; |
3438 | |
3453 | |
3439 | while (fdchangecnt) |
3454 | while (fdchangecnt) |
3440 | { |
3455 | { |
3441 | fd_reify (EV_A); |
3456 | fd_reify (EV_A); |
3442 | ev_loop (EV_A_ EVLOOP_NONBLOCK); |
3457 | ev_run (EV_A_ EVRUN_NOWAIT); |
3443 | } |
3458 | } |
3444 | } |
3459 | } |
3445 | } |
3460 | } |
3446 | |
3461 | |
3447 | static void |
3462 | static void |
… | |
… | |
3453 | |
3468 | |
3454 | { |
3469 | { |
3455 | EV_P = w->other; |
3470 | EV_P = w->other; |
3456 | |
3471 | |
3457 | ev_loop_fork (EV_A); |
3472 | ev_loop_fork (EV_A); |
3458 | ev_loop (EV_A_ EVLOOP_NONBLOCK); |
3473 | ev_run (EV_A_ EVRUN_NOWAIT); |
3459 | } |
3474 | } |
3460 | |
3475 | |
3461 | ev_embed_start (EV_A_ w); |
3476 | ev_embed_start (EV_A_ w); |
3462 | } |
3477 | } |
3463 | |
3478 | |
… | |
… | |
3561 | void |
3576 | void |
3562 | ev_async_start (EV_P_ ev_async *w) |
3577 | ev_async_start (EV_P_ ev_async *w) |
3563 | { |
3578 | { |
3564 | if (expect_false (ev_is_active (w))) |
3579 | if (expect_false (ev_is_active (w))) |
3565 | return; |
3580 | return; |
|
|
3581 | |
|
|
3582 | w->sent = 0; |
3566 | |
3583 | |
3567 | evpipe_init (EV_A); |
3584 | evpipe_init (EV_A); |
3568 | |
3585 | |
3569 | EV_FREQUENT_CHECK; |
3586 | EV_FREQUENT_CHECK; |
3570 | |
3587 | |