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

Comparing libev/ev.c (file contents):
Revision 1.296 by root, Thu Jul 9 09:11:20 2009 UTC vs.
Revision 1.300 by root, Tue Jul 14 20:31:21 2009 UTC

490#define ev_malloc(size) ev_realloc (0, (size)) 490#define ev_malloc(size) ev_realloc (0, (size))
491#define ev_free(ptr) ev_realloc ((ptr), 0) 491#define ev_free(ptr) ev_realloc ((ptr), 0)
492 492
493/*****************************************************************************/ 493/*****************************************************************************/
494 494
495/* set in reify when reification needed */
496#define EV_ANFD_REIFY 1
497
495/* file descriptor info structure */ 498/* file descriptor info structure */
496typedef struct 499typedef struct
497{ 500{
498 WL head; 501 WL head;
499 unsigned char events; /* the events watched for */ 502 unsigned char events; /* the events watched for */
500 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) */
501 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 */
502 unsigned char unused; 505 unsigned char unused;
503#if EV_USE_EPOLL 506#if EV_USE_EPOLL
504 unsigned int egen; /* generation counter to counter epoll bugs */ 507 unsigned int egen; /* generation counter to counter epoll bugs */
505#endif 508#endif
567 570
568 static int ev_default_loop_ptr; 571 static int ev_default_loop_ptr;
569 572
570#endif 573#endif
571 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
572/*****************************************************************************/ 587/*****************************************************************************/
573 588
574#ifndef EV_HAVE_EV_TIME 589#ifndef EV_HAVE_EV_TIME
575ev_tstamp 590ev_tstamp
576ev_time (void) 591ev_time (void)
750} 765}
751 766
752/*****************************************************************************/ 767/*****************************************************************************/
753 768
754inline_speed void 769inline_speed void
755fd_event (EV_P_ int fd, int revents) 770fd_event_nc (EV_P_ int fd, int revents)
756{ 771{
757 ANFD *anfd = anfds + fd; 772 ANFD *anfd = anfds + fd;
758 ev_io *w; 773 ev_io *w;
759 774
760 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)
764 if (ev) 779 if (ev)
765 ev_feed_event (EV_A_ (W)w, ev); 780 ev_feed_event (EV_A_ (W)w, ev);
766 } 781 }
767} 782}
768 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
769void 795void
770ev_feed_fd_event (EV_P_ int fd, int revents) 796ev_feed_fd_event (EV_P_ int fd, int revents)
771{ 797{
772 if (fd >= 0 && fd < anfdmax) 798 if (fd >= 0 && fd < anfdmax)
773 fd_event (EV_A_ fd, revents); 799 fd_event_nc (EV_A_ fd, revents);
774} 800}
775 801
776/* make sure the external fd watch events are in-sync */ 802/* make sure the external fd watch events are in-sync */
777/* with the kernel/libev internal state */ 803/* with the kernel/libev internal state */
778inline_size void 804inline_size void
893 for (fd = 0; fd < anfdmax; ++fd) 919 for (fd = 0; fd < anfdmax; ++fd)
894 if (anfds [fd].events) 920 if (anfds [fd].events)
895 { 921 {
896 anfds [fd].events = 0; 922 anfds [fd].events = 0;
897 anfds [fd].emask = 0; 923 anfds [fd].emask = 0;
898 fd_change (EV_A_ fd, EV__IOFDSET | 1); 924 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
899 } 925 }
900} 926}
901 927
902/*****************************************************************************/ 928/*****************************************************************************/
903 929
1357ev_backend (EV_P) 1383ev_backend (EV_P)
1358{ 1384{
1359 return backend; 1385 return backend;
1360} 1386}
1361 1387
1388#if EV_MINIMAL < 2
1362unsigned int 1389unsigned int
1363ev_loop_count (EV_P) 1390ev_loop_count (EV_P)
1364{ 1391{
1365 return loop_count; 1392 return loop_count;
1366} 1393}
1380void 1407void
1381ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 1408ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1382{ 1409{
1383 timeout_blocktime = interval; 1410 timeout_blocktime = interval;
1384} 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
1385 1436
1386/* initialise a loop structure, must be zero-initialised */ 1437/* initialise a loop structure, must be zero-initialised */
1387static void noinline 1438static void noinline
1388loop_init (EV_P_ unsigned int flags) 1439loop_init (EV_P_ unsigned int flags)
1389{ 1440{
1411 1462
1412 ev_rt_now = ev_time (); 1463 ev_rt_now = ev_time ();
1413 mn_now = get_clock (); 1464 mn_now = get_clock ();
1414 now_floor = mn_now; 1465 now_floor = mn_now;
1415 rtmn_diff = ev_rt_now - mn_now; 1466 rtmn_diff = ev_rt_now - mn_now;
1467#if EV_MINIMAL < 2
1416 invoke_cb = ev_invoke_pending; 1468 invoke_cb = ev_invoke_pending;
1469#endif
1417 1470
1418 io_blocktime = 0.; 1471 io_blocktime = 0.;
1419 timeout_blocktime = 0.; 1472 timeout_blocktime = 0.;
1420 backend = 0; 1473 backend = 0;
1421 backend_fd = -1; 1474 backend_fd = -1;
1615void 1668void
1616ev_loop_fork (EV_P) 1669ev_loop_fork (EV_P)
1617{ 1670{
1618 postfork = 1; /* must be in line with ev_default_fork */ 1671 postfork = 1; /* must be in line with ev_default_fork */
1619} 1672}
1673#endif /* multiplicity */
1620 1674
1621#if EV_VERIFY 1675#if EV_VERIFY
1622static void noinline 1676static void noinline
1623verify_watcher (EV_P_ W w) 1677verify_watcher (EV_P_ W w)
1624{ 1678{
1652 verify_watcher (EV_A_ ws [cnt]); 1706 verify_watcher (EV_A_ ws [cnt]);
1653 } 1707 }
1654} 1708}
1655#endif 1709#endif
1656 1710
1711#if EV_MINIMAL < 2
1657void 1712void
1658ev_loop_verify (EV_P) 1713ev_loop_verify (EV_P)
1659{ 1714{
1660#if EV_VERIFY 1715#if EV_VERIFY
1661 int i; 1716 int i;
1714 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)
1715 for (signum = signalmax; signum--; ) if (signals [signum].gotsig) 1770 for (signum = signalmax; signum--; ) if (signals [signum].gotsig)
1716# endif 1771# endif
1717#endif 1772#endif
1718} 1773}
1719 1774#endif
1720#endif /* multiplicity */
1721 1775
1722#if EV_MULTIPLICITY 1776#if EV_MULTIPLICITY
1723struct ev_loop * 1777struct ev_loop *
1724ev_default_loop_init (unsigned int flags) 1778ev_default_loop_init (unsigned int flags)
1725#else 1779#else
1786ev_invoke (EV_P_ void *w, int revents) 1840ev_invoke (EV_P_ void *w, int revents)
1787{ 1841{
1788 EV_CB_INVOKE ((W)w, revents); 1842 EV_CB_INVOKE ((W)w, revents);
1789} 1843}
1790 1844
1791void 1845unsigned int
1846ev_pending_count (EV_P)
1847{
1848 int pri;
1849 unsigned int count = 0;
1850
1851 for (pri = NUMPRI; pri--; )
1852 count += pendingcnt [pri];
1853
1854 return count;
1855}
1856
1857void noinline
1792ev_invoke_pending (EV_P) 1858ev_invoke_pending (EV_P)
1793{ 1859{
1794 int pri; 1860 int pri;
1795 1861
1796 for (pri = NUMPRI; pri--; ) 1862 for (pri = NUMPRI; pri--; )
2035} 2101}
2036 2102
2037void 2103void
2038ev_loop (EV_P_ int flags) 2104ev_loop (EV_P_ int flags)
2039{ 2105{
2106#if EV_MINIMAL < 2
2040 ++loop_depth; 2107 ++loop_depth;
2108#endif
2109
2110 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE));
2041 2111
2042 loop_done = EVUNLOOP_CANCEL; 2112 loop_done = EVUNLOOP_CANCEL;
2043 2113
2044 invoke_cb (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 2114 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
2045 2115
2046 do 2116 do
2047 { 2117 {
2048#if EV_VERIFY >= 2 2118#if EV_VERIFY >= 2
2049 ev_loop_verify (EV_A); 2119 ev_loop_verify (EV_A);
2062 /* we might have forked, so queue fork handlers */ 2132 /* we might have forked, so queue fork handlers */
2063 if (expect_false (postfork)) 2133 if (expect_false (postfork))
2064 if (forkcnt) 2134 if (forkcnt)
2065 { 2135 {
2066 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2136 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2067 invoke_cb (EV_A); 2137 EV_INVOKE_PENDING;
2068 } 2138 }
2069#endif 2139#endif
2070 2140
2071 /* queue prepare watchers (and execute them) */ 2141 /* queue prepare watchers (and execute them) */
2072 if (expect_false (preparecnt)) 2142 if (expect_false (preparecnt))
2073 { 2143 {
2074 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2144 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2075 invoke_cb (EV_A); 2145 EV_INVOKE_PENDING;
2076 } 2146 }
2147
2148 if (expect_false (loop_done))
2149 break;
2077 2150
2078 /* we might have forked, so reify kernel state if necessary */ 2151 /* we might have forked, so reify kernel state if necessary */
2079 if (expect_false (postfork)) 2152 if (expect_false (postfork))
2080 loop_fork (EV_A); 2153 loop_fork (EV_A);
2081 2154
2129 waittime -= sleeptime; 2202 waittime -= sleeptime;
2130 } 2203 }
2131 } 2204 }
2132 } 2205 }
2133 2206
2207#if EV_MINIMAL < 2
2134 ++loop_count; 2208 ++loop_count;
2209#endif
2210 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */
2135 backend_poll (EV_A_ waittime); 2211 backend_poll (EV_A_ waittime);
2212 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */
2136 2213
2137 /* update ev_rt_now, do magic */ 2214 /* update ev_rt_now, do magic */
2138 time_update (EV_A_ waittime + sleeptime); 2215 time_update (EV_A_ waittime + sleeptime);
2139 } 2216 }
2140 2217
2151 2228
2152 /* queue check watchers, to be executed first */ 2229 /* queue check watchers, to be executed first */
2153 if (expect_false (checkcnt)) 2230 if (expect_false (checkcnt))
2154 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2231 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2155 2232
2156 invoke_cb (EV_A); 2233 EV_INVOKE_PENDING;
2157 } 2234 }
2158 while (expect_true ( 2235 while (expect_true (
2159 activecnt 2236 activecnt
2160 && !loop_done 2237 && !loop_done
2161 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2238 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2162 )); 2239 ));
2163 2240
2164 if (loop_done == EVUNLOOP_ONE) 2241 if (loop_done == EVUNLOOP_ONE)
2165 loop_done = EVUNLOOP_CANCEL; 2242 loop_done = EVUNLOOP_CANCEL;
2166 2243
2244#if EV_MINIMAL < 2
2167 --loop_depth; 2245 --loop_depth;
2246#endif
2168} 2247}
2169 2248
2170void 2249void
2171ev_unloop (EV_P_ int how) 2250ev_unloop (EV_P_ int how)
2172{ 2251{
2304 2383
2305 ev_start (EV_A_ (W)w, 1); 2384 ev_start (EV_A_ (W)w, 1);
2306 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 2385 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
2307 wlist_add (&anfds[fd].head, (WL)w); 2386 wlist_add (&anfds[fd].head, (WL)w);
2308 2387
2309 fd_change (EV_A_ fd, w->events & EV__IOFDSET | 1); 2388 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
2310 w->events &= ~EV__IOFDSET; 2389 w->events &= ~EV__IOFDSET;
2311 2390
2312 EV_FREQUENT_CHECK; 2391 EV_FREQUENT_CHECK;
2313} 2392}
2314 2393
2518 if (!((WL)w)->next) 2597 if (!((WL)w)->next)
2519 { 2598 {
2520#if _WIN32 2599#if _WIN32
2521 signal (w->signum, ev_sighandler); 2600 signal (w->signum, ev_sighandler);
2522#else 2601#else
2523 struct sigaction sa; 2602 struct sigaction sa = { };
2524 sa.sa_handler = ev_sighandler; 2603 sa.sa_handler = ev_sighandler;
2525 sigfillset (&sa.sa_mask); 2604 sigfillset (&sa.sa_mask);
2526 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 2605 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
2527 sigaction (w->signum, &sa, 0); 2606 sigaction (w->signum, &sa, 0);
2528#endif 2607#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines