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.294 by root, Wed Jul 8 02:46:05 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" {
478#define ev_malloc(size) ev_realloc (0, (size)) 485#define ev_malloc(size) ev_realloc (0, (size))
479#define ev_free(ptr) ev_realloc ((ptr), 0) 486#define ev_free(ptr) ev_realloc ((ptr), 0)
480 487
481/*****************************************************************************/ 488/*****************************************************************************/
482 489
490/* file descriptor info structure */
483typedef struct 491typedef struct
484{ 492{
485 WL head; 493 WL head;
486 unsigned char events; 494 unsigned char events; /* the events watched for */
487 unsigned char reify; 495 unsigned char reify; /* flag set when this ANFD needs reification */
488 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ 496 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */
489 unsigned char unused; 497 unsigned char unused;
490#if EV_USE_EPOLL 498#if EV_USE_EPOLL
491 unsigned int egen; /* generation counter to counter epoll bugs */ 499 unsigned int egen; /* generation counter to counter epoll bugs */
492#endif 500#endif
493#if EV_SELECT_IS_WINSOCKET 501#if EV_SELECT_IS_WINSOCKET
494 SOCKET handle; 502 SOCKET handle;
495#endif 503#endif
496} ANFD; 504} ANFD;
497 505
506/* stores the pending event set for a given watcher */
498typedef struct 507typedef struct
499{ 508{
500 W w; 509 W w;
501 int events; 510 int events; /* the pending event set for the given watcher */
502} ANPENDING; 511} ANPENDING;
503 512
504#if EV_USE_INOTIFY 513#if EV_USE_INOTIFY
505/* hash table entry per inotify-id */ 514/* hash table entry per inotify-id */
506typedef struct 515typedef struct
509} ANFS; 518} ANFS;
510#endif 519#endif
511 520
512/* Heap Entry */ 521/* Heap Entry */
513#if EV_HEAP_CACHE_AT 522#if EV_HEAP_CACHE_AT
523 /* a heap element */
514 typedef struct { 524 typedef struct {
515 ev_tstamp at; 525 ev_tstamp at;
516 WT w; 526 WT w;
517 } ANHE; 527 } ANHE;
518 528
519 #define ANHE_w(he) (he).w /* access watcher, read-write */ 529 #define ANHE_w(he) (he).w /* access watcher, read-write */
520 #define ANHE_at(he) (he).at /* access cached at, read-only */ 530 #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 */ 531 #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */
522#else 532#else
533 /* a heap element */
523 typedef WT ANHE; 534 typedef WT ANHE;
524 535
525 #define ANHE_w(he) (he) 536 #define ANHE_w(he) (he)
526 #define ANHE_at(he) (he)->at 537 #define ANHE_at(he) (he)->at
527 #define ANHE_at_cache(he) 538 #define ANHE_at_cache(he)
553 564
554#endif 565#endif
555 566
556/*****************************************************************************/ 567/*****************************************************************************/
557 568
569#ifndef EV_HAVE_EV_TIME
558ev_tstamp 570ev_tstamp
559ev_time (void) 571ev_time (void)
560{ 572{
561#if EV_USE_REALTIME 573#if EV_USE_REALTIME
562 if (expect_true (have_realtime)) 574 if (expect_true (have_realtime))
569 581
570 struct timeval tv; 582 struct timeval tv;
571 gettimeofday (&tv, 0); 583 gettimeofday (&tv, 0);
572 return tv.tv_sec + tv.tv_usec * 1e-6; 584 return tv.tv_sec + tv.tv_usec * 1e-6;
573} 585}
586#endif
574 587
575inline_size ev_tstamp 588inline_size ev_tstamp
576get_clock (void) 589get_clock (void)
577{ 590{
578#if EV_USE_MONOTONIC 591#if EV_USE_MONOTONIC
614 627
615 tv.tv_sec = (time_t)delay; 628 tv.tv_sec = (time_t)delay;
616 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 629 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
617 630
618 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 631 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
619 /* somehting nto guaranteed by newer posix versions, but guaranteed */ 632 /* somehting not guaranteed by newer posix versions, but guaranteed */
620 /* by older ones */ 633 /* by older ones */
621 select (0, 0, 0, 0, &tv); 634 select (0, 0, 0, 0, &tv);
622#endif 635#endif
623 } 636 }
624} 637}
625 638
626/*****************************************************************************/ 639/*****************************************************************************/
627 640
628#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ 641#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
629 642
643/* find a suitable new size for the given array, */
644/* hopefully by rounding to a ncie-to-malloc size */
630inline_size int 645inline_size int
631array_nextsize (int elem, int cur, int cnt) 646array_nextsize (int elem, int cur, int cnt)
632{ 647{
633 int ncur = cur + 1; 648 int ncur = cur + 1;
634 649
680#define array_free(stem, idx) \ 695#define array_free(stem, idx) \
681 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0 696 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0
682 697
683/*****************************************************************************/ 698/*****************************************************************************/
684 699
700/* dummy callback for pending events */
701static void noinline
702pendingcb (EV_P_ ev_prepare *w, int revents)
703{
704}
705
685void noinline 706void noinline
686ev_feed_event (EV_P_ void *w, int revents) 707ev_feed_event (EV_P_ void *w, int revents)
687{ 708{
688 W w_ = (W)w; 709 W w_ = (W)w;
689 int pri = ABSPRI (w_); 710 int pri = ABSPRI (w_);
745{ 766{
746 if (fd >= 0 && fd < anfdmax) 767 if (fd >= 0 && fd < anfdmax)
747 fd_event (EV_A_ fd, revents); 768 fd_event (EV_A_ fd, revents);
748} 769}
749 770
771/* make sure the external fd watch events are in-sync */
772/* with the kernel/libev internal state */
750inline_size void 773inline_size void
751fd_reify (EV_P) 774fd_reify (EV_P)
752{ 775{
753 int i; 776 int i;
754 777
789 } 812 }
790 813
791 fdchangecnt = 0; 814 fdchangecnt = 0;
792} 815}
793 816
817/* something about the given fd changed */
794inline_size void 818inline_size void
795fd_change (EV_P_ int fd, int flags) 819fd_change (EV_P_ int fd, int flags)
796{ 820{
797 unsigned char reify = anfds [fd].reify; 821 unsigned char reify = anfds [fd].reify;
798 anfds [fd].reify |= flags; 822 anfds [fd].reify |= flags;
803 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 827 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
804 fdchanges [fdchangecnt - 1] = fd; 828 fdchanges [fdchangecnt - 1] = fd;
805 } 829 }
806} 830}
807 831
832/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
808inline_speed void 833inline_speed void
809fd_kill (EV_P_ int fd) 834fd_kill (EV_P_ int fd)
810{ 835{
811 ev_io *w; 836 ev_io *w;
812 837
815 ev_io_stop (EV_A_ w); 840 ev_io_stop (EV_A_ w);
816 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 841 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
817 } 842 }
818} 843}
819 844
845/* check whether the given fd is atcually valid, for error recovery */
820inline_size int 846inline_size int
821fd_valid (int fd) 847fd_valid (int fd)
822{ 848{
823#ifdef _WIN32 849#ifdef _WIN32
824 return _get_osfhandle (fd) != -1; 850 return _get_osfhandle (fd) != -1;
989 1015
990 heap [k] = he; 1016 heap [k] = he;
991 ev_active (ANHE_w (he)) = k; 1017 ev_active (ANHE_w (he)) = k;
992} 1018}
993 1019
1020/* move an element suitably so it is in a correct place */
994inline_size void 1021inline_size void
995adjustheap (ANHE *heap, int N, int k) 1022adjustheap (ANHE *heap, int N, int k)
996{ 1023{
997 if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) 1024 if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k]))
998 upheap (heap, k); 1025 upheap (heap, k);
1012 upheap (heap, i + HEAP0); 1039 upheap (heap, i + HEAP0);
1013} 1040}
1014 1041
1015/*****************************************************************************/ 1042/*****************************************************************************/
1016 1043
1044/* associate signal watchers to a signal signal */
1017typedef struct 1045typedef struct
1018{ 1046{
1019 WL head; 1047 WL head;
1020 EV_ATOMIC_T gotsig; 1048 EV_ATOMIC_T gotsig;
1021} ANSIG; 1049} ANSIG;
1025 1053
1026static EV_ATOMIC_T gotsig; 1054static EV_ATOMIC_T gotsig;
1027 1055
1028/*****************************************************************************/ 1056/*****************************************************************************/
1029 1057
1058/* used to prepare libev internal fd's */
1059/* this is not fork-safe */
1030inline_speed void 1060inline_speed void
1031fd_intern (int fd) 1061fd_intern (int fd)
1032{ 1062{
1033#ifdef _WIN32 1063#ifdef _WIN32
1034 unsigned long arg = 1; 1064 unsigned long arg = 1;
1040} 1070}
1041 1071
1042static void noinline 1072static void noinline
1043evpipe_init (EV_P) 1073evpipe_init (EV_P)
1044{ 1074{
1045 if (!ev_is_active (&pipeev)) 1075 if (!ev_is_active (&pipe_w))
1046 { 1076 {
1047#if EV_USE_EVENTFD 1077#if EV_USE_EVENTFD
1048 if ((evfd = eventfd (0, 0)) >= 0) 1078 if ((evfd = eventfd (0, 0)) >= 0)
1049 { 1079 {
1050 evpipe [0] = -1; 1080 evpipe [0] = -1;
1051 fd_intern (evfd); 1081 fd_intern (evfd);
1052 ev_io_set (&pipeev, evfd, EV_READ); 1082 ev_io_set (&pipe_w, evfd, EV_READ);
1053 } 1083 }
1054 else 1084 else
1055#endif 1085#endif
1056 { 1086 {
1057 while (pipe (evpipe)) 1087 while (pipe (evpipe))
1058 ev_syserr ("(libev) error creating signal/async pipe"); 1088 ev_syserr ("(libev) error creating signal/async pipe");
1059 1089
1060 fd_intern (evpipe [0]); 1090 fd_intern (evpipe [0]);
1061 fd_intern (evpipe [1]); 1091 fd_intern (evpipe [1]);
1062 ev_io_set (&pipeev, evpipe [0], EV_READ); 1092 ev_io_set (&pipe_w, evpipe [0], EV_READ);
1063 } 1093 }
1064 1094
1065 ev_io_start (EV_A_ &pipeev); 1095 ev_io_start (EV_A_ &pipe_w);
1066 ev_unref (EV_A); /* watcher should not keep loop alive */ 1096 ev_unref (EV_A); /* watcher should not keep loop alive */
1067 } 1097 }
1068} 1098}
1069 1099
1070inline_size void 1100inline_size void
1088 1118
1089 errno = old_errno; 1119 errno = old_errno;
1090 } 1120 }
1091} 1121}
1092 1122
1123/* called whenever the libev signal pipe */
1124/* got some events (signal, async) */
1093static void 1125static void
1094pipecb (EV_P_ ev_io *iow, int revents) 1126pipecb (EV_P_ ev_io *iow, int revents)
1095{ 1127{
1096#if EV_USE_EVENTFD 1128#if EV_USE_EVENTFD
1097 if (evfd >= 0) 1129 if (evfd >= 0)
1179 1211
1180#ifndef WIFCONTINUED 1212#ifndef WIFCONTINUED
1181# define WIFCONTINUED(status) 0 1213# define WIFCONTINUED(status) 0
1182#endif 1214#endif
1183 1215
1216/* handle a single child status event */
1184inline_speed void 1217inline_speed void
1185child_reap (EV_P_ int chain, int pid, int status) 1218child_reap (EV_P_ int chain, int pid, int status)
1186{ 1219{
1187 ev_child *w; 1220 ev_child *w;
1188 int traced = WIFSTOPPED (status) || WIFCONTINUED (status); 1221 int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
1202 1235
1203#ifndef WCONTINUED 1236#ifndef WCONTINUED
1204# define WCONTINUED 0 1237# define WCONTINUED 0
1205#endif 1238#endif
1206 1239
1240/* called on sigchld etc., calls waitpid */
1207static void 1241static void
1208childcb (EV_P_ ev_signal *sw, int revents) 1242childcb (EV_P_ ev_signal *sw, int revents)
1209{ 1243{
1210 int pid, status; 1244 int pid, status;
1211 1245
1324ev_loop_count (EV_P) 1358ev_loop_count (EV_P)
1325{ 1359{
1326 return loop_count; 1360 return loop_count;
1327} 1361}
1328 1362
1363unsigned int
1364ev_loop_depth (EV_P)
1365{
1366 return loop_depth;
1367}
1368
1329void 1369void
1330ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 1370ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
1331{ 1371{
1332 io_blocktime = interval; 1372 io_blocktime = interval;
1333} 1373}
1336ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 1376ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1337{ 1377{
1338 timeout_blocktime = interval; 1378 timeout_blocktime = interval;
1339} 1379}
1340 1380
1381/* initialise a loop structure, must be zero-initialised */
1341static void noinline 1382static void noinline
1342loop_init (EV_P_ unsigned int flags) 1383loop_init (EV_P_ unsigned int flags)
1343{ 1384{
1344 if (!backend) 1385 if (!backend)
1345 { 1386 {
1405#endif 1446#endif
1406#if EV_USE_SELECT 1447#if EV_USE_SELECT
1407 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); 1448 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
1408#endif 1449#endif
1409 1450
1451 ev_prepare_init (&pending_w, pendingcb);
1452
1410 ev_init (&pipeev, pipecb); 1453 ev_init (&pipe_w, pipecb);
1411 ev_set_priority (&pipeev, EV_MAXPRI); 1454 ev_set_priority (&pipe_w, EV_MAXPRI);
1412 } 1455 }
1413} 1456}
1414 1457
1458/* free up a loop structure */
1415static void noinline 1459static void noinline
1416loop_destroy (EV_P) 1460loop_destroy (EV_P)
1417{ 1461{
1418 int i; 1462 int i;
1419 1463
1420 if (ev_is_active (&pipeev)) 1464 if (ev_is_active (&pipe_w))
1421 { 1465 {
1422 ev_ref (EV_A); /* signal watcher */ 1466 ev_ref (EV_A); /* signal watcher */
1423 ev_io_stop (EV_A_ &pipeev); 1467 ev_io_stop (EV_A_ &pipe_w);
1424 1468
1425#if EV_USE_EVENTFD 1469#if EV_USE_EVENTFD
1426 if (evfd >= 0) 1470 if (evfd >= 0)
1427 close (evfd); 1471 close (evfd);
1428#endif 1472#endif
1505#endif 1549#endif
1506#if EV_USE_INOTIFY 1550#if EV_USE_INOTIFY
1507 infy_fork (EV_A); 1551 infy_fork (EV_A);
1508#endif 1552#endif
1509 1553
1510 if (ev_is_active (&pipeev)) 1554 if (ev_is_active (&pipe_w))
1511 { 1555 {
1512 /* this "locks" the handlers against writing to the pipe */ 1556 /* this "locks" the handlers against writing to the pipe */
1513 /* while we modify the fd vars */ 1557 /* while we modify the fd vars */
1514 gotsig = 1; 1558 gotsig = 1;
1515#if EV_ASYNC_ENABLE 1559#if EV_ASYNC_ENABLE
1516 gotasync = 1; 1560 gotasync = 1;
1517#endif 1561#endif
1518 1562
1519 ev_ref (EV_A); 1563 ev_ref (EV_A);
1520 ev_io_stop (EV_A_ &pipeev); 1564 ev_io_stop (EV_A_ &pipe_w);
1521 1565
1522#if EV_USE_EVENTFD 1566#if EV_USE_EVENTFD
1523 if (evfd >= 0) 1567 if (evfd >= 0)
1524 close (evfd); 1568 close (evfd);
1525#endif 1569#endif
1530 close (evpipe [1]); 1574 close (evpipe [1]);
1531 } 1575 }
1532 1576
1533 evpipe_init (EV_A); 1577 evpipe_init (EV_A);
1534 /* now iterate over everything, in case we missed something */ 1578 /* now iterate over everything, in case we missed something */
1535 pipecb (EV_A_ &pipeev, EV_READ); 1579 pipecb (EV_A_ &pipe_w, EV_READ);
1536 } 1580 }
1537 1581
1538 postfork = 0; 1582 postfork = 0;
1539} 1583}
1540 1584
1746 for (pri = NUMPRI; pri--; ) 1790 for (pri = NUMPRI; pri--; )
1747 while (pendingcnt [pri]) 1791 while (pendingcnt [pri])
1748 { 1792 {
1749 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1793 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1750 1794
1751 if (expect_true (p->w))
1752 {
1753 /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ 1795 /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/
1796 /* ^ this is no longer true, as pending_w could be here */
1754 1797
1755 p->w->pending = 0; 1798 p->w->pending = 0;
1756 EV_CB_INVOKE (p->w, p->events); 1799 EV_CB_INVOKE (p->w, p->events);
1757 EV_FREQUENT_CHECK; 1800 EV_FREQUENT_CHECK;
1758 }
1759 } 1801 }
1760} 1802}
1761 1803
1762#if EV_IDLE_ENABLE 1804#if EV_IDLE_ENABLE
1805/* make idle watchers pending. this handles the "call-idle */
1806/* only when higher priorities are idle" logic */
1763inline_size void 1807inline_size void
1764idle_reify (EV_P) 1808idle_reify (EV_P)
1765{ 1809{
1766 if (expect_false (idleall)) 1810 if (expect_false (idleall))
1767 { 1811 {
1780 } 1824 }
1781 } 1825 }
1782} 1826}
1783#endif 1827#endif
1784 1828
1829/* make timers pending */
1785inline_size void 1830inline_size void
1786timers_reify (EV_P) 1831timers_reify (EV_P)
1787{ 1832{
1788 EV_FREQUENT_CHECK; 1833 EV_FREQUENT_CHECK;
1789 1834
1818 feed_reverse_done (EV_A_ EV_TIMEOUT); 1863 feed_reverse_done (EV_A_ EV_TIMEOUT);
1819 } 1864 }
1820} 1865}
1821 1866
1822#if EV_PERIODIC_ENABLE 1867#if EV_PERIODIC_ENABLE
1868/* make periodics pending */
1823inline_size void 1869inline_size void
1824periodics_reify (EV_P) 1870periodics_reify (EV_P)
1825{ 1871{
1826 EV_FREQUENT_CHECK; 1872 EV_FREQUENT_CHECK;
1827 1873
1874 1920
1875 feed_reverse_done (EV_A_ EV_PERIODIC); 1921 feed_reverse_done (EV_A_ EV_PERIODIC);
1876 } 1922 }
1877} 1923}
1878 1924
1925/* simply recalculate all periodics */
1926/* TODO: maybe ensure that at leats one event happens when jumping forward? */
1879static void noinline 1927static void noinline
1880periodics_reschedule (EV_P) 1928periodics_reschedule (EV_P)
1881{ 1929{
1882 int i; 1930 int i;
1883 1931
1896 1944
1897 reheap (periodics, periodiccnt); 1945 reheap (periodics, periodiccnt);
1898} 1946}
1899#endif 1947#endif
1900 1948
1949/* adjust all timers by a given offset */
1901static void noinline 1950static void noinline
1902timers_reschedule (EV_P_ ev_tstamp adjust) 1951timers_reschedule (EV_P_ ev_tstamp adjust)
1903{ 1952{
1904 int i; 1953 int i;
1905 1954
1909 ANHE_w (*he)->at += adjust; 1958 ANHE_w (*he)->at += adjust;
1910 ANHE_at_cache (*he); 1959 ANHE_at_cache (*he);
1911 } 1960 }
1912} 1961}
1913 1962
1963/* fetch new monotonic and realtime times from the kernel */
1964/* also detetc if there was a timejump, and act accordingly */
1914inline_speed void 1965inline_speed void
1915time_update (EV_P_ ev_tstamp max_block) 1966time_update (EV_P_ ev_tstamp max_block)
1916{ 1967{
1917 int i;
1918
1919#if EV_USE_MONOTONIC 1968#if EV_USE_MONOTONIC
1920 if (expect_true (have_monotonic)) 1969 if (expect_true (have_monotonic))
1921 { 1970 {
1971 int i;
1922 ev_tstamp odiff = rtmn_diff; 1972 ev_tstamp odiff = rtmn_diff;
1923 1973
1924 mn_now = get_clock (); 1974 mn_now = get_clock ();
1925 1975
1926 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ 1976 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
1976 2026
1977 mn_now = ev_rt_now; 2027 mn_now = ev_rt_now;
1978 } 2028 }
1979} 2029}
1980 2030
1981static int loop_done;
1982
1983void 2031void
1984ev_loop (EV_P_ int flags) 2032ev_loop (EV_P_ int flags)
1985{ 2033{
2034 ++loop_depth;
2035
1986 loop_done = EVUNLOOP_CANCEL; 2036 loop_done = EVUNLOOP_CANCEL;
1987 2037
1988 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 2038 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1989 2039
1990 do 2040 do
2031 ev_tstamp waittime = 0.; 2081 ev_tstamp waittime = 0.;
2032 ev_tstamp sleeptime = 0.; 2082 ev_tstamp sleeptime = 0.;
2033 2083
2034 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2084 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
2035 { 2085 {
2086 /* remember old timestamp for io_blocktime calculation */
2087 ev_tstamp prev_mn_now = mn_now;
2088
2036 /* update time to cancel out callback processing overhead */ 2089 /* update time to cancel out callback processing overhead */
2037 time_update (EV_A_ 1e100); 2090 time_update (EV_A_ 1e100);
2038 2091
2039 waittime = MAX_BLOCKTIME; 2092 waittime = MAX_BLOCKTIME;
2040 2093
2050 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; 2103 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
2051 if (waittime > to) waittime = to; 2104 if (waittime > to) waittime = to;
2052 } 2105 }
2053#endif 2106#endif
2054 2107
2108 /* don't let timeouts decrease the waittime below timeout_blocktime */
2055 if (expect_false (waittime < timeout_blocktime)) 2109 if (expect_false (waittime < timeout_blocktime))
2056 waittime = timeout_blocktime; 2110 waittime = timeout_blocktime;
2057 2111
2058 sleeptime = waittime - backend_fudge; 2112 /* extra check because io_blocktime is commonly 0 */
2059
2060 if (expect_true (sleeptime > io_blocktime)) 2113 if (expect_false (io_blocktime))
2061 sleeptime = io_blocktime;
2062
2063 if (sleeptime)
2064 { 2114 {
2115 sleeptime = io_blocktime - (mn_now - prev_mn_now);
2116
2117 if (sleeptime > waittime - backend_fudge)
2118 sleeptime = waittime - backend_fudge;
2119
2120 if (expect_true (sleeptime > 0.))
2121 {
2065 ev_sleep (sleeptime); 2122 ev_sleep (sleeptime);
2066 waittime -= sleeptime; 2123 waittime -= sleeptime;
2124 }
2067 } 2125 }
2068 } 2126 }
2069 2127
2070 ++loop_count; 2128 ++loop_count;
2071 backend_poll (EV_A_ waittime); 2129 backend_poll (EV_A_ waittime);
2097 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2155 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2098 )); 2156 ));
2099 2157
2100 if (loop_done == EVUNLOOP_ONE) 2158 if (loop_done == EVUNLOOP_ONE)
2101 loop_done = EVUNLOOP_CANCEL; 2159 loop_done = EVUNLOOP_CANCEL;
2160
2161 --loop_depth;
2102} 2162}
2103 2163
2104void 2164void
2105ev_unloop (EV_P_ int how) 2165ev_unloop (EV_P_ int how)
2106{ 2166{
2137 ev_tstamp mn_prev = mn_now; 2197 ev_tstamp mn_prev = mn_now;
2138 2198
2139 ev_now_update (EV_A); 2199 ev_now_update (EV_A);
2140 timers_reschedule (EV_A_ mn_now - mn_prev); 2200 timers_reschedule (EV_A_ mn_now - mn_prev);
2141#if EV_PERIODIC_ENABLE 2201#if EV_PERIODIC_ENABLE
2202 /* TODO: really do this? */
2142 periodics_reschedule (EV_A); 2203 periodics_reschedule (EV_A);
2143#endif 2204#endif
2144} 2205}
2145 2206
2146/*****************************************************************************/ 2207/*****************************************************************************/
2208/* singly-linked list management, used when the expected list length is short */
2147 2209
2148inline_size void 2210inline_size void
2149wlist_add (WL *head, WL elem) 2211wlist_add (WL *head, WL elem)
2150{ 2212{
2151 elem->next = *head; 2213 elem->next = *head;
2165 2227
2166 head = &(*head)->next; 2228 head = &(*head)->next;
2167 } 2229 }
2168} 2230}
2169 2231
2232/* internal, faster, version of ev_clear_pending */
2170inline_speed void 2233inline_speed void
2171clear_pending (EV_P_ W w) 2234clear_pending (EV_P_ W w)
2172{ 2235{
2173 if (w->pending) 2236 if (w->pending)
2174 { 2237 {
2175 pendings [ABSPRI (w)][w->pending - 1].w = 0; 2238 pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w;
2176 w->pending = 0; 2239 w->pending = 0;
2177 } 2240 }
2178} 2241}
2179 2242
2180int 2243int
2184 int pending = w_->pending; 2247 int pending = w_->pending;
2185 2248
2186 if (expect_true (pending)) 2249 if (expect_true (pending))
2187 { 2250 {
2188 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; 2251 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
2252 p->w = (W)&pending_w;
2189 w_->pending = 0; 2253 w_->pending = 0;
2190 p->w = 0;
2191 return p->events; 2254 return p->events;
2192 } 2255 }
2193 else 2256 else
2194 return 0; 2257 return 0;
2195} 2258}
3201 } 3264 }
3202} 3265}
3203 3266
3204/*****************************************************************************/ 3267/*****************************************************************************/
3205 3268
3206#if 0 3269#if EV_WALK_ENABLE
3207void 3270void
3208ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) 3271ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w))
3209{ 3272{
3210 int i, j; 3273 int i, j;
3211 ev_watcher_list *wl, *wn; 3274 ev_watcher_list *wl, *wn;
3227#if EV_USE_INOTIFY 3290#if EV_USE_INOTIFY
3228 if (ev_cb ((ev_io *)wl) == infy_cb) 3291 if (ev_cb ((ev_io *)wl) == infy_cb)
3229 ; 3292 ;
3230 else 3293 else
3231#endif 3294#endif
3232 if ((ev_io *)wl != &pipeev) 3295 if ((ev_io *)wl != &pipe_w)
3233 if (types & EV_IO) 3296 if (types & EV_IO)
3234 cb (EV_A_ EV_IO, wl); 3297 cb (EV_A_ EV_IO, wl);
3235 3298
3236 wl = wn; 3299 wl = wn;
3237 } 3300 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines