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

Comparing libev/ev.c (file contents):
Revision 1.293 by root, Mon Jun 29 18:46:52 2009 UTC vs.
Revision 1.300 by root, Tue Jul 14 20:31:21 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}
1362 1394
1395unsigned int
1396ev_loop_depth (EV_P)
1397{
1398 return loop_depth;
1399}
1400
1363void 1401void
1364ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 1402ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
1365{ 1403{
1366 io_blocktime = interval; 1404 io_blocktime = interval;
1367} 1405}
1369void 1407void
1370ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 1408ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1371{ 1409{
1372 timeout_blocktime = interval; 1410 timeout_blocktime = interval;
1373} 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
1374 1436
1375/* initialise a loop structure, must be zero-initialised */ 1437/* initialise a loop structure, must be zero-initialised */
1376static void noinline 1438static void noinline
1377loop_init (EV_P_ unsigned int flags) 1439loop_init (EV_P_ unsigned int flags)
1378{ 1440{
1400 1462
1401 ev_rt_now = ev_time (); 1463 ev_rt_now = ev_time ();
1402 mn_now = get_clock (); 1464 mn_now = get_clock ();
1403 now_floor = mn_now; 1465 now_floor = mn_now;
1404 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
1405 1470
1406 io_blocktime = 0.; 1471 io_blocktime = 0.;
1407 timeout_blocktime = 0.; 1472 timeout_blocktime = 0.;
1408 backend = 0; 1473 backend = 0;
1409 backend_fd = -1; 1474 backend_fd = -1;
1603void 1668void
1604ev_loop_fork (EV_P) 1669ev_loop_fork (EV_P)
1605{ 1670{
1606 postfork = 1; /* must be in line with ev_default_fork */ 1671 postfork = 1; /* must be in line with ev_default_fork */
1607} 1672}
1673#endif /* multiplicity */
1608 1674
1609#if EV_VERIFY 1675#if EV_VERIFY
1610static void noinline 1676static void noinline
1611verify_watcher (EV_P_ W w) 1677verify_watcher (EV_P_ W w)
1612{ 1678{
1640 verify_watcher (EV_A_ ws [cnt]); 1706 verify_watcher (EV_A_ ws [cnt]);
1641 } 1707 }
1642} 1708}
1643#endif 1709#endif
1644 1710
1711#if EV_MINIMAL < 2
1645void 1712void
1646ev_loop_verify (EV_P) 1713ev_loop_verify (EV_P)
1647{ 1714{
1648#if EV_VERIFY 1715#if EV_VERIFY
1649 int i; 1716 int i;
1702 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)
1703 for (signum = signalmax; signum--; ) if (signals [signum].gotsig) 1770 for (signum = signalmax; signum--; ) if (signals [signum].gotsig)
1704# endif 1771# endif
1705#endif 1772#endif
1706} 1773}
1707 1774#endif
1708#endif /* multiplicity */
1709 1775
1710#if EV_MULTIPLICITY 1776#if EV_MULTIPLICITY
1711struct ev_loop * 1777struct ev_loop *
1712ev_default_loop_init (unsigned int flags) 1778ev_default_loop_init (unsigned int flags)
1713#else 1779#else
1774ev_invoke (EV_P_ void *w, int revents) 1840ev_invoke (EV_P_ void *w, int revents)
1775{ 1841{
1776 EV_CB_INVOKE ((W)w, revents); 1842 EV_CB_INVOKE ((W)w, revents);
1777} 1843}
1778 1844
1779inline_speed void 1845unsigned int
1780call_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)
1781{ 1859{
1782 int pri; 1860 int pri;
1783 1861
1784 for (pri = NUMPRI; pri--; ) 1862 for (pri = NUMPRI; pri--; )
1785 while (pendingcnt [pri]) 1863 while (pendingcnt [pri])
2020 2098
2021 mn_now = ev_rt_now; 2099 mn_now = ev_rt_now;
2022 } 2100 }
2023} 2101}
2024 2102
2025static int loop_done;
2026
2027void 2103void
2028ev_loop (EV_P_ int flags) 2104ev_loop (EV_P_ int flags)
2029{ 2105{
2106#if EV_MINIMAL < 2
2107 ++loop_depth;
2108#endif
2109
2110 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE));
2111
2030 loop_done = EVUNLOOP_CANCEL; 2112 loop_done = EVUNLOOP_CANCEL;
2031 2113
2032 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 */
2033 2115
2034 do 2116 do
2035 { 2117 {
2036#if EV_VERIFY >= 2 2118#if EV_VERIFY >= 2
2037 ev_loop_verify (EV_A); 2119 ev_loop_verify (EV_A);
2050 /* we might have forked, so queue fork handlers */ 2132 /* we might have forked, so queue fork handlers */
2051 if (expect_false (postfork)) 2133 if (expect_false (postfork))
2052 if (forkcnt) 2134 if (forkcnt)
2053 { 2135 {
2054 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2136 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2055 call_pending (EV_A); 2137 EV_INVOKE_PENDING;
2056 } 2138 }
2057#endif 2139#endif
2058 2140
2059 /* queue prepare watchers (and execute them) */ 2141 /* queue prepare watchers (and execute them) */
2060 if (expect_false (preparecnt)) 2142 if (expect_false (preparecnt))
2061 { 2143 {
2062 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2144 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2063 call_pending (EV_A); 2145 EV_INVOKE_PENDING;
2064 } 2146 }
2147
2148 if (expect_false (loop_done))
2149 break;
2065 2150
2066 /* we might have forked, so reify kernel state if necessary */ 2151 /* we might have forked, so reify kernel state if necessary */
2067 if (expect_false (postfork)) 2152 if (expect_false (postfork))
2068 loop_fork (EV_A); 2153 loop_fork (EV_A);
2069 2154
2117 waittime -= sleeptime; 2202 waittime -= sleeptime;
2118 } 2203 }
2119 } 2204 }
2120 } 2205 }
2121 2206
2207#if EV_MINIMAL < 2
2122 ++loop_count; 2208 ++loop_count;
2209#endif
2210 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */
2123 backend_poll (EV_A_ waittime); 2211 backend_poll (EV_A_ waittime);
2212 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */
2124 2213
2125 /* update ev_rt_now, do magic */ 2214 /* update ev_rt_now, do magic */
2126 time_update (EV_A_ waittime + sleeptime); 2215 time_update (EV_A_ waittime + sleeptime);
2127 } 2216 }
2128 2217
2139 2228
2140 /* queue check watchers, to be executed first */ 2229 /* queue check watchers, to be executed first */
2141 if (expect_false (checkcnt)) 2230 if (expect_false (checkcnt))
2142 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2231 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2143 2232
2144 call_pending (EV_A); 2233 EV_INVOKE_PENDING;
2145 } 2234 }
2146 while (expect_true ( 2235 while (expect_true (
2147 activecnt 2236 activecnt
2148 && !loop_done 2237 && !loop_done
2149 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2238 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2150 )); 2239 ));
2151 2240
2152 if (loop_done == EVUNLOOP_ONE) 2241 if (loop_done == EVUNLOOP_ONE)
2153 loop_done = EVUNLOOP_CANCEL; 2242 loop_done = EVUNLOOP_CANCEL;
2243
2244#if EV_MINIMAL < 2
2245 --loop_depth;
2246#endif
2154} 2247}
2155 2248
2156void 2249void
2157ev_unloop (EV_P_ int how) 2250ev_unloop (EV_P_ int how)
2158{ 2251{
2250} 2343}
2251 2344
2252inline_size void 2345inline_size void
2253pri_adjust (EV_P_ W w) 2346pri_adjust (EV_P_ W w)
2254{ 2347{
2255 int pri = w->priority; 2348 int pri = ev_priority (w);
2256 pri = pri < EV_MINPRI ? EV_MINPRI : pri; 2349 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2257 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; 2350 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2258 w->priority = pri; 2351 ev_set_priority (w, pri);
2259} 2352}
2260 2353
2261inline_speed void 2354inline_speed void
2262ev_start (EV_P_ W w, int active) 2355ev_start (EV_P_ W w, int active)
2263{ 2356{
2290 2383
2291 ev_start (EV_A_ (W)w, 1); 2384 ev_start (EV_A_ (W)w, 1);
2292 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 2385 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
2293 wlist_add (&anfds[fd].head, (WL)w); 2386 wlist_add (&anfds[fd].head, (WL)w);
2294 2387
2295 fd_change (EV_A_ fd, w->events & EV__IOFDSET | 1); 2388 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
2296 w->events &= ~EV__IOFDSET; 2389 w->events &= ~EV__IOFDSET;
2297 2390
2298 EV_FREQUENT_CHECK; 2391 EV_FREQUENT_CHECK;
2299} 2392}
2300 2393
2504 if (!((WL)w)->next) 2597 if (!((WL)w)->next)
2505 { 2598 {
2506#if _WIN32 2599#if _WIN32
2507 signal (w->signum, ev_sighandler); 2600 signal (w->signum, ev_sighandler);
2508#else 2601#else
2509 struct sigaction sa; 2602 struct sigaction sa = { };
2510 sa.sa_handler = ev_sighandler; 2603 sa.sa_handler = ev_sighandler;
2511 sigfillset (&sa.sa_mask); 2604 sigfillset (&sa.sa_mask);
2512 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 */
2513 sigaction (w->signum, &sa, 0); 2606 sigaction (w->signum, &sa, 0);
2514#endif 2607#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines