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

Comparing libev/ev.c (file contents):
Revision 1.248 by root, Wed May 21 23:25:21 2008 UTC vs.
Revision 1.261 by root, Mon Sep 29 03:31:14 2008 UTC

126# define EV_USE_EVENTFD 1 126# define EV_USE_EVENTFD 1
127# else 127# else
128# define EV_USE_EVENTFD 0 128# define EV_USE_EVENTFD 0
129# endif 129# endif
130# endif 130# endif
131 131
132#endif 132#endif
133 133
134#include <math.h> 134#include <math.h>
135#include <stdlib.h> 135#include <stdlib.h>
136#include <fcntl.h> 136#include <fcntl.h>
154#ifndef _WIN32 154#ifndef _WIN32
155# include <sys/time.h> 155# include <sys/time.h>
156# include <sys/wait.h> 156# include <sys/wait.h>
157# include <unistd.h> 157# include <unistd.h>
158#else 158#else
159# include <io.h>
159# define WIN32_LEAN_AND_MEAN 160# define WIN32_LEAN_AND_MEAN
160# include <windows.h> 161# include <windows.h>
161# ifndef EV_SELECT_IS_WINSOCKET 162# ifndef EV_SELECT_IS_WINSOCKET
162# define EV_SELECT_IS_WINSOCKET 1 163# define EV_SELECT_IS_WINSOCKET 1
163# endif 164# endif
164#endif 165#endif
165 166
166/* this block tries to deduce configuration from header-defined symbols and defaults */ 167/* this block tries to deduce configuration from header-defined symbols and defaults */
167 168
168#ifndef EV_USE_MONOTONIC 169#ifndef EV_USE_MONOTONIC
170# if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
171# define EV_USE_MONOTONIC 1
172# else
169# define EV_USE_MONOTONIC 0 173# define EV_USE_MONOTONIC 0
174# endif
170#endif 175#endif
171 176
172#ifndef EV_USE_REALTIME 177#ifndef EV_USE_REALTIME
173# define EV_USE_REALTIME 0 178# define EV_USE_REALTIME 0
174#endif 179#endif
175 180
176#ifndef EV_USE_NANOSLEEP 181#ifndef EV_USE_NANOSLEEP
182# if _POSIX_C_SOURCE >= 199309L
183# define EV_USE_NANOSLEEP 1
184# else
177# define EV_USE_NANOSLEEP 0 185# define EV_USE_NANOSLEEP 0
186# endif
178#endif 187#endif
179 188
180#ifndef EV_USE_SELECT 189#ifndef EV_USE_SELECT
181# define EV_USE_SELECT 1 190# define EV_USE_SELECT 1
182#endif 191#endif
233# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) 242# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
234# define EV_USE_EVENTFD 1 243# define EV_USE_EVENTFD 1
235# else 244# else
236# define EV_USE_EVENTFD 0 245# define EV_USE_EVENTFD 0
237# endif 246# endif
247#endif
248
249#if 0 /* debugging */
250# define EV_VERIFY 3
251# define EV_USE_4HEAP 1
252# define EV_HEAP_CACHE_AT 1
253#endif
254
255#ifndef EV_VERIFY
256# define EV_VERIFY !EV_MINIMAL
238#endif 257#endif
239 258
240#ifndef EV_USE_4HEAP 259#ifndef EV_USE_4HEAP
241# define EV_USE_4HEAP !EV_MINIMAL 260# define EV_USE_4HEAP !EV_MINIMAL
242#endif 261#endif
288# endif 307# endif
289#endif 308#endif
290 309
291/**/ 310/**/
292 311
293/* undefined or zero: no verification done or available */
294/* 1 or higher: ev_loop_verify function available */
295/* 2 or higher: ev_loop_verify is called frequently */
296#define EV_VERIFY 1
297
298#if EV_VERIFY > 1 312#if EV_VERIFY >= 3
299# define EV_FREQUENT_CHECK ev_loop_verify (EV_A) 313# define EV_FREQUENT_CHECK ev_loop_verify (EV_A)
300#else 314#else
301# define EV_FREQUENT_CHECK do { } while (0) 315# define EV_FREQUENT_CHECK do { } while (0)
302#endif 316#endif
303 317
551 struct timeval tv; 565 struct timeval tv;
552 566
553 tv.tv_sec = (time_t)delay; 567 tv.tv_sec = (time_t)delay;
554 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 568 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
555 569
570 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
571 /* somehting nto guaranteed by newer posix versions, but guaranteed */
572 /* by older ones */
556 select (0, 0, 0, 0, &tv); 573 select (0, 0, 0, 0, &tv);
557#endif 574#endif
558 } 575 }
559} 576}
560 577
694 events |= (unsigned char)w->events; 711 events |= (unsigned char)w->events;
695 712
696#if EV_SELECT_IS_WINSOCKET 713#if EV_SELECT_IS_WINSOCKET
697 if (events) 714 if (events)
698 { 715 {
699 unsigned long argp; 716 unsigned long arg;
700 #ifdef EV_FD_TO_WIN32_HANDLE 717 #ifdef EV_FD_TO_WIN32_HANDLE
701 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 718 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
702 #else 719 #else
703 anfd->handle = _get_osfhandle (fd); 720 anfd->handle = _get_osfhandle (fd);
704 #endif 721 #endif
705 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); 722 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0));
706 } 723 }
707#endif 724#endif
708 725
709 { 726 {
710 unsigned char o_events = anfd->events; 727 unsigned char o_events = anfd->events;
763{ 780{
764 int fd; 781 int fd;
765 782
766 for (fd = 0; fd < anfdmax; ++fd) 783 for (fd = 0; fd < anfdmax; ++fd)
767 if (anfds [fd].events) 784 if (anfds [fd].events)
768 if (!fd_valid (fd) == -1 && errno == EBADF) 785 if (!fd_valid (fd) && errno == EBADF)
769 fd_kill (EV_A_ fd); 786 fd_kill (EV_A_ fd);
770} 787}
771 788
772/* called on ENOMEM in select/poll to kill some fds and retry */ 789/* called on ENOMEM in select/poll to kill some fds and retry */
773static void noinline 790static void noinline
932/* rebuild the heap: this function is used only once and executed rarely */ 949/* rebuild the heap: this function is used only once and executed rarely */
933void inline_size 950void inline_size
934reheap (ANHE *heap, int N) 951reheap (ANHE *heap, int N)
935{ 952{
936 int i; 953 int i;
954
937 /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */ 955 /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */
938 /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */ 956 /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */
939 for (i = 0; i < N; ++i) 957 for (i = 0; i < N; ++i)
940 upheap (heap, i + HEAP0); 958 upheap (heap, i + HEAP0);
941} 959}
942 960
943#if EV_VERIFY
944static void
945checkheap (ANHE *heap, int N)
946{
947 int i;
948
949 for (i = HEAP0; i < N + HEAP0; ++i)
950 {
951 assert (("active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i));
952 assert (("heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i])));
953 assert (("heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i]))));
954 }
955}
956#endif
957
958/*****************************************************************************/ 961/*****************************************************************************/
959 962
960typedef struct 963typedef struct
961{ 964{
962 WL head; 965 WL head;
984 987
985void inline_speed 988void inline_speed
986fd_intern (int fd) 989fd_intern (int fd)
987{ 990{
988#ifdef _WIN32 991#ifdef _WIN32
989 int arg = 1; 992 unsigned long arg = 1;
990 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 993 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
991#else 994#else
992 fcntl (fd, F_SETFD, FD_CLOEXEC); 995 fcntl (fd, F_SETFD, FD_CLOEXEC);
993 fcntl (fd, F_SETFL, O_NONBLOCK); 996 fcntl (fd, F_SETFL, O_NONBLOCK);
994#endif 997#endif
1478 1481
1479 postfork = 0; 1482 postfork = 0;
1480} 1483}
1481 1484
1482#if EV_MULTIPLICITY 1485#if EV_MULTIPLICITY
1486
1483struct ev_loop * 1487struct ev_loop *
1484ev_loop_new (unsigned int flags) 1488ev_loop_new (unsigned int flags)
1485{ 1489{
1486 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 1490 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
1487 1491
1507{ 1511{
1508 postfork = 1; /* must be in line with ev_default_fork */ 1512 postfork = 1; /* must be in line with ev_default_fork */
1509} 1513}
1510 1514
1511#if EV_VERIFY 1515#if EV_VERIFY
1512static void 1516static void noinline
1517verify_watcher (EV_P_ W w)
1518{
1519 assert (("watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI));
1520
1521 if (w->pending)
1522 assert (("pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w));
1523}
1524
1525static void noinline
1526verify_heap (EV_P_ ANHE *heap, int N)
1527{
1528 int i;
1529
1530 for (i = HEAP0; i < N + HEAP0; ++i)
1531 {
1532 assert (("active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i));
1533 assert (("heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i])));
1534 assert (("heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i]))));
1535
1536 verify_watcher (EV_A_ (W)ANHE_w (heap [i]));
1537 }
1538}
1539
1540static void noinline
1513array_check (W **ws, int cnt) 1541array_verify (EV_P_ W *ws, int cnt)
1514{ 1542{
1515 while (cnt--) 1543 while (cnt--)
1544 {
1516 assert (("active index mismatch", ev_active (ws [cnt]) == cnt + 1)); 1545 assert (("active index mismatch", ev_active (ws [cnt]) == cnt + 1));
1546 verify_watcher (EV_A_ ws [cnt]);
1547 }
1517} 1548}
1549#endif
1518 1550
1519static void 1551void
1520ev_loop_verify (EV_P) 1552ev_loop_verify (EV_P)
1521{ 1553{
1554#if EV_VERIFY
1522 int i; 1555 int i;
1556 WL w;
1523 1557
1558 assert (activecnt >= -1);
1559
1560 assert (fdchangemax >= fdchangecnt);
1561 for (i = 0; i < fdchangecnt; ++i)
1562 assert (("negative fd in fdchanges", fdchanges [i] >= 0));
1563
1564 assert (anfdmax >= 0);
1565 for (i = 0; i < anfdmax; ++i)
1566 for (w = anfds [i].head; w; w = w->next)
1567 {
1568 verify_watcher (EV_A_ (W)w);
1569 assert (("inactive fd watcher on anfd list", ev_active (w) == 1));
1570 assert (("fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i));
1571 }
1572
1573 assert (timermax >= timercnt);
1524 checkheap (timers, timercnt); 1574 verify_heap (EV_A_ timers, timercnt);
1575
1525#if EV_PERIODIC_ENABLE 1576#if EV_PERIODIC_ENABLE
1577 assert (periodicmax >= periodiccnt);
1526 checkheap (periodics, periodiccnt); 1578 verify_heap (EV_A_ periodics, periodiccnt);
1527#endif 1579#endif
1528 1580
1581 for (i = NUMPRI; i--; )
1582 {
1583 assert (pendingmax [i] >= pendingcnt [i]);
1529#if EV_IDLE_ENABLE 1584#if EV_IDLE_ENABLE
1530 for (i = NUMPRI; i--; ) 1585 assert (idleall >= 0);
1586 assert (idlemax [i] >= idlecnt [i]);
1531 array_check ((W **)idles [i], idlecnt [i]); 1587 array_verify (EV_A_ (W *)idles [i], idlecnt [i]);
1532#endif 1588#endif
1589 }
1590
1533#if EV_FORK_ENABLE 1591#if EV_FORK_ENABLE
1592 assert (forkmax >= forkcnt);
1534 array_check ((W **)forks, forkcnt); 1593 array_verify (EV_A_ (W *)forks, forkcnt);
1535#endif 1594#endif
1536 array_check ((W **)prepares, preparecnt); 1595
1537 array_check ((W **)checks, checkcnt);
1538#if EV_ASYNC_ENABLE 1596#if EV_ASYNC_ENABLE
1597 assert (asyncmax >= asynccnt);
1539 array_check ((W **)asyncs, asynccnt); 1598 array_verify (EV_A_ (W *)asyncs, asynccnt);
1599#endif
1600
1601 assert (preparemax >= preparecnt);
1602 array_verify (EV_A_ (W *)prepares, preparecnt);
1603
1604 assert (checkmax >= checkcnt);
1605 array_verify (EV_A_ (W *)checks, checkcnt);
1606
1607# if 0
1608 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
1609 for (signum = signalmax; signum--; ) if (signals [signum].gotsig)
1540#endif 1610# endif
1541}
1542#endif 1611#endif
1612}
1543 1613
1544#endif 1614#endif /* multiplicity */
1545 1615
1546#if EV_MULTIPLICITY 1616#if EV_MULTIPLICITY
1547struct ev_loop * 1617struct ev_loop *
1548ev_default_loop_init (unsigned int flags) 1618ev_default_loop_init (unsigned int flags)
1549#else 1619#else
1614void inline_speed 1684void inline_speed
1615call_pending (EV_P) 1685call_pending (EV_P)
1616{ 1686{
1617 int pri; 1687 int pri;
1618 1688
1619 EV_FREQUENT_CHECK;
1620
1621 for (pri = NUMPRI; pri--; ) 1689 for (pri = NUMPRI; pri--; )
1622 while (pendingcnt [pri]) 1690 while (pendingcnt [pri])
1623 { 1691 {
1624 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1692 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1625 1693
1627 { 1695 {
1628 /*assert (("non-pending watcher on pending list", p->w->pending));*/ 1696 /*assert (("non-pending watcher on pending list", p->w->pending));*/
1629 1697
1630 p->w->pending = 0; 1698 p->w->pending = 0;
1631 EV_CB_INVOKE (p->w, p->events); 1699 EV_CB_INVOKE (p->w, p->events);
1700 EV_FREQUENT_CHECK;
1632 } 1701 }
1633 } 1702 }
1634
1635 EV_FREQUENT_CHECK;
1636} 1703}
1637 1704
1638#if EV_IDLE_ENABLE 1705#if EV_IDLE_ENABLE
1639void inline_size 1706void inline_size
1640idle_reify (EV_P) 1707idle_reify (EV_P)
1692#if EV_PERIODIC_ENABLE 1759#if EV_PERIODIC_ENABLE
1693void inline_size 1760void inline_size
1694periodics_reify (EV_P) 1761periodics_reify (EV_P)
1695{ 1762{
1696 EV_FREQUENT_CHECK; 1763 EV_FREQUENT_CHECK;
1764
1697 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) 1765 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
1698 { 1766 {
1699 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); 1767 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1700 1768
1701 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1769 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1707 1775
1708 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now)); 1776 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now));
1709 1777
1710 ANHE_at_cache (periodics [HEAP0]); 1778 ANHE_at_cache (periodics [HEAP0]);
1711 downheap (periodics, periodiccnt, HEAP0); 1779 downheap (periodics, periodiccnt, HEAP0);
1712 EV_FREQUENT_CHECK;
1713 } 1780 }
1714 else if (w->interval) 1781 else if (w->interval)
1715 { 1782 {
1716 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1783 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1717 /* if next trigger time is not sufficiently in the future, put it there */ 1784 /* if next trigger time is not sufficiently in the future, put it there */
1842ev_unref (EV_P) 1909ev_unref (EV_P)
1843{ 1910{
1844 --activecnt; 1911 --activecnt;
1845} 1912}
1846 1913
1914void
1915ev_now_update (EV_P)
1916{
1917 time_update (EV_A_ 1e100);
1918}
1919
1847static int loop_done; 1920static int loop_done;
1848 1921
1849void 1922void
1850ev_loop (EV_P_ int flags) 1923ev_loop (EV_P_ int flags)
1851{ 1924{
1853 1926
1854 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 1927 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1855 1928
1856 do 1929 do
1857 { 1930 {
1931#if EV_VERIFY >= 2
1932 ev_loop_verify (EV_A);
1933#endif
1934
1858#ifndef _WIN32 1935#ifndef _WIN32
1859 if (expect_false (curpid)) /* penalise the forking check even more */ 1936 if (expect_false (curpid)) /* penalise the forking check even more */
1860 if (expect_false (getpid () != curpid)) 1937 if (expect_false (getpid () != curpid))
1861 { 1938 {
1862 curpid = getpid (); 1939 curpid = getpid ();
2500 } 2577 }
2501 2578
2502 } 2579 }
2503} 2580}
2504 2581
2582#endif
2583
2584#ifdef _WIN32
2585# define EV_LSTAT(p,b) _stati64 (p, b)
2586#else
2587# define EV_LSTAT(p,b) lstat (p, b)
2505#endif 2588#endif
2506 2589
2507void 2590void
2508ev_stat_stat (EV_P_ ev_stat *w) 2591ev_stat_stat (EV_P_ ev_stat *w)
2509{ 2592{
2750 ev_loop (EV_A_ EVLOOP_NONBLOCK); 2833 ev_loop (EV_A_ EVLOOP_NONBLOCK);
2751 } 2834 }
2752 } 2835 }
2753} 2836}
2754 2837
2838static void
2839embed_fork_cb (EV_P_ ev_fork *fork_w, int revents)
2840{
2841 ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork));
2842
2843 {
2844 struct ev_loop *loop = w->other;
2845
2846 ev_loop_fork (EV_A);
2847 }
2848}
2849
2755#if 0 2850#if 0
2756static void 2851static void
2757embed_idle_cb (EV_P_ ev_idle *idle, int revents) 2852embed_idle_cb (EV_P_ ev_idle *idle, int revents)
2758{ 2853{
2759 ev_idle_stop (EV_A_ idle); 2854 ev_idle_stop (EV_A_ idle);
2779 2874
2780 ev_prepare_init (&w->prepare, embed_prepare_cb); 2875 ev_prepare_init (&w->prepare, embed_prepare_cb);
2781 ev_set_priority (&w->prepare, EV_MINPRI); 2876 ev_set_priority (&w->prepare, EV_MINPRI);
2782 ev_prepare_start (EV_A_ &w->prepare); 2877 ev_prepare_start (EV_A_ &w->prepare);
2783 2878
2879 ev_fork_init (&w->fork, embed_fork_cb);
2880 ev_fork_start (EV_A_ &w->fork);
2881
2784 /*ev_idle_init (&w->idle, e,bed_idle_cb);*/ 2882 /*ev_idle_init (&w->idle, e,bed_idle_cb);*/
2785 2883
2786 ev_start (EV_A_ (W)w, 1); 2884 ev_start (EV_A_ (W)w, 1);
2787 2885
2788 EV_FREQUENT_CHECK; 2886 EV_FREQUENT_CHECK;
2795 if (expect_false (!ev_is_active (w))) 2893 if (expect_false (!ev_is_active (w)))
2796 return; 2894 return;
2797 2895
2798 EV_FREQUENT_CHECK; 2896 EV_FREQUENT_CHECK;
2799 2897
2800 ev_io_stop (EV_A_ &w->io); 2898 ev_io_stop (EV_A_ &w->io);
2801 ev_prepare_stop (EV_A_ &w->prepare); 2899 ev_prepare_stop (EV_A_ &w->prepare);
2802 2900 ev_fork_stop (EV_A_ &w->fork);
2803 ev_stop (EV_A_ (W)w);
2804 2901
2805 EV_FREQUENT_CHECK; 2902 EV_FREQUENT_CHECK;
2806} 2903}
2807#endif 2904#endif
2808 2905
2905once_cb (EV_P_ struct ev_once *once, int revents) 3002once_cb (EV_P_ struct ev_once *once, int revents)
2906{ 3003{
2907 void (*cb)(int revents, void *arg) = once->cb; 3004 void (*cb)(int revents, void *arg) = once->cb;
2908 void *arg = once->arg; 3005 void *arg = once->arg;
2909 3006
2910 ev_io_stop (EV_A_ &once->io); 3007 ev_io_stop (EV_A_ &once->io);
2911 ev_timer_stop (EV_A_ &once->to); 3008 ev_timer_stop (EV_A_ &once->to);
2912 ev_free (once); 3009 ev_free (once);
2913 3010
2914 cb (revents, arg); 3011 cb (revents, arg);
2915} 3012}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines