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

Comparing libev/ev.c (file contents):
Revision 1.283 by root, Wed Apr 15 09:51:19 2009 UTC vs.
Revision 1.291 by root, Mon Jun 29 04:44:18 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)
570 struct timeval tv; 581 struct timeval tv;
571 gettimeofday (&tv, 0); 582 gettimeofday (&tv, 0);
572 return tv.tv_sec + tv.tv_usec * 1e-6; 583 return tv.tv_sec + tv.tv_usec * 1e-6;
573} 584}
574 585
575ev_tstamp inline_size 586inline_size ev_tstamp
576get_clock (void) 587get_clock (void)
577{ 588{
578#if EV_USE_MONOTONIC 589#if EV_USE_MONOTONIC
579 if (expect_true (have_monotonic)) 590 if (expect_true (have_monotonic))
580 { 591 {
625 636
626/*****************************************************************************/ 637/*****************************************************************************/
627 638
628#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ 639#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
629 640
630int inline_size 641/* find a suitable new size for the given array, */
642/* hopefully by rounding to a ncie-to-malloc size */
643inline_size int
631array_nextsize (int elem, int cur, int cnt) 644array_nextsize (int elem, int cur, int cnt)
632{ 645{
633 int ncur = cur + 1; 646 int ncur = cur + 1;
634 647
635 do 648 do
680#define array_free(stem, idx) \ 693#define array_free(stem, idx) \
681 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0 694 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0
682 695
683/*****************************************************************************/ 696/*****************************************************************************/
684 697
698/* dummy callback for pending events */
699static void noinline
700pendingcb (EV_P_ ev_prepare *w, int revents)
701{
702}
703
685void noinline 704void noinline
686ev_feed_event (EV_P_ void *w, int revents) 705ev_feed_event (EV_P_ void *w, int revents)
687{ 706{
688 W w_ = (W)w; 707 W w_ = (W)w;
689 int pri = ABSPRI (w_); 708 int pri = ABSPRI (w_);
697 pendings [pri][w_->pending - 1].w = w_; 716 pendings [pri][w_->pending - 1].w = w_;
698 pendings [pri][w_->pending - 1].events = revents; 717 pendings [pri][w_->pending - 1].events = revents;
699 } 718 }
700} 719}
701 720
702void inline_speed 721inline_speed void
722feed_reverse (EV_P_ W w)
723{
724 array_needsize (W, rfeeds, rfeedmax, rfeedcnt + 1, EMPTY2);
725 rfeeds [rfeedcnt++] = w;
726}
727
728inline_size void
729feed_reverse_done (EV_P_ int revents)
730{
731 do
732 ev_feed_event (EV_A_ rfeeds [--rfeedcnt], revents);
733 while (rfeedcnt);
734}
735
736inline_speed void
703queue_events (EV_P_ W *events, int eventcnt, int type) 737queue_events (EV_P_ W *events, int eventcnt, int type)
704{ 738{
705 int i; 739 int i;
706 740
707 for (i = 0; i < eventcnt; ++i) 741 for (i = 0; i < eventcnt; ++i)
708 ev_feed_event (EV_A_ events [i], type); 742 ev_feed_event (EV_A_ events [i], type);
709} 743}
710 744
711/*****************************************************************************/ 745/*****************************************************************************/
712 746
713void inline_speed 747inline_speed void
714fd_event (EV_P_ int fd, int revents) 748fd_event (EV_P_ int fd, int revents)
715{ 749{
716 ANFD *anfd = anfds + fd; 750 ANFD *anfd = anfds + fd;
717 ev_io *w; 751 ev_io *w;
718 752
730{ 764{
731 if (fd >= 0 && fd < anfdmax) 765 if (fd >= 0 && fd < anfdmax)
732 fd_event (EV_A_ fd, revents); 766 fd_event (EV_A_ fd, revents);
733} 767}
734 768
735void inline_size 769/* make sure the external fd watch events are in-sync */
770/* with the kernel/libev internal state */
771inline_size void
736fd_reify (EV_P) 772fd_reify (EV_P)
737{ 773{
738 int i; 774 int i;
739 775
740 for (i = 0; i < fdchangecnt; ++i) 776 for (i = 0; i < fdchangecnt; ++i)
774 } 810 }
775 811
776 fdchangecnt = 0; 812 fdchangecnt = 0;
777} 813}
778 814
779void inline_size 815/* something about the given fd changed */
816inline_size void
780fd_change (EV_P_ int fd, int flags) 817fd_change (EV_P_ int fd, int flags)
781{ 818{
782 unsigned char reify = anfds [fd].reify; 819 unsigned char reify = anfds [fd].reify;
783 anfds [fd].reify |= flags; 820 anfds [fd].reify |= flags;
784 821
788 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 825 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
789 fdchanges [fdchangecnt - 1] = fd; 826 fdchanges [fdchangecnt - 1] = fd;
790 } 827 }
791} 828}
792 829
793void inline_speed 830/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
831inline_speed void
794fd_kill (EV_P_ int fd) 832fd_kill (EV_P_ int fd)
795{ 833{
796 ev_io *w; 834 ev_io *w;
797 835
798 while ((w = (ev_io *)anfds [fd].head)) 836 while ((w = (ev_io *)anfds [fd].head))
800 ev_io_stop (EV_A_ w); 838 ev_io_stop (EV_A_ w);
801 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 839 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
802 } 840 }
803} 841}
804 842
805int inline_size 843/* check whether the given fd is atcually valid, for error recovery */
844inline_size int
806fd_valid (int fd) 845fd_valid (int fd)
807{ 846{
808#ifdef _WIN32 847#ifdef _WIN32
809 return _get_osfhandle (fd) != -1; 848 return _get_osfhandle (fd) != -1;
810#else 849#else
873#define HEAP0 (DHEAP - 1) /* index of first element in heap */ 912#define HEAP0 (DHEAP - 1) /* index of first element in heap */
874#define HPARENT(k) ((((k) - HEAP0 - 1) / DHEAP) + HEAP0) 913#define HPARENT(k) ((((k) - HEAP0 - 1) / DHEAP) + HEAP0)
875#define UPHEAP_DONE(p,k) ((p) == (k)) 914#define UPHEAP_DONE(p,k) ((p) == (k))
876 915
877/* away from the root */ 916/* away from the root */
878void inline_speed 917inline_speed void
879downheap (ANHE *heap, int N, int k) 918downheap (ANHE *heap, int N, int k)
880{ 919{
881 ANHE he = heap [k]; 920 ANHE he = heap [k];
882 ANHE *E = heap + N + HEAP0; 921 ANHE *E = heap + N + HEAP0;
883 922
923#define HEAP0 1 962#define HEAP0 1
924#define HPARENT(k) ((k) >> 1) 963#define HPARENT(k) ((k) >> 1)
925#define UPHEAP_DONE(p,k) (!(p)) 964#define UPHEAP_DONE(p,k) (!(p))
926 965
927/* away from the root */ 966/* away from the root */
928void inline_speed 967inline_speed void
929downheap (ANHE *heap, int N, int k) 968downheap (ANHE *heap, int N, int k)
930{ 969{
931 ANHE he = heap [k]; 970 ANHE he = heap [k];
932 971
933 for (;;) 972 for (;;)
953 ev_active (ANHE_w (he)) = k; 992 ev_active (ANHE_w (he)) = k;
954} 993}
955#endif 994#endif
956 995
957/* towards the root */ 996/* towards the root */
958void inline_speed 997inline_speed void
959upheap (ANHE *heap, int k) 998upheap (ANHE *heap, int k)
960{ 999{
961 ANHE he = heap [k]; 1000 ANHE he = heap [k];
962 1001
963 for (;;) 1002 for (;;)
974 1013
975 heap [k] = he; 1014 heap [k] = he;
976 ev_active (ANHE_w (he)) = k; 1015 ev_active (ANHE_w (he)) = k;
977} 1016}
978 1017
979void inline_size 1018/* move an element suitably so it is in a correct place */
1019inline_size void
980adjustheap (ANHE *heap, int N, int k) 1020adjustheap (ANHE *heap, int N, int k)
981{ 1021{
982 if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) 1022 if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k]))
983 upheap (heap, k); 1023 upheap (heap, k);
984 else 1024 else
985 downheap (heap, N, k); 1025 downheap (heap, N, k);
986} 1026}
987 1027
988/* rebuild the heap: this function is used only once and executed rarely */ 1028/* rebuild the heap: this function is used only once and executed rarely */
989void inline_size 1029inline_size void
990reheap (ANHE *heap, int N) 1030reheap (ANHE *heap, int N)
991{ 1031{
992 int i; 1032 int i;
993 1033
994 /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */ 1034 /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */
997 upheap (heap, i + HEAP0); 1037 upheap (heap, i + HEAP0);
998} 1038}
999 1039
1000/*****************************************************************************/ 1040/*****************************************************************************/
1001 1041
1042/* associate signal watchers to a signal signal */
1002typedef struct 1043typedef struct
1003{ 1044{
1004 WL head; 1045 WL head;
1005 EV_ATOMIC_T gotsig; 1046 EV_ATOMIC_T gotsig;
1006} ANSIG; 1047} ANSIG;
1010 1051
1011static EV_ATOMIC_T gotsig; 1052static EV_ATOMIC_T gotsig;
1012 1053
1013/*****************************************************************************/ 1054/*****************************************************************************/
1014 1055
1015void inline_speed 1056/* used to prepare libev internal fd's */
1057/* this is not fork-safe */
1058inline_speed void
1016fd_intern (int fd) 1059fd_intern (int fd)
1017{ 1060{
1018#ifdef _WIN32 1061#ifdef _WIN32
1019 unsigned long arg = 1; 1062 unsigned long arg = 1;
1020 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 1063 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
1025} 1068}
1026 1069
1027static void noinline 1070static void noinline
1028evpipe_init (EV_P) 1071evpipe_init (EV_P)
1029{ 1072{
1030 if (!ev_is_active (&pipeev)) 1073 if (!ev_is_active (&pipe_w))
1031 { 1074 {
1032#if EV_USE_EVENTFD 1075#if EV_USE_EVENTFD
1033 if ((evfd = eventfd (0, 0)) >= 0) 1076 if ((evfd = eventfd (0, 0)) >= 0)
1034 { 1077 {
1035 evpipe [0] = -1; 1078 evpipe [0] = -1;
1036 fd_intern (evfd); 1079 fd_intern (evfd);
1037 ev_io_set (&pipeev, evfd, EV_READ); 1080 ev_io_set (&pipe_w, evfd, EV_READ);
1038 } 1081 }
1039 else 1082 else
1040#endif 1083#endif
1041 { 1084 {
1042 while (pipe (evpipe)) 1085 while (pipe (evpipe))
1043 ev_syserr ("(libev) error creating signal/async pipe"); 1086 ev_syserr ("(libev) error creating signal/async pipe");
1044 1087
1045 fd_intern (evpipe [0]); 1088 fd_intern (evpipe [0]);
1046 fd_intern (evpipe [1]); 1089 fd_intern (evpipe [1]);
1047 ev_io_set (&pipeev, evpipe [0], EV_READ); 1090 ev_io_set (&pipe_w, evpipe [0], EV_READ);
1048 } 1091 }
1049 1092
1050 ev_io_start (EV_A_ &pipeev); 1093 ev_io_start (EV_A_ &pipe_w);
1051 ev_unref (EV_A); /* watcher should not keep loop alive */ 1094 ev_unref (EV_A); /* watcher should not keep loop alive */
1052 } 1095 }
1053} 1096}
1054 1097
1055void inline_size 1098inline_size void
1056evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1099evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1057{ 1100{
1058 if (!*flag) 1101 if (!*flag)
1059 { 1102 {
1060 int old_errno = errno; /* save errno because write might clobber it */ 1103 int old_errno = errno; /* save errno because write might clobber it */
1073 1116
1074 errno = old_errno; 1117 errno = old_errno;
1075 } 1118 }
1076} 1119}
1077 1120
1121/* called whenever the libev signal pipe */
1122/* got some events (signal, async) */
1078static void 1123static void
1079pipecb (EV_P_ ev_io *iow, int revents) 1124pipecb (EV_P_ ev_io *iow, int revents)
1080{ 1125{
1081#if EV_USE_EVENTFD 1126#if EV_USE_EVENTFD
1082 if (evfd >= 0) 1127 if (evfd >= 0)
1164 1209
1165#ifndef WIFCONTINUED 1210#ifndef WIFCONTINUED
1166# define WIFCONTINUED(status) 0 1211# define WIFCONTINUED(status) 0
1167#endif 1212#endif
1168 1213
1169void inline_speed 1214/* handle a single child status event */
1215inline_speed void
1170child_reap (EV_P_ int chain, int pid, int status) 1216child_reap (EV_P_ int chain, int pid, int status)
1171{ 1217{
1172 ev_child *w; 1218 ev_child *w;
1173 int traced = WIFSTOPPED (status) || WIFCONTINUED (status); 1219 int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
1174 1220
1187 1233
1188#ifndef WCONTINUED 1234#ifndef WCONTINUED
1189# define WCONTINUED 0 1235# define WCONTINUED 0
1190#endif 1236#endif
1191 1237
1238/* called on sigchld etc., calls waitpid */
1192static void 1239static void
1193childcb (EV_P_ ev_signal *sw, int revents) 1240childcb (EV_P_ ev_signal *sw, int revents)
1194{ 1241{
1195 int pid, status; 1242 int pid, status;
1196 1243
1321ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 1368ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1322{ 1369{
1323 timeout_blocktime = interval; 1370 timeout_blocktime = interval;
1324} 1371}
1325 1372
1373/* initialise a loop structure, must be zero-initialised */
1326static void noinline 1374static void noinline
1327loop_init (EV_P_ unsigned int flags) 1375loop_init (EV_P_ unsigned int flags)
1328{ 1376{
1329 if (!backend) 1377 if (!backend)
1330 { 1378 {
1390#endif 1438#endif
1391#if EV_USE_SELECT 1439#if EV_USE_SELECT
1392 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); 1440 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
1393#endif 1441#endif
1394 1442
1443 ev_prepare_init (&pending_w, pendingcb);
1444
1395 ev_init (&pipeev, pipecb); 1445 ev_init (&pipe_w, pipecb);
1396 ev_set_priority (&pipeev, EV_MAXPRI); 1446 ev_set_priority (&pipe_w, EV_MAXPRI);
1397 } 1447 }
1398} 1448}
1399 1449
1450/* free up a loop structure */
1400static void noinline 1451static void noinline
1401loop_destroy (EV_P) 1452loop_destroy (EV_P)
1402{ 1453{
1403 int i; 1454 int i;
1404 1455
1405 if (ev_is_active (&pipeev)) 1456 if (ev_is_active (&pipe_w))
1406 { 1457 {
1407 ev_ref (EV_A); /* signal watcher */ 1458 ev_ref (EV_A); /* signal watcher */
1408 ev_io_stop (EV_A_ &pipeev); 1459 ev_io_stop (EV_A_ &pipe_w);
1409 1460
1410#if EV_USE_EVENTFD 1461#if EV_USE_EVENTFD
1411 if (evfd >= 0) 1462 if (evfd >= 0)
1412 close (evfd); 1463 close (evfd);
1413#endif 1464#endif
1452 } 1503 }
1453 1504
1454 ev_free (anfds); anfdmax = 0; 1505 ev_free (anfds); anfdmax = 0;
1455 1506
1456 /* have to use the microsoft-never-gets-it-right macro */ 1507 /* have to use the microsoft-never-gets-it-right macro */
1508 array_free (rfeed, EMPTY);
1457 array_free (fdchange, EMPTY); 1509 array_free (fdchange, EMPTY);
1458 array_free (timer, EMPTY); 1510 array_free (timer, EMPTY);
1459#if EV_PERIODIC_ENABLE 1511#if EV_PERIODIC_ENABLE
1460 array_free (periodic, EMPTY); 1512 array_free (periodic, EMPTY);
1461#endif 1513#endif
1470 1522
1471 backend = 0; 1523 backend = 0;
1472} 1524}
1473 1525
1474#if EV_USE_INOTIFY 1526#if EV_USE_INOTIFY
1475void inline_size infy_fork (EV_P); 1527inline_size void infy_fork (EV_P);
1476#endif 1528#endif
1477 1529
1478void inline_size 1530inline_size void
1479loop_fork (EV_P) 1531loop_fork (EV_P)
1480{ 1532{
1481#if EV_USE_PORT 1533#if EV_USE_PORT
1482 if (backend == EVBACKEND_PORT ) port_fork (EV_A); 1534 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
1483#endif 1535#endif
1489#endif 1541#endif
1490#if EV_USE_INOTIFY 1542#if EV_USE_INOTIFY
1491 infy_fork (EV_A); 1543 infy_fork (EV_A);
1492#endif 1544#endif
1493 1545
1494 if (ev_is_active (&pipeev)) 1546 if (ev_is_active (&pipe_w))
1495 { 1547 {
1496 /* this "locks" the handlers against writing to the pipe */ 1548 /* this "locks" the handlers against writing to the pipe */
1497 /* while we modify the fd vars */ 1549 /* while we modify the fd vars */
1498 gotsig = 1; 1550 gotsig = 1;
1499#if EV_ASYNC_ENABLE 1551#if EV_ASYNC_ENABLE
1500 gotasync = 1; 1552 gotasync = 1;
1501#endif 1553#endif
1502 1554
1503 ev_ref (EV_A); 1555 ev_ref (EV_A);
1504 ev_io_stop (EV_A_ &pipeev); 1556 ev_io_stop (EV_A_ &pipe_w);
1505 1557
1506#if EV_USE_EVENTFD 1558#if EV_USE_EVENTFD
1507 if (evfd >= 0) 1559 if (evfd >= 0)
1508 close (evfd); 1560 close (evfd);
1509#endif 1561#endif
1514 close (evpipe [1]); 1566 close (evpipe [1]);
1515 } 1567 }
1516 1568
1517 evpipe_init (EV_A); 1569 evpipe_init (EV_A);
1518 /* now iterate over everything, in case we missed something */ 1570 /* now iterate over everything, in case we missed something */
1519 pipecb (EV_A_ &pipeev, EV_READ); 1571 pipecb (EV_A_ &pipe_w, EV_READ);
1520 } 1572 }
1521 1573
1522 postfork = 0; 1574 postfork = 0;
1523} 1575}
1524 1576
1720ev_invoke (EV_P_ void *w, int revents) 1772ev_invoke (EV_P_ void *w, int revents)
1721{ 1773{
1722 EV_CB_INVOKE ((W)w, revents); 1774 EV_CB_INVOKE ((W)w, revents);
1723} 1775}
1724 1776
1725void inline_speed 1777inline_speed void
1726call_pending (EV_P) 1778call_pending (EV_P)
1727{ 1779{
1728 int pri; 1780 int pri;
1729 1781
1730 for (pri = NUMPRI; pri--; ) 1782 for (pri = NUMPRI; pri--; )
1731 while (pendingcnt [pri]) 1783 while (pendingcnt [pri])
1732 { 1784 {
1733 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1785 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1734 1786
1735 if (expect_true (p->w))
1736 {
1737 /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ 1787 /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/
1788 /* ^ this is no longer true, as pending_w could be here */
1738 1789
1739 p->w->pending = 0; 1790 p->w->pending = 0;
1740 EV_CB_INVOKE (p->w, p->events); 1791 EV_CB_INVOKE (p->w, p->events);
1741 EV_FREQUENT_CHECK; 1792 EV_FREQUENT_CHECK;
1742 }
1743 } 1793 }
1744} 1794}
1745 1795
1746#if EV_IDLE_ENABLE 1796#if EV_IDLE_ENABLE
1747void inline_size 1797/* make idle watchers pending. this handles the "call-idle */
1798/* only when higher priorities are idle" logic */
1799inline_size void
1748idle_reify (EV_P) 1800idle_reify (EV_P)
1749{ 1801{
1750 if (expect_false (idleall)) 1802 if (expect_false (idleall))
1751 { 1803 {
1752 int pri; 1804 int pri;
1764 } 1816 }
1765 } 1817 }
1766} 1818}
1767#endif 1819#endif
1768 1820
1769void inline_size 1821/* make timers pending */
1822inline_size void
1770timers_reify (EV_P) 1823timers_reify (EV_P)
1771{ 1824{
1772 EV_FREQUENT_CHECK; 1825 EV_FREQUENT_CHECK;
1773 1826
1774 while (timercnt && ANHE_at (timers [HEAP0]) < mn_now) 1827 if (timercnt && ANHE_at (timers [HEAP0]) < mn_now)
1775 { 1828 {
1776 ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]); 1829 do
1777
1778 /*assert (("libev: inactive timer on timer heap detected", ev_is_active (w)));*/
1779
1780 /* first reschedule or stop timer */
1781 if (w->repeat)
1782 { 1830 {
1831 ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
1832
1833 /*assert (("libev: inactive timer on timer heap detected", ev_is_active (w)));*/
1834
1835 /* first reschedule or stop timer */
1836 if (w->repeat)
1837 {
1783 ev_at (w) += w->repeat; 1838 ev_at (w) += w->repeat;
1784 if (ev_at (w) < mn_now) 1839 if (ev_at (w) < mn_now)
1785 ev_at (w) = mn_now; 1840 ev_at (w) = mn_now;
1786 1841
1787 assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 1842 assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1788 1843
1789 ANHE_at_cache (timers [HEAP0]); 1844 ANHE_at_cache (timers [HEAP0]);
1790 downheap (timers, timercnt, HEAP0); 1845 downheap (timers, timercnt, HEAP0);
1846 }
1847 else
1848 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1849
1850 EV_FREQUENT_CHECK;
1851 feed_reverse (EV_A_ (W)w);
1791 } 1852 }
1792 else 1853 while (timercnt && ANHE_at (timers [HEAP0]) < mn_now);
1793 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1794 1854
1795 EV_FREQUENT_CHECK;
1796 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1855 feed_reverse_done (EV_A_ EV_TIMEOUT);
1797 } 1856 }
1798} 1857}
1799 1858
1800#if EV_PERIODIC_ENABLE 1859#if EV_PERIODIC_ENABLE
1801void inline_size 1860/* make periodics pending */
1861inline_size void
1802periodics_reify (EV_P) 1862periodics_reify (EV_P)
1803{ 1863{
1804 EV_FREQUENT_CHECK; 1864 EV_FREQUENT_CHECK;
1805 1865
1806 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) 1866 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
1807 { 1867 {
1808 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); 1868 int feed_count = 0;
1809 1869
1810 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/ 1870 do
1811
1812 /* first reschedule or stop timer */
1813 if (w->reschedule_cb)
1814 { 1871 {
1872 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1873
1874 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/
1875
1876 /* first reschedule or stop timer */
1877 if (w->reschedule_cb)
1878 {
1815 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 1879 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1816 1880
1817 assert (("libev: ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now)); 1881 assert (("libev: ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now));
1818 1882
1819 ANHE_at_cache (periodics [HEAP0]); 1883 ANHE_at_cache (periodics [HEAP0]);
1820 downheap (periodics, periodiccnt, HEAP0); 1884 downheap (periodics, periodiccnt, HEAP0);
1885 }
1886 else if (w->interval)
1887 {
1888 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1889 /* if next trigger time is not sufficiently in the future, put it there */
1890 /* this might happen because of floating point inexactness */
1891 if (ev_at (w) - ev_rt_now < TIME_EPSILON)
1892 {
1893 ev_at (w) += w->interval;
1894
1895 /* if interval is unreasonably low we might still have a time in the past */
1896 /* so correct this. this will make the periodic very inexact, but the user */
1897 /* has effectively asked to get triggered more often than possible */
1898 if (ev_at (w) < ev_rt_now)
1899 ev_at (w) = ev_rt_now;
1900 }
1901
1902 ANHE_at_cache (periodics [HEAP0]);
1903 downheap (periodics, periodiccnt, HEAP0);
1904 }
1905 else
1906 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1907
1908 EV_FREQUENT_CHECK;
1909 feed_reverse (EV_A_ (W)w);
1821 } 1910 }
1822 else if (w->interval) 1911 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now);
1823 {
1824 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1825 /* if next trigger time is not sufficiently in the future, put it there */
1826 /* this might happen because of floating point inexactness */
1827 if (ev_at (w) - ev_rt_now < TIME_EPSILON)
1828 {
1829 ev_at (w) += w->interval;
1830 1912
1831 /* if interval is unreasonably low we might still have a time in the past */
1832 /* so correct this. this will make the periodic very inexact, but the user */
1833 /* has effectively asked to get triggered more often than possible */
1834 if (ev_at (w) < ev_rt_now)
1835 ev_at (w) = ev_rt_now;
1836 }
1837
1838 ANHE_at_cache (periodics [HEAP0]);
1839 downheap (periodics, periodiccnt, HEAP0);
1840 }
1841 else
1842 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1843
1844 EV_FREQUENT_CHECK;
1845 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1913 feed_reverse_done (EV_A_ EV_PERIODIC);
1846 } 1914 }
1847} 1915}
1848 1916
1917/* simply recalculate all periodics */
1918/* TODO: maybe ensure that at leats one event happens when jumping forward? */
1849static void noinline 1919static void noinline
1850periodics_reschedule (EV_P) 1920periodics_reschedule (EV_P)
1851{ 1921{
1852 int i; 1922 int i;
1853 1923
1866 1936
1867 reheap (periodics, periodiccnt); 1937 reheap (periodics, periodiccnt);
1868} 1938}
1869#endif 1939#endif
1870 1940
1871void inline_speed 1941/* adjust all timers by a given offset */
1942static void noinline
1943timers_reschedule (EV_P_ ev_tstamp adjust)
1944{
1945 int i;
1946
1947 for (i = 0; i < timercnt; ++i)
1948 {
1949 ANHE *he = timers + i + HEAP0;
1950 ANHE_w (*he)->at += adjust;
1951 ANHE_at_cache (*he);
1952 }
1953}
1954
1955/* fetch new monotonic and realtime times from the kernel */
1956/* also detetc if there was a timejump, and act accordingly */
1957inline_speed void
1872time_update (EV_P_ ev_tstamp max_block) 1958time_update (EV_P_ ev_tstamp max_block)
1873{ 1959{
1874 int i;
1875
1876#if EV_USE_MONOTONIC 1960#if EV_USE_MONOTONIC
1877 if (expect_true (have_monotonic)) 1961 if (expect_true (have_monotonic))
1878 { 1962 {
1963 int i;
1879 ev_tstamp odiff = rtmn_diff; 1964 ev_tstamp odiff = rtmn_diff;
1880 1965
1881 mn_now = get_clock (); 1966 mn_now = get_clock ();
1882 1967
1883 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ 1968 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
1909 ev_rt_now = ev_time (); 1994 ev_rt_now = ev_time ();
1910 mn_now = get_clock (); 1995 mn_now = get_clock ();
1911 now_floor = mn_now; 1996 now_floor = mn_now;
1912 } 1997 }
1913 1998
1999 /* no timer adjustment, as the monotonic clock doesn't jump */
2000 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1914# if EV_PERIODIC_ENABLE 2001# if EV_PERIODIC_ENABLE
1915 periodics_reschedule (EV_A); 2002 periodics_reschedule (EV_A);
1916# endif 2003# endif
1917 /* no timer adjustment, as the monotonic clock doesn't jump */
1918 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1919 } 2004 }
1920 else 2005 else
1921#endif 2006#endif
1922 { 2007 {
1923 ev_rt_now = ev_time (); 2008 ev_rt_now = ev_time ();
1924 2009
1925 if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) 2010 if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP))
1926 { 2011 {
2012 /* adjust timers. this is easy, as the offset is the same for all of them */
2013 timers_reschedule (EV_A_ ev_rt_now - mn_now);
1927#if EV_PERIODIC_ENABLE 2014#if EV_PERIODIC_ENABLE
1928 periodics_reschedule (EV_A); 2015 periodics_reschedule (EV_A);
1929#endif 2016#endif
1930 /* adjust timers. this is easy, as the offset is the same for all of them */
1931 for (i = 0; i < timercnt; ++i)
1932 {
1933 ANHE *he = timers + i + HEAP0;
1934 ANHE_w (*he)->at += ev_rt_now - mn_now;
1935 ANHE_at_cache (*he);
1936 }
1937 } 2017 }
1938 2018
1939 mn_now = ev_rt_now; 2019 mn_now = ev_rt_now;
1940 } 2020 }
1941}
1942
1943void
1944ev_ref (EV_P)
1945{
1946 ++activecnt;
1947}
1948
1949void
1950ev_unref (EV_P)
1951{
1952 --activecnt;
1953}
1954
1955void
1956ev_now_update (EV_P)
1957{
1958 time_update (EV_A_ 1e100);
1959} 2021}
1960 2022
1961static int loop_done; 2023static int loop_done;
1962 2024
1963void 2025void
2085ev_unloop (EV_P_ int how) 2147ev_unloop (EV_P_ int how)
2086{ 2148{
2087 loop_done = how; 2149 loop_done = how;
2088} 2150}
2089 2151
2152void
2153ev_ref (EV_P)
2154{
2155 ++activecnt;
2156}
2157
2158void
2159ev_unref (EV_P)
2160{
2161 --activecnt;
2162}
2163
2164void
2165ev_now_update (EV_P)
2166{
2167 time_update (EV_A_ 1e100);
2168}
2169
2170void
2171ev_suspend (EV_P)
2172{
2173 ev_now_update (EV_A);
2174}
2175
2176void
2177ev_resume (EV_P)
2178{
2179 ev_tstamp mn_prev = mn_now;
2180
2181 ev_now_update (EV_A);
2182 timers_reschedule (EV_A_ mn_now - mn_prev);
2183#if EV_PERIODIC_ENABLE
2184 /* TODO: really do this? */
2185 periodics_reschedule (EV_A);
2186#endif
2187}
2188
2090/*****************************************************************************/ 2189/*****************************************************************************/
2190/* singly-linked list management, used when the expected list length is short */
2091 2191
2092void inline_size 2192inline_size void
2093wlist_add (WL *head, WL elem) 2193wlist_add (WL *head, WL elem)
2094{ 2194{
2095 elem->next = *head; 2195 elem->next = *head;
2096 *head = elem; 2196 *head = elem;
2097} 2197}
2098 2198
2099void inline_size 2199inline_size void
2100wlist_del (WL *head, WL elem) 2200wlist_del (WL *head, WL elem)
2101{ 2201{
2102 while (*head) 2202 while (*head)
2103 { 2203 {
2104 if (*head == elem) 2204 if (*head == elem)
2109 2209
2110 head = &(*head)->next; 2210 head = &(*head)->next;
2111 } 2211 }
2112} 2212}
2113 2213
2114void inline_speed 2214/* internal, faster, version of ev_clear_pending */
2215inline_speed void
2115clear_pending (EV_P_ W w) 2216clear_pending (EV_P_ W w)
2116{ 2217{
2117 if (w->pending) 2218 if (w->pending)
2118 { 2219 {
2119 pendings [ABSPRI (w)][w->pending - 1].w = 0; 2220 pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w;
2120 w->pending = 0; 2221 w->pending = 0;
2121 } 2222 }
2122} 2223}
2123 2224
2124int 2225int
2128 int pending = w_->pending; 2229 int pending = w_->pending;
2129 2230
2130 if (expect_true (pending)) 2231 if (expect_true (pending))
2131 { 2232 {
2132 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; 2233 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
2234 p->w = (W)&pending_w;
2133 w_->pending = 0; 2235 w_->pending = 0;
2134 p->w = 0;
2135 return p->events; 2236 return p->events;
2136 } 2237 }
2137 else 2238 else
2138 return 0; 2239 return 0;
2139} 2240}
2140 2241
2141void inline_size 2242inline_size void
2142pri_adjust (EV_P_ W w) 2243pri_adjust (EV_P_ W w)
2143{ 2244{
2144 int pri = w->priority; 2245 int pri = w->priority;
2145 pri = pri < EV_MINPRI ? EV_MINPRI : pri; 2246 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2146 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; 2247 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2147 w->priority = pri; 2248 w->priority = pri;
2148} 2249}
2149 2250
2150void inline_speed 2251inline_speed void
2151ev_start (EV_P_ W w, int active) 2252ev_start (EV_P_ W w, int active)
2152{ 2253{
2153 pri_adjust (EV_A_ w); 2254 pri_adjust (EV_A_ w);
2154 w->active = active; 2255 w->active = active;
2155 ev_ref (EV_A); 2256 ev_ref (EV_A);
2156} 2257}
2157 2258
2158void inline_size 2259inline_size void
2159ev_stop (EV_P_ W w) 2260ev_stop (EV_P_ W w)
2160{ 2261{
2161 ev_unref (EV_A); 2262 ev_unref (EV_A);
2162 w->active = 0; 2263 w->active = 0;
2163} 2264}
2588 2689
2589 for (ofs = 0; ofs < len; ofs += sizeof (struct inotify_event) + ev->len) 2690 for (ofs = 0; ofs < len; ofs += sizeof (struct inotify_event) + ev->len)
2590 infy_wd (EV_A_ ev->wd, ev->wd, ev); 2691 infy_wd (EV_A_ ev->wd, ev->wd, ev);
2591} 2692}
2592 2693
2593void inline_size 2694inline_size void
2594check_2625 (EV_P) 2695check_2625 (EV_P)
2595{ 2696{
2596 /* kernels < 2.6.25 are borked 2697 /* kernels < 2.6.25 are borked
2597 * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html 2698 * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html
2598 */ 2699 */
2611 return; 2712 return;
2612 2713
2613 fs_2625 = 1; 2714 fs_2625 = 1;
2614} 2715}
2615 2716
2616void inline_size 2717inline_size void
2617infy_init (EV_P) 2718infy_init (EV_P)
2618{ 2719{
2619 if (fs_fd != -2) 2720 if (fs_fd != -2)
2620 return; 2721 return;
2621 2722
2631 ev_set_priority (&fs_w, EV_MAXPRI); 2732 ev_set_priority (&fs_w, EV_MAXPRI);
2632 ev_io_start (EV_A_ &fs_w); 2733 ev_io_start (EV_A_ &fs_w);
2633 } 2734 }
2634} 2735}
2635 2736
2636void inline_size 2737inline_size void
2637infy_fork (EV_P) 2738infy_fork (EV_P)
2638{ 2739{
2639 int slot; 2740 int slot;
2640 2741
2641 if (fs_fd < 0) 2742 if (fs_fd < 0)
3145 } 3246 }
3146} 3247}
3147 3248
3148/*****************************************************************************/ 3249/*****************************************************************************/
3149 3250
3150#if 0 3251#if EV_WALK_ENABLE
3151void 3252void
3152ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) 3253ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w))
3153{ 3254{
3154 int i, j; 3255 int i, j;
3155 ev_watcher_list *wl, *wn; 3256 ev_watcher_list *wl, *wn;
3171#if EV_USE_INOTIFY 3272#if EV_USE_INOTIFY
3172 if (ev_cb ((ev_io *)wl) == infy_cb) 3273 if (ev_cb ((ev_io *)wl) == infy_cb)
3173 ; 3274 ;
3174 else 3275 else
3175#endif 3276#endif
3176 if ((ev_io *)wl != &pipeev) 3277 if ((ev_io *)wl != &pipe_w)
3177 if (types & EV_IO) 3278 if (types & EV_IO)
3178 cb (EV_A_ EV_IO, wl); 3279 cb (EV_A_ EV_IO, wl);
3179 3280
3180 wl = wn; 3281 wl = wn;
3181 } 3282 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines