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

Comparing libev/ev.c (file contents):
Revision 1.294 by root, Wed Jul 8 02:46:05 2009 UTC vs.
Revision 1.301 by root, Wed Jul 15 16:58:53 2009 UTC

391# define inline_speed static noinline 391# define inline_speed static noinline
392#else 392#else
393# define inline_speed static inline 393# define inline_speed static inline
394#endif 394#endif
395 395
396#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
397#define ABSPRI(w) (((W)w)->priority - EV_MINPRI) 401# define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
402#endif
398 403
399#define EMPTY /* required for microsofts broken pseudo-c compiler */ 404#define EMPTY /* required for microsofts broken pseudo-c compiler */
400#define EMPTY2(a,b) /* used to suppress some warnings */ 405#define EMPTY2(a,b) /* used to suppress some warnings */
401 406
402typedef ev_watcher *W; 407typedef ev_watcher *W;
485#define ev_malloc(size) ev_realloc (0, (size)) 490#define ev_malloc(size) ev_realloc (0, (size))
486#define ev_free(ptr) ev_realloc ((ptr), 0) 491#define ev_free(ptr) ev_realloc ((ptr), 0)
487 492
488/*****************************************************************************/ 493/*****************************************************************************/
489 494
495/* set in reify when reification needed */
496#define EV_ANFD_REIFY 1
497
490/* file descriptor info structure */ 498/* file descriptor info structure */
491typedef struct 499typedef struct
492{ 500{
493 WL head; 501 WL head;
494 unsigned char events; /* the events watched for */ 502 unsigned char events; /* the events watched for */
495 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) */
496 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 */
497 unsigned char unused; 505 unsigned char unused;
498#if EV_USE_EPOLL 506#if EV_USE_EPOLL
499 unsigned int egen; /* generation counter to counter epoll bugs */ 507 unsigned int egen; /* generation counter to counter epoll bugs */
500#endif 508#endif
562 570
563 static int ev_default_loop_ptr; 571 static int ev_default_loop_ptr;
564 572
565#endif 573#endif
566 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
567/*****************************************************************************/ 587/*****************************************************************************/
568 588
569#ifndef EV_HAVE_EV_TIME 589#ifndef EV_HAVE_EV_TIME
570ev_tstamp 590ev_tstamp
571ev_time (void) 591ev_time (void)
745} 765}
746 766
747/*****************************************************************************/ 767/*****************************************************************************/
748 768
749inline_speed void 769inline_speed void
750fd_event (EV_P_ int fd, int revents) 770fd_event_nc (EV_P_ int fd, int revents)
751{ 771{
752 ANFD *anfd = anfds + fd; 772 ANFD *anfd = anfds + fd;
753 ev_io *w; 773 ev_io *w;
754 774
755 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)
759 if (ev) 779 if (ev)
760 ev_feed_event (EV_A_ (W)w, ev); 780 ev_feed_event (EV_A_ (W)w, ev);
761 } 781 }
762} 782}
763 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
764void 795void
765ev_feed_fd_event (EV_P_ int fd, int revents) 796ev_feed_fd_event (EV_P_ int fd, int revents)
766{ 797{
767 if (fd >= 0 && fd < anfdmax) 798 if (fd >= 0 && fd < anfdmax)
768 fd_event (EV_A_ fd, revents); 799 fd_event_nc (EV_A_ fd, revents);
769} 800}
770 801
771/* make sure the external fd watch events are in-sync */ 802/* make sure the external fd watch events are in-sync */
772/* with the kernel/libev internal state */ 803/* with the kernel/libev internal state */
773inline_size void 804inline_size void
888 for (fd = 0; fd < anfdmax; ++fd) 919 for (fd = 0; fd < anfdmax; ++fd)
889 if (anfds [fd].events) 920 if (anfds [fd].events)
890 { 921 {
891 anfds [fd].events = 0; 922 anfds [fd].events = 0;
892 anfds [fd].emask = 0; 923 anfds [fd].emask = 0;
893 fd_change (EV_A_ fd, EV__IOFDSET | 1); 924 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
894 } 925 }
895} 926}
896 927
897/*****************************************************************************/ 928/*****************************************************************************/
898 929
1352ev_backend (EV_P) 1383ev_backend (EV_P)
1353{ 1384{
1354 return backend; 1385 return backend;
1355} 1386}
1356 1387
1388#if EV_MINIMAL < 2
1357unsigned int 1389unsigned int
1358ev_loop_count (EV_P) 1390ev_loop_count (EV_P)
1359{ 1391{
1360 return loop_count; 1392 return loop_count;
1361} 1393}
1375void 1407void
1376ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 1408ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1377{ 1409{
1378 timeout_blocktime = interval; 1410 timeout_blocktime = interval;
1379} 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
1380 1436
1381/* initialise a loop structure, must be zero-initialised */ 1437/* initialise a loop structure, must be zero-initialised */
1382static void noinline 1438static void noinline
1383loop_init (EV_P_ unsigned int flags) 1439loop_init (EV_P_ unsigned int flags)
1384{ 1440{
1406 1462
1407 ev_rt_now = ev_time (); 1463 ev_rt_now = ev_time ();
1408 mn_now = get_clock (); 1464 mn_now = get_clock ();
1409 now_floor = mn_now; 1465 now_floor = mn_now;
1410 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
1411 1470
1412 io_blocktime = 0.; 1471 io_blocktime = 0.;
1413 timeout_blocktime = 0.; 1472 timeout_blocktime = 0.;
1414 backend = 0; 1473 backend = 0;
1415 backend_fd = -1; 1474 backend_fd = -1;
1609void 1668void
1610ev_loop_fork (EV_P) 1669ev_loop_fork (EV_P)
1611{ 1670{
1612 postfork = 1; /* must be in line with ev_default_fork */ 1671 postfork = 1; /* must be in line with ev_default_fork */
1613} 1672}
1673#endif /* multiplicity */
1614 1674
1615#if EV_VERIFY 1675#if EV_VERIFY
1616static void noinline 1676static void noinline
1617verify_watcher (EV_P_ W w) 1677verify_watcher (EV_P_ W w)
1618{ 1678{
1646 verify_watcher (EV_A_ ws [cnt]); 1706 verify_watcher (EV_A_ ws [cnt]);
1647 } 1707 }
1648} 1708}
1649#endif 1709#endif
1650 1710
1711#if EV_MINIMAL < 2
1651void 1712void
1652ev_loop_verify (EV_P) 1713ev_loop_verify (EV_P)
1653{ 1714{
1654#if EV_VERIFY 1715#if EV_VERIFY
1655 int i; 1716 int i;
1708 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)
1709 for (signum = signalmax; signum--; ) if (signals [signum].gotsig) 1770 for (signum = signalmax; signum--; ) if (signals [signum].gotsig)
1710# endif 1771# endif
1711#endif 1772#endif
1712} 1773}
1713 1774#endif
1714#endif /* multiplicity */
1715 1775
1716#if EV_MULTIPLICITY 1776#if EV_MULTIPLICITY
1717struct ev_loop * 1777struct ev_loop *
1718ev_default_loop_init (unsigned int flags) 1778ev_default_loop_init (unsigned int flags)
1719#else 1779#else
1780ev_invoke (EV_P_ void *w, int revents) 1840ev_invoke (EV_P_ void *w, int revents)
1781{ 1841{
1782 EV_CB_INVOKE ((W)w, revents); 1842 EV_CB_INVOKE ((W)w, revents);
1783} 1843}
1784 1844
1785inline_speed void 1845unsigned int
1786call_pending (EV_P) 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
1858ev_invoke_pending (EV_P)
1787{ 1859{
1788 int pri; 1860 int pri;
1789 1861
1790 for (pri = NUMPRI; pri--; ) 1862 for (pri = NUMPRI; pri--; )
1791 while (pendingcnt [pri]) 1863 while (pendingcnt [pri])
2029} 2101}
2030 2102
2031void 2103void
2032ev_loop (EV_P_ int flags) 2104ev_loop (EV_P_ int flags)
2033{ 2105{
2106#if EV_MINIMAL < 2
2034 ++loop_depth; 2107 ++loop_depth;
2108#endif
2109
2110 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE));
2035 2111
2036 loop_done = EVUNLOOP_CANCEL; 2112 loop_done = EVUNLOOP_CANCEL;
2037 2113
2038 call_pending (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 */
2039 2115
2040 do 2116 do
2041 { 2117 {
2042#if EV_VERIFY >= 2 2118#if EV_VERIFY >= 2
2043 ev_loop_verify (EV_A); 2119 ev_loop_verify (EV_A);
2056 /* we might have forked, so queue fork handlers */ 2132 /* we might have forked, so queue fork handlers */
2057 if (expect_false (postfork)) 2133 if (expect_false (postfork))
2058 if (forkcnt) 2134 if (forkcnt)
2059 { 2135 {
2060 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2136 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2061 call_pending (EV_A); 2137 EV_INVOKE_PENDING;
2062 } 2138 }
2063#endif 2139#endif
2064 2140
2065 /* queue prepare watchers (and execute them) */ 2141 /* queue prepare watchers (and execute them) */
2066 if (expect_false (preparecnt)) 2142 if (expect_false (preparecnt))
2067 { 2143 {
2068 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2144 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2069 call_pending (EV_A); 2145 EV_INVOKE_PENDING;
2070 } 2146 }
2147
2148 if (expect_false (loop_done))
2149 break;
2071 2150
2072 /* we might have forked, so reify kernel state if necessary */ 2151 /* we might have forked, so reify kernel state if necessary */
2073 if (expect_false (postfork)) 2152 if (expect_false (postfork))
2074 loop_fork (EV_A); 2153 loop_fork (EV_A);
2075 2154
2123 waittime -= sleeptime; 2202 waittime -= sleeptime;
2124 } 2203 }
2125 } 2204 }
2126 } 2205 }
2127 2206
2207#if EV_MINIMAL < 2
2128 ++loop_count; 2208 ++loop_count;
2209#endif
2210 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */
2129 backend_poll (EV_A_ waittime); 2211 backend_poll (EV_A_ waittime);
2212 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */
2130 2213
2131 /* update ev_rt_now, do magic */ 2214 /* update ev_rt_now, do magic */
2132 time_update (EV_A_ waittime + sleeptime); 2215 time_update (EV_A_ waittime + sleeptime);
2133 } 2216 }
2134 2217
2145 2228
2146 /* queue check watchers, to be executed first */ 2229 /* queue check watchers, to be executed first */
2147 if (expect_false (checkcnt)) 2230 if (expect_false (checkcnt))
2148 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2231 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2149 2232
2150 call_pending (EV_A); 2233 EV_INVOKE_PENDING;
2151 } 2234 }
2152 while (expect_true ( 2235 while (expect_true (
2153 activecnt 2236 activecnt
2154 && !loop_done 2237 && !loop_done
2155 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2238 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2156 )); 2239 ));
2157 2240
2158 if (loop_done == EVUNLOOP_ONE) 2241 if (loop_done == EVUNLOOP_ONE)
2159 loop_done = EVUNLOOP_CANCEL; 2242 loop_done = EVUNLOOP_CANCEL;
2160 2243
2244#if EV_MINIMAL < 2
2161 --loop_depth; 2245 --loop_depth;
2246#endif
2162} 2247}
2163 2248
2164void 2249void
2165ev_unloop (EV_P_ int how) 2250ev_unloop (EV_P_ int how)
2166{ 2251{
2258} 2343}
2259 2344
2260inline_size void 2345inline_size void
2261pri_adjust (EV_P_ W w) 2346pri_adjust (EV_P_ W w)
2262{ 2347{
2263 int pri = w->priority; 2348 int pri = ev_priority (w);
2264 pri = pri < EV_MINPRI ? EV_MINPRI : pri; 2349 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2265 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; 2350 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2266 w->priority = pri; 2351 ev_set_priority (w, pri);
2267} 2352}
2268 2353
2269inline_speed void 2354inline_speed void
2270ev_start (EV_P_ W w, int active) 2355ev_start (EV_P_ W w, int active)
2271{ 2356{
2298 2383
2299 ev_start (EV_A_ (W)w, 1); 2384 ev_start (EV_A_ (W)w, 1);
2300 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 2385 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
2301 wlist_add (&anfds[fd].head, (WL)w); 2386 wlist_add (&anfds[fd].head, (WL)w);
2302 2387
2303 fd_change (EV_A_ fd, w->events & EV__IOFDSET | 1); 2388 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
2304 w->events &= ~EV__IOFDSET; 2389 w->events &= ~EV__IOFDSET;
2305 2390
2306 EV_FREQUENT_CHECK; 2391 EV_FREQUENT_CHECK;
2307} 2392}
2308 2393
2402 } 2487 }
2403 2488
2404 EV_FREQUENT_CHECK; 2489 EV_FREQUENT_CHECK;
2405} 2490}
2406 2491
2492ev_tstamp
2493ev_timer_remaining (EV_P_ ev_timer *w)
2494{
2495 return ev_at (w) - (ev_is_active (w) ? mn_now : 0.);
2496}
2497
2407#if EV_PERIODIC_ENABLE 2498#if EV_PERIODIC_ENABLE
2408void noinline 2499void noinline
2409ev_periodic_start (EV_P_ ev_periodic *w) 2500ev_periodic_start (EV_P_ ev_periodic *w)
2410{ 2501{
2411 if (expect_false (ev_is_active (w))) 2502 if (expect_false (ev_is_active (w)))
2512 if (!((WL)w)->next) 2603 if (!((WL)w)->next)
2513 { 2604 {
2514#if _WIN32 2605#if _WIN32
2515 signal (w->signum, ev_sighandler); 2606 signal (w->signum, ev_sighandler);
2516#else 2607#else
2517 struct sigaction sa; 2608 struct sigaction sa = { };
2518 sa.sa_handler = ev_sighandler; 2609 sa.sa_handler = ev_sighandler;
2519 sigfillset (&sa.sa_mask); 2610 sigfillset (&sa.sa_mask);
2520 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 2611 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
2521 sigaction (w->signum, &sa, 0); 2612 sigaction (w->signum, &sa, 0);
2522#endif 2613#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines