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

Comparing libev/ev.c (file contents):
Revision 1.347 by root, Fri Oct 15 22:44:41 2010 UTC vs.
Revision 1.354 by root, Fri Oct 22 09:24:11 2010 UTC

35 * and other provisions required by the GPL. If you do not delete the 35 * and other provisions required by the GPL. If you do not delete the
36 * provisions above, a recipient may use your version of this file under 36 * provisions above, a recipient may use your version of this file under
37 * either the BSD or the GPL. 37 * either the BSD or the GPL.
38 */ 38 */
39 39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/* this big block deduces configuration from config.h */ 40/* this big block deduces configuration from config.h */
45#ifndef EV_STANDALONE 41#ifndef EV_STANDALONE
46# ifdef EV_CONFIG_H 42# ifdef EV_CONFIG_H
47# include EV_CONFIG_H 43# include EV_CONFIG_H
48# else 44# else
182# include EV_H 178# include EV_H
183#else 179#else
184# include "ev.h" 180# include "ev.h"
185#endif 181#endif
186 182
183EV_CPP(extern "C" {)
184
187#ifndef _WIN32 185#ifndef _WIN32
188# include <sys/time.h> 186# include <sys/time.h>
189# include <sys/wait.h> 187# include <sys/wait.h>
190# include <unistd.h> 188# include <unistd.h>
191#else 189#else
411# define EFD_CLOEXEC O_CLOEXEC 409# define EFD_CLOEXEC O_CLOEXEC
412# else 410# else
413# define EFD_CLOEXEC 02000000 411# define EFD_CLOEXEC 02000000
414# endif 412# endif
415# endif 413# endif
416# ifdef __cplusplus
417extern "C" {
418# endif
419int (eventfd) (unsigned int initval, int flags); 414EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags);
420# ifdef __cplusplus
421}
422# endif
423#endif 415#endif
424 416
425#if EV_USE_SIGNALFD 417#if EV_USE_SIGNALFD
426/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 418/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
427# include <stdint.h> 419# include <stdint.h>
433# define SFD_CLOEXEC O_CLOEXEC 425# define SFD_CLOEXEC O_CLOEXEC
434# else 426# else
435# define SFD_CLOEXEC 02000000 427# define SFD_CLOEXEC 02000000
436# endif 428# endif
437# endif 429# endif
438# ifdef __cplusplus
439extern "C" {
440# endif
441int signalfd (int fd, const sigset_t *mask, int flags); 430EV_CPP (extern "C") int signalfd (int fd, const sigset_t *mask, int flags);
442 431
443struct signalfd_siginfo 432struct signalfd_siginfo
444{ 433{
445 uint32_t ssi_signo; 434 uint32_t ssi_signo;
446 char pad[128 - sizeof (uint32_t)]; 435 char pad[128 - sizeof (uint32_t)];
447}; 436};
448# ifdef __cplusplus
449}
450# endif
451#endif 437#endif
452 438
453/**/ 439/**/
454 440
455#if EV_VERIFY >= 3 441#if EV_VERIFY >= 3
470 456
471#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 457#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
472#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 458#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
473 459
474#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_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; tv.tv_nsec = (long)((t - tv.tv_sec) * 1e9); } while (0) 461#define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0)
476 462
477#if __GNUC__ >= 4 463#if __GNUC__ >= 4
478# define expect(expr,value) __builtin_expect ((expr),(value)) 464# define expect(expr,value) __builtin_expect ((expr),(value))
479# define noinline __attribute__ ((noinline)) 465# define noinline __attribute__ ((noinline))
480#else 466#else
717# define EV_RELEASE_CB (void)0 703# define EV_RELEASE_CB (void)0
718# define EV_ACQUIRE_CB (void)0 704# define EV_ACQUIRE_CB (void)0
719# define EV_INVOKE_PENDING ev_invoke_pending (EV_A) 705# define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
720#endif 706#endif
721 707
722#define EVUNLOOP_RECURSE 0x80 708#define EVBREAK_RECURSE 0x80
723 709
724/*****************************************************************************/ 710/*****************************************************************************/
725 711
726#ifndef EV_HAVE_EV_TIME 712#ifndef EV_HAVE_EV_TIME
727ev_tstamp 713ev_tstamp
771 if (delay > 0.) 757 if (delay > 0.)
772 { 758 {
773#if EV_USE_NANOSLEEP 759#if EV_USE_NANOSLEEP
774 struct timespec ts; 760 struct timespec ts;
775 761
776 EV_SET_TS (ts, delay); 762 EV_TS_SET (ts, delay);
777 nanosleep (&ts, 0); 763 nanosleep (&ts, 0);
778#elif defined(_WIN32) 764#elif defined(_WIN32)
779 Sleep ((unsigned long)(delay * 1e3)); 765 Sleep ((unsigned long)(delay * 1e3));
780#else 766#else
781 struct timeval tv; 767 struct timeval tv;
782 768
783 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 769 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
784 /* something not guaranteed by newer posix versions, but guaranteed */ 770 /* something not guaranteed by newer posix versions, but guaranteed */
785 /* by older ones */ 771 /* by older ones */
786 EV_SET_TV (tv, delay); 772 EV_TV_SET (tv, delay);
787 select (0, 0, 0, 0, &tv); 773 select (0, 0, 0, 0, &tv);
788#endif 774#endif
789 } 775 }
790} 776}
791 777
943 { 929 {
944 int fd = fdchanges [i]; 930 int fd = fdchanges [i];
945 ANFD *anfd = anfds + fd; 931 ANFD *anfd = anfds + fd;
946 ev_io *w; 932 ev_io *w;
947 933
948 unsigned char events = 0; 934 unsigned char o_events = anfd->events;
935 unsigned char o_reify = anfd->reify;
949 936
950 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) 937 anfd->reify = 0;
951 events |= (unsigned char)w->events;
952 938
953#if EV_SELECT_IS_WINSOCKET 939#if EV_SELECT_IS_WINSOCKET
954 if (events) 940 if (o_reify & EV__IOFDSET)
955 { 941 {
956 unsigned long arg; 942 unsigned long arg;
957 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 943 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
958 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); 944 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0));
959 } 945 }
960#endif 946#endif
961 947
948 /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */
962 { 949 {
963 unsigned char o_events = anfd->events;
964 unsigned char o_reify = anfd->reify;
965
966 anfd->reify = 0;
967 anfd->events = events; 950 anfd->events = 0;
968 951
969 if (o_events != events || o_reify & EV__IOFDSET) 952 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
953 anfd->events |= (unsigned char)w->events;
954
955 if (o_events != anfd->events)
956 o_reify = EV__IOFDSET; /* actually |= */
957 }
958
959 if (o_reify & EV__IOFDSET)
970 backend_modify (EV_A_ fd, o_events, events); 960 backend_modify (EV_A_ fd, o_events, anfd->events);
971 }
972 } 961 }
973 962
974 fdchangecnt = 0; 963 fdchangecnt = 0;
975} 964}
976 965
2289 mn_now = ev_rt_now; 2278 mn_now = ev_rt_now;
2290 } 2279 }
2291} 2280}
2292 2281
2293void 2282void
2294ev_loop (EV_P_ int flags) 2283ev_run (EV_P_ int flags)
2295{ 2284{
2296#if EV_FEATURE_API 2285#if EV_FEATURE_API
2297 ++loop_depth; 2286 ++loop_depth;
2298#endif 2287#endif
2299 2288
2300 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE)); 2289 assert (("libev: ev_loop recursion during release detected", loop_done != EVBREAK_RECURSE));
2301 2290
2302 loop_done = EVUNLOOP_CANCEL; 2291 loop_done = EVBREAK_CANCEL;
2303 2292
2304 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */ 2293 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
2305 2294
2306 do 2295 do
2307 { 2296 {
2350 /* calculate blocking time */ 2339 /* calculate blocking time */
2351 { 2340 {
2352 ev_tstamp waittime = 0.; 2341 ev_tstamp waittime = 0.;
2353 ev_tstamp sleeptime = 0.; 2342 ev_tstamp sleeptime = 0.;
2354 2343
2344 /* remember old timestamp for io_blocktime calculation */
2345 ev_tstamp prev_mn_now = mn_now;
2346
2347 /* update time to cancel out callback processing overhead */
2348 time_update (EV_A_ 1e100);
2349
2355 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2350 if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt)))
2356 { 2351 {
2357 /* remember old timestamp for io_blocktime calculation */
2358 ev_tstamp prev_mn_now = mn_now;
2359
2360 /* update time to cancel out callback processing overhead */
2361 time_update (EV_A_ 1e100);
2362
2363 waittime = MAX_BLOCKTIME; 2352 waittime = MAX_BLOCKTIME;
2364 2353
2365 if (timercnt) 2354 if (timercnt)
2366 { 2355 {
2367 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; 2356 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
2397 } 2386 }
2398 2387
2399#if EV_FEATURE_API 2388#if EV_FEATURE_API
2400 ++loop_count; 2389 ++loop_count;
2401#endif 2390#endif
2402 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */ 2391 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
2403 backend_poll (EV_A_ waittime); 2392 backend_poll (EV_A_ waittime);
2404 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */ 2393 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
2405 2394
2406 /* update ev_rt_now, do magic */ 2395 /* update ev_rt_now, do magic */
2407 time_update (EV_A_ waittime + sleeptime); 2396 time_update (EV_A_ waittime + sleeptime);
2408 } 2397 }
2409 2398
2427 EV_INVOKE_PENDING; 2416 EV_INVOKE_PENDING;
2428 } 2417 }
2429 while (expect_true ( 2418 while (expect_true (
2430 activecnt 2419 activecnt
2431 && !loop_done 2420 && !loop_done
2432 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2421 && !(flags & (EVRUN_ONCE | EVRUN_NOWAIT))
2433 )); 2422 ));
2434 2423
2435 if (loop_done == EVUNLOOP_ONE) 2424 if (loop_done == EVBREAK_ONE)
2436 loop_done = EVUNLOOP_CANCEL; 2425 loop_done = EVBREAK_CANCEL;
2437 2426
2438#if EV_FEATURE_API 2427#if EV_FEATURE_API
2439 --loop_depth; 2428 --loop_depth;
2440#endif 2429#endif
2441} 2430}
2442 2431
2443void 2432void
2444ev_unloop (EV_P_ int how) 2433ev_break (EV_P_ int how)
2445{ 2434{
2446 loop_done = how; 2435 loop_done = how;
2447} 2436}
2448 2437
2449void 2438void
2597 EV_FREQUENT_CHECK; 2586 EV_FREQUENT_CHECK;
2598 2587
2599 wlist_del (&anfds[w->fd].head, (WL)w); 2588 wlist_del (&anfds[w->fd].head, (WL)w);
2600 ev_stop (EV_A_ (W)w); 2589 ev_stop (EV_A_ (W)w);
2601 2590
2602 fd_change (EV_A_ w->fd, 1); 2591 fd_change (EV_A_ w->fd, EV_ANFD_REIFY);
2603 2592
2604 EV_FREQUENT_CHECK; 2593 EV_FREQUENT_CHECK;
2605} 2594}
2606 2595
2607void noinline 2596void noinline
3424 3413
3425#if EV_EMBED_ENABLE 3414#if EV_EMBED_ENABLE
3426void noinline 3415void noinline
3427ev_embed_sweep (EV_P_ ev_embed *w) 3416ev_embed_sweep (EV_P_ ev_embed *w)
3428{ 3417{
3429 ev_loop (w->other, EVLOOP_NONBLOCK); 3418 ev_run (w->other, EVRUN_NOWAIT);
3430} 3419}
3431 3420
3432static void 3421static void
3433embed_io_cb (EV_P_ ev_io *io, int revents) 3422embed_io_cb (EV_P_ ev_io *io, int revents)
3434{ 3423{
3435 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); 3424 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
3436 3425
3437 if (ev_cb (w)) 3426 if (ev_cb (w))
3438 ev_feed_event (EV_A_ (W)w, EV_EMBED); 3427 ev_feed_event (EV_A_ (W)w, EV_EMBED);
3439 else 3428 else
3440 ev_loop (w->other, EVLOOP_NONBLOCK); 3429 ev_run (w->other, EVRUN_NOWAIT);
3441} 3430}
3442 3431
3443static void 3432static void
3444embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) 3433embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents)
3445{ 3434{
3449 EV_P = w->other; 3438 EV_P = w->other;
3450 3439
3451 while (fdchangecnt) 3440 while (fdchangecnt)
3452 { 3441 {
3453 fd_reify (EV_A); 3442 fd_reify (EV_A);
3454 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3443 ev_run (EV_A_ EVRUN_NOWAIT);
3455 } 3444 }
3456 } 3445 }
3457} 3446}
3458 3447
3459static void 3448static void
3465 3454
3466 { 3455 {
3467 EV_P = w->other; 3456 EV_P = w->other;
3468 3457
3469 ev_loop_fork (EV_A); 3458 ev_loop_fork (EV_A);
3470 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3459 ev_run (EV_A_ EVRUN_NOWAIT);
3471 } 3460 }
3472 3461
3473 ev_embed_start (EV_A_ w); 3462 ev_embed_start (EV_A_ w);
3474} 3463}
3475 3464
3573void 3562void
3574ev_async_start (EV_P_ ev_async *w) 3563ev_async_start (EV_P_ ev_async *w)
3575{ 3564{
3576 if (expect_false (ev_is_active (w))) 3565 if (expect_false (ev_is_active (w)))
3577 return; 3566 return;
3567
3568 w->sent = 0;
3578 3569
3579 evpipe_init (EV_A); 3570 evpipe_init (EV_A);
3580 3571
3581 EV_FREQUENT_CHECK; 3572 EV_FREQUENT_CHECK;
3582 3573
3802 3793
3803#if EV_MULTIPLICITY 3794#if EV_MULTIPLICITY
3804 #include "ev_wrap.h" 3795 #include "ev_wrap.h"
3805#endif 3796#endif
3806 3797
3807#ifdef __cplusplus 3798EV_CPP(})
3808}
3809#endif
3810 3799

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines