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

Comparing libev/ev.c (file contents):
Revision 1.289 by root, Sat Jun 6 11:13:16 2009 UTC vs.
Revision 1.298 by root, Fri Jul 10 19:10:19 2009 UTC

57# endif 57# endif
58# ifndef EV_USE_MONOTONIC 58# ifndef EV_USE_MONOTONIC
59# define EV_USE_MONOTONIC 1 59# define EV_USE_MONOTONIC 1
60# endif 60# endif
61# endif 61# endif
62# elif !defined(EV_USE_CLOCK_SYSCALL)
63# define EV_USE_CLOCK_SYSCALL 0
62# endif 64# endif
63 65
64# if HAVE_CLOCK_GETTIME 66# if HAVE_CLOCK_GETTIME
65# ifndef EV_USE_MONOTONIC 67# ifndef EV_USE_MONOTONIC
66# define EV_USE_MONOTONIC 1 68# define EV_USE_MONOTONIC 1
282 284
283#ifndef EV_HEAP_CACHE_AT 285#ifndef EV_HEAP_CACHE_AT
284# define EV_HEAP_CACHE_AT !EV_MINIMAL 286# define EV_HEAP_CACHE_AT !EV_MINIMAL
285#endif 287#endif
286 288
289/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
290/* which makes programs even slower. might work on other unices, too. */
291#if EV_USE_CLOCK_SYSCALL
292# include <syscall.h>
293# ifdef SYS_clock_gettime
294# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
295# undef EV_USE_MONOTONIC
296# define EV_USE_MONOTONIC 1
297# else
298# undef EV_USE_CLOCK_SYSCALL
299# define EV_USE_CLOCK_SYSCALL 0
300# endif
301#endif
302
287/* this block fixes any misconfiguration where we know we run into trouble otherwise */ 303/* this block fixes any misconfiguration where we know we run into trouble otherwise */
288 304
289#ifndef CLOCK_MONOTONIC 305#ifndef CLOCK_MONOTONIC
290# undef EV_USE_MONOTONIC 306# undef EV_USE_MONOTONIC
291# define EV_USE_MONOTONIC 0 307# define EV_USE_MONOTONIC 0
320 336
321#if EV_SELECT_IS_WINSOCKET 337#if EV_SELECT_IS_WINSOCKET
322# include <winsock.h> 338# include <winsock.h>
323#endif 339#endif
324 340
325/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
326/* which makes programs even slower. might work on other unices, too. */
327#if EV_USE_CLOCK_SYSCALL
328# include <syscall.h>
329# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
330# undef EV_USE_MONOTONIC
331# define EV_USE_MONOTONIC 1
332#endif
333
334#if EV_USE_EVENTFD 341#if EV_USE_EVENTFD
335/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 342/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
336# include <stdint.h> 343# include <stdint.h>
337# ifdef __cplusplus 344# ifdef __cplusplus
338extern "C" { 345extern "C" {
384# define inline_speed static noinline 391# define inline_speed static noinline
385#else 392#else
386# define inline_speed static inline 393# define inline_speed static inline
387#endif 394#endif
388 395
389#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 396#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
397
398#if EV_MINPRI == EV_MAXPRI
399# define ABSPRI(w) (((W)w), 0)
400#else
390#define ABSPRI(w) (((W)w)->priority - EV_MINPRI) 401# define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
402#endif
391 403
392#define EMPTY /* required for microsofts broken pseudo-c compiler */ 404#define EMPTY /* required for microsofts broken pseudo-c compiler */
393#define EMPTY2(a,b) /* used to suppress some warnings */ 405#define EMPTY2(a,b) /* used to suppress some warnings */
394 406
395typedef ev_watcher *W; 407typedef ev_watcher *W;
478#define ev_malloc(size) ev_realloc (0, (size)) 490#define ev_malloc(size) ev_realloc (0, (size))
479#define ev_free(ptr) ev_realloc ((ptr), 0) 491#define ev_free(ptr) ev_realloc ((ptr), 0)
480 492
481/*****************************************************************************/ 493/*****************************************************************************/
482 494
495/* set in reify when reification needed */
496#define EV_ANFD_REIFY 1
497
483/* file descriptor info structure */ 498/* file descriptor info structure */
484typedef struct 499typedef struct
485{ 500{
486 WL head; 501 WL head;
487 unsigned char events; /* the events watched for */ 502 unsigned char events; /* the events watched for */
488 unsigned char reify; /* flag set when this ANFD needs reification */ 503 unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */
489 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ 504 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */
490 unsigned char unused; 505 unsigned char unused;
491#if EV_USE_EPOLL 506#if EV_USE_EPOLL
492 unsigned int egen; /* generation counter to counter epoll bugs */ 507 unsigned int egen; /* generation counter to counter epoll bugs */
493#endif 508#endif
555 570
556 static int ev_default_loop_ptr; 571 static int ev_default_loop_ptr;
557 572
558#endif 573#endif
559 574
575#if EV_MINIMAL < 2
576# define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A)
577# define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A)
578# define EV_INVOKE_PENDING invoke_cb (EV_A)
579#else
580# define EV_RELEASE_CB (void)0
581# define EV_ACQUIRE_CB (void)0
582# define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
583#endif
584
585#define EVUNLOOP_RECURSE 0x80
586
560/*****************************************************************************/ 587/*****************************************************************************/
561 588
589#ifndef EV_HAVE_EV_TIME
562ev_tstamp 590ev_tstamp
563ev_time (void) 591ev_time (void)
564{ 592{
565#if EV_USE_REALTIME 593#if EV_USE_REALTIME
566 if (expect_true (have_realtime)) 594 if (expect_true (have_realtime))
573 601
574 struct timeval tv; 602 struct timeval tv;
575 gettimeofday (&tv, 0); 603 gettimeofday (&tv, 0);
576 return tv.tv_sec + tv.tv_usec * 1e-6; 604 return tv.tv_sec + tv.tv_usec * 1e-6;
577} 605}
606#endif
578 607
579inline_size ev_tstamp 608inline_size ev_tstamp
580get_clock (void) 609get_clock (void)
581{ 610{
582#if EV_USE_MONOTONIC 611#if EV_USE_MONOTONIC
618 647
619 tv.tv_sec = (time_t)delay; 648 tv.tv_sec = (time_t)delay;
620 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 649 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
621 650
622 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 651 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
623 /* somehting nto guaranteed by newer posix versions, but guaranteed */ 652 /* somehting not guaranteed by newer posix versions, but guaranteed */
624 /* by older ones */ 653 /* by older ones */
625 select (0, 0, 0, 0, &tv); 654 select (0, 0, 0, 0, &tv);
626#endif 655#endif
627 } 656 }
628} 657}
736} 765}
737 766
738/*****************************************************************************/ 767/*****************************************************************************/
739 768
740inline_speed void 769inline_speed void
741fd_event (EV_P_ int fd, int revents) 770fd_event_nc (EV_P_ int fd, int revents)
742{ 771{
743 ANFD *anfd = anfds + fd; 772 ANFD *anfd = anfds + fd;
744 ev_io *w; 773 ev_io *w;
745 774
746 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) 775 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
750 if (ev) 779 if (ev)
751 ev_feed_event (EV_A_ (W)w, ev); 780 ev_feed_event (EV_A_ (W)w, ev);
752 } 781 }
753} 782}
754 783
784/* do not submit kernel events for fds that have reify set */
785/* because that means they changed while we were polling for new events */
786inline_speed void
787fd_event (EV_P_ int fd, int revents)
788{
789 ANFD *anfd = anfds + fd;
790
791 if (expect_true (!anfd->reify))
792 fd_event_nc (EV_A_ fd, revents);
793}
794
755void 795void
756ev_feed_fd_event (EV_P_ int fd, int revents) 796ev_feed_fd_event (EV_P_ int fd, int revents)
757{ 797{
758 if (fd >= 0 && fd < anfdmax) 798 if (fd >= 0 && fd < anfdmax)
759 fd_event (EV_A_ fd, revents); 799 fd_event_nc (EV_A_ fd, revents);
760} 800}
761 801
762/* make sure the external fd watch events are in-sync */ 802/* make sure the external fd watch events are in-sync */
763/* with the kernel/libev internal state */ 803/* with the kernel/libev internal state */
764inline_size void 804inline_size void
879 for (fd = 0; fd < anfdmax; ++fd) 919 for (fd = 0; fd < anfdmax; ++fd)
880 if (anfds [fd].events) 920 if (anfds [fd].events)
881 { 921 {
882 anfds [fd].events = 0; 922 anfds [fd].events = 0;
883 anfds [fd].emask = 0; 923 anfds [fd].emask = 0;
884 fd_change (EV_A_ fd, EV__IOFDSET | 1); 924 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
885 } 925 }
886} 926}
887 927
888/*****************************************************************************/ 928/*****************************************************************************/
889 929
1343ev_backend (EV_P) 1383ev_backend (EV_P)
1344{ 1384{
1345 return backend; 1385 return backend;
1346} 1386}
1347 1387
1388#if EV_MINIMAL < 2
1348unsigned int 1389unsigned int
1349ev_loop_count (EV_P) 1390ev_loop_count (EV_P)
1350{ 1391{
1351 return loop_count; 1392 return loop_count;
1352} 1393}
1353 1394
1395unsigned int
1396ev_loop_depth (EV_P)
1397{
1398 return loop_depth;
1399}
1400
1354void 1401void
1355ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 1402ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
1356{ 1403{
1357 io_blocktime = interval; 1404 io_blocktime = interval;
1358} 1405}
1360void 1407void
1361ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 1408ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1362{ 1409{
1363 timeout_blocktime = interval; 1410 timeout_blocktime = interval;
1364} 1411}
1412
1413void
1414ev_set_userdata (EV_P_ void *data)
1415{
1416 userdata = data;
1417}
1418
1419void *
1420ev_userdata (EV_P)
1421{
1422 return userdata;
1423}
1424
1425void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P))
1426{
1427 invoke_cb = invoke_pending_cb;
1428}
1429
1430void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P))
1431{
1432 release_cb = release;
1433 acquire_cb = acquire;
1434}
1435#endif
1365 1436
1366/* initialise a loop structure, must be zero-initialised */ 1437/* initialise a loop structure, must be zero-initialised */
1367static void noinline 1438static void noinline
1368loop_init (EV_P_ unsigned int flags) 1439loop_init (EV_P_ unsigned int flags)
1369{ 1440{
1391 1462
1392 ev_rt_now = ev_time (); 1463 ev_rt_now = ev_time ();
1393 mn_now = get_clock (); 1464 mn_now = get_clock ();
1394 now_floor = mn_now; 1465 now_floor = mn_now;
1395 rtmn_diff = ev_rt_now - mn_now; 1466 rtmn_diff = ev_rt_now - mn_now;
1467#if EV_MINIMAL < 2
1468 invoke_cb = ev_invoke_pending;
1469#endif
1396 1470
1397 io_blocktime = 0.; 1471 io_blocktime = 0.;
1398 timeout_blocktime = 0.; 1472 timeout_blocktime = 0.;
1399 backend = 0; 1473 backend = 0;
1400 backend_fd = -1; 1474 backend_fd = -1;
1594void 1668void
1595ev_loop_fork (EV_P) 1669ev_loop_fork (EV_P)
1596{ 1670{
1597 postfork = 1; /* must be in line with ev_default_fork */ 1671 postfork = 1; /* must be in line with ev_default_fork */
1598} 1672}
1673#endif /* multiplicity */
1599 1674
1600#if EV_VERIFY 1675#if EV_VERIFY
1601static void noinline 1676static void noinline
1602verify_watcher (EV_P_ W w) 1677verify_watcher (EV_P_ W w)
1603{ 1678{
1631 verify_watcher (EV_A_ ws [cnt]); 1706 verify_watcher (EV_A_ ws [cnt]);
1632 } 1707 }
1633} 1708}
1634#endif 1709#endif
1635 1710
1711#if EV_MINIMAL < 2
1636void 1712void
1637ev_loop_verify (EV_P) 1713ev_loop_verify (EV_P)
1638{ 1714{
1639#if EV_VERIFY 1715#if EV_VERIFY
1640 int i; 1716 int i;
1693 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1769 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
1694 for (signum = signalmax; signum--; ) if (signals [signum].gotsig) 1770 for (signum = signalmax; signum--; ) if (signals [signum].gotsig)
1695# endif 1771# endif
1696#endif 1772#endif
1697} 1773}
1698 1774#endif
1699#endif /* multiplicity */
1700 1775
1701#if EV_MULTIPLICITY 1776#if EV_MULTIPLICITY
1702struct ev_loop * 1777struct ev_loop *
1703ev_default_loop_init (unsigned int flags) 1778ev_default_loop_init (unsigned int flags)
1704#else 1779#else
1765ev_invoke (EV_P_ void *w, int revents) 1840ev_invoke (EV_P_ void *w, int revents)
1766{ 1841{
1767 EV_CB_INVOKE ((W)w, revents); 1842 EV_CB_INVOKE ((W)w, revents);
1768} 1843}
1769 1844
1770inline_speed void 1845void noinline
1771call_pending (EV_P) 1846ev_invoke_pending (EV_P)
1772{ 1847{
1773 int pri; 1848 int pri;
1774 1849
1775 for (pri = NUMPRI; pri--; ) 1850 for (pri = NUMPRI; pri--; )
1776 while (pendingcnt [pri]) 1851 while (pendingcnt [pri])
2011 2086
2012 mn_now = ev_rt_now; 2087 mn_now = ev_rt_now;
2013 } 2088 }
2014} 2089}
2015 2090
2016static int loop_done;
2017
2018void 2091void
2019ev_loop (EV_P_ int flags) 2092ev_loop (EV_P_ int flags)
2020{ 2093{
2094#if EV_MINIMAL < 2
2095 ++loop_depth;
2096#endif
2097
2098 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE));
2099
2021 loop_done = EVUNLOOP_CANCEL; 2100 loop_done = EVUNLOOP_CANCEL;
2022 2101
2023 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 2102 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
2024 2103
2025 do 2104 do
2026 { 2105 {
2027#if EV_VERIFY >= 2 2106#if EV_VERIFY >= 2
2028 ev_loop_verify (EV_A); 2107 ev_loop_verify (EV_A);
2041 /* we might have forked, so queue fork handlers */ 2120 /* we might have forked, so queue fork handlers */
2042 if (expect_false (postfork)) 2121 if (expect_false (postfork))
2043 if (forkcnt) 2122 if (forkcnt)
2044 { 2123 {
2045 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2124 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2046 call_pending (EV_A); 2125 EV_INVOKE_PENDING;
2047 } 2126 }
2048#endif 2127#endif
2049 2128
2050 /* queue prepare watchers (and execute them) */ 2129 /* queue prepare watchers (and execute them) */
2051 if (expect_false (preparecnt)) 2130 if (expect_false (preparecnt))
2052 { 2131 {
2053 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2132 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2054 call_pending (EV_A); 2133 EV_INVOKE_PENDING;
2055 } 2134 }
2135
2136 if (expect_false (loop_done))
2137 break;
2056 2138
2057 /* we might have forked, so reify kernel state if necessary */ 2139 /* we might have forked, so reify kernel state if necessary */
2058 if (expect_false (postfork)) 2140 if (expect_false (postfork))
2059 loop_fork (EV_A); 2141 loop_fork (EV_A);
2060 2142
2066 ev_tstamp waittime = 0.; 2148 ev_tstamp waittime = 0.;
2067 ev_tstamp sleeptime = 0.; 2149 ev_tstamp sleeptime = 0.;
2068 2150
2069 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2151 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
2070 { 2152 {
2153 /* remember old timestamp for io_blocktime calculation */
2154 ev_tstamp prev_mn_now = mn_now;
2155
2071 /* update time to cancel out callback processing overhead */ 2156 /* update time to cancel out callback processing overhead */
2072 time_update (EV_A_ 1e100); 2157 time_update (EV_A_ 1e100);
2073 2158
2074 waittime = MAX_BLOCKTIME; 2159 waittime = MAX_BLOCKTIME;
2075 2160
2085 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; 2170 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
2086 if (waittime > to) waittime = to; 2171 if (waittime > to) waittime = to;
2087 } 2172 }
2088#endif 2173#endif
2089 2174
2175 /* don't let timeouts decrease the waittime below timeout_blocktime */
2090 if (expect_false (waittime < timeout_blocktime)) 2176 if (expect_false (waittime < timeout_blocktime))
2091 waittime = timeout_blocktime; 2177 waittime = timeout_blocktime;
2092 2178
2093 sleeptime = waittime - backend_fudge; 2179 /* extra check because io_blocktime is commonly 0 */
2094
2095 if (expect_true (sleeptime > io_blocktime)) 2180 if (expect_false (io_blocktime))
2096 sleeptime = io_blocktime;
2097
2098 if (sleeptime)
2099 { 2181 {
2182 sleeptime = io_blocktime - (mn_now - prev_mn_now);
2183
2184 if (sleeptime > waittime - backend_fudge)
2185 sleeptime = waittime - backend_fudge;
2186
2187 if (expect_true (sleeptime > 0.))
2188 {
2100 ev_sleep (sleeptime); 2189 ev_sleep (sleeptime);
2101 waittime -= sleeptime; 2190 waittime -= sleeptime;
2191 }
2102 } 2192 }
2103 } 2193 }
2104 2194
2195#if EV_MINIMAL < 2
2105 ++loop_count; 2196 ++loop_count;
2197#endif
2198 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */
2106 backend_poll (EV_A_ waittime); 2199 backend_poll (EV_A_ waittime);
2200 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */
2107 2201
2108 /* update ev_rt_now, do magic */ 2202 /* update ev_rt_now, do magic */
2109 time_update (EV_A_ waittime + sleeptime); 2203 time_update (EV_A_ waittime + sleeptime);
2110 } 2204 }
2111 2205
2122 2216
2123 /* queue check watchers, to be executed first */ 2217 /* queue check watchers, to be executed first */
2124 if (expect_false (checkcnt)) 2218 if (expect_false (checkcnt))
2125 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2219 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2126 2220
2127 call_pending (EV_A); 2221 EV_INVOKE_PENDING;
2128 } 2222 }
2129 while (expect_true ( 2223 while (expect_true (
2130 activecnt 2224 activecnt
2131 && !loop_done 2225 && !loop_done
2132 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2226 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2133 )); 2227 ));
2134 2228
2135 if (loop_done == EVUNLOOP_ONE) 2229 if (loop_done == EVUNLOOP_ONE)
2136 loop_done = EVUNLOOP_CANCEL; 2230 loop_done = EVUNLOOP_CANCEL;
2231
2232#if EV_MINIMAL < 2
2233 --loop_depth;
2234#endif
2137} 2235}
2138 2236
2139void 2237void
2140ev_unloop (EV_P_ int how) 2238ev_unloop (EV_P_ int how)
2141{ 2239{
2233} 2331}
2234 2332
2235inline_size void 2333inline_size void
2236pri_adjust (EV_P_ W w) 2334pri_adjust (EV_P_ W w)
2237{ 2335{
2238 int pri = w->priority; 2336 int pri = ev_priority (w);
2239 pri = pri < EV_MINPRI ? EV_MINPRI : pri; 2337 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2240 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; 2338 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2241 w->priority = pri; 2339 ev_set_priority (w, pri);
2242} 2340}
2243 2341
2244inline_speed void 2342inline_speed void
2245ev_start (EV_P_ W w, int active) 2343ev_start (EV_P_ W w, int active)
2246{ 2344{
2273 2371
2274 ev_start (EV_A_ (W)w, 1); 2372 ev_start (EV_A_ (W)w, 1);
2275 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 2373 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
2276 wlist_add (&anfds[fd].head, (WL)w); 2374 wlist_add (&anfds[fd].head, (WL)w);
2277 2375
2278 fd_change (EV_A_ fd, w->events & EV__IOFDSET | 1); 2376 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
2279 w->events &= ~EV__IOFDSET; 2377 w->events &= ~EV__IOFDSET;
2280 2378
2281 EV_FREQUENT_CHECK; 2379 EV_FREQUENT_CHECK;
2282} 2380}
2283 2381

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines