ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.346 by root, Thu Oct 14 05:07:04 2010 UTC vs.
Revision 1.353 by root, Thu Oct 21 12:32:47 2010 UTC

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)
715# define EV_RELEASE_CB (void)0 717# define EV_RELEASE_CB (void)0
716# define EV_ACQUIRE_CB (void)0 718# define EV_ACQUIRE_CB (void)0
717# define EV_INVOKE_PENDING ev_invoke_pending (EV_A) 719# define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
718#endif 720#endif
719 721
720#define EVUNLOOP_RECURSE 0x80 722#define EVBREAK_RECURSE 0x80
721 723
722/*****************************************************************************/ 724/*****************************************************************************/
723 725
724#ifndef EV_HAVE_EV_TIME 726#ifndef EV_HAVE_EV_TIME
725ev_tstamp 727ev_tstamp
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
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
2291 mn_now = ev_rt_now; 2292 mn_now = ev_rt_now;
2292 } 2293 }
2293} 2294}
2294 2295
2295void 2296void
2296ev_loop (EV_P_ int flags) 2297ev_run (EV_P_ int flags)
2297{ 2298{
2298#if EV_FEATURE_API 2299#if EV_FEATURE_API
2299 ++loop_depth; 2300 ++loop_depth;
2300#endif 2301#endif
2301 2302
2302 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));
2303 2304
2304 loop_done = EVUNLOOP_CANCEL; 2305 loop_done = EVBREAK_CANCEL;
2305 2306
2306 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 */
2307 2308
2308 do 2309 do
2309 { 2310 {
2352 /* calculate blocking time */ 2353 /* calculate blocking time */
2353 { 2354 {
2354 ev_tstamp waittime = 0.; 2355 ev_tstamp waittime = 0.;
2355 ev_tstamp sleeptime = 0.; 2356 ev_tstamp sleeptime = 0.;
2356 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
2357 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2364 if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt)))
2358 { 2365 {
2359 /* remember old timestamp for io_blocktime calculation */
2360 ev_tstamp prev_mn_now = mn_now;
2361
2362 /* update time to cancel out callback processing overhead */
2363 time_update (EV_A_ 1e100);
2364
2365 waittime = MAX_BLOCKTIME; 2366 waittime = MAX_BLOCKTIME;
2366 2367
2367 if (timercnt) 2368 if (timercnt)
2368 { 2369 {
2369 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; 2370 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
2399 } 2400 }
2400 2401
2401#if EV_FEATURE_API 2402#if EV_FEATURE_API
2402 ++loop_count; 2403 ++loop_count;
2403#endif 2404#endif
2404 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */ 2405 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
2405 backend_poll (EV_A_ waittime); 2406 backend_poll (EV_A_ waittime);
2406 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */ 2407 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
2407 2408
2408 /* update ev_rt_now, do magic */ 2409 /* update ev_rt_now, do magic */
2409 time_update (EV_A_ waittime + sleeptime); 2410 time_update (EV_A_ waittime + sleeptime);
2410 } 2411 }
2411 2412
2429 EV_INVOKE_PENDING; 2430 EV_INVOKE_PENDING;
2430 } 2431 }
2431 while (expect_true ( 2432 while (expect_true (
2432 activecnt 2433 activecnt
2433 && !loop_done 2434 && !loop_done
2434 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2435 && !(flags & (EVRUN_ONCE | EVRUN_NOWAIT))
2435 )); 2436 ));
2436 2437
2437 if (loop_done == EVUNLOOP_ONE) 2438 if (loop_done == EVBREAK_ONE)
2438 loop_done = EVUNLOOP_CANCEL; 2439 loop_done = EVBREAK_CANCEL;
2439 2440
2440#if EV_FEATURE_API 2441#if EV_FEATURE_API
2441 --loop_depth; 2442 --loop_depth;
2442#endif 2443#endif
2443} 2444}
2444 2445
2445void 2446void
2446ev_unloop (EV_P_ int how) 2447ev_break (EV_P_ int how)
2447{ 2448{
2448 loop_done = how; 2449 loop_done = how;
2449} 2450}
2450 2451
2451void 2452void
2599 EV_FREQUENT_CHECK; 2600 EV_FREQUENT_CHECK;
2600 2601
2601 wlist_del (&anfds[w->fd].head, (WL)w); 2602 wlist_del (&anfds[w->fd].head, (WL)w);
2602 ev_stop (EV_A_ (W)w); 2603 ev_stop (EV_A_ (W)w);
2603 2604
2604 fd_change (EV_A_ w->fd, 1); 2605 fd_change (EV_A_ w->fd, EV_ANFD_REIFY);
2605 2606
2606 EV_FREQUENT_CHECK; 2607 EV_FREQUENT_CHECK;
2607} 2608}
2608 2609
2609void noinline 2610void noinline
3426 3427
3427#if EV_EMBED_ENABLE 3428#if EV_EMBED_ENABLE
3428void noinline 3429void noinline
3429ev_embed_sweep (EV_P_ ev_embed *w) 3430ev_embed_sweep (EV_P_ ev_embed *w)
3430{ 3431{
3431 ev_loop (w->other, EVLOOP_NONBLOCK); 3432 ev_run (w->other, EVRUN_NOWAIT);
3432} 3433}
3433 3434
3434static void 3435static void
3435embed_io_cb (EV_P_ ev_io *io, int revents) 3436embed_io_cb (EV_P_ ev_io *io, int revents)
3436{ 3437{
3437 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); 3438 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
3438 3439
3439 if (ev_cb (w)) 3440 if (ev_cb (w))
3440 ev_feed_event (EV_A_ (W)w, EV_EMBED); 3441 ev_feed_event (EV_A_ (W)w, EV_EMBED);
3441 else 3442 else
3442 ev_loop (w->other, EVLOOP_NONBLOCK); 3443 ev_run (w->other, EVRUN_NOWAIT);
3443} 3444}
3444 3445
3445static void 3446static void
3446embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) 3447embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents)
3447{ 3448{
3451 EV_P = w->other; 3452 EV_P = w->other;
3452 3453
3453 while (fdchangecnt) 3454 while (fdchangecnt)
3454 { 3455 {
3455 fd_reify (EV_A); 3456 fd_reify (EV_A);
3456 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3457 ev_run (EV_A_ EVRUN_NOWAIT);
3457 } 3458 }
3458 } 3459 }
3459} 3460}
3460 3461
3461static void 3462static void
3467 3468
3468 { 3469 {
3469 EV_P = w->other; 3470 EV_P = w->other;
3470 3471
3471 ev_loop_fork (EV_A); 3472 ev_loop_fork (EV_A);
3472 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3473 ev_run (EV_A_ EVRUN_NOWAIT);
3473 } 3474 }
3474 3475
3475 ev_embed_start (EV_A_ w); 3476 ev_embed_start (EV_A_ w);
3476} 3477}
3477 3478
3575void 3576void
3576ev_async_start (EV_P_ ev_async *w) 3577ev_async_start (EV_P_ ev_async *w)
3577{ 3578{
3578 if (expect_false (ev_is_active (w))) 3579 if (expect_false (ev_is_active (w)))
3579 return; 3580 return;
3581
3582 w->sent = 0;
3580 3583
3581 evpipe_init (EV_A); 3584 evpipe_init (EV_A);
3582 3585
3583 EV_FREQUENT_CHECK; 3586 EV_FREQUENT_CHECK;
3584 3587

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines