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

Comparing libev/ev.c (file contents):
Revision 1.287 by root, Mon Apr 20 19:45:58 2009 UTC vs.
Revision 1.295 by root, Wed Jul 8 04:29:31 2009 UTC

57# endif 57# endif
58# ifndef EV_USE_MONOTONIC 58# ifndef EV_USE_MONOTONIC
59# define EV_USE_MONOTONIC 1 59# define EV_USE_MONOTONIC 1
60# endif 60# endif
61# endif 61# endif
62# elif !defined(EV_USE_CLOCK_SYSCALL)
63# define EV_USE_CLOCK_SYSCALL 0
62# endif 64# endif
63 65
64# if HAVE_CLOCK_GETTIME 66# if HAVE_CLOCK_GETTIME
65# ifndef EV_USE_MONOTONIC 67# ifndef EV_USE_MONOTONIC
66# define EV_USE_MONOTONIC 1 68# define EV_USE_MONOTONIC 1
282 284
283#ifndef EV_HEAP_CACHE_AT 285#ifndef EV_HEAP_CACHE_AT
284# define EV_HEAP_CACHE_AT !EV_MINIMAL 286# define EV_HEAP_CACHE_AT !EV_MINIMAL
285#endif 287#endif
286 288
289/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
290/* which makes programs even slower. might work on other unices, too. */
291#if EV_USE_CLOCK_SYSCALL
292# include <syscall.h>
293# ifdef SYS_clock_gettime
294# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
295# undef EV_USE_MONOTONIC
296# define EV_USE_MONOTONIC 1
297# else
298# undef EV_USE_CLOCK_SYSCALL
299# define EV_USE_CLOCK_SYSCALL 0
300# endif
301#endif
302
287/* this block fixes any misconfiguration where we know we run into trouble otherwise */ 303/* this block fixes any misconfiguration where we know we run into trouble otherwise */
288 304
289#ifndef CLOCK_MONOTONIC 305#ifndef CLOCK_MONOTONIC
290# undef EV_USE_MONOTONIC 306# undef EV_USE_MONOTONIC
291# define EV_USE_MONOTONIC 0 307# define EV_USE_MONOTONIC 0
320 336
321#if EV_SELECT_IS_WINSOCKET 337#if EV_SELECT_IS_WINSOCKET
322# include <winsock.h> 338# include <winsock.h>
323#endif 339#endif
324 340
325/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
326/* which makes programs even slower. might work on other unices, too. */
327#if EV_USE_CLOCK_SYSCALL
328# include <syscall.h>
329# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
330# undef EV_USE_MONOTONIC
331# define EV_USE_MONOTONIC 1
332#endif
333
334#if EV_USE_EVENTFD 341#if EV_USE_EVENTFD
335/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 342/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
336# include <stdint.h> 343# include <stdint.h>
337# ifdef __cplusplus 344# ifdef __cplusplus
338extern "C" { 345extern "C" {
384# define inline_speed static noinline 391# define inline_speed static noinline
385#else 392#else
386# define inline_speed static inline 393# define inline_speed static inline
387#endif 394#endif
388 395
389#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 396#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
397
398#if EV_MINPRI == EV_MAXPRI
399# define ABSPRI(w) (((W)w), 0)
400#else
390#define ABSPRI(w) (((W)w)->priority - EV_MINPRI) 401# define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
402#endif
391 403
392#define EMPTY /* required for microsofts broken pseudo-c compiler */ 404#define EMPTY /* required for microsofts broken pseudo-c compiler */
393#define EMPTY2(a,b) /* used to suppress some warnings */ 405#define EMPTY2(a,b) /* used to suppress some warnings */
394 406
395typedef ev_watcher *W; 407typedef ev_watcher *W;
478#define ev_malloc(size) ev_realloc (0, (size)) 490#define ev_malloc(size) ev_realloc (0, (size))
479#define ev_free(ptr) ev_realloc ((ptr), 0) 491#define ev_free(ptr) ev_realloc ((ptr), 0)
480 492
481/*****************************************************************************/ 493/*****************************************************************************/
482 494
495/* file descriptor info structure */
483typedef struct 496typedef struct
484{ 497{
485 WL head; 498 WL head;
486 unsigned char events; 499 unsigned char events; /* the events watched for */
487 unsigned char reify; 500 unsigned char reify; /* flag set when this ANFD needs reification */
488 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ 501 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */
489 unsigned char unused; 502 unsigned char unused;
490#if EV_USE_EPOLL 503#if EV_USE_EPOLL
491 unsigned int egen; /* generation counter to counter epoll bugs */ 504 unsigned int egen; /* generation counter to counter epoll bugs */
492#endif 505#endif
493#if EV_SELECT_IS_WINSOCKET 506#if EV_SELECT_IS_WINSOCKET
494 SOCKET handle; 507 SOCKET handle;
495#endif 508#endif
496} ANFD; 509} ANFD;
497 510
511/* stores the pending event set for a given watcher */
498typedef struct 512typedef struct
499{ 513{
500 W w; 514 W w;
501 int events; 515 int events; /* the pending event set for the given watcher */
502} ANPENDING; 516} ANPENDING;
503 517
504#if EV_USE_INOTIFY 518#if EV_USE_INOTIFY
505/* hash table entry per inotify-id */ 519/* hash table entry per inotify-id */
506typedef struct 520typedef struct
509} ANFS; 523} ANFS;
510#endif 524#endif
511 525
512/* Heap Entry */ 526/* Heap Entry */
513#if EV_HEAP_CACHE_AT 527#if EV_HEAP_CACHE_AT
528 /* a heap element */
514 typedef struct { 529 typedef struct {
515 ev_tstamp at; 530 ev_tstamp at;
516 WT w; 531 WT w;
517 } ANHE; 532 } ANHE;
518 533
519 #define ANHE_w(he) (he).w /* access watcher, read-write */ 534 #define ANHE_w(he) (he).w /* access watcher, read-write */
520 #define ANHE_at(he) (he).at /* access cached at, read-only */ 535 #define ANHE_at(he) (he).at /* access cached at, read-only */
521 #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */ 536 #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */
522#else 537#else
538 /* a heap element */
523 typedef WT ANHE; 539 typedef WT ANHE;
524 540
525 #define ANHE_w(he) (he) 541 #define ANHE_w(he) (he)
526 #define ANHE_at(he) (he)->at 542 #define ANHE_at(he) (he)->at
527 #define ANHE_at_cache(he) 543 #define ANHE_at_cache(he)
553 569
554#endif 570#endif
555 571
556/*****************************************************************************/ 572/*****************************************************************************/
557 573
574#ifndef EV_HAVE_EV_TIME
558ev_tstamp 575ev_tstamp
559ev_time (void) 576ev_time (void)
560{ 577{
561#if EV_USE_REALTIME 578#if EV_USE_REALTIME
562 if (expect_true (have_realtime)) 579 if (expect_true (have_realtime))
569 586
570 struct timeval tv; 587 struct timeval tv;
571 gettimeofday (&tv, 0); 588 gettimeofday (&tv, 0);
572 return tv.tv_sec + tv.tv_usec * 1e-6; 589 return tv.tv_sec + tv.tv_usec * 1e-6;
573} 590}
591#endif
574 592
575inline_size ev_tstamp 593inline_size ev_tstamp
576get_clock (void) 594get_clock (void)
577{ 595{
578#if EV_USE_MONOTONIC 596#if EV_USE_MONOTONIC
614 632
615 tv.tv_sec = (time_t)delay; 633 tv.tv_sec = (time_t)delay;
616 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 634 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
617 635
618 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 636 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
619 /* somehting nto guaranteed by newer posix versions, but guaranteed */ 637 /* somehting not guaranteed by newer posix versions, but guaranteed */
620 /* by older ones */ 638 /* by older ones */
621 select (0, 0, 0, 0, &tv); 639 select (0, 0, 0, 0, &tv);
622#endif 640#endif
623 } 641 }
624} 642}
625 643
626/*****************************************************************************/ 644/*****************************************************************************/
627 645
628#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ 646#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
629 647
648/* find a suitable new size for the given array, */
649/* hopefully by rounding to a ncie-to-malloc size */
630inline_size int 650inline_size int
631array_nextsize (int elem, int cur, int cnt) 651array_nextsize (int elem, int cur, int cnt)
632{ 652{
633 int ncur = cur + 1; 653 int ncur = cur + 1;
634 654
680#define array_free(stem, idx) \ 700#define array_free(stem, idx) \
681 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0 701 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0
682 702
683/*****************************************************************************/ 703/*****************************************************************************/
684 704
705/* dummy callback for pending events */
706static void noinline
707pendingcb (EV_P_ ev_prepare *w, int revents)
708{
709}
710
685void noinline 711void noinline
686ev_feed_event (EV_P_ void *w, int revents) 712ev_feed_event (EV_P_ void *w, int revents)
687{ 713{
688 W w_ = (W)w; 714 W w_ = (W)w;
689 int pri = ABSPRI (w_); 715 int pri = ABSPRI (w_);
745{ 771{
746 if (fd >= 0 && fd < anfdmax) 772 if (fd >= 0 && fd < anfdmax)
747 fd_event (EV_A_ fd, revents); 773 fd_event (EV_A_ fd, revents);
748} 774}
749 775
776/* make sure the external fd watch events are in-sync */
777/* with the kernel/libev internal state */
750inline_size void 778inline_size void
751fd_reify (EV_P) 779fd_reify (EV_P)
752{ 780{
753 int i; 781 int i;
754 782
789 } 817 }
790 818
791 fdchangecnt = 0; 819 fdchangecnt = 0;
792} 820}
793 821
822/* something about the given fd changed */
794inline_size void 823inline_size void
795fd_change (EV_P_ int fd, int flags) 824fd_change (EV_P_ int fd, int flags)
796{ 825{
797 unsigned char reify = anfds [fd].reify; 826 unsigned char reify = anfds [fd].reify;
798 anfds [fd].reify |= flags; 827 anfds [fd].reify |= flags;
803 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 832 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
804 fdchanges [fdchangecnt - 1] = fd; 833 fdchanges [fdchangecnt - 1] = fd;
805 } 834 }
806} 835}
807 836
837/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
808inline_speed void 838inline_speed void
809fd_kill (EV_P_ int fd) 839fd_kill (EV_P_ int fd)
810{ 840{
811 ev_io *w; 841 ev_io *w;
812 842
815 ev_io_stop (EV_A_ w); 845 ev_io_stop (EV_A_ w);
816 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 846 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
817 } 847 }
818} 848}
819 849
850/* check whether the given fd is atcually valid, for error recovery */
820inline_size int 851inline_size int
821fd_valid (int fd) 852fd_valid (int fd)
822{ 853{
823#ifdef _WIN32 854#ifdef _WIN32
824 return _get_osfhandle (fd) != -1; 855 return _get_osfhandle (fd) != -1;
989 1020
990 heap [k] = he; 1021 heap [k] = he;
991 ev_active (ANHE_w (he)) = k; 1022 ev_active (ANHE_w (he)) = k;
992} 1023}
993 1024
1025/* move an element suitably so it is in a correct place */
994inline_size void 1026inline_size void
995adjustheap (ANHE *heap, int N, int k) 1027adjustheap (ANHE *heap, int N, int k)
996{ 1028{
997 if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) 1029 if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k]))
998 upheap (heap, k); 1030 upheap (heap, k);
1012 upheap (heap, i + HEAP0); 1044 upheap (heap, i + HEAP0);
1013} 1045}
1014 1046
1015/*****************************************************************************/ 1047/*****************************************************************************/
1016 1048
1049/* associate signal watchers to a signal signal */
1017typedef struct 1050typedef struct
1018{ 1051{
1019 WL head; 1052 WL head;
1020 EV_ATOMIC_T gotsig; 1053 EV_ATOMIC_T gotsig;
1021} ANSIG; 1054} ANSIG;
1025 1058
1026static EV_ATOMIC_T gotsig; 1059static EV_ATOMIC_T gotsig;
1027 1060
1028/*****************************************************************************/ 1061/*****************************************************************************/
1029 1062
1063/* used to prepare libev internal fd's */
1064/* this is not fork-safe */
1030inline_speed void 1065inline_speed void
1031fd_intern (int fd) 1066fd_intern (int fd)
1032{ 1067{
1033#ifdef _WIN32 1068#ifdef _WIN32
1034 unsigned long arg = 1; 1069 unsigned long arg = 1;
1040} 1075}
1041 1076
1042static void noinline 1077static void noinline
1043evpipe_init (EV_P) 1078evpipe_init (EV_P)
1044{ 1079{
1045 if (!ev_is_active (&pipeev)) 1080 if (!ev_is_active (&pipe_w))
1046 { 1081 {
1047#if EV_USE_EVENTFD 1082#if EV_USE_EVENTFD
1048 if ((evfd = eventfd (0, 0)) >= 0) 1083 if ((evfd = eventfd (0, 0)) >= 0)
1049 { 1084 {
1050 evpipe [0] = -1; 1085 evpipe [0] = -1;
1051 fd_intern (evfd); 1086 fd_intern (evfd);
1052 ev_io_set (&pipeev, evfd, EV_READ); 1087 ev_io_set (&pipe_w, evfd, EV_READ);
1053 } 1088 }
1054 else 1089 else
1055#endif 1090#endif
1056 { 1091 {
1057 while (pipe (evpipe)) 1092 while (pipe (evpipe))
1058 ev_syserr ("(libev) error creating signal/async pipe"); 1093 ev_syserr ("(libev) error creating signal/async pipe");
1059 1094
1060 fd_intern (evpipe [0]); 1095 fd_intern (evpipe [0]);
1061 fd_intern (evpipe [1]); 1096 fd_intern (evpipe [1]);
1062 ev_io_set (&pipeev, evpipe [0], EV_READ); 1097 ev_io_set (&pipe_w, evpipe [0], EV_READ);
1063 } 1098 }
1064 1099
1065 ev_io_start (EV_A_ &pipeev); 1100 ev_io_start (EV_A_ &pipe_w);
1066 ev_unref (EV_A); /* watcher should not keep loop alive */ 1101 ev_unref (EV_A); /* watcher should not keep loop alive */
1067 } 1102 }
1068} 1103}
1069 1104
1070inline_size void 1105inline_size void
1088 1123
1089 errno = old_errno; 1124 errno = old_errno;
1090 } 1125 }
1091} 1126}
1092 1127
1128/* called whenever the libev signal pipe */
1129/* got some events (signal, async) */
1093static void 1130static void
1094pipecb (EV_P_ ev_io *iow, int revents) 1131pipecb (EV_P_ ev_io *iow, int revents)
1095{ 1132{
1096#if EV_USE_EVENTFD 1133#if EV_USE_EVENTFD
1097 if (evfd >= 0) 1134 if (evfd >= 0)
1179 1216
1180#ifndef WIFCONTINUED 1217#ifndef WIFCONTINUED
1181# define WIFCONTINUED(status) 0 1218# define WIFCONTINUED(status) 0
1182#endif 1219#endif
1183 1220
1221/* handle a single child status event */
1184inline_speed void 1222inline_speed void
1185child_reap (EV_P_ int chain, int pid, int status) 1223child_reap (EV_P_ int chain, int pid, int status)
1186{ 1224{
1187 ev_child *w; 1225 ev_child *w;
1188 int traced = WIFSTOPPED (status) || WIFCONTINUED (status); 1226 int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
1202 1240
1203#ifndef WCONTINUED 1241#ifndef WCONTINUED
1204# define WCONTINUED 0 1242# define WCONTINUED 0
1205#endif 1243#endif
1206 1244
1245/* called on sigchld etc., calls waitpid */
1207static void 1246static void
1208childcb (EV_P_ ev_signal *sw, int revents) 1247childcb (EV_P_ ev_signal *sw, int revents)
1209{ 1248{
1210 int pid, status; 1249 int pid, status;
1211 1250
1324ev_loop_count (EV_P) 1363ev_loop_count (EV_P)
1325{ 1364{
1326 return loop_count; 1365 return loop_count;
1327} 1366}
1328 1367
1368unsigned int
1369ev_loop_depth (EV_P)
1370{
1371 return loop_depth;
1372}
1373
1329void 1374void
1330ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 1375ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
1331{ 1376{
1332 io_blocktime = interval; 1377 io_blocktime = interval;
1333} 1378}
1336ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 1381ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1337{ 1382{
1338 timeout_blocktime = interval; 1383 timeout_blocktime = interval;
1339} 1384}
1340 1385
1386/* initialise a loop structure, must be zero-initialised */
1341static void noinline 1387static void noinline
1342loop_init (EV_P_ unsigned int flags) 1388loop_init (EV_P_ unsigned int flags)
1343{ 1389{
1344 if (!backend) 1390 if (!backend)
1345 { 1391 {
1405#endif 1451#endif
1406#if EV_USE_SELECT 1452#if EV_USE_SELECT
1407 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); 1453 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
1408#endif 1454#endif
1409 1455
1456 ev_prepare_init (&pending_w, pendingcb);
1457
1410 ev_init (&pipeev, pipecb); 1458 ev_init (&pipe_w, pipecb);
1411 ev_set_priority (&pipeev, EV_MAXPRI); 1459 ev_set_priority (&pipe_w, EV_MAXPRI);
1412 } 1460 }
1413} 1461}
1414 1462
1463/* free up a loop structure */
1415static void noinline 1464static void noinline
1416loop_destroy (EV_P) 1465loop_destroy (EV_P)
1417{ 1466{
1418 int i; 1467 int i;
1419 1468
1420 if (ev_is_active (&pipeev)) 1469 if (ev_is_active (&pipe_w))
1421 { 1470 {
1422 ev_ref (EV_A); /* signal watcher */ 1471 ev_ref (EV_A); /* signal watcher */
1423 ev_io_stop (EV_A_ &pipeev); 1472 ev_io_stop (EV_A_ &pipe_w);
1424 1473
1425#if EV_USE_EVENTFD 1474#if EV_USE_EVENTFD
1426 if (evfd >= 0) 1475 if (evfd >= 0)
1427 close (evfd); 1476 close (evfd);
1428#endif 1477#endif
1505#endif 1554#endif
1506#if EV_USE_INOTIFY 1555#if EV_USE_INOTIFY
1507 infy_fork (EV_A); 1556 infy_fork (EV_A);
1508#endif 1557#endif
1509 1558
1510 if (ev_is_active (&pipeev)) 1559 if (ev_is_active (&pipe_w))
1511 { 1560 {
1512 /* this "locks" the handlers against writing to the pipe */ 1561 /* this "locks" the handlers against writing to the pipe */
1513 /* while we modify the fd vars */ 1562 /* while we modify the fd vars */
1514 gotsig = 1; 1563 gotsig = 1;
1515#if EV_ASYNC_ENABLE 1564#if EV_ASYNC_ENABLE
1516 gotasync = 1; 1565 gotasync = 1;
1517#endif 1566#endif
1518 1567
1519 ev_ref (EV_A); 1568 ev_ref (EV_A);
1520 ev_io_stop (EV_A_ &pipeev); 1569 ev_io_stop (EV_A_ &pipe_w);
1521 1570
1522#if EV_USE_EVENTFD 1571#if EV_USE_EVENTFD
1523 if (evfd >= 0) 1572 if (evfd >= 0)
1524 close (evfd); 1573 close (evfd);
1525#endif 1574#endif
1530 close (evpipe [1]); 1579 close (evpipe [1]);
1531 } 1580 }
1532 1581
1533 evpipe_init (EV_A); 1582 evpipe_init (EV_A);
1534 /* now iterate over everything, in case we missed something */ 1583 /* now iterate over everything, in case we missed something */
1535 pipecb (EV_A_ &pipeev, EV_READ); 1584 pipecb (EV_A_ &pipe_w, EV_READ);
1536 } 1585 }
1537 1586
1538 postfork = 0; 1587 postfork = 0;
1539} 1588}
1540 1589
1746 for (pri = NUMPRI; pri--; ) 1795 for (pri = NUMPRI; pri--; )
1747 while (pendingcnt [pri]) 1796 while (pendingcnt [pri])
1748 { 1797 {
1749 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1798 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1750 1799
1751 if (expect_true (p->w))
1752 {
1753 /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ 1800 /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/
1801 /* ^ this is no longer true, as pending_w could be here */
1754 1802
1755 p->w->pending = 0; 1803 p->w->pending = 0;
1756 EV_CB_INVOKE (p->w, p->events); 1804 EV_CB_INVOKE (p->w, p->events);
1757 EV_FREQUENT_CHECK; 1805 EV_FREQUENT_CHECK;
1758 }
1759 } 1806 }
1760} 1807}
1761 1808
1762#if EV_IDLE_ENABLE 1809#if EV_IDLE_ENABLE
1810/* make idle watchers pending. this handles the "call-idle */
1811/* only when higher priorities are idle" logic */
1763inline_size void 1812inline_size void
1764idle_reify (EV_P) 1813idle_reify (EV_P)
1765{ 1814{
1766 if (expect_false (idleall)) 1815 if (expect_false (idleall))
1767 { 1816 {
1780 } 1829 }
1781 } 1830 }
1782} 1831}
1783#endif 1832#endif
1784 1833
1834/* make timers pending */
1785inline_size void 1835inline_size void
1786timers_reify (EV_P) 1836timers_reify (EV_P)
1787{ 1837{
1788 EV_FREQUENT_CHECK; 1838 EV_FREQUENT_CHECK;
1789 1839
1818 feed_reverse_done (EV_A_ EV_TIMEOUT); 1868 feed_reverse_done (EV_A_ EV_TIMEOUT);
1819 } 1869 }
1820} 1870}
1821 1871
1822#if EV_PERIODIC_ENABLE 1872#if EV_PERIODIC_ENABLE
1873/* make periodics pending */
1823inline_size void 1874inline_size void
1824periodics_reify (EV_P) 1875periodics_reify (EV_P)
1825{ 1876{
1826 EV_FREQUENT_CHECK; 1877 EV_FREQUENT_CHECK;
1827 1878
1874 1925
1875 feed_reverse_done (EV_A_ EV_PERIODIC); 1926 feed_reverse_done (EV_A_ EV_PERIODIC);
1876 } 1927 }
1877} 1928}
1878 1929
1930/* simply recalculate all periodics */
1931/* TODO: maybe ensure that at leats one event happens when jumping forward? */
1879static void noinline 1932static void noinline
1880periodics_reschedule (EV_P) 1933periodics_reschedule (EV_P)
1881{ 1934{
1882 int i; 1935 int i;
1883 1936
1896 1949
1897 reheap (periodics, periodiccnt); 1950 reheap (periodics, periodiccnt);
1898} 1951}
1899#endif 1952#endif
1900 1953
1954/* adjust all timers by a given offset */
1901static void noinline 1955static void noinline
1902timers_reschedule (EV_P_ ev_tstamp adjust) 1956timers_reschedule (EV_P_ ev_tstamp adjust)
1903{ 1957{
1904 int i; 1958 int i;
1905 1959
1909 ANHE_w (*he)->at += adjust; 1963 ANHE_w (*he)->at += adjust;
1910 ANHE_at_cache (*he); 1964 ANHE_at_cache (*he);
1911 } 1965 }
1912} 1966}
1913 1967
1968/* fetch new monotonic and realtime times from the kernel */
1969/* also detetc if there was a timejump, and act accordingly */
1914inline_speed void 1970inline_speed void
1915time_update (EV_P_ ev_tstamp max_block) 1971time_update (EV_P_ ev_tstamp max_block)
1916{ 1972{
1917 int i;
1918
1919#if EV_USE_MONOTONIC 1973#if EV_USE_MONOTONIC
1920 if (expect_true (have_monotonic)) 1974 if (expect_true (have_monotonic))
1921 { 1975 {
1976 int i;
1922 ev_tstamp odiff = rtmn_diff; 1977 ev_tstamp odiff = rtmn_diff;
1923 1978
1924 mn_now = get_clock (); 1979 mn_now = get_clock ();
1925 1980
1926 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ 1981 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
1976 2031
1977 mn_now = ev_rt_now; 2032 mn_now = ev_rt_now;
1978 } 2033 }
1979} 2034}
1980 2035
1981static int loop_done;
1982
1983void 2036void
1984ev_loop (EV_P_ int flags) 2037ev_loop (EV_P_ int flags)
1985{ 2038{
2039 ++loop_depth;
2040
1986 loop_done = EVUNLOOP_CANCEL; 2041 loop_done = EVUNLOOP_CANCEL;
1987 2042
1988 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 2043 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1989 2044
1990 do 2045 do
2031 ev_tstamp waittime = 0.; 2086 ev_tstamp waittime = 0.;
2032 ev_tstamp sleeptime = 0.; 2087 ev_tstamp sleeptime = 0.;
2033 2088
2034 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2089 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
2035 { 2090 {
2091 /* remember old timestamp for io_blocktime calculation */
2092 ev_tstamp prev_mn_now = mn_now;
2093
2036 /* update time to cancel out callback processing overhead */ 2094 /* update time to cancel out callback processing overhead */
2037 time_update (EV_A_ 1e100); 2095 time_update (EV_A_ 1e100);
2038 2096
2039 waittime = MAX_BLOCKTIME; 2097 waittime = MAX_BLOCKTIME;
2040 2098
2050 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; 2108 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
2051 if (waittime > to) waittime = to; 2109 if (waittime > to) waittime = to;
2052 } 2110 }
2053#endif 2111#endif
2054 2112
2113 /* don't let timeouts decrease the waittime below timeout_blocktime */
2055 if (expect_false (waittime < timeout_blocktime)) 2114 if (expect_false (waittime < timeout_blocktime))
2056 waittime = timeout_blocktime; 2115 waittime = timeout_blocktime;
2057 2116
2058 sleeptime = waittime - backend_fudge; 2117 /* extra check because io_blocktime is commonly 0 */
2059
2060 if (expect_true (sleeptime > io_blocktime)) 2118 if (expect_false (io_blocktime))
2061 sleeptime = io_blocktime;
2062
2063 if (sleeptime)
2064 { 2119 {
2120 sleeptime = io_blocktime - (mn_now - prev_mn_now);
2121
2122 if (sleeptime > waittime - backend_fudge)
2123 sleeptime = waittime - backend_fudge;
2124
2125 if (expect_true (sleeptime > 0.))
2126 {
2065 ev_sleep (sleeptime); 2127 ev_sleep (sleeptime);
2066 waittime -= sleeptime; 2128 waittime -= sleeptime;
2129 }
2067 } 2130 }
2068 } 2131 }
2069 2132
2070 ++loop_count; 2133 ++loop_count;
2071 backend_poll (EV_A_ waittime); 2134 backend_poll (EV_A_ waittime);
2097 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2160 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2098 )); 2161 ));
2099 2162
2100 if (loop_done == EVUNLOOP_ONE) 2163 if (loop_done == EVUNLOOP_ONE)
2101 loop_done = EVUNLOOP_CANCEL; 2164 loop_done = EVUNLOOP_CANCEL;
2165
2166 --loop_depth;
2102} 2167}
2103 2168
2104void 2169void
2105ev_unloop (EV_P_ int how) 2170ev_unloop (EV_P_ int how)
2106{ 2171{
2137 ev_tstamp mn_prev = mn_now; 2202 ev_tstamp mn_prev = mn_now;
2138 2203
2139 ev_now_update (EV_A); 2204 ev_now_update (EV_A);
2140 timers_reschedule (EV_A_ mn_now - mn_prev); 2205 timers_reschedule (EV_A_ mn_now - mn_prev);
2141#if EV_PERIODIC_ENABLE 2206#if EV_PERIODIC_ENABLE
2207 /* TODO: really do this? */
2142 periodics_reschedule (EV_A); 2208 periodics_reschedule (EV_A);
2143#endif 2209#endif
2144} 2210}
2145 2211
2146/*****************************************************************************/ 2212/*****************************************************************************/
2213/* singly-linked list management, used when the expected list length is short */
2147 2214
2148inline_size void 2215inline_size void
2149wlist_add (WL *head, WL elem) 2216wlist_add (WL *head, WL elem)
2150{ 2217{
2151 elem->next = *head; 2218 elem->next = *head;
2165 2232
2166 head = &(*head)->next; 2233 head = &(*head)->next;
2167 } 2234 }
2168} 2235}
2169 2236
2237/* internal, faster, version of ev_clear_pending */
2170inline_speed void 2238inline_speed void
2171clear_pending (EV_P_ W w) 2239clear_pending (EV_P_ W w)
2172{ 2240{
2173 if (w->pending) 2241 if (w->pending)
2174 { 2242 {
2175 pendings [ABSPRI (w)][w->pending - 1].w = 0; 2243 pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w;
2176 w->pending = 0; 2244 w->pending = 0;
2177 } 2245 }
2178} 2246}
2179 2247
2180int 2248int
2184 int pending = w_->pending; 2252 int pending = w_->pending;
2185 2253
2186 if (expect_true (pending)) 2254 if (expect_true (pending))
2187 { 2255 {
2188 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; 2256 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
2257 p->w = (W)&pending_w;
2189 w_->pending = 0; 2258 w_->pending = 0;
2190 p->w = 0;
2191 return p->events; 2259 return p->events;
2192 } 2260 }
2193 else 2261 else
2194 return 0; 2262 return 0;
2195} 2263}
2196 2264
2197inline_size void 2265inline_size void
2198pri_adjust (EV_P_ W w) 2266pri_adjust (EV_P_ W w)
2199{ 2267{
2200 int pri = w->priority; 2268 int pri = ev_priority (w);
2201 pri = pri < EV_MINPRI ? EV_MINPRI : pri; 2269 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2202 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; 2270 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2203 w->priority = pri; 2271 ev_set_priority (w, pri);
2204} 2272}
2205 2273
2206inline_speed void 2274inline_speed void
2207ev_start (EV_P_ W w, int active) 2275ev_start (EV_P_ W w, int active)
2208{ 2276{
3201 } 3269 }
3202} 3270}
3203 3271
3204/*****************************************************************************/ 3272/*****************************************************************************/
3205 3273
3206#if 0 3274#if EV_WALK_ENABLE
3207void 3275void
3208ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) 3276ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w))
3209{ 3277{
3210 int i, j; 3278 int i, j;
3211 ev_watcher_list *wl, *wn; 3279 ev_watcher_list *wl, *wn;
3227#if EV_USE_INOTIFY 3295#if EV_USE_INOTIFY
3228 if (ev_cb ((ev_io *)wl) == infy_cb) 3296 if (ev_cb ((ev_io *)wl) == infy_cb)
3229 ; 3297 ;
3230 else 3298 else
3231#endif 3299#endif
3232 if ((ev_io *)wl != &pipeev) 3300 if ((ev_io *)wl != &pipe_w)
3233 if (types & EV_IO) 3301 if (types & EV_IO)
3234 cb (EV_A_ EV_IO, wl); 3302 cb (EV_A_ EV_IO, wl);
3235 3303
3236 wl = wn; 3304 wl = wn;
3237 } 3305 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines