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

Comparing libev/ev.c (file contents):
Revision 1.343 by root, Fri Apr 2 21:03:46 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
195# ifndef EV_SELECT_IS_WINSOCKET 193# ifndef EV_SELECT_IS_WINSOCKET
196# define EV_SELECT_IS_WINSOCKET 1 194# define EV_SELECT_IS_WINSOCKET 1
197# endif 195# endif
198# undef EV_AVOID_STDIO 196# undef EV_AVOID_STDIO
199#endif 197#endif
198
199/* OS X, in its infinite idiocy, actually HARDCODES
200 * a limit of 1024 into their select. Where people have brains,
201 * OS X engineers apparently have a vacuum. Or maybe they were
202 * ordered to have a vacuum, or they do anything for money.
203 * This might help. Or not.
204 */
205#define _DARWIN_UNLIMITED_SELECT 1
200 206
201/* this block tries to deduce configuration from header-defined symbols and defaults */ 207/* this block tries to deduce configuration from header-defined symbols and defaults */
202 208
203/* try to deduce the maximum number of signals on this platform */ 209/* try to deduce the maximum number of signals on this platform */
204#if defined (EV_NSIG) 210#if defined (EV_NSIG)
403# define EFD_CLOEXEC O_CLOEXEC 409# define EFD_CLOEXEC O_CLOEXEC
404# else 410# else
405# define EFD_CLOEXEC 02000000 411# define EFD_CLOEXEC 02000000
406# endif 412# endif
407# endif 413# endif
408# ifdef __cplusplus
409extern "C" {
410# endif
411int (eventfd) (unsigned int initval, int flags); 414EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags);
412# ifdef __cplusplus
413}
414# endif
415#endif 415#endif
416 416
417#if EV_USE_SIGNALFD 417#if EV_USE_SIGNALFD
418/* 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 */
419# include <stdint.h> 419# include <stdint.h>
425# define SFD_CLOEXEC O_CLOEXEC 425# define SFD_CLOEXEC O_CLOEXEC
426# else 426# else
427# define SFD_CLOEXEC 02000000 427# define SFD_CLOEXEC 02000000
428# endif 428# endif
429# endif 429# endif
430# ifdef __cplusplus
431extern "C" {
432# endif
433int signalfd (int fd, const sigset_t *mask, int flags); 430EV_CPP (extern "C") int signalfd (int fd, const sigset_t *mask, int flags);
434 431
435struct signalfd_siginfo 432struct signalfd_siginfo
436{ 433{
437 uint32_t ssi_signo; 434 uint32_t ssi_signo;
438 char pad[128 - sizeof (uint32_t)]; 435 char pad[128 - sizeof (uint32_t)];
439}; 436};
440# ifdef __cplusplus
441}
442# endif 437#endif
443#endif
444
445 438
446/**/ 439/**/
447 440
448#if EV_VERIFY >= 3 441#if EV_VERIFY >= 3
449# define EV_FREQUENT_CHECK ev_verify (EV_A) 442# define EV_FREQUENT_CHECK ev_verify (EV_A)
462#define TIME_EPSILON 0.0001220703125 /* 1/8192 */ 455#define TIME_EPSILON 0.0001220703125 /* 1/8192 */
463 456
464#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) */
465#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) */
466 459
460#define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } 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)
462
467#if __GNUC__ >= 4 463#if __GNUC__ >= 4
468# define expect(expr,value) __builtin_expect ((expr),(value)) 464# define expect(expr,value) __builtin_expect ((expr),(value))
469# define noinline __attribute__ ((noinline)) 465# define noinline __attribute__ ((noinline))
470#else 466#else
471# define expect(expr,value) (expr) 467# define expect(expr,value) (expr)
503#define ev_active(w) ((W)(w))->active 499#define ev_active(w) ((W)(w))->active
504#define ev_at(w) ((WT)(w))->at 500#define ev_at(w) ((WT)(w))->at
505 501
506#if EV_USE_REALTIME 502#if EV_USE_REALTIME
507/* sig_atomic_t is used to avoid per-thread variables or locking but still */ 503/* 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 */ 504/* giving it a reasonably high chance of working on typical architectures */
509static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */ 505static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */
510#endif 506#endif
511 507
512#if EV_USE_MONOTONIC 508#if EV_USE_MONOTONIC
513static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 509static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
707# define EV_RELEASE_CB (void)0 703# define EV_RELEASE_CB (void)0
708# define EV_ACQUIRE_CB (void)0 704# define EV_ACQUIRE_CB (void)0
709# define EV_INVOKE_PENDING ev_invoke_pending (EV_A) 705# define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
710#endif 706#endif
711 707
712#define EVUNLOOP_RECURSE 0x80 708#define EVBREAK_RECURSE 0x80
713 709
714/*****************************************************************************/ 710/*****************************************************************************/
715 711
716#ifndef EV_HAVE_EV_TIME 712#ifndef EV_HAVE_EV_TIME
717ev_tstamp 713ev_tstamp
761 if (delay > 0.) 757 if (delay > 0.)
762 { 758 {
763#if EV_USE_NANOSLEEP 759#if EV_USE_NANOSLEEP
764 struct timespec ts; 760 struct timespec ts;
765 761
766 ts.tv_sec = (time_t)delay; 762 EV_TS_SET (ts, delay);
767 ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9);
768
769 nanosleep (&ts, 0); 763 nanosleep (&ts, 0);
770#elif defined(_WIN32) 764#elif defined(_WIN32)
771 Sleep ((unsigned long)(delay * 1e3)); 765 Sleep ((unsigned long)(delay * 1e3));
772#else 766#else
773 struct timeval tv; 767 struct timeval tv;
774 768
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 */ 769 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
779 /* something not guaranteed by newer posix versions, but guaranteed */ 770 /* something not guaranteed by newer posix versions, but guaranteed */
780 /* by older ones */ 771 /* by older ones */
772 EV_TV_SET (tv, delay);
781 select (0, 0, 0, 0, &tv); 773 select (0, 0, 0, 0, &tv);
782#endif 774#endif
783 } 775 }
784} 776}
785 777
786/*****************************************************************************/ 778/*****************************************************************************/
787 779
788#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ 780#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
789 781
790/* find a suitable new size for the given array, */ 782/* find a suitable new size for the given array, */
791/* hopefully by rounding to a ncie-to-malloc size */ 783/* hopefully by rounding to a nice-to-malloc size */
792inline_size int 784inline_size int
793array_nextsize (int elem, int cur, int cnt) 785array_nextsize (int elem, int cur, int cnt)
794{ 786{
795 int ncur = cur + 1; 787 int ncur = cur + 1;
796 788
937 { 929 {
938 int fd = fdchanges [i]; 930 int fd = fdchanges [i];
939 ANFD *anfd = anfds + fd; 931 ANFD *anfd = anfds + fd;
940 ev_io *w; 932 ev_io *w;
941 933
942 unsigned char events = 0; 934 unsigned char o_events = anfd->events;
935 unsigned char o_reify = anfd->reify;
943 936
944 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) 937 anfd->reify = 0;
945 events |= (unsigned char)w->events;
946 938
947#if EV_SELECT_IS_WINSOCKET 939#if EV_SELECT_IS_WINSOCKET
948 if (events) 940 if (o_reify & EV__IOFDSET)
949 { 941 {
950 unsigned long arg; 942 unsigned long arg;
951 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 943 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
952 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));
953 } 945 }
954#endif 946#endif
955 947
948 /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */
956 { 949 {
957 unsigned char o_events = anfd->events;
958 unsigned char o_reify = anfd->reify;
959
960 anfd->reify = 0;
961 anfd->events = events; 950 anfd->events = 0;
962 951
963 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)
964 backend_modify (EV_A_ fd, o_events, events); 960 backend_modify (EV_A_ fd, o_events, anfd->events);
965 }
966 } 961 }
967 962
968 fdchangecnt = 0; 963 fdchangecnt = 0;
969} 964}
970 965
1063} 1058}
1064 1059
1065/*****************************************************************************/ 1060/*****************************************************************************/
1066 1061
1067/* 1062/*
1068 * the heap functions want a real array index. array index 0 uis guaranteed to not 1063 * 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 1064 * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives
1070 * the branching factor of the d-tree. 1065 * the branching factor of the d-tree.
1071 */ 1066 */
1072 1067
1073/* 1068/*
1273 uint64_t counter = 1; 1268 uint64_t counter = 1;
1274 write (evfd, &counter, sizeof (uint64_t)); 1269 write (evfd, &counter, sizeof (uint64_t));
1275 } 1270 }
1276 else 1271 else
1277#endif 1272#endif
1273 /* win32 people keep sending patches that change this write() to send() */
1274 /* and then run away. but send() is wrong, it wants a socket handle on win32 */
1275 /* so when you think this write should be a send instead, please find out */
1276 /* where your send() is from - it's definitely not the microsoft send, and */
1277 /* tell me. thank you. */
1278 write (evpipe [1], &dummy, 1); 1278 write (evpipe [1], &dummy, 1);
1279 1279
1280 errno = old_errno; 1280 errno = old_errno;
1281 } 1281 }
1282} 1282}
1296 } 1296 }
1297 else 1297 else
1298#endif 1298#endif
1299 { 1299 {
1300 char dummy; 1300 char dummy;
1301 /* see discussion in evpipe_write when you think this read should be recv in win32 */
1301 read (evpipe [0], &dummy, 1); 1302 read (evpipe [0], &dummy, 1);
1302 } 1303 }
1303 1304
1304 if (sig_pending) 1305 if (sig_pending)
1305 { 1306 {
2171 feed_reverse_done (EV_A_ EV_PERIODIC); 2172 feed_reverse_done (EV_A_ EV_PERIODIC);
2172 } 2173 }
2173} 2174}
2174 2175
2175/* simply recalculate all periodics */ 2176/* simply recalculate all periodics */
2176/* TODO: maybe ensure that at leats one event happens when jumping forward? */ 2177/* TODO: maybe ensure that at least one event happens when jumping forward? */
2177static void noinline 2178static void noinline
2178periodics_reschedule (EV_P) 2179periodics_reschedule (EV_P)
2179{ 2180{
2180 int i; 2181 int i;
2181 2182
2277 mn_now = ev_rt_now; 2278 mn_now = ev_rt_now;
2278 } 2279 }
2279} 2280}
2280 2281
2281void 2282void
2282ev_loop (EV_P_ int flags) 2283ev_run (EV_P_ int flags)
2283{ 2284{
2284#if EV_FEATURE_API 2285#if EV_FEATURE_API
2285 ++loop_depth; 2286 ++loop_depth;
2286#endif 2287#endif
2287 2288
2288 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));
2289 2290
2290 loop_done = EVUNLOOP_CANCEL; 2291 loop_done = EVBREAK_CANCEL;
2291 2292
2292 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 */
2293 2294
2294 do 2295 do
2295 { 2296 {
2338 /* calculate blocking time */ 2339 /* calculate blocking time */
2339 { 2340 {
2340 ev_tstamp waittime = 0.; 2341 ev_tstamp waittime = 0.;
2341 ev_tstamp sleeptime = 0.; 2342 ev_tstamp sleeptime = 0.;
2342 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
2343 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2350 if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt)))
2344 { 2351 {
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; 2352 waittime = MAX_BLOCKTIME;
2352 2353
2353 if (timercnt) 2354 if (timercnt)
2354 { 2355 {
2355 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; 2356 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
2385 } 2386 }
2386 2387
2387#if EV_FEATURE_API 2388#if EV_FEATURE_API
2388 ++loop_count; 2389 ++loop_count;
2389#endif 2390#endif
2390 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */ 2391 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
2391 backend_poll (EV_A_ waittime); 2392 backend_poll (EV_A_ waittime);
2392 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */ 2393 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
2393 2394
2394 /* update ev_rt_now, do magic */ 2395 /* update ev_rt_now, do magic */
2395 time_update (EV_A_ waittime + sleeptime); 2396 time_update (EV_A_ waittime + sleeptime);
2396 } 2397 }
2397 2398
2415 EV_INVOKE_PENDING; 2416 EV_INVOKE_PENDING;
2416 } 2417 }
2417 while (expect_true ( 2418 while (expect_true (
2418 activecnt 2419 activecnt
2419 && !loop_done 2420 && !loop_done
2420 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2421 && !(flags & (EVRUN_ONCE | EVRUN_NOWAIT))
2421 )); 2422 ));
2422 2423
2423 if (loop_done == EVUNLOOP_ONE) 2424 if (loop_done == EVBREAK_ONE)
2424 loop_done = EVUNLOOP_CANCEL; 2425 loop_done = EVBREAK_CANCEL;
2425 2426
2426#if EV_FEATURE_API 2427#if EV_FEATURE_API
2427 --loop_depth; 2428 --loop_depth;
2428#endif 2429#endif
2429} 2430}
2430 2431
2431void 2432void
2432ev_unloop (EV_P_ int how) 2433ev_break (EV_P_ int how)
2433{ 2434{
2434 loop_done = how; 2435 loop_done = how;
2435} 2436}
2436 2437
2437void 2438void
2585 EV_FREQUENT_CHECK; 2586 EV_FREQUENT_CHECK;
2586 2587
2587 wlist_del (&anfds[w->fd].head, (WL)w); 2588 wlist_del (&anfds[w->fd].head, (WL)w);
2588 ev_stop (EV_A_ (W)w); 2589 ev_stop (EV_A_ (W)w);
2589 2590
2590 fd_change (EV_A_ w->fd, 1); 2591 fd_change (EV_A_ w->fd, EV_ANFD_REIFY);
2591 2592
2592 EV_FREQUENT_CHECK; 2593 EV_FREQUENT_CHECK;
2593} 2594}
2594 2595
2595void noinline 2596void noinline
3412 3413
3413#if EV_EMBED_ENABLE 3414#if EV_EMBED_ENABLE
3414void noinline 3415void noinline
3415ev_embed_sweep (EV_P_ ev_embed *w) 3416ev_embed_sweep (EV_P_ ev_embed *w)
3416{ 3417{
3417 ev_loop (w->other, EVLOOP_NONBLOCK); 3418 ev_run (w->other, EVRUN_NOWAIT);
3418} 3419}
3419 3420
3420static void 3421static void
3421embed_io_cb (EV_P_ ev_io *io, int revents) 3422embed_io_cb (EV_P_ ev_io *io, int revents)
3422{ 3423{
3423 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); 3424 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
3424 3425
3425 if (ev_cb (w)) 3426 if (ev_cb (w))
3426 ev_feed_event (EV_A_ (W)w, EV_EMBED); 3427 ev_feed_event (EV_A_ (W)w, EV_EMBED);
3427 else 3428 else
3428 ev_loop (w->other, EVLOOP_NONBLOCK); 3429 ev_run (w->other, EVRUN_NOWAIT);
3429} 3430}
3430 3431
3431static void 3432static void
3432embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) 3433embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents)
3433{ 3434{
3437 EV_P = w->other; 3438 EV_P = w->other;
3438 3439
3439 while (fdchangecnt) 3440 while (fdchangecnt)
3440 { 3441 {
3441 fd_reify (EV_A); 3442 fd_reify (EV_A);
3442 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3443 ev_run (EV_A_ EVRUN_NOWAIT);
3443 } 3444 }
3444 } 3445 }
3445} 3446}
3446 3447
3447static void 3448static void
3453 3454
3454 { 3455 {
3455 EV_P = w->other; 3456 EV_P = w->other;
3456 3457
3457 ev_loop_fork (EV_A); 3458 ev_loop_fork (EV_A);
3458 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3459 ev_run (EV_A_ EVRUN_NOWAIT);
3459 } 3460 }
3460 3461
3461 ev_embed_start (EV_A_ w); 3462 ev_embed_start (EV_A_ w);
3462} 3463}
3463 3464
3561void 3562void
3562ev_async_start (EV_P_ ev_async *w) 3563ev_async_start (EV_P_ ev_async *w)
3563{ 3564{
3564 if (expect_false (ev_is_active (w))) 3565 if (expect_false (ev_is_active (w)))
3565 return; 3566 return;
3567
3568 w->sent = 0;
3566 3569
3567 evpipe_init (EV_A); 3570 evpipe_init (EV_A);
3568 3571
3569 EV_FREQUENT_CHECK; 3572 EV_FREQUENT_CHECK;
3570 3573
3790 3793
3791#if EV_MULTIPLICITY 3794#if EV_MULTIPLICITY
3792 #include "ev_wrap.h" 3795 #include "ev_wrap.h"
3793#endif 3796#endif
3794 3797
3795#ifdef __cplusplus 3798EV_CPP(})
3796}
3797#endif
3798 3799

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines