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

Comparing libev/ev.c (file contents):
Revision 1.285 by root, Wed Apr 15 19:35:53 2009 UTC vs.
Revision 1.296 by root, Thu Jul 9 09:11:20 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 {
1365 1411
1366 ev_rt_now = ev_time (); 1412 ev_rt_now = ev_time ();
1367 mn_now = get_clock (); 1413 mn_now = get_clock ();
1368 now_floor = mn_now; 1414 now_floor = mn_now;
1369 rtmn_diff = ev_rt_now - mn_now; 1415 rtmn_diff = ev_rt_now - mn_now;
1416 invoke_cb = ev_invoke_pending;
1370 1417
1371 io_blocktime = 0.; 1418 io_blocktime = 0.;
1372 timeout_blocktime = 0.; 1419 timeout_blocktime = 0.;
1373 backend = 0; 1420 backend = 0;
1374 backend_fd = -1; 1421 backend_fd = -1;
1405#endif 1452#endif
1406#if EV_USE_SELECT 1453#if EV_USE_SELECT
1407 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); 1454 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
1408#endif 1455#endif
1409 1456
1457 ev_prepare_init (&pending_w, pendingcb);
1458
1410 ev_init (&pipeev, pipecb); 1459 ev_init (&pipe_w, pipecb);
1411 ev_set_priority (&pipeev, EV_MAXPRI); 1460 ev_set_priority (&pipe_w, EV_MAXPRI);
1412 } 1461 }
1413} 1462}
1414 1463
1464/* free up a loop structure */
1415static void noinline 1465static void noinline
1416loop_destroy (EV_P) 1466loop_destroy (EV_P)
1417{ 1467{
1418 int i; 1468 int i;
1419 1469
1420 if (ev_is_active (&pipeev)) 1470 if (ev_is_active (&pipe_w))
1421 { 1471 {
1422 ev_ref (EV_A); /* signal watcher */ 1472 ev_ref (EV_A); /* signal watcher */
1423 ev_io_stop (EV_A_ &pipeev); 1473 ev_io_stop (EV_A_ &pipe_w);
1424 1474
1425#if EV_USE_EVENTFD 1475#if EV_USE_EVENTFD
1426 if (evfd >= 0) 1476 if (evfd >= 0)
1427 close (evfd); 1477 close (evfd);
1428#endif 1478#endif
1505#endif 1555#endif
1506#if EV_USE_INOTIFY 1556#if EV_USE_INOTIFY
1507 infy_fork (EV_A); 1557 infy_fork (EV_A);
1508#endif 1558#endif
1509 1559
1510 if (ev_is_active (&pipeev)) 1560 if (ev_is_active (&pipe_w))
1511 { 1561 {
1512 /* this "locks" the handlers against writing to the pipe */ 1562 /* this "locks" the handlers against writing to the pipe */
1513 /* while we modify the fd vars */ 1563 /* while we modify the fd vars */
1514 gotsig = 1; 1564 gotsig = 1;
1515#if EV_ASYNC_ENABLE 1565#if EV_ASYNC_ENABLE
1516 gotasync = 1; 1566 gotasync = 1;
1517#endif 1567#endif
1518 1568
1519 ev_ref (EV_A); 1569 ev_ref (EV_A);
1520 ev_io_stop (EV_A_ &pipeev); 1570 ev_io_stop (EV_A_ &pipe_w);
1521 1571
1522#if EV_USE_EVENTFD 1572#if EV_USE_EVENTFD
1523 if (evfd >= 0) 1573 if (evfd >= 0)
1524 close (evfd); 1574 close (evfd);
1525#endif 1575#endif
1530 close (evpipe [1]); 1580 close (evpipe [1]);
1531 } 1581 }
1532 1582
1533 evpipe_init (EV_A); 1583 evpipe_init (EV_A);
1534 /* now iterate over everything, in case we missed something */ 1584 /* now iterate over everything, in case we missed something */
1535 pipecb (EV_A_ &pipeev, EV_READ); 1585 pipecb (EV_A_ &pipe_w, EV_READ);
1536 } 1586 }
1537 1587
1538 postfork = 0; 1588 postfork = 0;
1539} 1589}
1540 1590
1736ev_invoke (EV_P_ void *w, int revents) 1786ev_invoke (EV_P_ void *w, int revents)
1737{ 1787{
1738 EV_CB_INVOKE ((W)w, revents); 1788 EV_CB_INVOKE ((W)w, revents);
1739} 1789}
1740 1790
1741inline_speed void 1791void
1742call_pending (EV_P) 1792ev_invoke_pending (EV_P)
1743{ 1793{
1744 int pri; 1794 int pri;
1745 1795
1746 for (pri = NUMPRI; pri--; ) 1796 for (pri = NUMPRI; pri--; )
1747 while (pendingcnt [pri]) 1797 while (pendingcnt [pri])
1748 { 1798 {
1749 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1799 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1750 1800
1751 if (expect_true (p->w))
1752 {
1753 /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ 1801 /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/
1802 /* ^ this is no longer true, as pending_w could be here */
1754 1803
1755 p->w->pending = 0; 1804 p->w->pending = 0;
1756 EV_CB_INVOKE (p->w, p->events); 1805 EV_CB_INVOKE (p->w, p->events);
1757 EV_FREQUENT_CHECK; 1806 EV_FREQUENT_CHECK;
1758 }
1759 } 1807 }
1760} 1808}
1761 1809
1762#if EV_IDLE_ENABLE 1810#if EV_IDLE_ENABLE
1811/* make idle watchers pending. this handles the "call-idle */
1812/* only when higher priorities are idle" logic */
1763inline_size void 1813inline_size void
1764idle_reify (EV_P) 1814idle_reify (EV_P)
1765{ 1815{
1766 if (expect_false (idleall)) 1816 if (expect_false (idleall))
1767 { 1817 {
1780 } 1830 }
1781 } 1831 }
1782} 1832}
1783#endif 1833#endif
1784 1834
1835/* make timers pending */
1785inline_size void 1836inline_size void
1786timers_reify (EV_P) 1837timers_reify (EV_P)
1787{ 1838{
1788 EV_FREQUENT_CHECK; 1839 EV_FREQUENT_CHECK;
1789 1840
1818 feed_reverse_done (EV_A_ EV_TIMEOUT); 1869 feed_reverse_done (EV_A_ EV_TIMEOUT);
1819 } 1870 }
1820} 1871}
1821 1872
1822#if EV_PERIODIC_ENABLE 1873#if EV_PERIODIC_ENABLE
1874/* make periodics pending */
1823inline_size void 1875inline_size void
1824periodics_reify (EV_P) 1876periodics_reify (EV_P)
1825{ 1877{
1826 EV_FREQUENT_CHECK; 1878 EV_FREQUENT_CHECK;
1827 1879
1874 1926
1875 feed_reverse_done (EV_A_ EV_PERIODIC); 1927 feed_reverse_done (EV_A_ EV_PERIODIC);
1876 } 1928 }
1877} 1929}
1878 1930
1931/* simply recalculate all periodics */
1932/* TODO: maybe ensure that at leats one event happens when jumping forward? */
1879static void noinline 1933static void noinline
1880periodics_reschedule (EV_P) 1934periodics_reschedule (EV_P)
1881{ 1935{
1882 int i; 1936 int i;
1883 1937
1896 1950
1897 reheap (periodics, periodiccnt); 1951 reheap (periodics, periodiccnt);
1898} 1952}
1899#endif 1953#endif
1900 1954
1955/* adjust all timers by a given offset */
1901static void noinline 1956static void noinline
1902timers_reschedule (EV_P_ ev_tstamp adjust) 1957timers_reschedule (EV_P_ ev_tstamp adjust)
1903{ 1958{
1904 int i; 1959 int i;
1905 1960
1909 ANHE_w (*he)->at += adjust; 1964 ANHE_w (*he)->at += adjust;
1910 ANHE_at_cache (*he); 1965 ANHE_at_cache (*he);
1911 } 1966 }
1912} 1967}
1913 1968
1969/* fetch new monotonic and realtime times from the kernel */
1970/* also detetc if there was a timejump, and act accordingly */
1914inline_speed void 1971inline_speed void
1915time_update (EV_P_ ev_tstamp max_block) 1972time_update (EV_P_ ev_tstamp max_block)
1916{ 1973{
1917 int i;
1918
1919#if EV_USE_MONOTONIC 1974#if EV_USE_MONOTONIC
1920 if (expect_true (have_monotonic)) 1975 if (expect_true (have_monotonic))
1921 { 1976 {
1977 int i;
1922 ev_tstamp odiff = rtmn_diff; 1978 ev_tstamp odiff = rtmn_diff;
1923 1979
1924 mn_now = get_clock (); 1980 mn_now = get_clock ();
1925 1981
1926 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ 1982 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
1976 2032
1977 mn_now = ev_rt_now; 2033 mn_now = ev_rt_now;
1978 } 2034 }
1979} 2035}
1980 2036
1981static int loop_done;
1982
1983void 2037void
1984ev_loop (EV_P_ int flags) 2038ev_loop (EV_P_ int flags)
1985{ 2039{
2040 ++loop_depth;
2041
1986 loop_done = EVUNLOOP_CANCEL; 2042 loop_done = EVUNLOOP_CANCEL;
1987 2043
1988 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 2044 invoke_cb (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1989 2045
1990 do 2046 do
1991 { 2047 {
1992#if EV_VERIFY >= 2 2048#if EV_VERIFY >= 2
1993 ev_loop_verify (EV_A); 2049 ev_loop_verify (EV_A);
2006 /* we might have forked, so queue fork handlers */ 2062 /* we might have forked, so queue fork handlers */
2007 if (expect_false (postfork)) 2063 if (expect_false (postfork))
2008 if (forkcnt) 2064 if (forkcnt)
2009 { 2065 {
2010 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2066 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2011 call_pending (EV_A); 2067 invoke_cb (EV_A);
2012 } 2068 }
2013#endif 2069#endif
2014 2070
2015 /* queue prepare watchers (and execute them) */ 2071 /* queue prepare watchers (and execute them) */
2016 if (expect_false (preparecnt)) 2072 if (expect_false (preparecnt))
2017 { 2073 {
2018 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2074 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2019 call_pending (EV_A); 2075 invoke_cb (EV_A);
2020 } 2076 }
2021 2077
2022 /* we might have forked, so reify kernel state if necessary */ 2078 /* we might have forked, so reify kernel state if necessary */
2023 if (expect_false (postfork)) 2079 if (expect_false (postfork))
2024 loop_fork (EV_A); 2080 loop_fork (EV_A);
2031 ev_tstamp waittime = 0.; 2087 ev_tstamp waittime = 0.;
2032 ev_tstamp sleeptime = 0.; 2088 ev_tstamp sleeptime = 0.;
2033 2089
2034 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2090 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
2035 { 2091 {
2092 /* remember old timestamp for io_blocktime calculation */
2093 ev_tstamp prev_mn_now = mn_now;
2094
2036 /* update time to cancel out callback processing overhead */ 2095 /* update time to cancel out callback processing overhead */
2037 time_update (EV_A_ 1e100); 2096 time_update (EV_A_ 1e100);
2097
2098 waittime = MAX_BLOCKTIME;
2038 2099
2039 if (timercnt) 2100 if (timercnt)
2040 { 2101 {
2041 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; 2102 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
2042 if (waittime > to) waittime = to; 2103 if (waittime > to) waittime = to;
2048 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; 2109 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
2049 if (waittime > to) waittime = to; 2110 if (waittime > to) waittime = to;
2050 } 2111 }
2051#endif 2112#endif
2052 2113
2114 /* don't let timeouts decrease the waittime below timeout_blocktime */
2053 if (expect_false (waittime < timeout_blocktime)) 2115 if (expect_false (waittime < timeout_blocktime))
2054 waittime = timeout_blocktime; 2116 waittime = timeout_blocktime;
2055 2117
2056 sleeptime = waittime - backend_fudge; 2118 /* extra check because io_blocktime is commonly 0 */
2057
2058 if (expect_true (sleeptime > io_blocktime)) 2119 if (expect_false (io_blocktime))
2059 sleeptime = io_blocktime;
2060
2061 if (sleeptime)
2062 { 2120 {
2121 sleeptime = io_blocktime - (mn_now - prev_mn_now);
2122
2123 if (sleeptime > waittime - backend_fudge)
2124 sleeptime = waittime - backend_fudge;
2125
2126 if (expect_true (sleeptime > 0.))
2127 {
2063 ev_sleep (sleeptime); 2128 ev_sleep (sleeptime);
2064 waittime -= sleeptime; 2129 waittime -= sleeptime;
2130 }
2065 } 2131 }
2066 } 2132 }
2067 2133
2068 ++loop_count; 2134 ++loop_count;
2069 backend_poll (EV_A_ waittime); 2135 backend_poll (EV_A_ waittime);
2085 2151
2086 /* queue check watchers, to be executed first */ 2152 /* queue check watchers, to be executed first */
2087 if (expect_false (checkcnt)) 2153 if (expect_false (checkcnt))
2088 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2154 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2089 2155
2090 call_pending (EV_A); 2156 invoke_cb (EV_A);
2091 } 2157 }
2092 while (expect_true ( 2158 while (expect_true (
2093 activecnt 2159 activecnt
2094 && !loop_done 2160 && !loop_done
2095 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2161 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2096 )); 2162 ));
2097 2163
2098 if (loop_done == EVUNLOOP_ONE) 2164 if (loop_done == EVUNLOOP_ONE)
2099 loop_done = EVUNLOOP_CANCEL; 2165 loop_done = EVUNLOOP_CANCEL;
2166
2167 --loop_depth;
2100} 2168}
2101 2169
2102void 2170void
2103ev_unloop (EV_P_ int how) 2171ev_unloop (EV_P_ int how)
2104{ 2172{
2133ev_resume (EV_P) 2201ev_resume (EV_P)
2134{ 2202{
2135 ev_tstamp mn_prev = mn_now; 2203 ev_tstamp mn_prev = mn_now;
2136 2204
2137 ev_now_update (EV_A); 2205 ev_now_update (EV_A);
2138 printf ("update %f\n", mn_now - mn_prev);//D
2139 timers_reschedule (EV_A_ mn_now - mn_prev); 2206 timers_reschedule (EV_A_ mn_now - mn_prev);
2207#if EV_PERIODIC_ENABLE
2208 /* TODO: really do this? */
2140 periodics_reschedule (EV_A); 2209 periodics_reschedule (EV_A);
2210#endif
2141} 2211}
2142 2212
2143/*****************************************************************************/ 2213/*****************************************************************************/
2214/* singly-linked list management, used when the expected list length is short */
2144 2215
2145inline_size void 2216inline_size void
2146wlist_add (WL *head, WL elem) 2217wlist_add (WL *head, WL elem)
2147{ 2218{
2148 elem->next = *head; 2219 elem->next = *head;
2162 2233
2163 head = &(*head)->next; 2234 head = &(*head)->next;
2164 } 2235 }
2165} 2236}
2166 2237
2238/* internal, faster, version of ev_clear_pending */
2167inline_speed void 2239inline_speed void
2168clear_pending (EV_P_ W w) 2240clear_pending (EV_P_ W w)
2169{ 2241{
2170 if (w->pending) 2242 if (w->pending)
2171 { 2243 {
2172 pendings [ABSPRI (w)][w->pending - 1].w = 0; 2244 pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w;
2173 w->pending = 0; 2245 w->pending = 0;
2174 } 2246 }
2175} 2247}
2176 2248
2177int 2249int
2181 int pending = w_->pending; 2253 int pending = w_->pending;
2182 2254
2183 if (expect_true (pending)) 2255 if (expect_true (pending))
2184 { 2256 {
2185 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; 2257 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
2258 p->w = (W)&pending_w;
2186 w_->pending = 0; 2259 w_->pending = 0;
2187 p->w = 0;
2188 return p->events; 2260 return p->events;
2189 } 2261 }
2190 else 2262 else
2191 return 0; 2263 return 0;
2192} 2264}
2193 2265
2194inline_size void 2266inline_size void
2195pri_adjust (EV_P_ W w) 2267pri_adjust (EV_P_ W w)
2196{ 2268{
2197 int pri = w->priority; 2269 int pri = ev_priority (w);
2198 pri = pri < EV_MINPRI ? EV_MINPRI : pri; 2270 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2199 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; 2271 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2200 w->priority = pri; 2272 ev_set_priority (w, pri);
2201} 2273}
2202 2274
2203inline_speed void 2275inline_speed void
2204ev_start (EV_P_ W w, int active) 2276ev_start (EV_P_ W w, int active)
2205{ 2277{
3198 } 3270 }
3199} 3271}
3200 3272
3201/*****************************************************************************/ 3273/*****************************************************************************/
3202 3274
3203#if 0 3275#if EV_WALK_ENABLE
3204void 3276void
3205ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) 3277ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w))
3206{ 3278{
3207 int i, j; 3279 int i, j;
3208 ev_watcher_list *wl, *wn; 3280 ev_watcher_list *wl, *wn;
3224#if EV_USE_INOTIFY 3296#if EV_USE_INOTIFY
3225 if (ev_cb ((ev_io *)wl) == infy_cb) 3297 if (ev_cb ((ev_io *)wl) == infy_cb)
3226 ; 3298 ;
3227 else 3299 else
3228#endif 3300#endif
3229 if ((ev_io *)wl != &pipeev) 3301 if ((ev_io *)wl != &pipe_w)
3230 if (types & EV_IO) 3302 if (types & EV_IO)
3231 cb (EV_A_ EV_IO, wl); 3303 cb (EV_A_ EV_IO, wl);
3232 3304
3233 wl = wn; 3305 wl = wn;
3234 } 3306 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines