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.255 by root, Mon Jun 9 14:11:30 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>
164#endif 164#endif
165 165
166/* this block tries to deduce configuration from header-defined symbols and defaults */ 166/* this block tries to deduce configuration from header-defined symbols and defaults */
167 167
168#ifndef EV_USE_MONOTONIC 168#ifndef EV_USE_MONOTONIC
169# if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
170# define EV_USE_MONOTONIC 1
171# else
169# define EV_USE_MONOTONIC 0 172# define EV_USE_MONOTONIC 0
173# endif
170#endif 174#endif
171 175
172#ifndef EV_USE_REALTIME 176#ifndef EV_USE_REALTIME
173# define EV_USE_REALTIME 0 177# define EV_USE_REALTIME 0
174#endif 178#endif
175 179
176#ifndef EV_USE_NANOSLEEP 180#ifndef EV_USE_NANOSLEEP
181# if _POSIX_C_SOURCE >= 199309L
182# define EV_USE_NANOSLEEP 1
183# else
177# define EV_USE_NANOSLEEP 0 184# define EV_USE_NANOSLEEP 0
185# endif
178#endif 186#endif
179 187
180#ifndef EV_USE_SELECT 188#ifndef EV_USE_SELECT
181# define EV_USE_SELECT 1 189# define EV_USE_SELECT 1
182#endif 190#endif
233# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) 241# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
234# define EV_USE_EVENTFD 1 242# define EV_USE_EVENTFD 1
235# else 243# else
236# define EV_USE_EVENTFD 0 244# define EV_USE_EVENTFD 0
237# endif 245# endif
246#endif
247
248#if 0 /* debugging */
249# define EV_VERIFY 3
250# define EV_USE_4HEAP 1
251# define EV_HEAP_CACHE_AT 1
252#endif
253
254#ifndef EV_VERIFY
255# define EV_VERIFY !EV_MINIMAL
238#endif 256#endif
239 257
240#ifndef EV_USE_4HEAP 258#ifndef EV_USE_4HEAP
241# define EV_USE_4HEAP !EV_MINIMAL 259# define EV_USE_4HEAP !EV_MINIMAL
242#endif 260#endif
288# endif 306# endif
289#endif 307#endif
290 308
291/**/ 309/**/
292 310
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 311#if EV_VERIFY >= 3
299# define EV_FREQUENT_CHECK ev_loop_verify (EV_A) 312# define EV_FREQUENT_CHECK ev_loop_verify (EV_A)
300#else 313#else
301# define EV_FREQUENT_CHECK do { } while (0) 314# define EV_FREQUENT_CHECK do { } while (0)
302#endif 315#endif
303 316
694 events |= (unsigned char)w->events; 707 events |= (unsigned char)w->events;
695 708
696#if EV_SELECT_IS_WINSOCKET 709#if EV_SELECT_IS_WINSOCKET
697 if (events) 710 if (events)
698 { 711 {
699 unsigned long argp; 712 unsigned long arg;
700 #ifdef EV_FD_TO_WIN32_HANDLE 713 #ifdef EV_FD_TO_WIN32_HANDLE
701 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 714 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
702 #else 715 #else
703 anfd->handle = _get_osfhandle (fd); 716 anfd->handle = _get_osfhandle (fd);
704 #endif 717 #endif
705 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); 718 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0));
706 } 719 }
707#endif 720#endif
708 721
709 { 722 {
710 unsigned char o_events = anfd->events; 723 unsigned char o_events = anfd->events;
763{ 776{
764 int fd; 777 int fd;
765 778
766 for (fd = 0; fd < anfdmax; ++fd) 779 for (fd = 0; fd < anfdmax; ++fd)
767 if (anfds [fd].events) 780 if (anfds [fd].events)
768 if (!fd_valid (fd) == -1 && errno == EBADF) 781 if (!fd_valid (fd) && errno == EBADF)
769 fd_kill (EV_A_ fd); 782 fd_kill (EV_A_ fd);
770} 783}
771 784
772/* called on ENOMEM in select/poll to kill some fds and retry */ 785/* called on ENOMEM in select/poll to kill some fds and retry */
773static void noinline 786static void noinline
932/* rebuild the heap: this function is used only once and executed rarely */ 945/* rebuild the heap: this function is used only once and executed rarely */
933void inline_size 946void inline_size
934reheap (ANHE *heap, int N) 947reheap (ANHE *heap, int N)
935{ 948{
936 int i; 949 int i;
950
937 /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */ 951 /* 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 */ 952 /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */
939 for (i = 0; i < N; ++i) 953 for (i = 0; i < N; ++i)
940 upheap (heap, i + HEAP0); 954 upheap (heap, i + HEAP0);
941} 955}
942 956
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/*****************************************************************************/ 957/*****************************************************************************/
959 958
960typedef struct 959typedef struct
961{ 960{
962 WL head; 961 WL head;
984 983
985void inline_speed 984void inline_speed
986fd_intern (int fd) 985fd_intern (int fd)
987{ 986{
988#ifdef _WIN32 987#ifdef _WIN32
989 int arg = 1; 988 unsigned long arg = 1;
990 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 989 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
991#else 990#else
992 fcntl (fd, F_SETFD, FD_CLOEXEC); 991 fcntl (fd, F_SETFD, FD_CLOEXEC);
993 fcntl (fd, F_SETFL, O_NONBLOCK); 992 fcntl (fd, F_SETFL, O_NONBLOCK);
994#endif 993#endif
1478 1477
1479 postfork = 0; 1478 postfork = 0;
1480} 1479}
1481 1480
1482#if EV_MULTIPLICITY 1481#if EV_MULTIPLICITY
1482
1483struct ev_loop * 1483struct ev_loop *
1484ev_loop_new (unsigned int flags) 1484ev_loop_new (unsigned int flags)
1485{ 1485{
1486 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); 1486 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
1487 1487
1507{ 1507{
1508 postfork = 1; /* must be in line with ev_default_fork */ 1508 postfork = 1; /* must be in line with ev_default_fork */
1509} 1509}
1510 1510
1511#if EV_VERIFY 1511#if EV_VERIFY
1512static void 1512void noinline
1513verify_watcher (EV_P_ W w)
1514{
1515 assert (("watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI));
1516
1517 if (w->pending)
1518 assert (("pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w));
1519}
1520
1521static void noinline
1522verify_heap (EV_P_ ANHE *heap, int N)
1523{
1524 int i;
1525
1526 for (i = HEAP0; i < N + HEAP0; ++i)
1527 {
1528 assert (("active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i));
1529 assert (("heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i])));
1530 assert (("heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i]))));
1531
1532 verify_watcher (EV_A_ (W)ANHE_w (heap [i]));
1533 }
1534}
1535
1536static void noinline
1513array_check (W **ws, int cnt) 1537array_verify (EV_P_ W *ws, int cnt)
1514{ 1538{
1515 while (cnt--) 1539 while (cnt--)
1540 {
1516 assert (("active index mismatch", ev_active (ws [cnt]) == cnt + 1)); 1541 assert (("active index mismatch", ev_active (ws [cnt]) == cnt + 1));
1542 verify_watcher (EV_A_ ws [cnt]);
1543 }
1517} 1544}
1545#endif
1518 1546
1519static void 1547void
1520ev_loop_verify (EV_P) 1548ev_loop_verify (EV_P)
1521{ 1549{
1550#if EV_VERIFY
1522 int i; 1551 int i;
1552 WL w;
1523 1553
1554 assert (activecnt >= -1);
1555
1556 assert (fdchangemax >= fdchangecnt);
1557 for (i = 0; i < fdchangecnt; ++i)
1558 assert (("negative fd in fdchanges", fdchanges [i] >= 0));
1559
1560 assert (anfdmax >= 0);
1561 for (i = 0; i < anfdmax; ++i)
1562 for (w = anfds [i].head; w; w = w->next)
1563 {
1564 verify_watcher (EV_A_ (W)w);
1565 assert (("inactive fd watcher on anfd list", ev_active (w) == 1));
1566 assert (("fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i));
1567 }
1568
1569 assert (timermax >= timercnt);
1524 checkheap (timers, timercnt); 1570 verify_heap (EV_A_ timers, timercnt);
1571
1525#if EV_PERIODIC_ENABLE 1572#if EV_PERIODIC_ENABLE
1573 assert (periodicmax >= periodiccnt);
1526 checkheap (periodics, periodiccnt); 1574 verify_heap (EV_A_ periodics, periodiccnt);
1527#endif 1575#endif
1528 1576
1577 for (i = NUMPRI; i--; )
1578 {
1579 assert (pendingmax [i] >= pendingcnt [i]);
1529#if EV_IDLE_ENABLE 1580#if EV_IDLE_ENABLE
1530 for (i = NUMPRI; i--; ) 1581 assert (idleall >= 0);
1582 assert (idlemax [i] >= idlecnt [i]);
1531 array_check ((W **)idles [i], idlecnt [i]); 1583 array_verify (EV_A_ (W *)idles [i], idlecnt [i]);
1532#endif 1584#endif
1585 }
1586
1533#if EV_FORK_ENABLE 1587#if EV_FORK_ENABLE
1588 assert (forkmax >= forkcnt);
1534 array_check ((W **)forks, forkcnt); 1589 array_verify (EV_A_ (W *)forks, forkcnt);
1535#endif 1590#endif
1536 array_check ((W **)prepares, preparecnt); 1591
1537 array_check ((W **)checks, checkcnt);
1538#if EV_ASYNC_ENABLE 1592#if EV_ASYNC_ENABLE
1593 assert (asyncmax >= asynccnt);
1539 array_check ((W **)asyncs, asynccnt); 1594 array_verify (EV_A_ (W *)asyncs, asynccnt);
1595#endif
1596
1597 assert (preparemax >= preparecnt);
1598 array_verify (EV_A_ (W *)prepares, preparecnt);
1599
1600 assert (checkmax >= checkcnt);
1601 array_verify (EV_A_ (W *)checks, checkcnt);
1602
1603# if 0
1604 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
1605 for (signum = signalmax; signum--; ) if (signals [signum].gotsig)
1540#endif 1606# endif
1541}
1542#endif 1607#endif
1608}
1543 1609
1544#endif 1610#endif /* multiplicity */
1545 1611
1546#if EV_MULTIPLICITY 1612#if EV_MULTIPLICITY
1547struct ev_loop * 1613struct ev_loop *
1548ev_default_loop_init (unsigned int flags) 1614ev_default_loop_init (unsigned int flags)
1549#else 1615#else
1614void inline_speed 1680void inline_speed
1615call_pending (EV_P) 1681call_pending (EV_P)
1616{ 1682{
1617 int pri; 1683 int pri;
1618 1684
1619 EV_FREQUENT_CHECK;
1620
1621 for (pri = NUMPRI; pri--; ) 1685 for (pri = NUMPRI; pri--; )
1622 while (pendingcnt [pri]) 1686 while (pendingcnt [pri])
1623 { 1687 {
1624 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1688 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1625 1689
1627 { 1691 {
1628 /*assert (("non-pending watcher on pending list", p->w->pending));*/ 1692 /*assert (("non-pending watcher on pending list", p->w->pending));*/
1629 1693
1630 p->w->pending = 0; 1694 p->w->pending = 0;
1631 EV_CB_INVOKE (p->w, p->events); 1695 EV_CB_INVOKE (p->w, p->events);
1696 EV_FREQUENT_CHECK;
1632 } 1697 }
1633 } 1698 }
1634
1635 EV_FREQUENT_CHECK;
1636} 1699}
1637 1700
1638#if EV_IDLE_ENABLE 1701#if EV_IDLE_ENABLE
1639void inline_size 1702void inline_size
1640idle_reify (EV_P) 1703idle_reify (EV_P)
1692#if EV_PERIODIC_ENABLE 1755#if EV_PERIODIC_ENABLE
1693void inline_size 1756void inline_size
1694periodics_reify (EV_P) 1757periodics_reify (EV_P)
1695{ 1758{
1696 EV_FREQUENT_CHECK; 1759 EV_FREQUENT_CHECK;
1760
1697 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) 1761 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
1698 { 1762 {
1699 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); 1763 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1700 1764
1701 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1765 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1707 1771
1708 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now)); 1772 assert (("ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now));
1709 1773
1710 ANHE_at_cache (periodics [HEAP0]); 1774 ANHE_at_cache (periodics [HEAP0]);
1711 downheap (periodics, periodiccnt, HEAP0); 1775 downheap (periodics, periodiccnt, HEAP0);
1712 EV_FREQUENT_CHECK;
1713 } 1776 }
1714 else if (w->interval) 1777 else if (w->interval)
1715 { 1778 {
1716 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 1779 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 */ 1780 /* if next trigger time is not sufficiently in the future, put it there */
1853 1916
1854 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 1917 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1855 1918
1856 do 1919 do
1857 { 1920 {
1921#if EV_VERIFY >= 2
1922 ev_loop_verify (EV_A);
1923#endif
1924
1858#ifndef _WIN32 1925#ifndef _WIN32
1859 if (expect_false (curpid)) /* penalise the forking check even more */ 1926 if (expect_false (curpid)) /* penalise the forking check even more */
1860 if (expect_false (getpid () != curpid)) 1927 if (expect_false (getpid () != curpid))
1861 { 1928 {
1862 curpid = getpid (); 1929 curpid = getpid ();
2500 } 2567 }
2501 2568
2502 } 2569 }
2503} 2570}
2504 2571
2572#endif
2573
2574#ifdef _WIN32
2575# define EV_LSTAT(p,b) _stati64 (p, b)
2576#else
2577# define EV_LSTAT(p,b) lstat (p, b)
2505#endif 2578#endif
2506 2579
2507void 2580void
2508ev_stat_stat (EV_P_ ev_stat *w) 2581ev_stat_stat (EV_P_ ev_stat *w)
2509{ 2582{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines