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

Comparing libev/ev.c (file contents):
Revision 1.362 by root, Sun Oct 24 19:15:52 2010 UTC vs.
Revision 1.371 by root, Mon Feb 7 21:45:32 2011 UTC

1/* 1/*
2 * libev event processing core, watcher management 2 * libev event processing core, watcher management
3 * 3 *
4 * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without modifica- 7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met: 8 * tion, are permitted provided that the following conditions are met:
9 * 9 *
376# undef EV_USE_INOTIFY 376# undef EV_USE_INOTIFY
377# define EV_USE_INOTIFY 0 377# define EV_USE_INOTIFY 0
378#endif 378#endif
379 379
380#if !EV_USE_NANOSLEEP 380#if !EV_USE_NANOSLEEP
381# ifndef _WIN32 381/* hp-ux has it in sys/time.h, which we unconditionally include above */
382# if !defined(_WIN32) && !defined(__hpux)
382# include <sys/select.h> 383# include <sys/select.h>
383# endif 384# endif
384#endif 385#endif
385 386
386#if EV_USE_INOTIFY 387#if EV_USE_INOTIFY
591 if (syserr_cb) 592 if (syserr_cb)
592 syserr_cb (msg); 593 syserr_cb (msg);
593 else 594 else
594 { 595 {
595#if EV_AVOID_STDIO 596#if EV_AVOID_STDIO
596 const char *err = strerror (errno);
597
598 ev_printerr (msg); 597 ev_printerr (msg);
599 ev_printerr (": "); 598 ev_printerr (": ");
600 ev_printerr (err); 599 ev_printerr (strerror (errno));
601 ev_printerr ("\n"); 600 ev_printerr ("\n");
602#else 601#else
603 perror (msg); 602 perror (msg);
604#endif 603#endif
605 abort (); 604 abort ();
639 ptr = alloc (ptr, size); 638 ptr = alloc (ptr, size);
640 639
641 if (!ptr && size) 640 if (!ptr && size)
642 { 641 {
643#if EV_AVOID_STDIO 642#if EV_AVOID_STDIO
644 ev_printerr ("libev: memory allocation failed, aborting.\n"); 643 ev_printerr ("(libev) memory allocation failed, aborting.\n");
645#else 644#else
646 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); 645 fprintf (stderr, "(libev) cannot allocate %ld bytes, aborting.", size);
647#endif 646#endif
648 abort (); 647 abort ();
649 } 648 }
650 649
651 return ptr; 650 return ptr;
817 select (0, 0, 0, 0, &tv); 816 select (0, 0, 0, 0, &tv);
818#endif 817#endif
819 } 818 }
820} 819}
821 820
821inline_speed int
822ev_timeout_to_ms (ev_tstamp timeout)
823{
824 int ms = timeout * 1000. + .999999;
825
826 return expect_true (ms) ? ms : timeout < 1e-6 ? 0 : 1;
827}
828
822/*****************************************************************************/ 829/*****************************************************************************/
823 830
824#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ 831#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
825 832
826/* find a suitable new size for the given array, */ 833/* find a suitable new size for the given array, */
967inline_size void 974inline_size void
968fd_reify (EV_P) 975fd_reify (EV_P)
969{ 976{
970 int i; 977 int i;
971 978
979#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
980 for (i = 0; i < fdchangecnt; ++i)
981 {
982 int fd = fdchanges [i];
983 ANFD *anfd = anfds + fd;
984
985 if (anfd->reify & EV__IOFDSET)
986 {
987 SOCKET handle = EV_FD_TO_WIN32_HANDLE (fd);
988
989 if (handle != anfd->handle)
990 {
991 unsigned long arg;
992
993 assert (("libev: only socket fds supported in this configuration", ioctlsocket (handle, FIONREAD, &arg) == 0));
994
995 /* handle changed, but fd didn't - we need to do it in two steps */
996 backend_modify (EV_A_ fd, anfd->events, 0);
997 anfd->events = 0;
998 anfd->handle = handle;
999 }
1000 }
1001 }
1002#endif
1003
972 for (i = 0; i < fdchangecnt; ++i) 1004 for (i = 0; i < fdchangecnt; ++i)
973 { 1005 {
974 int fd = fdchanges [i]; 1006 int fd = fdchanges [i];
975 ANFD *anfd = anfds + fd; 1007 ANFD *anfd = anfds + fd;
976 ev_io *w; 1008 ev_io *w;
977 1009
978 unsigned char o_events = anfd->events; 1010 unsigned char o_events = anfd->events;
979 unsigned char o_reify = anfd->reify; 1011 unsigned char o_reify = anfd->reify;
980 1012
981 anfd->reify = 0; 1013 anfd->reify = 0;
982
983#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
984 if (o_reify & EV__IOFDSET)
985 {
986 unsigned long arg;
987 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
988 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0));
989 printf ("oi %d %x\n", fd, anfd->handle);//D
990 }
991#endif
992 1014
993 /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */ 1015 /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */
994 { 1016 {
995 anfd->events = 0; 1017 anfd->events = 0;
996 1018
1345 char dummy; 1367 char dummy;
1346 /* see discussion in evpipe_write when you think this read should be recv in win32 */ 1368 /* see discussion in evpipe_write when you think this read should be recv in win32 */
1347 read (evpipe [0], &dummy, 1); 1369 read (evpipe [0], &dummy, 1);
1348 } 1370 }
1349 1371
1372#if EV_SIGNAL_ENABLE
1350 if (sig_pending) 1373 if (sig_pending)
1351 { 1374 {
1352 sig_pending = 0; 1375 sig_pending = 0;
1353 1376
1354 for (i = EV_NSIG - 1; i--; ) 1377 for (i = EV_NSIG - 1; i--; )
1355 if (expect_false (signals [i].pending)) 1378 if (expect_false (signals [i].pending))
1356 ev_feed_signal_event (EV_A_ i + 1); 1379 ev_feed_signal_event (EV_A_ i + 1);
1357 } 1380 }
1381#endif
1358 1382
1359#if EV_ASYNC_ENABLE 1383#if EV_ASYNC_ENABLE
1360 if (async_pending) 1384 if (async_pending)
1361 { 1385 {
1362 async_pending = 0; 1386 async_pending = 0;
1371#endif 1395#endif
1372} 1396}
1373 1397
1374/*****************************************************************************/ 1398/*****************************************************************************/
1375 1399
1400void
1401ev_feed_signal (int signum)
1402{
1403#if EV_MULTIPLICITY
1404 EV_P = signals [signum - 1].loop;
1405
1406 if (!EV_A)
1407 return;
1408#endif
1409
1410 signals [signum - 1].pending = 1;
1411 evpipe_write (EV_A_ &sig_pending);
1412}
1413
1376static void 1414static void
1377ev_sighandler (int signum) 1415ev_sighandler (int signum)
1378{ 1416{
1379#if EV_MULTIPLICITY
1380 EV_P = signals [signum - 1].loop;
1381#endif
1382
1383#ifdef _WIN32 1417#ifdef _WIN32
1384 signal (signum, ev_sighandler); 1418 signal (signum, ev_sighandler);
1385#endif 1419#endif
1386 1420
1387 signals [signum - 1].pending = 1; 1421 ev_feed_signal (signum);
1388 evpipe_write (EV_A_ &sig_pending);
1389} 1422}
1390 1423
1391void noinline 1424void noinline
1392ev_feed_signal_event (EV_P_ int signum) 1425ev_feed_signal_event (EV_P_ int signum)
1393{ 1426{
1645static void noinline 1678static void noinline
1646loop_init (EV_P_ unsigned int flags) 1679loop_init (EV_P_ unsigned int flags)
1647{ 1680{
1648 if (!backend) 1681 if (!backend)
1649 { 1682 {
1683 origflags = flags;
1684
1650#if EV_USE_REALTIME 1685#if EV_USE_REALTIME
1651 if (!have_realtime) 1686 if (!have_realtime)
1652 { 1687 {
1653 struct timespec ts; 1688 struct timespec ts;
1654 1689
1699#endif 1734#endif
1700#if EV_USE_SIGNALFD 1735#if EV_USE_SIGNALFD
1701 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; 1736 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
1702#endif 1737#endif
1703 1738
1704 if (!(flags & 0x0000ffffU)) 1739 if (!(flags & EVBACKEND_MASK))
1705 flags |= ev_recommended_backends (); 1740 flags |= ev_recommended_backends ();
1706 1741
1707#if EV_USE_IOCP 1742#if EV_USE_IOCP
1708 if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags); 1743 if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags);
1709#endif 1744#endif
1735/* free up a loop structure */ 1770/* free up a loop structure */
1736void 1771void
1737ev_loop_destroy (EV_P) 1772ev_loop_destroy (EV_P)
1738{ 1773{
1739 int i; 1774 int i;
1775
1776#if EV_MULTIPLICITY
1777 /* mimic free (0) */
1778 if (!EV_A)
1779 return;
1780#endif
1740 1781
1741#if EV_CLEANUP_ENABLE 1782#if EV_CLEANUP_ENABLE
1742 /* queue cleanup watchers (and execute them) */ 1783 /* queue cleanup watchers (and execute them) */
1743 if (expect_false (cleanupcnt)) 1784 if (expect_false (cleanupcnt))
1744 { 1785 {
2096 for (pri = NUMPRI; pri--; ) 2137 for (pri = NUMPRI; pri--; )
2097 while (pendingcnt [pri]) 2138 while (pendingcnt [pri])
2098 { 2139 {
2099 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 2140 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
2100 2141
2101 /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/
2102 /* ^ this is no longer true, as pending_w could be here */
2103
2104 p->w->pending = 0; 2142 p->w->pending = 0;
2105 EV_CB_INVOKE (p->w, p->events); 2143 EV_CB_INVOKE (p->w, p->events);
2106 EV_FREQUENT_CHECK; 2144 EV_FREQUENT_CHECK;
2107 } 2145 }
2108} 2146}
2169 feed_reverse_done (EV_A_ EV_TIMER); 2207 feed_reverse_done (EV_A_ EV_TIMER);
2170 } 2208 }
2171} 2209}
2172 2210
2173#if EV_PERIODIC_ENABLE 2211#if EV_PERIODIC_ENABLE
2212
2213inline_speed void
2214periodic_recalc (EV_P_ ev_periodic *w)
2215{
2216 /* TODO: use slow but potentially more correct incremental algo, */
2217 /* also do not rely on ceil */
2218 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
2219}
2220
2174/* make periodics pending */ 2221/* make periodics pending */
2175inline_size void 2222inline_size void
2176periodics_reify (EV_P) 2223periodics_reify (EV_P)
2177{ 2224{
2178 EV_FREQUENT_CHECK; 2225 EV_FREQUENT_CHECK;
2197 ANHE_at_cache (periodics [HEAP0]); 2244 ANHE_at_cache (periodics [HEAP0]);
2198 downheap (periodics, periodiccnt, HEAP0); 2245 downheap (periodics, periodiccnt, HEAP0);
2199 } 2246 }
2200 else if (w->interval) 2247 else if (w->interval)
2201 { 2248 {
2202 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 2249 periodic_recalc (EV_A_ w);
2250
2203 /* if next trigger time is not sufficiently in the future, put it there */ 2251 /* if next trigger time is not sufficiently in the future, put it there */
2204 /* this might happen because of floating point inexactness */ 2252 /* this might happen because of floating point inexactness */
2205 if (ev_at (w) - ev_rt_now < TIME_EPSILON) 2253 if (ev_at (w) - ev_rt_now < TIME_EPSILON)
2206 { 2254 {
2207 ev_at (w) += w->interval; 2255 ev_at (w) += w->interval;
2241 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]); 2289 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]);
2242 2290
2243 if (w->reschedule_cb) 2291 if (w->reschedule_cb)
2244 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 2292 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
2245 else if (w->interval) 2293 else if (w->interval)
2246 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval; 2294 periodic_recalc (EV_A_ w);
2247 2295
2248 ANHE_at_cache (periodics [i]); 2296 ANHE_at_cache (periodics [i]);
2249 } 2297 }
2250 2298
2251 reheap (periodics, periodiccnt); 2299 reheap (periodics, periodiccnt);
2743 if (w->reschedule_cb) 2791 if (w->reschedule_cb)
2744 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 2792 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
2745 else if (w->interval) 2793 else if (w->interval)
2746 { 2794 {
2747 assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.)); 2795 assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.));
2748 /* this formula differs from the one in periodic_reify because we do not always round up */ 2796 periodic_recalc (EV_A_ w);
2749 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
2750 } 2797 }
2751 else 2798 else
2752 ev_at (w) = w->offset; 2799 ev_at (w) = w->offset;
2753 2800
2754 EV_FREQUENT_CHECK; 2801 EV_FREQUENT_CHECK;
2875 sa.sa_handler = ev_sighandler; 2922 sa.sa_handler = ev_sighandler;
2876 sigfillset (&sa.sa_mask); 2923 sigfillset (&sa.sa_mask);
2877 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 2924 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
2878 sigaction (w->signum, &sa, 0); 2925 sigaction (w->signum, &sa, 0);
2879 2926
2927 if (origflags & EVFLAG_NOSIGMASK)
2928 {
2880 sigemptyset (&sa.sa_mask); 2929 sigemptyset (&sa.sa_mask);
2881 sigaddset (&sa.sa_mask, w->signum); 2930 sigaddset (&sa.sa_mask, w->signum);
2882 sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); 2931 sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0);
2932 }
2883#endif 2933#endif
2884 } 2934 }
2885 2935
2886 EV_FREQUENT_CHECK; 2936 EV_FREQUENT_CHECK;
2887} 2937}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines