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.352 by root, Thu Oct 21 02:33:08 2010 UTC

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 */
509static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */ 519static 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
513static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 523static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
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 */
792inline_size int 798inline_size int
793array_nextsize (int elem, int cur, int cnt) 799array_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? */
2177static void noinline 2192static void noinline
2178periodics_reschedule (EV_P) 2193periodics_reschedule (EV_P)
2179{ 2194{
2180 int i; 2195 int i;
2181 2196
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
2595void noinline 2610void noinline
3562ev_async_start (EV_P_ ev_async *w) 3577ev_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;
3566 3581
3582 w->sent = 0;
3583
3567 evpipe_init (EV_A); 3584 evpipe_init (EV_A);
3568 3585
3569 EV_FREQUENT_CHECK; 3586 EV_FREQUENT_CHECK;
3570 3587
3571 ev_start (EV_A_ (W)w, ++asynccnt); 3588 ev_start (EV_A_ (W)w, ++asynccnt);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines