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

Comparing libev/ev.c (file contents):
Revision 1.249 by root, Wed May 21 23:30:52 2008 UTC vs.
Revision 1.256 by root, Thu Jun 19 06:53:49 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
236# define EV_USE_EVENTFD 0 245# define EV_USE_EVENTFD 0
237# endif 246# endif
238#endif 247#endif
239 248
240#if 0 /* debugging */ 249#if 0 /* debugging */
241# define EV_VERIFY 1 250# define EV_VERIFY 3
242# define EV_USE_4HEAP 1 251# define EV_USE_4HEAP 1
243# define EV_HEAP_CACHE_AT 1 252# define EV_HEAP_CACHE_AT 1
253#endif
254
255#ifndef EV_VERIFY
256# define EV_VERIFY !EV_MINIMAL
244#endif 257#endif
245 258
246#ifndef EV_USE_4HEAP 259#ifndef EV_USE_4HEAP
247# define EV_USE_4HEAP !EV_MINIMAL 260# define EV_USE_4HEAP !EV_MINIMAL
248#endif 261#endif
294# endif 307# endif
295#endif 308#endif
296 309
297/**/ 310/**/
298 311
299/* EV_VERIFY: enable internal consistency checks
300 * undefined or zero: no verification done or available
301 * 1 or higher: ev_loop_verify function available
302 * 2 or higher: ev_loop_verify is called frequently
303 */
304#if EV_VERIFY >= 1 312#if EV_VERIFY >= 3
305# define EV_FREQUENT_CHECK ev_loop_verify (EV_A) 313# define EV_FREQUENT_CHECK ev_loop_verify (EV_A)
306#else 314#else
307# define EV_FREQUENT_CHECK do { } while (0) 315# define EV_FREQUENT_CHECK do { } while (0)
308#endif 316#endif
309 317
700 events |= (unsigned char)w->events; 708 events |= (unsigned char)w->events;
701 709
702#if EV_SELECT_IS_WINSOCKET 710#if EV_SELECT_IS_WINSOCKET
703 if (events) 711 if (events)
704 { 712 {
705 unsigned long argp; 713 unsigned long arg;
706 #ifdef EV_FD_TO_WIN32_HANDLE 714 #ifdef EV_FD_TO_WIN32_HANDLE
707 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 715 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
708 #else 716 #else
709 anfd->handle = _get_osfhandle (fd); 717 anfd->handle = _get_osfhandle (fd);
710 #endif 718 #endif
711 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); 719 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0));
712 } 720 }
713#endif 721#endif
714 722
715 { 723 {
716 unsigned char o_events = anfd->events; 724 unsigned char o_events = anfd->events;
769{ 777{
770 int fd; 778 int fd;
771 779
772 for (fd = 0; fd < anfdmax; ++fd) 780 for (fd = 0; fd < anfdmax; ++fd)
773 if (anfds [fd].events) 781 if (anfds [fd].events)
774 if (!fd_valid (fd) == -1 && errno == EBADF) 782 if (!fd_valid (fd) && errno == EBADF)
775 fd_kill (EV_A_ fd); 783 fd_kill (EV_A_ fd);
776} 784}
777 785
778/* called on ENOMEM in select/poll to kill some fds and retry */ 786/* called on ENOMEM in select/poll to kill some fds and retry */
779static void noinline 787static void noinline
938/* rebuild the heap: this function is used only once and executed rarely */ 946/* rebuild the heap: this function is used only once and executed rarely */
939void inline_size 947void inline_size
940reheap (ANHE *heap, int N) 948reheap (ANHE *heap, int N)
941{ 949{
942 int i; 950 int i;
951
943 /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */ 952 /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */
944 /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */ 953 /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */
945 for (i = 0; i < N; ++i) 954 for (i = 0; i < N; ++i)
946 upheap (heap, i + HEAP0); 955 upheap (heap, i + HEAP0);
947} 956}
948 957
949#if EV_VERIFY
950static void
951checkheap (ANHE *heap, int N)
952{
953 int i;
954
955 for (i = HEAP0; i < N + HEAP0; ++i)
956 {
957 assert (("active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i));
958 assert (("heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i])));
959 assert (("heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i]))));
960 }
961}
962#endif
963
964/*****************************************************************************/ 958/*****************************************************************************/
965 959
966typedef struct 960typedef struct
967{ 961{
968 WL head; 962 WL head;
990 984
991void inline_speed 985void inline_speed
992fd_intern (int fd) 986fd_intern (int fd)
993{ 987{
994#ifdef _WIN32 988#ifdef _WIN32
995 int arg = 1; 989 unsigned long arg = 1;
996 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 990 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
997#else 991#else
998 fcntl (fd, F_SETFD, FD_CLOEXEC); 992 fcntl (fd, F_SETFD, FD_CLOEXEC);
999 fcntl (fd, F_SETFL, O_NONBLOCK); 993 fcntl (fd, F_SETFL, O_NONBLOCK);
1000#endif 994#endif
1484 1478
1485 postfork = 0; 1479 postfork = 0;
1486} 1480}
1487 1481
1488#if EV_MULTIPLICITY 1482#if EV_MULTIPLICITY
1483
1489struct ev_loop * 1484struct ev_loop *
1490ev_loop_new (unsigned int flags) 1485ev_loop_new (unsigned int flags)
1491{ 1486{
1492 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 1487 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
1493 1488
1513{ 1508{
1514 postfork = 1; /* must be in line with ev_default_fork */ 1509 postfork = 1; /* must be in line with ev_default_fork */
1515} 1510}
1516 1511
1517#if EV_VERIFY 1512#if EV_VERIFY
1518static void 1513void noinline
1514verify_watcher (EV_P_ W w)
1515{
1516 assert (("watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI));
1517
1518 if (w->pending)
1519 assert (("pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w));
1520}
1521
1522static void noinline
1523verify_heap (EV_P_ ANHE *heap, int N)
1524{
1525 int i;
1526
1527 for (i = HEAP0; i < N + HEAP0; ++i)
1528 {
1529 assert (("active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i));
1530 assert (("heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i])));
1531 assert (("heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i]))));
1532
1533 verify_watcher (EV_A_ (W)ANHE_w (heap [i]));
1534 }
1535}
1536
1537static void noinline
1519array_check (W **ws, int cnt) 1538array_verify (EV_P_ W *ws, int cnt)
1520{ 1539{
1521 while (cnt--) 1540 while (cnt--)
1541 {
1522 assert (("active index mismatch", ev_active (ws [cnt]) == cnt + 1)); 1542 assert (("active index mismatch", ev_active (ws [cnt]) == cnt + 1));
1543 verify_watcher (EV_A_ ws [cnt]);
1544 }
1523} 1545}
1546#endif
1524 1547
1525static void 1548void
1526ev_loop_verify (EV_P) 1549ev_loop_verify (EV_P)
1527{ 1550{
1551#if EV_VERIFY
1528 int i; 1552 int i;
1553 WL w;
1529 1554
1555 assert (activecnt >= -1);
1556
1557 assert (fdchangemax >= fdchangecnt);
1558 for (i = 0; i < fdchangecnt; ++i)
1559 assert (("negative fd in fdchanges", fdchanges [i] >= 0));
1560
1561 assert (anfdmax >= 0);
1562 for (i = 0; i < anfdmax; ++i)
1563 for (w = anfds [i].head; w; w = w->next)
1564 {
1565 verify_watcher (EV_A_ (W)w);
1566 assert (("inactive fd watcher on anfd list", ev_active (w) == 1));
1567 assert (("fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i));
1568 }
1569
1570 assert (timermax >= timercnt);
1530 checkheap (timers, timercnt); 1571 verify_heap (EV_A_ timers, timercnt);
1572
1531#if EV_PERIODIC_ENABLE 1573#if EV_PERIODIC_ENABLE
1574 assert (periodicmax >= periodiccnt);
1532 checkheap (periodics, periodiccnt); 1575 verify_heap (EV_A_ periodics, periodiccnt);
1533#endif 1576#endif
1534 1577
1578 for (i = NUMPRI; i--; )
1579 {
1580 assert (pendingmax [i] >= pendingcnt [i]);
1535#if EV_IDLE_ENABLE 1581#if EV_IDLE_ENABLE
1536 for (i = NUMPRI; i--; ) 1582 assert (idleall >= 0);
1583 assert (idlemax [i] >= idlecnt [i]);
1537 array_check ((W **)idles [i], idlecnt [i]); 1584 array_verify (EV_A_ (W *)idles [i], idlecnt [i]);
1538#endif 1585#endif
1586 }
1587
1539#if EV_FORK_ENABLE 1588#if EV_FORK_ENABLE
1589 assert (forkmax >= forkcnt);
1540 array_check ((W **)forks, forkcnt); 1590 array_verify (EV_A_ (W *)forks, forkcnt);
1541#endif 1591#endif
1542 array_check ((W **)prepares, preparecnt); 1592
1543 array_check ((W **)checks, checkcnt);
1544#if EV_ASYNC_ENABLE 1593#if EV_ASYNC_ENABLE
1594 assert (asyncmax >= asynccnt);
1545 array_check ((W **)asyncs, asynccnt); 1595 array_verify (EV_A_ (W *)asyncs, asynccnt);
1596#endif
1597
1598 assert (preparemax >= preparecnt);
1599 array_verify (EV_A_ (W *)prepares, preparecnt);
1600
1601 assert (checkmax >= checkcnt);
1602 array_verify (EV_A_ (W *)checks, checkcnt);
1603
1604# if 0
1605 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
1606 for (signum = signalmax; signum--; ) if (signals [signum].gotsig)
1546#endif 1607# endif
1547}
1548#endif 1608#endif
1609}
1549 1610
1550#endif 1611#endif /* multiplicity */
1551 1612
1552#if EV_MULTIPLICITY 1613#if EV_MULTIPLICITY
1553struct ev_loop * 1614struct ev_loop *
1554ev_default_loop_init (unsigned int flags) 1615ev_default_loop_init (unsigned int flags)
1555#else 1616#else
1620void inline_speed 1681void inline_speed
1621call_pending (EV_P) 1682call_pending (EV_P)
1622{ 1683{
1623 int pri; 1684 int pri;
1624 1685
1625 EV_FREQUENT_CHECK;
1626
1627 for (pri = NUMPRI; pri--; ) 1686 for (pri = NUMPRI; pri--; )
1628 while (pendingcnt [pri]) 1687 while (pendingcnt [pri])
1629 { 1688 {
1630 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1689 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1631 1690
1633 { 1692 {
1634 /*assert (("non-pending watcher on pending list", p->w->pending));*/ 1693 /*assert (("non-pending watcher on pending list", p->w->pending));*/
1635 1694
1636 p->w->pending = 0; 1695 p->w->pending = 0;
1637 EV_CB_INVOKE (p->w, p->events); 1696 EV_CB_INVOKE (p->w, p->events);
1697 EV_FREQUENT_CHECK;
1638 } 1698 }
1639 } 1699 }
1640
1641 EV_FREQUENT_CHECK;
1642} 1700}
1643 1701
1644#if EV_IDLE_ENABLE 1702#if EV_IDLE_ENABLE
1645void inline_size 1703void inline_size
1646idle_reify (EV_P) 1704idle_reify (EV_P)
1698#if EV_PERIODIC_ENABLE 1756#if EV_PERIODIC_ENABLE
1699void inline_size 1757void inline_size
1700periodics_reify (EV_P) 1758periodics_reify (EV_P)
1701{ 1759{
1702 EV_FREQUENT_CHECK; 1760 EV_FREQUENT_CHECK;
1761
1703 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) 1762 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
1704 { 1763 {
1705 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); 1764 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1706 1765
1707 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1766 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1713 1772
1714 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now)); 1773 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now));
1715 1774
1716 ANHE_at_cache (periodics [HEAP0]); 1775 ANHE_at_cache (periodics [HEAP0]);
1717 downheap (periodics, periodiccnt, HEAP0); 1776 downheap (periodics, periodiccnt, HEAP0);
1718 EV_FREQUENT_CHECK;
1719 } 1777 }
1720 else if (w->interval) 1778 else if (w->interval)
1721 { 1779 {
1722 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1780 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1723 /* if next trigger time is not sufficiently in the future, put it there */ 1781 /* if next trigger time is not sufficiently in the future, put it there */
1859 1917
1860 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 1918 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1861 1919
1862 do 1920 do
1863 { 1921 {
1922#if EV_VERIFY >= 2
1923 ev_loop_verify (EV_A);
1924#endif
1925
1864#ifndef _WIN32 1926#ifndef _WIN32
1865 if (expect_false (curpid)) /* penalise the forking check even more */ 1927 if (expect_false (curpid)) /* penalise the forking check even more */
1866 if (expect_false (getpid () != curpid)) 1928 if (expect_false (getpid () != curpid))
1867 { 1929 {
1868 curpid = getpid (); 1930 curpid = getpid ();
2506 } 2568 }
2507 2569
2508 } 2570 }
2509} 2571}
2510 2572
2573#endif
2574
2575#ifdef _WIN32
2576# define EV_LSTAT(p,b) _stati64 (p, b)
2577#else
2578# define EV_LSTAT(p,b) lstat (p, b)
2511#endif 2579#endif
2512 2580
2513void 2581void
2514ev_stat_stat (EV_P_ ev_stat *w) 2582ev_stat_stat (EV_P_ ev_stat *w)
2515{ 2583{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines