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.298 by root, Fri Jul 10 19:10:19 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/* set in reify when reification needed */
496#define EV_ANFD_REIFY 1
497
498/* file descriptor info structure */
483typedef struct 499typedef struct
484{ 500{
485 WL head; 501 WL head;
486 unsigned char events; 502 unsigned char events; /* the events watched for */
487 unsigned char reify; 503 unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */
488 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ 504 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */
489 unsigned char unused; 505 unsigned char unused;
490#if EV_USE_EPOLL 506#if EV_USE_EPOLL
491 unsigned int egen; /* generation counter to counter epoll bugs */ 507 unsigned int egen; /* generation counter to counter epoll bugs */
492#endif 508#endif
493#if EV_SELECT_IS_WINSOCKET 509#if EV_SELECT_IS_WINSOCKET
494 SOCKET handle; 510 SOCKET handle;
495#endif 511#endif
496} ANFD; 512} ANFD;
497 513
514/* stores the pending event set for a given watcher */
498typedef struct 515typedef struct
499{ 516{
500 W w; 517 W w;
501 int events; 518 int events; /* the pending event set for the given watcher */
502} ANPENDING; 519} ANPENDING;
503 520
504#if EV_USE_INOTIFY 521#if EV_USE_INOTIFY
505/* hash table entry per inotify-id */ 522/* hash table entry per inotify-id */
506typedef struct 523typedef struct
509} ANFS; 526} ANFS;
510#endif 527#endif
511 528
512/* Heap Entry */ 529/* Heap Entry */
513#if EV_HEAP_CACHE_AT 530#if EV_HEAP_CACHE_AT
531 /* a heap element */
514 typedef struct { 532 typedef struct {
515 ev_tstamp at; 533 ev_tstamp at;
516 WT w; 534 WT w;
517 } ANHE; 535 } ANHE;
518 536
519 #define ANHE_w(he) (he).w /* access watcher, read-write */ 537 #define ANHE_w(he) (he).w /* access watcher, read-write */
520 #define ANHE_at(he) (he).at /* access cached at, read-only */ 538 #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 */ 539 #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */
522#else 540#else
541 /* a heap element */
523 typedef WT ANHE; 542 typedef WT ANHE;
524 543
525 #define ANHE_w(he) (he) 544 #define ANHE_w(he) (he)
526 #define ANHE_at(he) (he)->at 545 #define ANHE_at(he) (he)->at
527 #define ANHE_at_cache(he) 546 #define ANHE_at_cache(he)
551 570
552 static int ev_default_loop_ptr; 571 static int ev_default_loop_ptr;
553 572
554#endif 573#endif
555 574
575#if EV_MINIMAL < 2
576# define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A)
577# define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A)
578# define EV_INVOKE_PENDING invoke_cb (EV_A)
579#else
580# define EV_RELEASE_CB (void)0
581# define EV_ACQUIRE_CB (void)0
582# define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
583#endif
584
585#define EVUNLOOP_RECURSE 0x80
586
556/*****************************************************************************/ 587/*****************************************************************************/
557 588
589#ifndef EV_HAVE_EV_TIME
558ev_tstamp 590ev_tstamp
559ev_time (void) 591ev_time (void)
560{ 592{
561#if EV_USE_REALTIME 593#if EV_USE_REALTIME
562 if (expect_true (have_realtime)) 594 if (expect_true (have_realtime))
569 601
570 struct timeval tv; 602 struct timeval tv;
571 gettimeofday (&tv, 0); 603 gettimeofday (&tv, 0);
572 return tv.tv_sec + tv.tv_usec * 1e-6; 604 return tv.tv_sec + tv.tv_usec * 1e-6;
573} 605}
606#endif
574 607
575inline_size ev_tstamp 608inline_size ev_tstamp
576get_clock (void) 609get_clock (void)
577{ 610{
578#if EV_USE_MONOTONIC 611#if EV_USE_MONOTONIC
614 647
615 tv.tv_sec = (time_t)delay; 648 tv.tv_sec = (time_t)delay;
616 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 649 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
617 650
618 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 651 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
619 /* somehting nto guaranteed by newer posix versions, but guaranteed */ 652 /* somehting not guaranteed by newer posix versions, but guaranteed */
620 /* by older ones */ 653 /* by older ones */
621 select (0, 0, 0, 0, &tv); 654 select (0, 0, 0, 0, &tv);
622#endif 655#endif
623 } 656 }
624} 657}
625 658
626/*****************************************************************************/ 659/*****************************************************************************/
627 660
628#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ 661#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
629 662
663/* find a suitable new size for the given array, */
664/* hopefully by rounding to a ncie-to-malloc size */
630inline_size int 665inline_size int
631array_nextsize (int elem, int cur, int cnt) 666array_nextsize (int elem, int cur, int cnt)
632{ 667{
633 int ncur = cur + 1; 668 int ncur = cur + 1;
634 669
680#define array_free(stem, idx) \ 715#define array_free(stem, idx) \
681 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0 716 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0
682 717
683/*****************************************************************************/ 718/*****************************************************************************/
684 719
720/* dummy callback for pending events */
721static void noinline
722pendingcb (EV_P_ ev_prepare *w, int revents)
723{
724}
725
685void noinline 726void noinline
686ev_feed_event (EV_P_ void *w, int revents) 727ev_feed_event (EV_P_ void *w, int revents)
687{ 728{
688 W w_ = (W)w; 729 W w_ = (W)w;
689 int pri = ABSPRI (w_); 730 int pri = ABSPRI (w_);
724} 765}
725 766
726/*****************************************************************************/ 767/*****************************************************************************/
727 768
728inline_speed void 769inline_speed void
729fd_event (EV_P_ int fd, int revents) 770fd_event_nc (EV_P_ int fd, int revents)
730{ 771{
731 ANFD *anfd = anfds + fd; 772 ANFD *anfd = anfds + fd;
732 ev_io *w; 773 ev_io *w;
733 774
734 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) 775 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
738 if (ev) 779 if (ev)
739 ev_feed_event (EV_A_ (W)w, ev); 780 ev_feed_event (EV_A_ (W)w, ev);
740 } 781 }
741} 782}
742 783
784/* do not submit kernel events for fds that have reify set */
785/* because that means they changed while we were polling for new events */
786inline_speed void
787fd_event (EV_P_ int fd, int revents)
788{
789 ANFD *anfd = anfds + fd;
790
791 if (expect_true (!anfd->reify))
792 fd_event_nc (EV_A_ fd, revents);
793}
794
743void 795void
744ev_feed_fd_event (EV_P_ int fd, int revents) 796ev_feed_fd_event (EV_P_ int fd, int revents)
745{ 797{
746 if (fd >= 0 && fd < anfdmax) 798 if (fd >= 0 && fd < anfdmax)
747 fd_event (EV_A_ fd, revents); 799 fd_event_nc (EV_A_ fd, revents);
748} 800}
749 801
802/* make sure the external fd watch events are in-sync */
803/* with the kernel/libev internal state */
750inline_size void 804inline_size void
751fd_reify (EV_P) 805fd_reify (EV_P)
752{ 806{
753 int i; 807 int i;
754 808
789 } 843 }
790 844
791 fdchangecnt = 0; 845 fdchangecnt = 0;
792} 846}
793 847
848/* something about the given fd changed */
794inline_size void 849inline_size void
795fd_change (EV_P_ int fd, int flags) 850fd_change (EV_P_ int fd, int flags)
796{ 851{
797 unsigned char reify = anfds [fd].reify; 852 unsigned char reify = anfds [fd].reify;
798 anfds [fd].reify |= flags; 853 anfds [fd].reify |= flags;
803 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 858 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
804 fdchanges [fdchangecnt - 1] = fd; 859 fdchanges [fdchangecnt - 1] = fd;
805 } 860 }
806} 861}
807 862
863/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
808inline_speed void 864inline_speed void
809fd_kill (EV_P_ int fd) 865fd_kill (EV_P_ int fd)
810{ 866{
811 ev_io *w; 867 ev_io *w;
812 868
815 ev_io_stop (EV_A_ w); 871 ev_io_stop (EV_A_ w);
816 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 872 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
817 } 873 }
818} 874}
819 875
876/* check whether the given fd is atcually valid, for error recovery */
820inline_size int 877inline_size int
821fd_valid (int fd) 878fd_valid (int fd)
822{ 879{
823#ifdef _WIN32 880#ifdef _WIN32
824 return _get_osfhandle (fd) != -1; 881 return _get_osfhandle (fd) != -1;
862 for (fd = 0; fd < anfdmax; ++fd) 919 for (fd = 0; fd < anfdmax; ++fd)
863 if (anfds [fd].events) 920 if (anfds [fd].events)
864 { 921 {
865 anfds [fd].events = 0; 922 anfds [fd].events = 0;
866 anfds [fd].emask = 0; 923 anfds [fd].emask = 0;
867 fd_change (EV_A_ fd, EV__IOFDSET | 1); 924 fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
868 } 925 }
869} 926}
870 927
871/*****************************************************************************/ 928/*****************************************************************************/
872 929
989 1046
990 heap [k] = he; 1047 heap [k] = he;
991 ev_active (ANHE_w (he)) = k; 1048 ev_active (ANHE_w (he)) = k;
992} 1049}
993 1050
1051/* move an element suitably so it is in a correct place */
994inline_size void 1052inline_size void
995adjustheap (ANHE *heap, int N, int k) 1053adjustheap (ANHE *heap, int N, int k)
996{ 1054{
997 if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) 1055 if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k]))
998 upheap (heap, k); 1056 upheap (heap, k);
1012 upheap (heap, i + HEAP0); 1070 upheap (heap, i + HEAP0);
1013} 1071}
1014 1072
1015/*****************************************************************************/ 1073/*****************************************************************************/
1016 1074
1075/* associate signal watchers to a signal signal */
1017typedef struct 1076typedef struct
1018{ 1077{
1019 WL head; 1078 WL head;
1020 EV_ATOMIC_T gotsig; 1079 EV_ATOMIC_T gotsig;
1021} ANSIG; 1080} ANSIG;
1025 1084
1026static EV_ATOMIC_T gotsig; 1085static EV_ATOMIC_T gotsig;
1027 1086
1028/*****************************************************************************/ 1087/*****************************************************************************/
1029 1088
1089/* used to prepare libev internal fd's */
1090/* this is not fork-safe */
1030inline_speed void 1091inline_speed void
1031fd_intern (int fd) 1092fd_intern (int fd)
1032{ 1093{
1033#ifdef _WIN32 1094#ifdef _WIN32
1034 unsigned long arg = 1; 1095 unsigned long arg = 1;
1040} 1101}
1041 1102
1042static void noinline 1103static void noinline
1043evpipe_init (EV_P) 1104evpipe_init (EV_P)
1044{ 1105{
1045 if (!ev_is_active (&pipeev)) 1106 if (!ev_is_active (&pipe_w))
1046 { 1107 {
1047#if EV_USE_EVENTFD 1108#if EV_USE_EVENTFD
1048 if ((evfd = eventfd (0, 0)) >= 0) 1109 if ((evfd = eventfd (0, 0)) >= 0)
1049 { 1110 {
1050 evpipe [0] = -1; 1111 evpipe [0] = -1;
1051 fd_intern (evfd); 1112 fd_intern (evfd);
1052 ev_io_set (&pipeev, evfd, EV_READ); 1113 ev_io_set (&pipe_w, evfd, EV_READ);
1053 } 1114 }
1054 else 1115 else
1055#endif 1116#endif
1056 { 1117 {
1057 while (pipe (evpipe)) 1118 while (pipe (evpipe))
1058 ev_syserr ("(libev) error creating signal/async pipe"); 1119 ev_syserr ("(libev) error creating signal/async pipe");
1059 1120
1060 fd_intern (evpipe [0]); 1121 fd_intern (evpipe [0]);
1061 fd_intern (evpipe [1]); 1122 fd_intern (evpipe [1]);
1062 ev_io_set (&pipeev, evpipe [0], EV_READ); 1123 ev_io_set (&pipe_w, evpipe [0], EV_READ);
1063 } 1124 }
1064 1125
1065 ev_io_start (EV_A_ &pipeev); 1126 ev_io_start (EV_A_ &pipe_w);
1066 ev_unref (EV_A); /* watcher should not keep loop alive */ 1127 ev_unref (EV_A); /* watcher should not keep loop alive */
1067 } 1128 }
1068} 1129}
1069 1130
1070inline_size void 1131inline_size void
1088 1149
1089 errno = old_errno; 1150 errno = old_errno;
1090 } 1151 }
1091} 1152}
1092 1153
1154/* called whenever the libev signal pipe */
1155/* got some events (signal, async) */
1093static void 1156static void
1094pipecb (EV_P_ ev_io *iow, int revents) 1157pipecb (EV_P_ ev_io *iow, int revents)
1095{ 1158{
1096#if EV_USE_EVENTFD 1159#if EV_USE_EVENTFD
1097 if (evfd >= 0) 1160 if (evfd >= 0)
1179 1242
1180#ifndef WIFCONTINUED 1243#ifndef WIFCONTINUED
1181# define WIFCONTINUED(status) 0 1244# define WIFCONTINUED(status) 0
1182#endif 1245#endif
1183 1246
1247/* handle a single child status event */
1184inline_speed void 1248inline_speed void
1185child_reap (EV_P_ int chain, int pid, int status) 1249child_reap (EV_P_ int chain, int pid, int status)
1186{ 1250{
1187 ev_child *w; 1251 ev_child *w;
1188 int traced = WIFSTOPPED (status) || WIFCONTINUED (status); 1252 int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
1202 1266
1203#ifndef WCONTINUED 1267#ifndef WCONTINUED
1204# define WCONTINUED 0 1268# define WCONTINUED 0
1205#endif 1269#endif
1206 1270
1271/* called on sigchld etc., calls waitpid */
1207static void 1272static void
1208childcb (EV_P_ ev_signal *sw, int revents) 1273childcb (EV_P_ ev_signal *sw, int revents)
1209{ 1274{
1210 int pid, status; 1275 int pid, status;
1211 1276
1318ev_backend (EV_P) 1383ev_backend (EV_P)
1319{ 1384{
1320 return backend; 1385 return backend;
1321} 1386}
1322 1387
1388#if EV_MINIMAL < 2
1323unsigned int 1389unsigned int
1324ev_loop_count (EV_P) 1390ev_loop_count (EV_P)
1325{ 1391{
1326 return loop_count; 1392 return loop_count;
1327} 1393}
1328 1394
1395unsigned int
1396ev_loop_depth (EV_P)
1397{
1398 return loop_depth;
1399}
1400
1329void 1401void
1330ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 1402ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
1331{ 1403{
1332 io_blocktime = interval; 1404 io_blocktime = interval;
1333} 1405}
1336ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 1408ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1337{ 1409{
1338 timeout_blocktime = interval; 1410 timeout_blocktime = interval;
1339} 1411}
1340 1412
1413void
1414ev_set_userdata (EV_P_ void *data)
1415{
1416 userdata = data;
1417}
1418
1419void *
1420ev_userdata (EV_P)
1421{
1422 return userdata;
1423}
1424
1425void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P))
1426{
1427 invoke_cb = invoke_pending_cb;
1428}
1429
1430void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P))
1431{
1432 release_cb = release;
1433 acquire_cb = acquire;
1434}
1435#endif
1436
1437/* initialise a loop structure, must be zero-initialised */
1341static void noinline 1438static void noinline
1342loop_init (EV_P_ unsigned int flags) 1439loop_init (EV_P_ unsigned int flags)
1343{ 1440{
1344 if (!backend) 1441 if (!backend)
1345 { 1442 {
1365 1462
1366 ev_rt_now = ev_time (); 1463 ev_rt_now = ev_time ();
1367 mn_now = get_clock (); 1464 mn_now = get_clock ();
1368 now_floor = mn_now; 1465 now_floor = mn_now;
1369 rtmn_diff = ev_rt_now - mn_now; 1466 rtmn_diff = ev_rt_now - mn_now;
1467#if EV_MINIMAL < 2
1468 invoke_cb = ev_invoke_pending;
1469#endif
1370 1470
1371 io_blocktime = 0.; 1471 io_blocktime = 0.;
1372 timeout_blocktime = 0.; 1472 timeout_blocktime = 0.;
1373 backend = 0; 1473 backend = 0;
1374 backend_fd = -1; 1474 backend_fd = -1;
1405#endif 1505#endif
1406#if EV_USE_SELECT 1506#if EV_USE_SELECT
1407 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); 1507 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
1408#endif 1508#endif
1409 1509
1510 ev_prepare_init (&pending_w, pendingcb);
1511
1410 ev_init (&pipeev, pipecb); 1512 ev_init (&pipe_w, pipecb);
1411 ev_set_priority (&pipeev, EV_MAXPRI); 1513 ev_set_priority (&pipe_w, EV_MAXPRI);
1412 } 1514 }
1413} 1515}
1414 1516
1517/* free up a loop structure */
1415static void noinline 1518static void noinline
1416loop_destroy (EV_P) 1519loop_destroy (EV_P)
1417{ 1520{
1418 int i; 1521 int i;
1419 1522
1420 if (ev_is_active (&pipeev)) 1523 if (ev_is_active (&pipe_w))
1421 { 1524 {
1422 ev_ref (EV_A); /* signal watcher */ 1525 ev_ref (EV_A); /* signal watcher */
1423 ev_io_stop (EV_A_ &pipeev); 1526 ev_io_stop (EV_A_ &pipe_w);
1424 1527
1425#if EV_USE_EVENTFD 1528#if EV_USE_EVENTFD
1426 if (evfd >= 0) 1529 if (evfd >= 0)
1427 close (evfd); 1530 close (evfd);
1428#endif 1531#endif
1505#endif 1608#endif
1506#if EV_USE_INOTIFY 1609#if EV_USE_INOTIFY
1507 infy_fork (EV_A); 1610 infy_fork (EV_A);
1508#endif 1611#endif
1509 1612
1510 if (ev_is_active (&pipeev)) 1613 if (ev_is_active (&pipe_w))
1511 { 1614 {
1512 /* this "locks" the handlers against writing to the pipe */ 1615 /* this "locks" the handlers against writing to the pipe */
1513 /* while we modify the fd vars */ 1616 /* while we modify the fd vars */
1514 gotsig = 1; 1617 gotsig = 1;
1515#if EV_ASYNC_ENABLE 1618#if EV_ASYNC_ENABLE
1516 gotasync = 1; 1619 gotasync = 1;
1517#endif 1620#endif
1518 1621
1519 ev_ref (EV_A); 1622 ev_ref (EV_A);
1520 ev_io_stop (EV_A_ &pipeev); 1623 ev_io_stop (EV_A_ &pipe_w);
1521 1624
1522#if EV_USE_EVENTFD 1625#if EV_USE_EVENTFD
1523 if (evfd >= 0) 1626 if (evfd >= 0)
1524 close (evfd); 1627 close (evfd);
1525#endif 1628#endif
1530 close (evpipe [1]); 1633 close (evpipe [1]);
1531 } 1634 }
1532 1635
1533 evpipe_init (EV_A); 1636 evpipe_init (EV_A);
1534 /* now iterate over everything, in case we missed something */ 1637 /* now iterate over everything, in case we missed something */
1535 pipecb (EV_A_ &pipeev, EV_READ); 1638 pipecb (EV_A_ &pipe_w, EV_READ);
1536 } 1639 }
1537 1640
1538 postfork = 0; 1641 postfork = 0;
1539} 1642}
1540 1643
1565void 1668void
1566ev_loop_fork (EV_P) 1669ev_loop_fork (EV_P)
1567{ 1670{
1568 postfork = 1; /* must be in line with ev_default_fork */ 1671 postfork = 1; /* must be in line with ev_default_fork */
1569} 1672}
1673#endif /* multiplicity */
1570 1674
1571#if EV_VERIFY 1675#if EV_VERIFY
1572static void noinline 1676static void noinline
1573verify_watcher (EV_P_ W w) 1677verify_watcher (EV_P_ W w)
1574{ 1678{
1602 verify_watcher (EV_A_ ws [cnt]); 1706 verify_watcher (EV_A_ ws [cnt]);
1603 } 1707 }
1604} 1708}
1605#endif 1709#endif
1606 1710
1711#if EV_MINIMAL < 2
1607void 1712void
1608ev_loop_verify (EV_P) 1713ev_loop_verify (EV_P)
1609{ 1714{
1610#if EV_VERIFY 1715#if EV_VERIFY
1611 int i; 1716 int i;
1664 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1769 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
1665 for (signum = signalmax; signum--; ) if (signals [signum].gotsig) 1770 for (signum = signalmax; signum--; ) if (signals [signum].gotsig)
1666# endif 1771# endif
1667#endif 1772#endif
1668} 1773}
1669 1774#endif
1670#endif /* multiplicity */
1671 1775
1672#if EV_MULTIPLICITY 1776#if EV_MULTIPLICITY
1673struct ev_loop * 1777struct ev_loop *
1674ev_default_loop_init (unsigned int flags) 1778ev_default_loop_init (unsigned int flags)
1675#else 1779#else
1736ev_invoke (EV_P_ void *w, int revents) 1840ev_invoke (EV_P_ void *w, int revents)
1737{ 1841{
1738 EV_CB_INVOKE ((W)w, revents); 1842 EV_CB_INVOKE ((W)w, revents);
1739} 1843}
1740 1844
1741inline_speed void 1845void noinline
1742call_pending (EV_P) 1846ev_invoke_pending (EV_P)
1743{ 1847{
1744 int pri; 1848 int pri;
1745 1849
1746 for (pri = NUMPRI; pri--; ) 1850 for (pri = NUMPRI; pri--; )
1747 while (pendingcnt [pri]) 1851 while (pendingcnt [pri])
1748 { 1852 {
1749 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1853 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1750 1854
1751 if (expect_true (p->w))
1752 {
1753 /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ 1855 /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/
1856 /* ^ this is no longer true, as pending_w could be here */
1754 1857
1755 p->w->pending = 0; 1858 p->w->pending = 0;
1756 EV_CB_INVOKE (p->w, p->events); 1859 EV_CB_INVOKE (p->w, p->events);
1757 EV_FREQUENT_CHECK; 1860 EV_FREQUENT_CHECK;
1758 }
1759 } 1861 }
1760} 1862}
1761 1863
1762#if EV_IDLE_ENABLE 1864#if EV_IDLE_ENABLE
1865/* make idle watchers pending. this handles the "call-idle */
1866/* only when higher priorities are idle" logic */
1763inline_size void 1867inline_size void
1764idle_reify (EV_P) 1868idle_reify (EV_P)
1765{ 1869{
1766 if (expect_false (idleall)) 1870 if (expect_false (idleall))
1767 { 1871 {
1780 } 1884 }
1781 } 1885 }
1782} 1886}
1783#endif 1887#endif
1784 1888
1889/* make timers pending */
1785inline_size void 1890inline_size void
1786timers_reify (EV_P) 1891timers_reify (EV_P)
1787{ 1892{
1788 EV_FREQUENT_CHECK; 1893 EV_FREQUENT_CHECK;
1789 1894
1818 feed_reverse_done (EV_A_ EV_TIMEOUT); 1923 feed_reverse_done (EV_A_ EV_TIMEOUT);
1819 } 1924 }
1820} 1925}
1821 1926
1822#if EV_PERIODIC_ENABLE 1927#if EV_PERIODIC_ENABLE
1928/* make periodics pending */
1823inline_size void 1929inline_size void
1824periodics_reify (EV_P) 1930periodics_reify (EV_P)
1825{ 1931{
1826 EV_FREQUENT_CHECK; 1932 EV_FREQUENT_CHECK;
1827 1933
1874 1980
1875 feed_reverse_done (EV_A_ EV_PERIODIC); 1981 feed_reverse_done (EV_A_ EV_PERIODIC);
1876 } 1982 }
1877} 1983}
1878 1984
1985/* simply recalculate all periodics */
1986/* TODO: maybe ensure that at leats one event happens when jumping forward? */
1879static void noinline 1987static void noinline
1880periodics_reschedule (EV_P) 1988periodics_reschedule (EV_P)
1881{ 1989{
1882 int i; 1990 int i;
1883 1991
1896 2004
1897 reheap (periodics, periodiccnt); 2005 reheap (periodics, periodiccnt);
1898} 2006}
1899#endif 2007#endif
1900 2008
2009/* adjust all timers by a given offset */
1901static void noinline 2010static void noinline
1902timers_reschedule (EV_P_ ev_tstamp adjust) 2011timers_reschedule (EV_P_ ev_tstamp adjust)
1903{ 2012{
1904 int i; 2013 int i;
1905 2014
1909 ANHE_w (*he)->at += adjust; 2018 ANHE_w (*he)->at += adjust;
1910 ANHE_at_cache (*he); 2019 ANHE_at_cache (*he);
1911 } 2020 }
1912} 2021}
1913 2022
2023/* fetch new monotonic and realtime times from the kernel */
2024/* also detetc if there was a timejump, and act accordingly */
1914inline_speed void 2025inline_speed void
1915time_update (EV_P_ ev_tstamp max_block) 2026time_update (EV_P_ ev_tstamp max_block)
1916{ 2027{
1917 int i;
1918
1919#if EV_USE_MONOTONIC 2028#if EV_USE_MONOTONIC
1920 if (expect_true (have_monotonic)) 2029 if (expect_true (have_monotonic))
1921 { 2030 {
2031 int i;
1922 ev_tstamp odiff = rtmn_diff; 2032 ev_tstamp odiff = rtmn_diff;
1923 2033
1924 mn_now = get_clock (); 2034 mn_now = get_clock ();
1925 2035
1926 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ 2036 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
1976 2086
1977 mn_now = ev_rt_now; 2087 mn_now = ev_rt_now;
1978 } 2088 }
1979} 2089}
1980 2090
1981static int loop_done;
1982
1983void 2091void
1984ev_loop (EV_P_ int flags) 2092ev_loop (EV_P_ int flags)
1985{ 2093{
2094#if EV_MINIMAL < 2
2095 ++loop_depth;
2096#endif
2097
2098 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE));
2099
1986 loop_done = EVUNLOOP_CANCEL; 2100 loop_done = EVUNLOOP_CANCEL;
1987 2101
1988 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 2102 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
1989 2103
1990 do 2104 do
1991 { 2105 {
1992#if EV_VERIFY >= 2 2106#if EV_VERIFY >= 2
1993 ev_loop_verify (EV_A); 2107 ev_loop_verify (EV_A);
2006 /* we might have forked, so queue fork handlers */ 2120 /* we might have forked, so queue fork handlers */
2007 if (expect_false (postfork)) 2121 if (expect_false (postfork))
2008 if (forkcnt) 2122 if (forkcnt)
2009 { 2123 {
2010 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2124 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2011 call_pending (EV_A); 2125 EV_INVOKE_PENDING;
2012 } 2126 }
2013#endif 2127#endif
2014 2128
2015 /* queue prepare watchers (and execute them) */ 2129 /* queue prepare watchers (and execute them) */
2016 if (expect_false (preparecnt)) 2130 if (expect_false (preparecnt))
2017 { 2131 {
2018 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2132 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2019 call_pending (EV_A); 2133 EV_INVOKE_PENDING;
2020 } 2134 }
2135
2136 if (expect_false (loop_done))
2137 break;
2021 2138
2022 /* we might have forked, so reify kernel state if necessary */ 2139 /* we might have forked, so reify kernel state if necessary */
2023 if (expect_false (postfork)) 2140 if (expect_false (postfork))
2024 loop_fork (EV_A); 2141 loop_fork (EV_A);
2025 2142
2031 ev_tstamp waittime = 0.; 2148 ev_tstamp waittime = 0.;
2032 ev_tstamp sleeptime = 0.; 2149 ev_tstamp sleeptime = 0.;
2033 2150
2034 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2151 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
2035 { 2152 {
2153 /* remember old timestamp for io_blocktime calculation */
2154 ev_tstamp prev_mn_now = mn_now;
2155
2036 /* update time to cancel out callback processing overhead */ 2156 /* update time to cancel out callback processing overhead */
2037 time_update (EV_A_ 1e100); 2157 time_update (EV_A_ 1e100);
2158
2159 waittime = MAX_BLOCKTIME;
2038 2160
2039 if (timercnt) 2161 if (timercnt)
2040 { 2162 {
2041 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; 2163 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
2042 if (waittime > to) waittime = to; 2164 if (waittime > to) waittime = to;
2048 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; 2170 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
2049 if (waittime > to) waittime = to; 2171 if (waittime > to) waittime = to;
2050 } 2172 }
2051#endif 2173#endif
2052 2174
2175 /* don't let timeouts decrease the waittime below timeout_blocktime */
2053 if (expect_false (waittime < timeout_blocktime)) 2176 if (expect_false (waittime < timeout_blocktime))
2054 waittime = timeout_blocktime; 2177 waittime = timeout_blocktime;
2055 2178
2056 sleeptime = waittime - backend_fudge; 2179 /* extra check because io_blocktime is commonly 0 */
2057
2058 if (expect_true (sleeptime > io_blocktime)) 2180 if (expect_false (io_blocktime))
2059 sleeptime = io_blocktime;
2060
2061 if (sleeptime)
2062 { 2181 {
2182 sleeptime = io_blocktime - (mn_now - prev_mn_now);
2183
2184 if (sleeptime > waittime - backend_fudge)
2185 sleeptime = waittime - backend_fudge;
2186
2187 if (expect_true (sleeptime > 0.))
2188 {
2063 ev_sleep (sleeptime); 2189 ev_sleep (sleeptime);
2064 waittime -= sleeptime; 2190 waittime -= sleeptime;
2191 }
2065 } 2192 }
2066 } 2193 }
2067 2194
2195#if EV_MINIMAL < 2
2068 ++loop_count; 2196 ++loop_count;
2197#endif
2198 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */
2069 backend_poll (EV_A_ waittime); 2199 backend_poll (EV_A_ waittime);
2200 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */
2070 2201
2071 /* update ev_rt_now, do magic */ 2202 /* update ev_rt_now, do magic */
2072 time_update (EV_A_ waittime + sleeptime); 2203 time_update (EV_A_ waittime + sleeptime);
2073 } 2204 }
2074 2205
2085 2216
2086 /* queue check watchers, to be executed first */ 2217 /* queue check watchers, to be executed first */
2087 if (expect_false (checkcnt)) 2218 if (expect_false (checkcnt))
2088 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2219 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2089 2220
2090 call_pending (EV_A); 2221 EV_INVOKE_PENDING;
2091 } 2222 }
2092 while (expect_true ( 2223 while (expect_true (
2093 activecnt 2224 activecnt
2094 && !loop_done 2225 && !loop_done
2095 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2226 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2096 )); 2227 ));
2097 2228
2098 if (loop_done == EVUNLOOP_ONE) 2229 if (loop_done == EVUNLOOP_ONE)
2099 loop_done = EVUNLOOP_CANCEL; 2230 loop_done = EVUNLOOP_CANCEL;
2231
2232#if EV_MINIMAL < 2
2233 --loop_depth;
2234#endif
2100} 2235}
2101 2236
2102void 2237void
2103ev_unloop (EV_P_ int how) 2238ev_unloop (EV_P_ int how)
2104{ 2239{
2133ev_resume (EV_P) 2268ev_resume (EV_P)
2134{ 2269{
2135 ev_tstamp mn_prev = mn_now; 2270 ev_tstamp mn_prev = mn_now;
2136 2271
2137 ev_now_update (EV_A); 2272 ev_now_update (EV_A);
2138 printf ("update %f\n", mn_now - mn_prev);//D
2139 timers_reschedule (EV_A_ mn_now - mn_prev); 2273 timers_reschedule (EV_A_ mn_now - mn_prev);
2274#if EV_PERIODIC_ENABLE
2275 /* TODO: really do this? */
2140 periodics_reschedule (EV_A); 2276 periodics_reschedule (EV_A);
2277#endif
2141} 2278}
2142 2279
2143/*****************************************************************************/ 2280/*****************************************************************************/
2281/* singly-linked list management, used when the expected list length is short */
2144 2282
2145inline_size void 2283inline_size void
2146wlist_add (WL *head, WL elem) 2284wlist_add (WL *head, WL elem)
2147{ 2285{
2148 elem->next = *head; 2286 elem->next = *head;
2162 2300
2163 head = &(*head)->next; 2301 head = &(*head)->next;
2164 } 2302 }
2165} 2303}
2166 2304
2305/* internal, faster, version of ev_clear_pending */
2167inline_speed void 2306inline_speed void
2168clear_pending (EV_P_ W w) 2307clear_pending (EV_P_ W w)
2169{ 2308{
2170 if (w->pending) 2309 if (w->pending)
2171 { 2310 {
2172 pendings [ABSPRI (w)][w->pending - 1].w = 0; 2311 pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w;
2173 w->pending = 0; 2312 w->pending = 0;
2174 } 2313 }
2175} 2314}
2176 2315
2177int 2316int
2181 int pending = w_->pending; 2320 int pending = w_->pending;
2182 2321
2183 if (expect_true (pending)) 2322 if (expect_true (pending))
2184 { 2323 {
2185 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; 2324 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
2325 p->w = (W)&pending_w;
2186 w_->pending = 0; 2326 w_->pending = 0;
2187 p->w = 0;
2188 return p->events; 2327 return p->events;
2189 } 2328 }
2190 else 2329 else
2191 return 0; 2330 return 0;
2192} 2331}
2193 2332
2194inline_size void 2333inline_size void
2195pri_adjust (EV_P_ W w) 2334pri_adjust (EV_P_ W w)
2196{ 2335{
2197 int pri = w->priority; 2336 int pri = ev_priority (w);
2198 pri = pri < EV_MINPRI ? EV_MINPRI : pri; 2337 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2199 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; 2338 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2200 w->priority = pri; 2339 ev_set_priority (w, pri);
2201} 2340}
2202 2341
2203inline_speed void 2342inline_speed void
2204ev_start (EV_P_ W w, int active) 2343ev_start (EV_P_ W w, int active)
2205{ 2344{
2232 2371
2233 ev_start (EV_A_ (W)w, 1); 2372 ev_start (EV_A_ (W)w, 1);
2234 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 2373 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
2235 wlist_add (&anfds[fd].head, (WL)w); 2374 wlist_add (&anfds[fd].head, (WL)w);
2236 2375
2237 fd_change (EV_A_ fd, w->events & EV__IOFDSET | 1); 2376 fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
2238 w->events &= ~EV__IOFDSET; 2377 w->events &= ~EV__IOFDSET;
2239 2378
2240 EV_FREQUENT_CHECK; 2379 EV_FREQUENT_CHECK;
2241} 2380}
2242 2381
3198 } 3337 }
3199} 3338}
3200 3339
3201/*****************************************************************************/ 3340/*****************************************************************************/
3202 3341
3203#if 0 3342#if EV_WALK_ENABLE
3204void 3343void
3205ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) 3344ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w))
3206{ 3345{
3207 int i, j; 3346 int i, j;
3208 ev_watcher_list *wl, *wn; 3347 ev_watcher_list *wl, *wn;
3224#if EV_USE_INOTIFY 3363#if EV_USE_INOTIFY
3225 if (ev_cb ((ev_io *)wl) == infy_cb) 3364 if (ev_cb ((ev_io *)wl) == infy_cb)
3226 ; 3365 ;
3227 else 3366 else
3228#endif 3367#endif
3229 if ((ev_io *)wl != &pipeev) 3368 if ((ev_io *)wl != &pipe_w)
3230 if (types & EV_IO) 3369 if (types & EV_IO)
3231 cb (EV_A_ EV_IO, wl); 3370 cb (EV_A_ EV_IO, wl);
3232 3371
3233 wl = wn; 3372 wl = wn;
3234 } 3373 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines