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.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
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
694 events |= (unsigned char)w->events; 708 events |= (unsigned char)w->events;
695 709
696#if EV_SELECT_IS_WINSOCKET 710#if EV_SELECT_IS_WINSOCKET
697 if (events) 711 if (events)
698 { 712 {
699 unsigned long argp; 713 unsigned long arg;
700 #ifdef EV_FD_TO_WIN32_HANDLE 714 #ifdef EV_FD_TO_WIN32_HANDLE
701 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 715 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
702 #else 716 #else
703 anfd->handle = _get_osfhandle (fd); 717 anfd->handle = _get_osfhandle (fd);
704 #endif 718 #endif
705 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));
706 } 720 }
707#endif 721#endif
708 722
709 { 723 {
710 unsigned char o_events = anfd->events; 724 unsigned char o_events = anfd->events;
763{ 777{
764 int fd; 778 int fd;
765 779
766 for (fd = 0; fd < anfdmax; ++fd) 780 for (fd = 0; fd < anfdmax; ++fd)
767 if (anfds [fd].events) 781 if (anfds [fd].events)
768 if (!fd_valid (fd) == -1 && errno == EBADF) 782 if (!fd_valid (fd) && errno == EBADF)
769 fd_kill (EV_A_ fd); 783 fd_kill (EV_A_ fd);
770} 784}
771 785
772/* 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 */
773static void noinline 787static void noinline
932/* 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 */
933void inline_size 947void inline_size
934reheap (ANHE *heap, int N) 948reheap (ANHE *heap, int N)
935{ 949{
936 int i; 950 int i;
951
937 /* 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 */
938 /* 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 */
939 for (i = 0; i < N; ++i) 954 for (i = 0; i < N; ++i)
940 upheap (heap, i + HEAP0); 955 upheap (heap, i + HEAP0);
941} 956}
942 957
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/*****************************************************************************/ 958/*****************************************************************************/
959 959
960typedef struct 960typedef struct
961{ 961{
962 WL head; 962 WL head;
984 984
985void inline_speed 985void inline_speed
986fd_intern (int fd) 986fd_intern (int fd)
987{ 987{
988#ifdef _WIN32 988#ifdef _WIN32
989 int arg = 1; 989 unsigned long arg = 1;
990 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 990 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
991#else 991#else
992 fcntl (fd, F_SETFD, FD_CLOEXEC); 992 fcntl (fd, F_SETFD, FD_CLOEXEC);
993 fcntl (fd, F_SETFL, O_NONBLOCK); 993 fcntl (fd, F_SETFL, O_NONBLOCK);
994#endif 994#endif
1478 1478
1479 postfork = 0; 1479 postfork = 0;
1480} 1480}
1481 1481
1482#if EV_MULTIPLICITY 1482#if EV_MULTIPLICITY
1483
1483struct ev_loop * 1484struct ev_loop *
1484ev_loop_new (unsigned int flags) 1485ev_loop_new (unsigned int flags)
1485{ 1486{
1486 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));
1487 1488
1507{ 1508{
1508 postfork = 1; /* must be in line with ev_default_fork */ 1509 postfork = 1; /* must be in line with ev_default_fork */
1509} 1510}
1510 1511
1511#if EV_VERIFY 1512#if EV_VERIFY
1512static 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
1513array_check (W **ws, int cnt) 1538array_verify (EV_P_ W *ws, int cnt)
1514{ 1539{
1515 while (cnt--) 1540 while (cnt--)
1541 {
1516 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 }
1517} 1545}
1546#endif
1518 1547
1519static void 1548void
1520ev_loop_verify (EV_P) 1549ev_loop_verify (EV_P)
1521{ 1550{
1551#if EV_VERIFY
1522 int i; 1552 int i;
1553 WL w;
1523 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);
1524 checkheap (timers, timercnt); 1571 verify_heap (EV_A_ timers, timercnt);
1572
1525#if EV_PERIODIC_ENABLE 1573#if EV_PERIODIC_ENABLE
1574 assert (periodicmax >= periodiccnt);
1526 checkheap (periodics, periodiccnt); 1575 verify_heap (EV_A_ periodics, periodiccnt);
1527#endif 1576#endif
1528 1577
1578 for (i = NUMPRI; i--; )
1579 {
1580 assert (pendingmax [i] >= pendingcnt [i]);
1529#if EV_IDLE_ENABLE 1581#if EV_IDLE_ENABLE
1530 for (i = NUMPRI; i--; ) 1582 assert (idleall >= 0);
1583 assert (idlemax [i] >= idlecnt [i]);
1531 array_check ((W **)idles [i], idlecnt [i]); 1584 array_verify (EV_A_ (W *)idles [i], idlecnt [i]);
1532#endif 1585#endif
1586 }
1587
1533#if EV_FORK_ENABLE 1588#if EV_FORK_ENABLE
1589 assert (forkmax >= forkcnt);
1534 array_check ((W **)forks, forkcnt); 1590 array_verify (EV_A_ (W *)forks, forkcnt);
1535#endif 1591#endif
1536 array_check ((W **)prepares, preparecnt); 1592
1537 array_check ((W **)checks, checkcnt);
1538#if EV_ASYNC_ENABLE 1593#if EV_ASYNC_ENABLE
1594 assert (asyncmax >= asynccnt);
1539 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)
1540#endif 1607# endif
1541}
1542#endif 1608#endif
1609}
1543 1610
1544#endif 1611#endif /* multiplicity */
1545 1612
1546#if EV_MULTIPLICITY 1613#if EV_MULTIPLICITY
1547struct ev_loop * 1614struct ev_loop *
1548ev_default_loop_init (unsigned int flags) 1615ev_default_loop_init (unsigned int flags)
1549#else 1616#else
1614void inline_speed 1681void inline_speed
1615call_pending (EV_P) 1682call_pending (EV_P)
1616{ 1683{
1617 int pri; 1684 int pri;
1618 1685
1619 EV_FREQUENT_CHECK;
1620
1621 for (pri = NUMPRI; pri--; ) 1686 for (pri = NUMPRI; pri--; )
1622 while (pendingcnt [pri]) 1687 while (pendingcnt [pri])
1623 { 1688 {
1624 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1689 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1625 1690
1627 { 1692 {
1628 /*assert (("non-pending watcher on pending list", p->w->pending));*/ 1693 /*assert (("non-pending watcher on pending list", p->w->pending));*/
1629 1694
1630 p->w->pending = 0; 1695 p->w->pending = 0;
1631 EV_CB_INVOKE (p->w, p->events); 1696 EV_CB_INVOKE (p->w, p->events);
1697 EV_FREQUENT_CHECK;
1632 } 1698 }
1633 } 1699 }
1634
1635 EV_FREQUENT_CHECK;
1636} 1700}
1637 1701
1638#if EV_IDLE_ENABLE 1702#if EV_IDLE_ENABLE
1639void inline_size 1703void inline_size
1640idle_reify (EV_P) 1704idle_reify (EV_P)
1692#if EV_PERIODIC_ENABLE 1756#if EV_PERIODIC_ENABLE
1693void inline_size 1757void inline_size
1694periodics_reify (EV_P) 1758periodics_reify (EV_P)
1695{ 1759{
1696 EV_FREQUENT_CHECK; 1760 EV_FREQUENT_CHECK;
1761
1697 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) 1762 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
1698 { 1763 {
1699 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); 1764 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1700 1765
1701 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/ 1766 /*assert (("inactive timer on periodic heap detected", ev_is_active (w)));*/
1707 1772
1708 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));
1709 1774
1710 ANHE_at_cache (periodics [HEAP0]); 1775 ANHE_at_cache (periodics [HEAP0]);
1711 downheap (periodics, periodiccnt, HEAP0); 1776 downheap (periodics, periodiccnt, HEAP0);
1712 EV_FREQUENT_CHECK;
1713 } 1777 }
1714 else if (w->interval) 1778 else if (w->interval)
1715 { 1779 {
1716 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;
1717 /* 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 */
1853 1917
1854 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 */
1855 1919
1856 do 1920 do
1857 { 1921 {
1922#if EV_VERIFY >= 2
1923 ev_loop_verify (EV_A);
1924#endif
1925
1858#ifndef _WIN32 1926#ifndef _WIN32
1859 if (expect_false (curpid)) /* penalise the forking check even more */ 1927 if (expect_false (curpid)) /* penalise the forking check even more */
1860 if (expect_false (getpid () != curpid)) 1928 if (expect_false (getpid () != curpid))
1861 { 1929 {
1862 curpid = getpid (); 1930 curpid = getpid ();
2500 } 2568 }
2501 2569
2502 } 2570 }
2503} 2571}
2504 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)
2505#endif 2579#endif
2506 2580
2507void 2581void
2508ev_stat_stat (EV_P_ ev_stat *w) 2582ev_stat_stat (EV_P_ ev_stat *w)
2509{ 2583{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines