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

Comparing libev/ev.c (file contents):
Revision 1.250 by root, Thu May 22 02:44:57 2008 UTC vs.
Revision 1.260 by root, Mon Sep 8 17:24:39 2008 UTC

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
556 struct timeval tv; 565 struct timeval tv;
557 566
558 tv.tv_sec = (time_t)delay; 567 tv.tv_sec = (time_t)delay;
559 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 568 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
560 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 */
561 select (0, 0, 0, 0, &tv); 573 select (0, 0, 0, 0, &tv);
562#endif 574#endif
563 } 575 }
564} 576}
565 577
699 events |= (unsigned char)w->events; 711 events |= (unsigned char)w->events;
700 712
701#if EV_SELECT_IS_WINSOCKET 713#if EV_SELECT_IS_WINSOCKET
702 if (events) 714 if (events)
703 { 715 {
704 unsigned long argp; 716 unsigned long arg;
705 #ifdef EV_FD_TO_WIN32_HANDLE 717 #ifdef EV_FD_TO_WIN32_HANDLE
706 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 718 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
707 #else 719 #else
708 anfd->handle = _get_osfhandle (fd); 720 anfd->handle = _get_osfhandle (fd);
709 #endif 721 #endif
710 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));
711 } 723 }
712#endif 724#endif
713 725
714 { 726 {
715 unsigned char o_events = anfd->events; 727 unsigned char o_events = anfd->events;
768{ 780{
769 int fd; 781 int fd;
770 782
771 for (fd = 0; fd < anfdmax; ++fd) 783 for (fd = 0; fd < anfdmax; ++fd)
772 if (anfds [fd].events) 784 if (anfds [fd].events)
773 if (!fd_valid (fd) == -1 && errno == EBADF) 785 if (!fd_valid (fd) && errno == EBADF)
774 fd_kill (EV_A_ fd); 786 fd_kill (EV_A_ fd);
775} 787}
776 788
777/* 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 */
778static void noinline 790static void noinline
937/* 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 */
938void inline_size 950void inline_size
939reheap (ANHE *heap, int N) 951reheap (ANHE *heap, int N)
940{ 952{
941 int i; 953 int i;
954
942 /* 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 */
943 /* 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 */
944 for (i = 0; i < N; ++i) 957 for (i = 0; i < N; ++i)
945 upheap (heap, i + HEAP0); 958 upheap (heap, i + HEAP0);
946} 959}
947 960
948#if EV_VERIFY
949static void
950checkheap (ANHE *heap, int N)
951{
952 int i;
953
954 for (i = HEAP0; i < N + HEAP0; ++i)
955 {
956 assert (("active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i));
957 assert (("heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i])));
958 assert (("heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i]))));
959 }
960}
961#endif
962
963/*****************************************************************************/ 961/*****************************************************************************/
964 962
965typedef struct 963typedef struct
966{ 964{
967 WL head; 965 WL head;
989 987
990void inline_speed 988void inline_speed
991fd_intern (int fd) 989fd_intern (int fd)
992{ 990{
993#ifdef _WIN32 991#ifdef _WIN32
994 int arg = 1; 992 unsigned long arg = 1;
995 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 993 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
996#else 994#else
997 fcntl (fd, F_SETFD, FD_CLOEXEC); 995 fcntl (fd, F_SETFD, FD_CLOEXEC);
998 fcntl (fd, F_SETFL, O_NONBLOCK); 996 fcntl (fd, F_SETFL, O_NONBLOCK);
999#endif 997#endif
1513{ 1511{
1514 postfork = 1; /* must be in line with ev_default_fork */ 1512 postfork = 1; /* must be in line with ev_default_fork */
1515} 1513}
1516 1514
1517#if EV_VERIFY 1515#if EV_VERIFY
1518static 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
1519array_check (W **ws, int cnt) 1541array_verify (EV_P_ W *ws, int cnt)
1520{ 1542{
1521 while (cnt--) 1543 while (cnt--)
1544 {
1522 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 }
1523} 1548}
1524#endif 1549#endif
1525 1550
1526void 1551void
1527ev_loop_verify (EV_P) 1552ev_loop_verify (EV_P)
1528{ 1553{
1529#if EV_VERIFY 1554#if EV_VERIFY
1530 int i; 1555 int i;
1556 WL w;
1531 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);
1532 checkheap (timers, timercnt); 1574 verify_heap (EV_A_ timers, timercnt);
1575
1533#if EV_PERIODIC_ENABLE 1576#if EV_PERIODIC_ENABLE
1577 assert (periodicmax >= periodiccnt);
1534 checkheap (periodics, periodiccnt); 1578 verify_heap (EV_A_ periodics, periodiccnt);
1535#endif 1579#endif
1536 1580
1581 for (i = NUMPRI; i--; )
1582 {
1583 assert (pendingmax [i] >= pendingcnt [i]);
1537#if EV_IDLE_ENABLE 1584#if EV_IDLE_ENABLE
1538 for (i = NUMPRI; i--; ) 1585 assert (idleall >= 0);
1586 assert (idlemax [i] >= idlecnt [i]);
1539 array_check ((W **)idles [i], idlecnt [i]); 1587 array_verify (EV_A_ (W *)idles [i], idlecnt [i]);
1540#endif 1588#endif
1589 }
1590
1541#if EV_FORK_ENABLE 1591#if EV_FORK_ENABLE
1592 assert (forkmax >= forkcnt);
1542 array_check ((W **)forks, forkcnt); 1593 array_verify (EV_A_ (W *)forks, forkcnt);
1543#endif 1594#endif
1595
1544#if EV_ASYNC_ENABLE 1596#if EV_ASYNC_ENABLE
1597 assert (asyncmax >= asynccnt);
1545 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)
1546#endif 1610# endif
1547 array_check ((W **)prepares, preparecnt);
1548 array_check ((W **)checks, checkcnt);
1549#endif 1611#endif
1550} 1612}
1551 1613
1552#endif /* multiplicity */ 1614#endif /* multiplicity */
1553 1615
1847ev_unref (EV_P) 1909ev_unref (EV_P)
1848{ 1910{
1849 --activecnt; 1911 --activecnt;
1850} 1912}
1851 1913
1914void
1915ev_now_update (EV_P)
1916{
1917 time_update (EV_A_ 1e100);
1918}
1919
1852static int loop_done; 1920static int loop_done;
1853 1921
1854void 1922void
1855ev_loop (EV_P_ int flags) 1923ev_loop (EV_P_ int flags)
1856{ 1924{
2509 } 2577 }
2510 2578
2511 } 2579 }
2512} 2580}
2513 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)
2514#endif 2588#endif
2515 2589
2516void 2590void
2517ev_stat_stat (EV_P_ ev_stat *w) 2591ev_stat_stat (EV_P_ ev_stat *w)
2518{ 2592{
2914once_cb (EV_P_ struct ev_once *once, int revents) 2988once_cb (EV_P_ struct ev_once *once, int revents)
2915{ 2989{
2916 void (*cb)(int revents, void *arg) = once->cb; 2990 void (*cb)(int revents, void *arg) = once->cb;
2917 void *arg = once->arg; 2991 void *arg = once->arg;
2918 2992
2919 ev_io_stop (EV_A_ &once->io); 2993 ev_io_stop (EV_A_ &once->io);
2920 ev_timer_stop (EV_A_ &once->to); 2994 ev_timer_stop (EV_A_ &once->to);
2921 ev_free (once); 2995 ev_free (once);
2922 2996
2923 cb (revents, arg); 2997 cb (revents, arg);
2924} 2998}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines