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

Comparing libev/ev.c (file contents):
Revision 1.181 by root, Wed Dec 12 00:17:08 2007 UTC vs.
Revision 1.188 by root, Thu Dec 20 07:12:57 2007 UTC

202#ifndef CLOCK_REALTIME 202#ifndef CLOCK_REALTIME
203# undef EV_USE_REALTIME 203# undef EV_USE_REALTIME
204# define EV_USE_REALTIME 0 204# define EV_USE_REALTIME 0
205#endif 205#endif
206 206
207#if !EV_STAT_ENABLE
208# undef EV_USE_INOTIFY
209# define EV_USE_INOTIFY 0
210#endif
211
212#if EV_USE_INOTIFY
213# include <sys/inotify.h>
214#endif
215
207#if EV_SELECT_IS_WINSOCKET 216#if EV_SELECT_IS_WINSOCKET
208# include <winsock.h> 217# include <winsock.h>
209#endif
210
211#if !EV_STAT_ENABLE
212# define EV_USE_INOTIFY 0
213#endif
214
215#if EV_USE_INOTIFY
216# include <sys/inotify.h>
217#endif 218#endif
218 219
219/**/ 220/**/
220 221
221/* 222/*
230 231
231#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 232#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
232#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ 233#define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
233/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */ 234/*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds, TODO */
234 235
235#if __GNUC__ >= 3 236#if __GNUC__ >= 4
236# define expect(expr,value) __builtin_expect ((expr),(value)) 237# define expect(expr,value) __builtin_expect ((expr),(value))
237# define noinline __attribute__ ((noinline)) 238# define noinline __attribute__ ((noinline))
238#else 239#else
239# define expect(expr,value) (expr) 240# define expect(expr,value) (expr)
240# define noinline 241# define noinline
533 { 534 {
534 int fd = fdchanges [i]; 535 int fd = fdchanges [i];
535 ANFD *anfd = anfds + fd; 536 ANFD *anfd = anfds + fd;
536 ev_io *w; 537 ev_io *w;
537 538
538 int events = 0; 539 unsigned char events = 0;
539 540
540 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) 541 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
541 events |= w->events; 542 events |= (unsigned char)w->events;
542 543
543#if EV_SELECT_IS_WINSOCKET 544#if EV_SELECT_IS_WINSOCKET
544 if (events) 545 if (events)
545 { 546 {
546 unsigned long argp; 547 unsigned long argp;
547 anfd->handle = _get_osfhandle (fd); 548 anfd->handle = _get_osfhandle (fd);
548 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); 549 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
549 } 550 }
550#endif 551#endif
551 552
553 {
554 unsigned char o_events = anfd->events;
555 unsigned char o_reify = anfd->reify;
556
552 anfd->reify = 0; 557 anfd->reify = 0;
553
554 backend_modify (EV_A_ fd, anfd->events, events);
555 anfd->events = events; 558 anfd->events = events;
559
560 if (o_events != events || o_reify & EV_IOFDSET)
561 backend_modify (EV_A_ fd, o_events, events);
562 }
556 } 563 }
557 564
558 fdchangecnt = 0; 565 fdchangecnt = 0;
559} 566}
560 567
561void inline_size 568void inline_size
562fd_change (EV_P_ int fd) 569fd_change (EV_P_ int fd, int flags)
563{ 570{
564 if (expect_false (anfds [fd].reify)) 571 unsigned char reify = anfds [fd].reify;
565 return;
566
567 anfds [fd].reify = 1; 572 anfds [fd].reify |= flags;
568 573
574 if (expect_true (!reify))
575 {
569 ++fdchangecnt; 576 ++fdchangecnt;
570 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 577 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
571 fdchanges [fdchangecnt - 1] = fd; 578 fdchanges [fdchangecnt - 1] = fd;
579 }
572} 580}
573 581
574void inline_speed 582void inline_speed
575fd_kill (EV_P_ int fd) 583fd_kill (EV_P_ int fd)
576{ 584{
627 635
628 for (fd = 0; fd < anfdmax; ++fd) 636 for (fd = 0; fd < anfdmax; ++fd)
629 if (anfds [fd].events) 637 if (anfds [fd].events)
630 { 638 {
631 anfds [fd].events = 0; 639 anfds [fd].events = 0;
632 fd_change (EV_A_ fd); 640 fd_change (EV_A_ fd, EV_IOFDSET | 1);
633 } 641 }
634} 642}
635 643
636/*****************************************************************************/ 644/*****************************************************************************/
637 645
792 ev_unref (EV_A); /* child watcher should not keep loop alive */ 800 ev_unref (EV_A); /* child watcher should not keep loop alive */
793} 801}
794 802
795/*****************************************************************************/ 803/*****************************************************************************/
796 804
797static ev_child *childs [EV_PID_HASHSIZE]; 805static WL childs [EV_PID_HASHSIZE];
798 806
799#ifndef _WIN32 807#ifndef _WIN32
800 808
801static ev_signal childev; 809static ev_signal childev;
802 810
1030#if EV_IDLE_ENABLE 1038#if EV_IDLE_ENABLE
1031 array_free (idle, [i]); 1039 array_free (idle, [i]);
1032#endif 1040#endif
1033 } 1041 }
1034 1042
1043 ev_free (anfds); anfdmax = 0;
1044
1035 /* have to use the microsoft-never-gets-it-right macro */ 1045 /* have to use the microsoft-never-gets-it-right macro */
1036 array_free (fdchange, EMPTY); 1046 array_free (fdchange, EMPTY);
1037 array_free (timer, EMPTY); 1047 array_free (timer, EMPTY);
1038#if EV_PERIODIC_ENABLE 1048#if EV_PERIODIC_ENABLE
1039 array_free (periodic, EMPTY); 1049 array_free (periodic, EMPTY);
1050#endif
1051#if EV_FORK_ENABLE
1052 array_free (fork, EMPTY);
1040#endif 1053#endif
1041 array_free (prepare, EMPTY); 1054 array_free (prepare, EMPTY);
1042 array_free (check, EMPTY); 1055 array_free (check, EMPTY);
1043 1056
1044 backend = 0; 1057 backend = 0;
1597 1610
1598 assert (("ev_io_start called with negative fd", fd >= 0)); 1611 assert (("ev_io_start called with negative fd", fd >= 0));
1599 1612
1600 ev_start (EV_A_ (W)w, 1); 1613 ev_start (EV_A_ (W)w, 1);
1601 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); 1614 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1602 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1615 wlist_add (&anfds[fd].head, (WL)w);
1603 1616
1604 fd_change (EV_A_ fd); 1617 fd_change (EV_A_ fd, w->events & EV_IOFDSET | 1);
1618 w->events &= ~EV_IOFDSET;
1605} 1619}
1606 1620
1607void noinline 1621void noinline
1608ev_io_stop (EV_P_ ev_io *w) 1622ev_io_stop (EV_P_ ev_io *w)
1609{ 1623{
1611 if (expect_false (!ev_is_active (w))) 1625 if (expect_false (!ev_is_active (w)))
1612 return; 1626 return;
1613 1627
1614 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1628 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1615 1629
1616 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1630 wlist_del (&anfds[w->fd].head, (WL)w);
1617 ev_stop (EV_A_ (W)w); 1631 ev_stop (EV_A_ (W)w);
1618 1632
1619 fd_change (EV_A_ w->fd); 1633 fd_change (EV_A_ w->fd, 1);
1620} 1634}
1621 1635
1622void noinline 1636void noinline
1623ev_timer_start (EV_P_ ev_timer *w) 1637ev_timer_start (EV_P_ ev_timer *w)
1624{ 1638{
1766 sigprocmask (SIG_SETMASK, &prev, 0); 1780 sigprocmask (SIG_SETMASK, &prev, 0);
1767#endif 1781#endif
1768 } 1782 }
1769 1783
1770 ev_start (EV_A_ (W)w, 1); 1784 ev_start (EV_A_ (W)w, 1);
1771 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1785 wlist_add (&signals [w->signum - 1].head, (WL)w);
1772 1786
1773 if (!((WL)w)->next) 1787 if (!((WL)w)->next)
1774 { 1788 {
1775#if _WIN32 1789#if _WIN32
1776 signal (w->signum, sighandler); 1790 signal (w->signum, sighandler);
1789{ 1803{
1790 clear_pending (EV_A_ (W)w); 1804 clear_pending (EV_A_ (W)w);
1791 if (expect_false (!ev_is_active (w))) 1805 if (expect_false (!ev_is_active (w)))
1792 return; 1806 return;
1793 1807
1794 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1808 wlist_del (&signals [w->signum - 1].head, (WL)w);
1795 ev_stop (EV_A_ (W)w); 1809 ev_stop (EV_A_ (W)w);
1796 1810
1797 if (!signals [w->signum - 1].head) 1811 if (!signals [w->signum - 1].head)
1798 signal (w->signum, SIG_DFL); 1812 signal (w->signum, SIG_DFL);
1799} 1813}
1806#endif 1820#endif
1807 if (expect_false (ev_is_active (w))) 1821 if (expect_false (ev_is_active (w)))
1808 return; 1822 return;
1809 1823
1810 ev_start (EV_A_ (W)w, 1); 1824 ev_start (EV_A_ (W)w, 1);
1811 wlist_add ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 1825 wlist_add (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1812} 1826}
1813 1827
1814void 1828void
1815ev_child_stop (EV_P_ ev_child *w) 1829ev_child_stop (EV_P_ ev_child *w)
1816{ 1830{
1817 clear_pending (EV_A_ (W)w); 1831 clear_pending (EV_A_ (W)w);
1818 if (expect_false (!ev_is_active (w))) 1832 if (expect_false (!ev_is_active (w)))
1819 return; 1833 return;
1820 1834
1821 wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); 1835 wlist_del (&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w);
1822 ev_stop (EV_A_ (W)w); 1836 ev_stop (EV_A_ (W)w);
1823} 1837}
1824 1838
1825#if EV_STAT_ENABLE 1839#if EV_STAT_ENABLE
1826 1840
2168 2182
2169#if EV_EMBED_ENABLE 2183#if EV_EMBED_ENABLE
2170void noinline 2184void noinline
2171ev_embed_sweep (EV_P_ ev_embed *w) 2185ev_embed_sweep (EV_P_ ev_embed *w)
2172{ 2186{
2173 ev_loop (w->loop, EVLOOP_NONBLOCK); 2187 ev_loop (w->other, EVLOOP_NONBLOCK);
2174} 2188}
2175 2189
2176static void 2190static void
2177embed_cb (EV_P_ ev_io *io, int revents) 2191embed_cb (EV_P_ ev_io *io, int revents)
2178{ 2192{
2189{ 2203{
2190 if (expect_false (ev_is_active (w))) 2204 if (expect_false (ev_is_active (w)))
2191 return; 2205 return;
2192 2206
2193 { 2207 {
2194 struct ev_loop *loop = w->loop; 2208 struct ev_loop *loop = w->other;
2195 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); 2209 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
2196 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); 2210 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
2197 } 2211 }
2198 2212
2199 ev_set_priority (&w->io, ev_priority (w)); 2213 ev_set_priority (&w->io, ev_priority (w));
2306 ev_timer_set (&once->to, timeout, 0.); 2320 ev_timer_set (&once->to, timeout, 0.);
2307 ev_timer_start (EV_A_ &once->to); 2321 ev_timer_start (EV_A_ &once->to);
2308 } 2322 }
2309} 2323}
2310 2324
2325#if EV_MULTIPLICITY
2326 #include "ev_wrap.h"
2327#endif
2328
2311#ifdef __cplusplus 2329#ifdef __cplusplus
2312} 2330}
2313#endif 2331#endif
2314 2332

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines