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

Comparing libev/ev.c (file contents):
Revision 1.297 by root, Fri Jul 10 00:36:21 2009 UTC vs.
Revision 1.302 by root, Thu Jul 16 15:08:08 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
568 static int ev_default_loop_ptr; 571 static int ev_default_loop_ptr;
569 572
570#endif 573#endif
571 574
572#if EV_MINIMAL < 2 575#if EV_MINIMAL < 2
573# define EV_SUSPEND_CB if (expect_false (suspend_cb)) suspend_cb (EV_A)
574# define EV_RESUME_CB if (expect_false (resume_cb )) resume_cb (EV_A) 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)
575# define EV_INVOKE_PENDING invoke_cb (EV_A) 578# define EV_INVOKE_PENDING invoke_cb (EV_A)
576#else 579#else
577# define EV_SUSPEND_CB (void)0
578# define EV_RESUME_CB (void)0 580# define EV_RELEASE_CB (void)0
581# define EV_ACQUIRE_CB (void)0
579# define EV_INVOKE_PENDING ev_invoke_pending (EV_A) 582# define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
580#endif 583#endif
584
585#define EVUNLOOP_RECURSE 0x80
581 586
582/*****************************************************************************/ 587/*****************************************************************************/
583 588
584#ifndef EV_HAVE_EV_TIME 589#ifndef EV_HAVE_EV_TIME
585ev_tstamp 590ev_tstamp
642 647
643 tv.tv_sec = (time_t)delay; 648 tv.tv_sec = (time_t)delay;
644 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 649 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
645 650
646 /* 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 */
647 /* somehting not guaranteed by newer posix versions, but guaranteed */ 652 /* something not guaranteed by newer posix versions, but guaranteed */
648 /* by older ones */ 653 /* by older ones */
649 select (0, 0, 0, 0, &tv); 654 select (0, 0, 0, 0, &tv);
650#endif 655#endif
651 } 656 }
652} 657}
760} 765}
761 766
762/*****************************************************************************/ 767/*****************************************************************************/
763 768
764inline_speed void 769inline_speed void
765fd_event (EV_P_ int fd, int revents) 770fd_event_nc (EV_P_ int fd, int revents)
766{ 771{
767 ANFD *anfd = anfds + fd; 772 ANFD *anfd = anfds + fd;
768 ev_io *w; 773 ev_io *w;
769 774
770 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)
774 if (ev) 779 if (ev)
775 ev_feed_event (EV_A_ (W)w, ev); 780 ev_feed_event (EV_A_ (W)w, ev);
776 } 781 }
777} 782}
778 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
779void 795void
780ev_feed_fd_event (EV_P_ int fd, int revents) 796ev_feed_fd_event (EV_P_ int fd, int revents)
781{ 797{
782 if (fd >= 0 && fd < anfdmax) 798 if (fd >= 0 && fd < anfdmax)
783 fd_event (EV_A_ fd, revents); 799 fd_event_nc (EV_A_ fd, revents);
784} 800}
785 801
786/* make sure the external fd watch events are in-sync */ 802/* make sure the external fd watch events are in-sync */
787/* with the kernel/libev internal state */ 803/* with the kernel/libev internal state */
788inline_size void 804inline_size void
903 for (fd = 0; fd < anfdmax; ++fd) 919 for (fd = 0; fd < anfdmax; ++fd)
904 if (anfds [fd].events) 920 if (anfds [fd].events)
905 { 921 {
906 anfds [fd].events = 0; 922 anfds [fd].events = 0;
907 anfds [fd].emask = 0; 923 anfds [fd].emask = 0;
908 fd_change (EV_A_ fd, EV__IOFDSET | 1); 924 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
909 } 925 }
910} 926}
911 927
912/*****************************************************************************/ 928/*****************************************************************************/
913 929
1409void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) 1425void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P))
1410{ 1426{
1411 invoke_cb = invoke_pending_cb; 1427 invoke_cb = invoke_pending_cb;
1412} 1428}
1413 1429
1414void ev_set_blocking_cb (EV_P_ void (*suspend_cb_)(EV_P), void (*resume_cb_)(EV_P)) 1430void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P))
1415{ 1431{
1416 suspend_cb = suspend_cb_; 1432 release_cb = release;
1417 resume_cb = resume_cb_; 1433 acquire_cb = acquire;
1418} 1434}
1419#endif 1435#endif
1420 1436
1421/* initialise a loop structure, must be zero-initialised */ 1437/* initialise a loop structure, must be zero-initialised */
1422static void noinline 1438static void noinline
1822 1838
1823void 1839void
1824ev_invoke (EV_P_ void *w, int revents) 1840ev_invoke (EV_P_ void *w, int revents)
1825{ 1841{
1826 EV_CB_INVOKE ((W)w, revents); 1842 EV_CB_INVOKE ((W)w, revents);
1843}
1844
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;
1827} 1855}
1828 1856
1829void noinline 1857void noinline
1830ev_invoke_pending (EV_P) 1858ev_invoke_pending (EV_P)
1831{ 1859{
2077{ 2105{
2078#if EV_MINIMAL < 2 2106#if EV_MINIMAL < 2
2079 ++loop_depth; 2107 ++loop_depth;
2080#endif 2108#endif
2081 2109
2110 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE));
2111
2082 loop_done = EVUNLOOP_CANCEL; 2112 loop_done = EVUNLOOP_CANCEL;
2083 2113
2084 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */ 2114 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
2085 2115
2086 do 2116 do
2113 { 2143 {
2114 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2144 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2115 EV_INVOKE_PENDING; 2145 EV_INVOKE_PENDING;
2116 } 2146 }
2117 2147
2148 if (expect_false (loop_done))
2149 break;
2150
2118 /* we might have forked, so reify kernel state if necessary */ 2151 /* we might have forked, so reify kernel state if necessary */
2119 if (expect_false (postfork)) 2152 if (expect_false (postfork))
2120 loop_fork (EV_A); 2153 loop_fork (EV_A);
2121 2154
2122 /* update fd-related kernel structures */ 2155 /* update fd-related kernel structures */
2172 } 2205 }
2173 2206
2174#if EV_MINIMAL < 2 2207#if EV_MINIMAL < 2
2175 ++loop_count; 2208 ++loop_count;
2176#endif 2209#endif
2210 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */
2177 backend_poll (EV_A_ waittime); 2211 backend_poll (EV_A_ waittime);
2212 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */
2178 2213
2179 /* update ev_rt_now, do magic */ 2214 /* update ev_rt_now, do magic */
2180 time_update (EV_A_ waittime + sleeptime); 2215 time_update (EV_A_ waittime + sleeptime);
2181 } 2216 }
2182 2217
2348 2383
2349 ev_start (EV_A_ (W)w, 1); 2384 ev_start (EV_A_ (W)w, 1);
2350 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 2385 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
2351 wlist_add (&anfds[fd].head, (WL)w); 2386 wlist_add (&anfds[fd].head, (WL)w);
2352 2387
2353 fd_change (EV_A_ fd, w->events & EV__IOFDSET | 1); 2388 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
2354 w->events &= ~EV__IOFDSET; 2389 w->events &= ~EV__IOFDSET;
2355 2390
2356 EV_FREQUENT_CHECK; 2391 EV_FREQUENT_CHECK;
2357} 2392}
2358 2393
2452 } 2487 }
2453 2488
2454 EV_FREQUENT_CHECK; 2489 EV_FREQUENT_CHECK;
2455} 2490}
2456 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
2457#if EV_PERIODIC_ENABLE 2498#if EV_PERIODIC_ENABLE
2458void noinline 2499void noinline
2459ev_periodic_start (EV_P_ ev_periodic *w) 2500ev_periodic_start (EV_P_ ev_periodic *w)
2460{ 2501{
2461 if (expect_false (ev_is_active (w))) 2502 if (expect_false (ev_is_active (w)))
2562 if (!((WL)w)->next) 2603 if (!((WL)w)->next)
2563 { 2604 {
2564#if _WIN32 2605#if _WIN32
2565 signal (w->signum, ev_sighandler); 2606 signal (w->signum, ev_sighandler);
2566#else 2607#else
2567 struct sigaction sa; 2608 struct sigaction sa = { };
2568 sa.sa_handler = ev_sighandler; 2609 sa.sa_handler = ev_sighandler;
2569 sigfillset (&sa.sa_mask); 2610 sigfillset (&sa.sa_mask);
2570 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 */
2571 sigaction (w->signum, &sa, 0); 2612 sigaction (w->signum, &sa, 0);
2572#endif 2613#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines