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

Comparing libev/ev.c (file contents):
Revision 1.281 by root, Mon Mar 16 21:15:06 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
575ev_tstamp inline_size 588inline_size ev_tstamp
576get_clock (void) 589get_clock (void)
577{ 590{
578#if EV_USE_MONOTONIC 591#if EV_USE_MONOTONIC
579 if (expect_true (have_monotonic)) 592 if (expect_true (have_monotonic))
580 { 593 {
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
630int inline_size 643/* find a suitable new size for the given array, */
644/* hopefully by rounding to a ncie-to-malloc size */
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
635 do 650 do
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_);
697 pendings [pri][w_->pending - 1].w = w_; 718 pendings [pri][w_->pending - 1].w = w_;
698 pendings [pri][w_->pending - 1].events = revents; 719 pendings [pri][w_->pending - 1].events = revents;
699 } 720 }
700} 721}
701 722
702void inline_speed 723inline_speed void
724feed_reverse (EV_P_ W w)
725{
726 array_needsize (W, rfeeds, rfeedmax, rfeedcnt + 1, EMPTY2);
727 rfeeds [rfeedcnt++] = w;
728}
729
730inline_size void
731feed_reverse_done (EV_P_ int revents)
732{
733 do
734 ev_feed_event (EV_A_ rfeeds [--rfeedcnt], revents);
735 while (rfeedcnt);
736}
737
738inline_speed void
703queue_events (EV_P_ W *events, int eventcnt, int type) 739queue_events (EV_P_ W *events, int eventcnt, int type)
704{ 740{
705 int i; 741 int i;
706 742
707 for (i = 0; i < eventcnt; ++i) 743 for (i = 0; i < eventcnt; ++i)
708 ev_feed_event (EV_A_ events [i], type); 744 ev_feed_event (EV_A_ events [i], type);
709} 745}
710 746
711/*****************************************************************************/ 747/*****************************************************************************/
712 748
713void inline_speed 749inline_speed void
714fd_event (EV_P_ int fd, int revents) 750fd_event (EV_P_ int fd, int revents)
715{ 751{
716 ANFD *anfd = anfds + fd; 752 ANFD *anfd = anfds + fd;
717 ev_io *w; 753 ev_io *w;
718 754
730{ 766{
731 if (fd >= 0 && fd < anfdmax) 767 if (fd >= 0 && fd < anfdmax)
732 fd_event (EV_A_ fd, revents); 768 fd_event (EV_A_ fd, revents);
733} 769}
734 770
735void inline_size 771/* make sure the external fd watch events are in-sync */
772/* with the kernel/libev internal state */
773inline_size void
736fd_reify (EV_P) 774fd_reify (EV_P)
737{ 775{
738 int i; 776 int i;
739 777
740 for (i = 0; i < fdchangecnt; ++i) 778 for (i = 0; i < fdchangecnt; ++i)
774 } 812 }
775 813
776 fdchangecnt = 0; 814 fdchangecnt = 0;
777} 815}
778 816
779void inline_size 817/* something about the given fd changed */
818inline_size void
780fd_change (EV_P_ int fd, int flags) 819fd_change (EV_P_ int fd, int flags)
781{ 820{
782 unsigned char reify = anfds [fd].reify; 821 unsigned char reify = anfds [fd].reify;
783 anfds [fd].reify |= flags; 822 anfds [fd].reify |= flags;
784 823
788 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 827 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
789 fdchanges [fdchangecnt - 1] = fd; 828 fdchanges [fdchangecnt - 1] = fd;
790 } 829 }
791} 830}
792 831
793void inline_speed 832/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
833inline_speed void
794fd_kill (EV_P_ int fd) 834fd_kill (EV_P_ int fd)
795{ 835{
796 ev_io *w; 836 ev_io *w;
797 837
798 while ((w = (ev_io *)anfds [fd].head)) 838 while ((w = (ev_io *)anfds [fd].head))
800 ev_io_stop (EV_A_ w); 840 ev_io_stop (EV_A_ w);
801 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);
802 } 842 }
803} 843}
804 844
805int inline_size 845/* check whether the given fd is atcually valid, for error recovery */
846inline_size int
806fd_valid (int fd) 847fd_valid (int fd)
807{ 848{
808#ifdef _WIN32 849#ifdef _WIN32
809 return _get_osfhandle (fd) != -1; 850 return _get_osfhandle (fd) != -1;
810#else 851#else
873#define HEAP0 (DHEAP - 1) /* index of first element in heap */ 914#define HEAP0 (DHEAP - 1) /* index of first element in heap */
874#define HPARENT(k) ((((k) - HEAP0 - 1) / DHEAP) + HEAP0) 915#define HPARENT(k) ((((k) - HEAP0 - 1) / DHEAP) + HEAP0)
875#define UPHEAP_DONE(p,k) ((p) == (k)) 916#define UPHEAP_DONE(p,k) ((p) == (k))
876 917
877/* away from the root */ 918/* away from the root */
878void inline_speed 919inline_speed void
879downheap (ANHE *heap, int N, int k) 920downheap (ANHE *heap, int N, int k)
880{ 921{
881 ANHE he = heap [k]; 922 ANHE he = heap [k];
882 ANHE *E = heap + N + HEAP0; 923 ANHE *E = heap + N + HEAP0;
883 924
923#define HEAP0 1 964#define HEAP0 1
924#define HPARENT(k) ((k) >> 1) 965#define HPARENT(k) ((k) >> 1)
925#define UPHEAP_DONE(p,k) (!(p)) 966#define UPHEAP_DONE(p,k) (!(p))
926 967
927/* away from the root */ 968/* away from the root */
928void inline_speed 969inline_speed void
929downheap (ANHE *heap, int N, int k) 970downheap (ANHE *heap, int N, int k)
930{ 971{
931 ANHE he = heap [k]; 972 ANHE he = heap [k];
932 973
933 for (;;) 974 for (;;)
953 ev_active (ANHE_w (he)) = k; 994 ev_active (ANHE_w (he)) = k;
954} 995}
955#endif 996#endif
956 997
957/* towards the root */ 998/* towards the root */
958void inline_speed 999inline_speed void
959upheap (ANHE *heap, int k) 1000upheap (ANHE *heap, int k)
960{ 1001{
961 ANHE he = heap [k]; 1002 ANHE he = heap [k];
962 1003
963 for (;;) 1004 for (;;)
974 1015
975 heap [k] = he; 1016 heap [k] = he;
976 ev_active (ANHE_w (he)) = k; 1017 ev_active (ANHE_w (he)) = k;
977} 1018}
978 1019
979void inline_size 1020/* move an element suitably so it is in a correct place */
1021inline_size void
980adjustheap (ANHE *heap, int N, int k) 1022adjustheap (ANHE *heap, int N, int k)
981{ 1023{
982 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]))
983 upheap (heap, k); 1025 upheap (heap, k);
984 else 1026 else
985 downheap (heap, N, k); 1027 downheap (heap, N, k);
986} 1028}
987 1029
988/* rebuild the heap: this function is used only once and executed rarely */ 1030/* rebuild the heap: this function is used only once and executed rarely */
989void inline_size 1031inline_size void
990reheap (ANHE *heap, int N) 1032reheap (ANHE *heap, int N)
991{ 1033{
992 int i; 1034 int i;
993 1035
994 /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */ 1036 /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */
997 upheap (heap, i + HEAP0); 1039 upheap (heap, i + HEAP0);
998} 1040}
999 1041
1000/*****************************************************************************/ 1042/*****************************************************************************/
1001 1043
1044/* associate signal watchers to a signal signal */
1002typedef struct 1045typedef struct
1003{ 1046{
1004 WL head; 1047 WL head;
1005 EV_ATOMIC_T gotsig; 1048 EV_ATOMIC_T gotsig;
1006} ANSIG; 1049} ANSIG;
1010 1053
1011static EV_ATOMIC_T gotsig; 1054static EV_ATOMIC_T gotsig;
1012 1055
1013/*****************************************************************************/ 1056/*****************************************************************************/
1014 1057
1015void inline_speed 1058/* used to prepare libev internal fd's */
1059/* this is not fork-safe */
1060inline_speed void
1016fd_intern (int fd) 1061fd_intern (int fd)
1017{ 1062{
1018#ifdef _WIN32 1063#ifdef _WIN32
1019 unsigned long arg = 1; 1064 unsigned long arg = 1;
1020 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 1065 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
1025} 1070}
1026 1071
1027static void noinline 1072static void noinline
1028evpipe_init (EV_P) 1073evpipe_init (EV_P)
1029{ 1074{
1030 if (!ev_is_active (&pipeev)) 1075 if (!ev_is_active (&pipe_w))
1031 { 1076 {
1032#if EV_USE_EVENTFD 1077#if EV_USE_EVENTFD
1033 if ((evfd = eventfd (0, 0)) >= 0) 1078 if ((evfd = eventfd (0, 0)) >= 0)
1034 { 1079 {
1035 evpipe [0] = -1; 1080 evpipe [0] = -1;
1036 fd_intern (evfd); 1081 fd_intern (evfd);
1037 ev_io_set (&pipeev, evfd, EV_READ); 1082 ev_io_set (&pipe_w, evfd, EV_READ);
1038 } 1083 }
1039 else 1084 else
1040#endif 1085#endif
1041 { 1086 {
1042 while (pipe (evpipe)) 1087 while (pipe (evpipe))
1043 ev_syserr ("(libev) error creating signal/async pipe"); 1088 ev_syserr ("(libev) error creating signal/async pipe");
1044 1089
1045 fd_intern (evpipe [0]); 1090 fd_intern (evpipe [0]);
1046 fd_intern (evpipe [1]); 1091 fd_intern (evpipe [1]);
1047 ev_io_set (&pipeev, evpipe [0], EV_READ); 1092 ev_io_set (&pipe_w, evpipe [0], EV_READ);
1048 } 1093 }
1049 1094
1050 ev_io_start (EV_A_ &pipeev); 1095 ev_io_start (EV_A_ &pipe_w);
1051 ev_unref (EV_A); /* watcher should not keep loop alive */ 1096 ev_unref (EV_A); /* watcher should not keep loop alive */
1052 } 1097 }
1053} 1098}
1054 1099
1055void inline_size 1100inline_size void
1056evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1101evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1057{ 1102{
1058 if (!*flag) 1103 if (!*flag)
1059 { 1104 {
1060 int old_errno = errno; /* save errno because write might clobber it */ 1105 int old_errno = errno; /* save errno because write might clobber it */
1073 1118
1074 errno = old_errno; 1119 errno = old_errno;
1075 } 1120 }
1076} 1121}
1077 1122
1123/* called whenever the libev signal pipe */
1124/* got some events (signal, async) */
1078static void 1125static void
1079pipecb (EV_P_ ev_io *iow, int revents) 1126pipecb (EV_P_ ev_io *iow, int revents)
1080{ 1127{
1081#if EV_USE_EVENTFD 1128#if EV_USE_EVENTFD
1082 if (evfd >= 0) 1129 if (evfd >= 0)
1164 1211
1165#ifndef WIFCONTINUED 1212#ifndef WIFCONTINUED
1166# define WIFCONTINUED(status) 0 1213# define WIFCONTINUED(status) 0
1167#endif 1214#endif
1168 1215
1169void inline_speed 1216/* handle a single child status event */
1217inline_speed void
1170child_reap (EV_P_ int chain, int pid, int status) 1218child_reap (EV_P_ int chain, int pid, int status)
1171{ 1219{
1172 ev_child *w; 1220 ev_child *w;
1173 int traced = WIFSTOPPED (status) || WIFCONTINUED (status); 1221 int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
1174 1222
1187 1235
1188#ifndef WCONTINUED 1236#ifndef WCONTINUED
1189# define WCONTINUED 0 1237# define WCONTINUED 0
1190#endif 1238#endif
1191 1239
1240/* called on sigchld etc., calls waitpid */
1192static void 1241static void
1193childcb (EV_P_ ev_signal *sw, int revents) 1242childcb (EV_P_ ev_signal *sw, int revents)
1194{ 1243{
1195 int pid, status; 1244 int pid, status;
1196 1245
1309ev_loop_count (EV_P) 1358ev_loop_count (EV_P)
1310{ 1359{
1311 return loop_count; 1360 return loop_count;
1312} 1361}
1313 1362
1363unsigned int
1364ev_loop_depth (EV_P)
1365{
1366 return loop_depth;
1367}
1368
1314void 1369void
1315ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 1370ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
1316{ 1371{
1317 io_blocktime = interval; 1372 io_blocktime = interval;
1318} 1373}
1321ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 1376ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1322{ 1377{
1323 timeout_blocktime = interval; 1378 timeout_blocktime = interval;
1324} 1379}
1325 1380
1381/* initialise a loop structure, must be zero-initialised */
1326static void noinline 1382static void noinline
1327loop_init (EV_P_ unsigned int flags) 1383loop_init (EV_P_ unsigned int flags)
1328{ 1384{
1329 if (!backend) 1385 if (!backend)
1330 { 1386 {
1390#endif 1446#endif
1391#if EV_USE_SELECT 1447#if EV_USE_SELECT
1392 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); 1448 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
1393#endif 1449#endif
1394 1450
1451 ev_prepare_init (&pending_w, pendingcb);
1452
1395 ev_init (&pipeev, pipecb); 1453 ev_init (&pipe_w, pipecb);
1396 ev_set_priority (&pipeev, EV_MAXPRI); 1454 ev_set_priority (&pipe_w, EV_MAXPRI);
1397 } 1455 }
1398} 1456}
1399 1457
1458/* free up a loop structure */
1400static void noinline 1459static void noinline
1401loop_destroy (EV_P) 1460loop_destroy (EV_P)
1402{ 1461{
1403 int i; 1462 int i;
1404 1463
1405 if (ev_is_active (&pipeev)) 1464 if (ev_is_active (&pipe_w))
1406 { 1465 {
1407 ev_ref (EV_A); /* signal watcher */ 1466 ev_ref (EV_A); /* signal watcher */
1408 ev_io_stop (EV_A_ &pipeev); 1467 ev_io_stop (EV_A_ &pipe_w);
1409 1468
1410#if EV_USE_EVENTFD 1469#if EV_USE_EVENTFD
1411 if (evfd >= 0) 1470 if (evfd >= 0)
1412 close (evfd); 1471 close (evfd);
1413#endif 1472#endif
1452 } 1511 }
1453 1512
1454 ev_free (anfds); anfdmax = 0; 1513 ev_free (anfds); anfdmax = 0;
1455 1514
1456 /* have to use the microsoft-never-gets-it-right macro */ 1515 /* have to use the microsoft-never-gets-it-right macro */
1516 array_free (rfeed, EMPTY);
1457 array_free (fdchange, EMPTY); 1517 array_free (fdchange, EMPTY);
1458 array_free (timer, EMPTY); 1518 array_free (timer, EMPTY);
1459#if EV_PERIODIC_ENABLE 1519#if EV_PERIODIC_ENABLE
1460 array_free (periodic, EMPTY); 1520 array_free (periodic, EMPTY);
1461#endif 1521#endif
1470 1530
1471 backend = 0; 1531 backend = 0;
1472} 1532}
1473 1533
1474#if EV_USE_INOTIFY 1534#if EV_USE_INOTIFY
1475void inline_size infy_fork (EV_P); 1535inline_size void infy_fork (EV_P);
1476#endif 1536#endif
1477 1537
1478void inline_size 1538inline_size void
1479loop_fork (EV_P) 1539loop_fork (EV_P)
1480{ 1540{
1481#if EV_USE_PORT 1541#if EV_USE_PORT
1482 if (backend == EVBACKEND_PORT ) port_fork (EV_A); 1542 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
1483#endif 1543#endif
1489#endif 1549#endif
1490#if EV_USE_INOTIFY 1550#if EV_USE_INOTIFY
1491 infy_fork (EV_A); 1551 infy_fork (EV_A);
1492#endif 1552#endif
1493 1553
1494 if (ev_is_active (&pipeev)) 1554 if (ev_is_active (&pipe_w))
1495 { 1555 {
1496 /* this "locks" the handlers against writing to the pipe */ 1556 /* this "locks" the handlers against writing to the pipe */
1497 /* while we modify the fd vars */ 1557 /* while we modify the fd vars */
1498 gotsig = 1; 1558 gotsig = 1;
1499#if EV_ASYNC_ENABLE 1559#if EV_ASYNC_ENABLE
1500 gotasync = 1; 1560 gotasync = 1;
1501#endif 1561#endif
1502 1562
1503 ev_ref (EV_A); 1563 ev_ref (EV_A);
1504 ev_io_stop (EV_A_ &pipeev); 1564 ev_io_stop (EV_A_ &pipe_w);
1505 1565
1506#if EV_USE_EVENTFD 1566#if EV_USE_EVENTFD
1507 if (evfd >= 0) 1567 if (evfd >= 0)
1508 close (evfd); 1568 close (evfd);
1509#endif 1569#endif
1514 close (evpipe [1]); 1574 close (evpipe [1]);
1515 } 1575 }
1516 1576
1517 evpipe_init (EV_A); 1577 evpipe_init (EV_A);
1518 /* now iterate over everything, in case we missed something */ 1578 /* now iterate over everything, in case we missed something */
1519 pipecb (EV_A_ &pipeev, EV_READ); 1579 pipecb (EV_A_ &pipe_w, EV_READ);
1520 } 1580 }
1521 1581
1522 postfork = 0; 1582 postfork = 0;
1523} 1583}
1524 1584
1720ev_invoke (EV_P_ void *w, int revents) 1780ev_invoke (EV_P_ void *w, int revents)
1721{ 1781{
1722 EV_CB_INVOKE ((W)w, revents); 1782 EV_CB_INVOKE ((W)w, revents);
1723} 1783}
1724 1784
1725void inline_speed 1785inline_speed void
1726call_pending (EV_P) 1786call_pending (EV_P)
1727{ 1787{
1728 int pri; 1788 int pri;
1729 1789
1730 for (pri = NUMPRI; pri--; ) 1790 for (pri = NUMPRI; pri--; )
1731 while (pendingcnt [pri]) 1791 while (pendingcnt [pri])
1732 { 1792 {
1733 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1793 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1734 1794
1735 if (expect_true (p->w))
1736 {
1737 /*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 */
1738 1797
1739 p->w->pending = 0; 1798 p->w->pending = 0;
1740 EV_CB_INVOKE (p->w, p->events); 1799 EV_CB_INVOKE (p->w, p->events);
1741 EV_FREQUENT_CHECK; 1800 EV_FREQUENT_CHECK;
1742 }
1743 } 1801 }
1744} 1802}
1745 1803
1746#if EV_IDLE_ENABLE 1804#if EV_IDLE_ENABLE
1747void inline_size 1805/* make idle watchers pending. this handles the "call-idle */
1806/* only when higher priorities are idle" logic */
1807inline_size void
1748idle_reify (EV_P) 1808idle_reify (EV_P)
1749{ 1809{
1750 if (expect_false (idleall)) 1810 if (expect_false (idleall))
1751 { 1811 {
1752 int pri; 1812 int pri;
1764 } 1824 }
1765 } 1825 }
1766} 1826}
1767#endif 1827#endif
1768 1828
1769void inline_size 1829/* make timers pending */
1830inline_size void
1770timers_reify (EV_P) 1831timers_reify (EV_P)
1771{ 1832{
1772 EV_FREQUENT_CHECK; 1833 EV_FREQUENT_CHECK;
1773 1834
1774 while (timercnt && ANHE_at (timers [HEAP0]) < mn_now) 1835 if (timercnt && ANHE_at (timers [HEAP0]) < mn_now)
1775 { 1836 {
1776 ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]); 1837 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 { 1838 {
1839 ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
1840
1841 /*assert (("libev: inactive timer on timer heap detected", ev_is_active (w)));*/
1842
1843 /* first reschedule or stop timer */
1844 if (w->repeat)
1845 {
1783 ev_at (w) += w->repeat; 1846 ev_at (w) += w->repeat;
1784 if (ev_at (w) < mn_now) 1847 if (ev_at (w) < mn_now)
1785 ev_at (w) = mn_now; 1848 ev_at (w) = mn_now;
1786 1849
1787 assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 1850 assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1788 1851
1789 ANHE_at_cache (timers [HEAP0]); 1852 ANHE_at_cache (timers [HEAP0]);
1790 downheap (timers, timercnt, HEAP0); 1853 downheap (timers, timercnt, HEAP0);
1854 }
1855 else
1856 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1857
1858 EV_FREQUENT_CHECK;
1859 feed_reverse (EV_A_ (W)w);
1791 } 1860 }
1792 else 1861 while (timercnt && ANHE_at (timers [HEAP0]) < mn_now);
1793 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
1794 1862
1795 EV_FREQUENT_CHECK;
1796 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1863 feed_reverse_done (EV_A_ EV_TIMEOUT);
1797 } 1864 }
1798} 1865}
1799 1866
1800#if EV_PERIODIC_ENABLE 1867#if EV_PERIODIC_ENABLE
1801void inline_size 1868/* make periodics pending */
1869inline_size void
1802periodics_reify (EV_P) 1870periodics_reify (EV_P)
1803{ 1871{
1804 EV_FREQUENT_CHECK; 1872 EV_FREQUENT_CHECK;
1805 1873
1806 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now) 1874 while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
1807 { 1875 {
1808 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]); 1876 int feed_count = 0;
1809 1877
1810 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/ 1878 do
1811
1812 /* first reschedule or stop timer */
1813 if (w->reschedule_cb)
1814 { 1879 {
1880 ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
1881
1882 /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/
1883
1884 /* first reschedule or stop timer */
1885 if (w->reschedule_cb)
1886 {
1815 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 1887 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1816 1888
1817 assert (("libev: ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now)); 1889 assert (("libev: ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now));
1818 1890
1819 ANHE_at_cache (periodics [HEAP0]); 1891 ANHE_at_cache (periodics [HEAP0]);
1820 downheap (periodics, periodiccnt, HEAP0); 1892 downheap (periodics, periodiccnt, HEAP0);
1893 }
1894 else if (w->interval)
1895 {
1896 ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
1897 /* if next trigger time is not sufficiently in the future, put it there */
1898 /* this might happen because of floating point inexactness */
1899 if (ev_at (w) - ev_rt_now < TIME_EPSILON)
1900 {
1901 ev_at (w) += w->interval;
1902
1903 /* if interval is unreasonably low we might still have a time in the past */
1904 /* so correct this. this will make the periodic very inexact, but the user */
1905 /* has effectively asked to get triggered more often than possible */
1906 if (ev_at (w) < ev_rt_now)
1907 ev_at (w) = ev_rt_now;
1908 }
1909
1910 ANHE_at_cache (periodics [HEAP0]);
1911 downheap (periodics, periodiccnt, HEAP0);
1912 }
1913 else
1914 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
1915
1916 EV_FREQUENT_CHECK;
1917 feed_reverse (EV_A_ (W)w);
1821 } 1918 }
1822 else if (w->interval) 1919 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 1920
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); 1921 feed_reverse_done (EV_A_ EV_PERIODIC);
1846 } 1922 }
1847} 1923}
1848 1924
1925/* simply recalculate all periodics */
1926/* TODO: maybe ensure that at leats one event happens when jumping forward? */
1849static void noinline 1927static void noinline
1850periodics_reschedule (EV_P) 1928periodics_reschedule (EV_P)
1851{ 1929{
1852 int i; 1930 int i;
1853 1931
1866 1944
1867 reheap (periodics, periodiccnt); 1945 reheap (periodics, periodiccnt);
1868} 1946}
1869#endif 1947#endif
1870 1948
1871void inline_speed 1949/* adjust all timers by a given offset */
1950static void noinline
1951timers_reschedule (EV_P_ ev_tstamp adjust)
1952{
1953 int i;
1954
1955 for (i = 0; i < timercnt; ++i)
1956 {
1957 ANHE *he = timers + i + HEAP0;
1958 ANHE_w (*he)->at += adjust;
1959 ANHE_at_cache (*he);
1960 }
1961}
1962
1963/* fetch new monotonic and realtime times from the kernel */
1964/* also detetc if there was a timejump, and act accordingly */
1965inline_speed void
1872time_update (EV_P_ ev_tstamp max_block) 1966time_update (EV_P_ ev_tstamp max_block)
1873{ 1967{
1874 int i;
1875
1876#if EV_USE_MONOTONIC 1968#if EV_USE_MONOTONIC
1877 if (expect_true (have_monotonic)) 1969 if (expect_true (have_monotonic))
1878 { 1970 {
1971 int i;
1879 ev_tstamp odiff = rtmn_diff; 1972 ev_tstamp odiff = rtmn_diff;
1880 1973
1881 mn_now = get_clock (); 1974 mn_now = get_clock ();
1882 1975
1883 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ 1976 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
1909 ev_rt_now = ev_time (); 2002 ev_rt_now = ev_time ();
1910 mn_now = get_clock (); 2003 mn_now = get_clock ();
1911 now_floor = mn_now; 2004 now_floor = mn_now;
1912 } 2005 }
1913 2006
2007 /* no timer adjustment, as the monotonic clock doesn't jump */
2008 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1914# if EV_PERIODIC_ENABLE 2009# if EV_PERIODIC_ENABLE
1915 periodics_reschedule (EV_A); 2010 periodics_reschedule (EV_A);
1916# endif 2011# endif
1917 /* no timer adjustment, as the monotonic clock doesn't jump */
1918 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1919 } 2012 }
1920 else 2013 else
1921#endif 2014#endif
1922 { 2015 {
1923 ev_rt_now = ev_time (); 2016 ev_rt_now = ev_time ();
1924 2017
1925 if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP)) 2018 if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP))
1926 { 2019 {
2020 /* adjust timers. this is easy, as the offset is the same for all of them */
2021 timers_reschedule (EV_A_ ev_rt_now - mn_now);
1927#if EV_PERIODIC_ENABLE 2022#if EV_PERIODIC_ENABLE
1928 periodics_reschedule (EV_A); 2023 periodics_reschedule (EV_A);
1929#endif 2024#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 } 2025 }
1938 2026
1939 mn_now = ev_rt_now; 2027 mn_now = ev_rt_now;
1940 } 2028 }
1941} 2029}
1942 2030
1943void 2031void
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}
1960
1961static int loop_done;
1962
1963void
1964ev_loop (EV_P_ int flags) 2032ev_loop (EV_P_ int flags)
1965{ 2033{
2034 ++loop_depth;
2035
1966 loop_done = EVUNLOOP_CANCEL; 2036 loop_done = EVUNLOOP_CANCEL;
1967 2037
1968 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 */
1969 2039
1970 do 2040 do
1997 { 2067 {
1998 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2068 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1999 call_pending (EV_A); 2069 call_pending (EV_A);
2000 } 2070 }
2001 2071
2002 if (expect_false (!activecnt))
2003 break;
2004
2005 /* we might have forked, so reify kernel state if necessary */ 2072 /* we might have forked, so reify kernel state if necessary */
2006 if (expect_false (postfork)) 2073 if (expect_false (postfork))
2007 loop_fork (EV_A); 2074 loop_fork (EV_A);
2008 2075
2009 /* update fd-related kernel structures */ 2076 /* update fd-related kernel structures */
2014 ev_tstamp waittime = 0.; 2081 ev_tstamp waittime = 0.;
2015 ev_tstamp sleeptime = 0.; 2082 ev_tstamp sleeptime = 0.;
2016 2083
2017 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2084 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
2018 { 2085 {
2086 /* remember old timestamp for io_blocktime calculation */
2087 ev_tstamp prev_mn_now = mn_now;
2088
2019 /* update time to cancel out callback processing overhead */ 2089 /* update time to cancel out callback processing overhead */
2020 time_update (EV_A_ 1e100); 2090 time_update (EV_A_ 1e100);
2021 2091
2022 waittime = MAX_BLOCKTIME; 2092 waittime = MAX_BLOCKTIME;
2023 2093
2033 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;
2034 if (waittime > to) waittime = to; 2104 if (waittime > to) waittime = to;
2035 } 2105 }
2036#endif 2106#endif
2037 2107
2108 /* don't let timeouts decrease the waittime below timeout_blocktime */
2038 if (expect_false (waittime < timeout_blocktime)) 2109 if (expect_false (waittime < timeout_blocktime))
2039 waittime = timeout_blocktime; 2110 waittime = timeout_blocktime;
2040 2111
2041 sleeptime = waittime - backend_fudge; 2112 /* extra check because io_blocktime is commonly 0 */
2042
2043 if (expect_true (sleeptime > io_blocktime)) 2113 if (expect_false (io_blocktime))
2044 sleeptime = io_blocktime;
2045
2046 if (sleeptime)
2047 { 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 {
2048 ev_sleep (sleeptime); 2122 ev_sleep (sleeptime);
2049 waittime -= sleeptime; 2123 waittime -= sleeptime;
2124 }
2050 } 2125 }
2051 } 2126 }
2052 2127
2053 ++loop_count; 2128 ++loop_count;
2054 backend_poll (EV_A_ waittime); 2129 backend_poll (EV_A_ waittime);
2080 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2155 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2081 )); 2156 ));
2082 2157
2083 if (loop_done == EVUNLOOP_ONE) 2158 if (loop_done == EVUNLOOP_ONE)
2084 loop_done = EVUNLOOP_CANCEL; 2159 loop_done = EVUNLOOP_CANCEL;
2160
2161 --loop_depth;
2085} 2162}
2086 2163
2087void 2164void
2088ev_unloop (EV_P_ int how) 2165ev_unloop (EV_P_ int how)
2089{ 2166{
2090 loop_done = how; 2167 loop_done = how;
2091} 2168}
2092 2169
2170void
2171ev_ref (EV_P)
2172{
2173 ++activecnt;
2174}
2175
2176void
2177ev_unref (EV_P)
2178{
2179 --activecnt;
2180}
2181
2182void
2183ev_now_update (EV_P)
2184{
2185 time_update (EV_A_ 1e100);
2186}
2187
2188void
2189ev_suspend (EV_P)
2190{
2191 ev_now_update (EV_A);
2192}
2193
2194void
2195ev_resume (EV_P)
2196{
2197 ev_tstamp mn_prev = mn_now;
2198
2199 ev_now_update (EV_A);
2200 timers_reschedule (EV_A_ mn_now - mn_prev);
2201#if EV_PERIODIC_ENABLE
2202 /* TODO: really do this? */
2203 periodics_reschedule (EV_A);
2204#endif
2205}
2206
2093/*****************************************************************************/ 2207/*****************************************************************************/
2208/* singly-linked list management, used when the expected list length is short */
2094 2209
2095void inline_size 2210inline_size void
2096wlist_add (WL *head, WL elem) 2211wlist_add (WL *head, WL elem)
2097{ 2212{
2098 elem->next = *head; 2213 elem->next = *head;
2099 *head = elem; 2214 *head = elem;
2100} 2215}
2101 2216
2102void inline_size 2217inline_size void
2103wlist_del (WL *head, WL elem) 2218wlist_del (WL *head, WL elem)
2104{ 2219{
2105 while (*head) 2220 while (*head)
2106 { 2221 {
2107 if (*head == elem) 2222 if (*head == elem)
2112 2227
2113 head = &(*head)->next; 2228 head = &(*head)->next;
2114 } 2229 }
2115} 2230}
2116 2231
2117void inline_speed 2232/* internal, faster, version of ev_clear_pending */
2233inline_speed void
2118clear_pending (EV_P_ W w) 2234clear_pending (EV_P_ W w)
2119{ 2235{
2120 if (w->pending) 2236 if (w->pending)
2121 { 2237 {
2122 pendings [ABSPRI (w)][w->pending - 1].w = 0; 2238 pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w;
2123 w->pending = 0; 2239 w->pending = 0;
2124 } 2240 }
2125} 2241}
2126 2242
2127int 2243int
2131 int pending = w_->pending; 2247 int pending = w_->pending;
2132 2248
2133 if (expect_true (pending)) 2249 if (expect_true (pending))
2134 { 2250 {
2135 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; 2251 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
2252 p->w = (W)&pending_w;
2136 w_->pending = 0; 2253 w_->pending = 0;
2137 p->w = 0;
2138 return p->events; 2254 return p->events;
2139 } 2255 }
2140 else 2256 else
2141 return 0; 2257 return 0;
2142} 2258}
2143 2259
2144void inline_size 2260inline_size void
2145pri_adjust (EV_P_ W w) 2261pri_adjust (EV_P_ W w)
2146{ 2262{
2147 int pri = w->priority; 2263 int pri = w->priority;
2148 pri = pri < EV_MINPRI ? EV_MINPRI : pri; 2264 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2149 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; 2265 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2150 w->priority = pri; 2266 w->priority = pri;
2151} 2267}
2152 2268
2153void inline_speed 2269inline_speed void
2154ev_start (EV_P_ W w, int active) 2270ev_start (EV_P_ W w, int active)
2155{ 2271{
2156 pri_adjust (EV_A_ w); 2272 pri_adjust (EV_A_ w);
2157 w->active = active; 2273 w->active = active;
2158 ev_ref (EV_A); 2274 ev_ref (EV_A);
2159} 2275}
2160 2276
2161void inline_size 2277inline_size void
2162ev_stop (EV_P_ W w) 2278ev_stop (EV_P_ W w)
2163{ 2279{
2164 ev_unref (EV_A); 2280 ev_unref (EV_A);
2165 w->active = 0; 2281 w->active = 0;
2166} 2282}
2591 2707
2592 for (ofs = 0; ofs < len; ofs += sizeof (struct inotify_event) + ev->len) 2708 for (ofs = 0; ofs < len; ofs += sizeof (struct inotify_event) + ev->len)
2593 infy_wd (EV_A_ ev->wd, ev->wd, ev); 2709 infy_wd (EV_A_ ev->wd, ev->wd, ev);
2594} 2710}
2595 2711
2596void inline_size 2712inline_size void
2597check_2625 (EV_P) 2713check_2625 (EV_P)
2598{ 2714{
2599 /* kernels < 2.6.25 are borked 2715 /* kernels < 2.6.25 are borked
2600 * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html 2716 * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html
2601 */ 2717 */
2614 return; 2730 return;
2615 2731
2616 fs_2625 = 1; 2732 fs_2625 = 1;
2617} 2733}
2618 2734
2619void inline_size 2735inline_size void
2620infy_init (EV_P) 2736infy_init (EV_P)
2621{ 2737{
2622 if (fs_fd != -2) 2738 if (fs_fd != -2)
2623 return; 2739 return;
2624 2740
2634 ev_set_priority (&fs_w, EV_MAXPRI); 2750 ev_set_priority (&fs_w, EV_MAXPRI);
2635 ev_io_start (EV_A_ &fs_w); 2751 ev_io_start (EV_A_ &fs_w);
2636 } 2752 }
2637} 2753}
2638 2754
2639void inline_size 2755inline_size void
2640infy_fork (EV_P) 2756infy_fork (EV_P)
2641{ 2757{
2642 int slot; 2758 int slot;
2643 2759
2644 if (fs_fd < 0) 2760 if (fs_fd < 0)
3146 ev_timer_set (&once->to, timeout, 0.); 3262 ev_timer_set (&once->to, timeout, 0.);
3147 ev_timer_start (EV_A_ &once->to); 3263 ev_timer_start (EV_A_ &once->to);
3148 } 3264 }
3149} 3265}
3150 3266
3267/*****************************************************************************/
3268
3269#if EV_WALK_ENABLE
3270void
3271ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w))
3272{
3273 int i, j;
3274 ev_watcher_list *wl, *wn;
3275
3276 if (types & (EV_IO | EV_EMBED))
3277 for (i = 0; i < anfdmax; ++i)
3278 for (wl = anfds [i].head; wl; )
3279 {
3280 wn = wl->next;
3281
3282#if EV_EMBED_ENABLE
3283 if (ev_cb ((ev_io *)wl) == embed_io_cb)
3284 {
3285 if (types & EV_EMBED)
3286 cb (EV_A_ EV_EMBED, ((char *)wl) - offsetof (struct ev_embed, io));
3287 }
3288 else
3289#endif
3290#if EV_USE_INOTIFY
3291 if (ev_cb ((ev_io *)wl) == infy_cb)
3292 ;
3293 else
3294#endif
3295 if ((ev_io *)wl != &pipe_w)
3296 if (types & EV_IO)
3297 cb (EV_A_ EV_IO, wl);
3298
3299 wl = wn;
3300 }
3301
3302 if (types & (EV_TIMER | EV_STAT))
3303 for (i = timercnt + HEAP0; i-- > HEAP0; )
3304#if EV_STAT_ENABLE
3305 /*TODO: timer is not always active*/
3306 if (ev_cb ((ev_timer *)ANHE_w (timers [i])) == stat_timer_cb)
3307 {
3308 if (types & EV_STAT)
3309 cb (EV_A_ EV_STAT, ((char *)ANHE_w (timers [i])) - offsetof (struct ev_stat, timer));
3310 }
3311 else
3312#endif
3313 if (types & EV_TIMER)
3314 cb (EV_A_ EV_TIMER, ANHE_w (timers [i]));
3315
3316#if EV_PERIODIC_ENABLE
3317 if (types & EV_PERIODIC)
3318 for (i = periodiccnt + HEAP0; i-- > HEAP0; )
3319 cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i]));
3320#endif
3321
3322#if EV_IDLE_ENABLE
3323 if (types & EV_IDLE)
3324 for (j = NUMPRI; i--; )
3325 for (i = idlecnt [j]; i--; )
3326 cb (EV_A_ EV_IDLE, idles [j][i]);
3327#endif
3328
3329#if EV_FORK_ENABLE
3330 if (types & EV_FORK)
3331 for (i = forkcnt; i--; )
3332 if (ev_cb (forks [i]) != embed_fork_cb)
3333 cb (EV_A_ EV_FORK, forks [i]);
3334#endif
3335
3336#if EV_ASYNC_ENABLE
3337 if (types & EV_ASYNC)
3338 for (i = asynccnt; i--; )
3339 cb (EV_A_ EV_ASYNC, asyncs [i]);
3340#endif
3341
3342 if (types & EV_PREPARE)
3343 for (i = preparecnt; i--; )
3344#if EV_EMBED_ENABLE
3345 if (ev_cb (prepares [i]) != embed_prepare_cb)
3346#endif
3347 cb (EV_A_ EV_PREPARE, prepares [i]);
3348
3349 if (types & EV_CHECK)
3350 for (i = checkcnt; i--; )
3351 cb (EV_A_ EV_CHECK, checks [i]);
3352
3353 if (types & EV_SIGNAL)
3354 for (i = 0; i < signalmax; ++i)
3355 for (wl = signals [i].head; wl; )
3356 {
3357 wn = wl->next;
3358 cb (EV_A_ EV_SIGNAL, wl);
3359 wl = wn;
3360 }
3361
3362 if (types & EV_CHILD)
3363 for (i = EV_PID_HASHSIZE; i--; )
3364 for (wl = childs [i]; wl; )
3365 {
3366 wn = wl->next;
3367 cb (EV_A_ EV_CHILD, wl);
3368 wl = wn;
3369 }
3370/* EV_STAT 0x00001000 /* stat data changed */
3371/* EV_EMBED 0x00010000 /* embedded event loop needs sweep */
3372}
3373#endif
3374
3151#if EV_MULTIPLICITY 3375#if EV_MULTIPLICITY
3152 #include "ev_wrap.h" 3376 #include "ev_wrap.h"
3153#endif 3377#endif
3154 3378
3155#ifdef __cplusplus 3379#ifdef __cplusplus

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines