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

Comparing libev/ev.c (file contents):
Revision 1.287 by root, Mon Apr 20 19:45:58 2009 UTC vs.
Revision 1.297 by root, Fri Jul 10 00:36:21 2009 UTC

57# endif 57# endif
58# ifndef EV_USE_MONOTONIC 58# ifndef EV_USE_MONOTONIC
59# define EV_USE_MONOTONIC 1 59# define EV_USE_MONOTONIC 1
60# endif 60# endif
61# endif 61# endif
62# elif !defined(EV_USE_CLOCK_SYSCALL)
63# define EV_USE_CLOCK_SYSCALL 0
62# endif 64# endif
63 65
64# if HAVE_CLOCK_GETTIME 66# if HAVE_CLOCK_GETTIME
65# ifndef EV_USE_MONOTONIC 67# ifndef EV_USE_MONOTONIC
66# define EV_USE_MONOTONIC 1 68# define EV_USE_MONOTONIC 1
282 284
283#ifndef EV_HEAP_CACHE_AT 285#ifndef EV_HEAP_CACHE_AT
284# define EV_HEAP_CACHE_AT !EV_MINIMAL 286# define EV_HEAP_CACHE_AT !EV_MINIMAL
285#endif 287#endif
286 288
289/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
290/* which makes programs even slower. might work on other unices, too. */
291#if EV_USE_CLOCK_SYSCALL
292# include <syscall.h>
293# ifdef SYS_clock_gettime
294# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
295# undef EV_USE_MONOTONIC
296# define EV_USE_MONOTONIC 1
297# else
298# undef EV_USE_CLOCK_SYSCALL
299# define EV_USE_CLOCK_SYSCALL 0
300# endif
301#endif
302
287/* this block fixes any misconfiguration where we know we run into trouble otherwise */ 303/* this block fixes any misconfiguration where we know we run into trouble otherwise */
288 304
289#ifndef CLOCK_MONOTONIC 305#ifndef CLOCK_MONOTONIC
290# undef EV_USE_MONOTONIC 306# undef EV_USE_MONOTONIC
291# define EV_USE_MONOTONIC 0 307# define EV_USE_MONOTONIC 0
320 336
321#if EV_SELECT_IS_WINSOCKET 337#if EV_SELECT_IS_WINSOCKET
322# include <winsock.h> 338# include <winsock.h>
323#endif 339#endif
324 340
325/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
326/* which makes programs even slower. might work on other unices, too. */
327#if EV_USE_CLOCK_SYSCALL
328# include <syscall.h>
329# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
330# undef EV_USE_MONOTONIC
331# define EV_USE_MONOTONIC 1
332#endif
333
334#if EV_USE_EVENTFD 341#if EV_USE_EVENTFD
335/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 342/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
336# include <stdint.h> 343# include <stdint.h>
337# ifdef __cplusplus 344# ifdef __cplusplus
338extern "C" { 345extern "C" {
384# define inline_speed static noinline 391# define inline_speed static noinline
385#else 392#else
386# define inline_speed static inline 393# define inline_speed static inline
387#endif 394#endif
388 395
389#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 396#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
397
398#if EV_MINPRI == EV_MAXPRI
399# define ABSPRI(w) (((W)w), 0)
400#else
390#define ABSPRI(w) (((W)w)->priority - EV_MINPRI) 401# define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
402#endif
391 403
392#define EMPTY /* required for microsofts broken pseudo-c compiler */ 404#define EMPTY /* required for microsofts broken pseudo-c compiler */
393#define EMPTY2(a,b) /* used to suppress some warnings */ 405#define EMPTY2(a,b) /* used to suppress some warnings */
394 406
395typedef ev_watcher *W; 407typedef ev_watcher *W;
478#define ev_malloc(size) ev_realloc (0, (size)) 490#define ev_malloc(size) ev_realloc (0, (size))
479#define ev_free(ptr) ev_realloc ((ptr), 0) 491#define ev_free(ptr) ev_realloc ((ptr), 0)
480 492
481/*****************************************************************************/ 493/*****************************************************************************/
482 494
495/* file descriptor info structure */
483typedef struct 496typedef struct
484{ 497{
485 WL head; 498 WL head;
486 unsigned char events; 499 unsigned char events; /* the events watched for */
487 unsigned char reify; 500 unsigned char reify; /* flag set when this ANFD needs reification */
488 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ 501 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */
489 unsigned char unused; 502 unsigned char unused;
490#if EV_USE_EPOLL 503#if EV_USE_EPOLL
491 unsigned int egen; /* generation counter to counter epoll bugs */ 504 unsigned int egen; /* generation counter to counter epoll bugs */
492#endif 505#endif
493#if EV_SELECT_IS_WINSOCKET 506#if EV_SELECT_IS_WINSOCKET
494 SOCKET handle; 507 SOCKET handle;
495#endif 508#endif
496} ANFD; 509} ANFD;
497 510
511/* stores the pending event set for a given watcher */
498typedef struct 512typedef struct
499{ 513{
500 W w; 514 W w;
501 int events; 515 int events; /* the pending event set for the given watcher */
502} ANPENDING; 516} ANPENDING;
503 517
504#if EV_USE_INOTIFY 518#if EV_USE_INOTIFY
505/* hash table entry per inotify-id */ 519/* hash table entry per inotify-id */
506typedef struct 520typedef struct
509} ANFS; 523} ANFS;
510#endif 524#endif
511 525
512/* Heap Entry */ 526/* Heap Entry */
513#if EV_HEAP_CACHE_AT 527#if EV_HEAP_CACHE_AT
528 /* a heap element */
514 typedef struct { 529 typedef struct {
515 ev_tstamp at; 530 ev_tstamp at;
516 WT w; 531 WT w;
517 } ANHE; 532 } ANHE;
518 533
519 #define ANHE_w(he) (he).w /* access watcher, read-write */ 534 #define ANHE_w(he) (he).w /* access watcher, read-write */
520 #define ANHE_at(he) (he).at /* access cached at, read-only */ 535 #define ANHE_at(he) (he).at /* access cached at, read-only */
521 #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */ 536 #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */
522#else 537#else
538 /* a heap element */
523 typedef WT ANHE; 539 typedef WT ANHE;
524 540
525 #define ANHE_w(he) (he) 541 #define ANHE_w(he) (he)
526 #define ANHE_at(he) (he)->at 542 #define ANHE_at(he) (he)->at
527 #define ANHE_at_cache(he) 543 #define ANHE_at_cache(he)
551 567
552 static int ev_default_loop_ptr; 568 static int ev_default_loop_ptr;
553 569
554#endif 570#endif
555 571
572#if EV_MINIMAL < 2
573# define EV_SUSPEND_CB if (expect_false (suspend_cb)) suspend_cb (EV_A)
574# define EV_RESUME_CB if (expect_false (resume_cb )) resume_cb (EV_A)
575# define EV_INVOKE_PENDING invoke_cb (EV_A)
576#else
577# define EV_SUSPEND_CB (void)0
578# define EV_RESUME_CB (void)0
579# define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
580#endif
581
556/*****************************************************************************/ 582/*****************************************************************************/
557 583
584#ifndef EV_HAVE_EV_TIME
558ev_tstamp 585ev_tstamp
559ev_time (void) 586ev_time (void)
560{ 587{
561#if EV_USE_REALTIME 588#if EV_USE_REALTIME
562 if (expect_true (have_realtime)) 589 if (expect_true (have_realtime))
569 596
570 struct timeval tv; 597 struct timeval tv;
571 gettimeofday (&tv, 0); 598 gettimeofday (&tv, 0);
572 return tv.tv_sec + tv.tv_usec * 1e-6; 599 return tv.tv_sec + tv.tv_usec * 1e-6;
573} 600}
601#endif
574 602
575inline_size ev_tstamp 603inline_size ev_tstamp
576get_clock (void) 604get_clock (void)
577{ 605{
578#if EV_USE_MONOTONIC 606#if EV_USE_MONOTONIC
614 642
615 tv.tv_sec = (time_t)delay; 643 tv.tv_sec = (time_t)delay;
616 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 644 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
617 645
618 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ 646 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
619 /* somehting nto guaranteed by newer posix versions, but guaranteed */ 647 /* somehting not guaranteed by newer posix versions, but guaranteed */
620 /* by older ones */ 648 /* by older ones */
621 select (0, 0, 0, 0, &tv); 649 select (0, 0, 0, 0, &tv);
622#endif 650#endif
623 } 651 }
624} 652}
625 653
626/*****************************************************************************/ 654/*****************************************************************************/
627 655
628#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ 656#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
629 657
658/* find a suitable new size for the given array, */
659/* hopefully by rounding to a ncie-to-malloc size */
630inline_size int 660inline_size int
631array_nextsize (int elem, int cur, int cnt) 661array_nextsize (int elem, int cur, int cnt)
632{ 662{
633 int ncur = cur + 1; 663 int ncur = cur + 1;
634 664
680#define array_free(stem, idx) \ 710#define array_free(stem, idx) \
681 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0 711 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0
682 712
683/*****************************************************************************/ 713/*****************************************************************************/
684 714
715/* dummy callback for pending events */
716static void noinline
717pendingcb (EV_P_ ev_prepare *w, int revents)
718{
719}
720
685void noinline 721void noinline
686ev_feed_event (EV_P_ void *w, int revents) 722ev_feed_event (EV_P_ void *w, int revents)
687{ 723{
688 W w_ = (W)w; 724 W w_ = (W)w;
689 int pri = ABSPRI (w_); 725 int pri = ABSPRI (w_);
745{ 781{
746 if (fd >= 0 && fd < anfdmax) 782 if (fd >= 0 && fd < anfdmax)
747 fd_event (EV_A_ fd, revents); 783 fd_event (EV_A_ fd, revents);
748} 784}
749 785
786/* make sure the external fd watch events are in-sync */
787/* with the kernel/libev internal state */
750inline_size void 788inline_size void
751fd_reify (EV_P) 789fd_reify (EV_P)
752{ 790{
753 int i; 791 int i;
754 792
789 } 827 }
790 828
791 fdchangecnt = 0; 829 fdchangecnt = 0;
792} 830}
793 831
832/* something about the given fd changed */
794inline_size void 833inline_size void
795fd_change (EV_P_ int fd, int flags) 834fd_change (EV_P_ int fd, int flags)
796{ 835{
797 unsigned char reify = anfds [fd].reify; 836 unsigned char reify = anfds [fd].reify;
798 anfds [fd].reify |= flags; 837 anfds [fd].reify |= flags;
803 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 842 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
804 fdchanges [fdchangecnt - 1] = fd; 843 fdchanges [fdchangecnt - 1] = fd;
805 } 844 }
806} 845}
807 846
847/* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
808inline_speed void 848inline_speed void
809fd_kill (EV_P_ int fd) 849fd_kill (EV_P_ int fd)
810{ 850{
811 ev_io *w; 851 ev_io *w;
812 852
815 ev_io_stop (EV_A_ w); 855 ev_io_stop (EV_A_ w);
816 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 856 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
817 } 857 }
818} 858}
819 859
860/* check whether the given fd is atcually valid, for error recovery */
820inline_size int 861inline_size int
821fd_valid (int fd) 862fd_valid (int fd)
822{ 863{
823#ifdef _WIN32 864#ifdef _WIN32
824 return _get_osfhandle (fd) != -1; 865 return _get_osfhandle (fd) != -1;
989 1030
990 heap [k] = he; 1031 heap [k] = he;
991 ev_active (ANHE_w (he)) = k; 1032 ev_active (ANHE_w (he)) = k;
992} 1033}
993 1034
1035/* move an element suitably so it is in a correct place */
994inline_size void 1036inline_size void
995adjustheap (ANHE *heap, int N, int k) 1037adjustheap (ANHE *heap, int N, int k)
996{ 1038{
997 if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) 1039 if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k]))
998 upheap (heap, k); 1040 upheap (heap, k);
1012 upheap (heap, i + HEAP0); 1054 upheap (heap, i + HEAP0);
1013} 1055}
1014 1056
1015/*****************************************************************************/ 1057/*****************************************************************************/
1016 1058
1059/* associate signal watchers to a signal signal */
1017typedef struct 1060typedef struct
1018{ 1061{
1019 WL head; 1062 WL head;
1020 EV_ATOMIC_T gotsig; 1063 EV_ATOMIC_T gotsig;
1021} ANSIG; 1064} ANSIG;
1025 1068
1026static EV_ATOMIC_T gotsig; 1069static EV_ATOMIC_T gotsig;
1027 1070
1028/*****************************************************************************/ 1071/*****************************************************************************/
1029 1072
1073/* used to prepare libev internal fd's */
1074/* this is not fork-safe */
1030inline_speed void 1075inline_speed void
1031fd_intern (int fd) 1076fd_intern (int fd)
1032{ 1077{
1033#ifdef _WIN32 1078#ifdef _WIN32
1034 unsigned long arg = 1; 1079 unsigned long arg = 1;
1040} 1085}
1041 1086
1042static void noinline 1087static void noinline
1043evpipe_init (EV_P) 1088evpipe_init (EV_P)
1044{ 1089{
1045 if (!ev_is_active (&pipeev)) 1090 if (!ev_is_active (&pipe_w))
1046 { 1091 {
1047#if EV_USE_EVENTFD 1092#if EV_USE_EVENTFD
1048 if ((evfd = eventfd (0, 0)) >= 0) 1093 if ((evfd = eventfd (0, 0)) >= 0)
1049 { 1094 {
1050 evpipe [0] = -1; 1095 evpipe [0] = -1;
1051 fd_intern (evfd); 1096 fd_intern (evfd);
1052 ev_io_set (&pipeev, evfd, EV_READ); 1097 ev_io_set (&pipe_w, evfd, EV_READ);
1053 } 1098 }
1054 else 1099 else
1055#endif 1100#endif
1056 { 1101 {
1057 while (pipe (evpipe)) 1102 while (pipe (evpipe))
1058 ev_syserr ("(libev) error creating signal/async pipe"); 1103 ev_syserr ("(libev) error creating signal/async pipe");
1059 1104
1060 fd_intern (evpipe [0]); 1105 fd_intern (evpipe [0]);
1061 fd_intern (evpipe [1]); 1106 fd_intern (evpipe [1]);
1062 ev_io_set (&pipeev, evpipe [0], EV_READ); 1107 ev_io_set (&pipe_w, evpipe [0], EV_READ);
1063 } 1108 }
1064 1109
1065 ev_io_start (EV_A_ &pipeev); 1110 ev_io_start (EV_A_ &pipe_w);
1066 ev_unref (EV_A); /* watcher should not keep loop alive */ 1111 ev_unref (EV_A); /* watcher should not keep loop alive */
1067 } 1112 }
1068} 1113}
1069 1114
1070inline_size void 1115inline_size void
1088 1133
1089 errno = old_errno; 1134 errno = old_errno;
1090 } 1135 }
1091} 1136}
1092 1137
1138/* called whenever the libev signal pipe */
1139/* got some events (signal, async) */
1093static void 1140static void
1094pipecb (EV_P_ ev_io *iow, int revents) 1141pipecb (EV_P_ ev_io *iow, int revents)
1095{ 1142{
1096#if EV_USE_EVENTFD 1143#if EV_USE_EVENTFD
1097 if (evfd >= 0) 1144 if (evfd >= 0)
1179 1226
1180#ifndef WIFCONTINUED 1227#ifndef WIFCONTINUED
1181# define WIFCONTINUED(status) 0 1228# define WIFCONTINUED(status) 0
1182#endif 1229#endif
1183 1230
1231/* handle a single child status event */
1184inline_speed void 1232inline_speed void
1185child_reap (EV_P_ int chain, int pid, int status) 1233child_reap (EV_P_ int chain, int pid, int status)
1186{ 1234{
1187 ev_child *w; 1235 ev_child *w;
1188 int traced = WIFSTOPPED (status) || WIFCONTINUED (status); 1236 int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
1202 1250
1203#ifndef WCONTINUED 1251#ifndef WCONTINUED
1204# define WCONTINUED 0 1252# define WCONTINUED 0
1205#endif 1253#endif
1206 1254
1255/* called on sigchld etc., calls waitpid */
1207static void 1256static void
1208childcb (EV_P_ ev_signal *sw, int revents) 1257childcb (EV_P_ ev_signal *sw, int revents)
1209{ 1258{
1210 int pid, status; 1259 int pid, status;
1211 1260
1318ev_backend (EV_P) 1367ev_backend (EV_P)
1319{ 1368{
1320 return backend; 1369 return backend;
1321} 1370}
1322 1371
1372#if EV_MINIMAL < 2
1323unsigned int 1373unsigned int
1324ev_loop_count (EV_P) 1374ev_loop_count (EV_P)
1325{ 1375{
1326 return loop_count; 1376 return loop_count;
1327} 1377}
1328 1378
1379unsigned int
1380ev_loop_depth (EV_P)
1381{
1382 return loop_depth;
1383}
1384
1329void 1385void
1330ev_set_io_collect_interval (EV_P_ ev_tstamp interval) 1386ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
1331{ 1387{
1332 io_blocktime = interval; 1388 io_blocktime = interval;
1333} 1389}
1336ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) 1392ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1337{ 1393{
1338 timeout_blocktime = interval; 1394 timeout_blocktime = interval;
1339} 1395}
1340 1396
1397void
1398ev_set_userdata (EV_P_ void *data)
1399{
1400 userdata = data;
1401}
1402
1403void *
1404ev_userdata (EV_P)
1405{
1406 return userdata;
1407}
1408
1409void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P))
1410{
1411 invoke_cb = invoke_pending_cb;
1412}
1413
1414void ev_set_blocking_cb (EV_P_ void (*suspend_cb_)(EV_P), void (*resume_cb_)(EV_P))
1415{
1416 suspend_cb = suspend_cb_;
1417 resume_cb = resume_cb_;
1418}
1419#endif
1420
1421/* initialise a loop structure, must be zero-initialised */
1341static void noinline 1422static void noinline
1342loop_init (EV_P_ unsigned int flags) 1423loop_init (EV_P_ unsigned int flags)
1343{ 1424{
1344 if (!backend) 1425 if (!backend)
1345 { 1426 {
1365 1446
1366 ev_rt_now = ev_time (); 1447 ev_rt_now = ev_time ();
1367 mn_now = get_clock (); 1448 mn_now = get_clock ();
1368 now_floor = mn_now; 1449 now_floor = mn_now;
1369 rtmn_diff = ev_rt_now - mn_now; 1450 rtmn_diff = ev_rt_now - mn_now;
1451#if EV_MINIMAL < 2
1452 invoke_cb = ev_invoke_pending;
1453#endif
1370 1454
1371 io_blocktime = 0.; 1455 io_blocktime = 0.;
1372 timeout_blocktime = 0.; 1456 timeout_blocktime = 0.;
1373 backend = 0; 1457 backend = 0;
1374 backend_fd = -1; 1458 backend_fd = -1;
1405#endif 1489#endif
1406#if EV_USE_SELECT 1490#if EV_USE_SELECT
1407 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); 1491 if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
1408#endif 1492#endif
1409 1493
1494 ev_prepare_init (&pending_w, pendingcb);
1495
1410 ev_init (&pipeev, pipecb); 1496 ev_init (&pipe_w, pipecb);
1411 ev_set_priority (&pipeev, EV_MAXPRI); 1497 ev_set_priority (&pipe_w, EV_MAXPRI);
1412 } 1498 }
1413} 1499}
1414 1500
1501/* free up a loop structure */
1415static void noinline 1502static void noinline
1416loop_destroy (EV_P) 1503loop_destroy (EV_P)
1417{ 1504{
1418 int i; 1505 int i;
1419 1506
1420 if (ev_is_active (&pipeev)) 1507 if (ev_is_active (&pipe_w))
1421 { 1508 {
1422 ev_ref (EV_A); /* signal watcher */ 1509 ev_ref (EV_A); /* signal watcher */
1423 ev_io_stop (EV_A_ &pipeev); 1510 ev_io_stop (EV_A_ &pipe_w);
1424 1511
1425#if EV_USE_EVENTFD 1512#if EV_USE_EVENTFD
1426 if (evfd >= 0) 1513 if (evfd >= 0)
1427 close (evfd); 1514 close (evfd);
1428#endif 1515#endif
1505#endif 1592#endif
1506#if EV_USE_INOTIFY 1593#if EV_USE_INOTIFY
1507 infy_fork (EV_A); 1594 infy_fork (EV_A);
1508#endif 1595#endif
1509 1596
1510 if (ev_is_active (&pipeev)) 1597 if (ev_is_active (&pipe_w))
1511 { 1598 {
1512 /* this "locks" the handlers against writing to the pipe */ 1599 /* this "locks" the handlers against writing to the pipe */
1513 /* while we modify the fd vars */ 1600 /* while we modify the fd vars */
1514 gotsig = 1; 1601 gotsig = 1;
1515#if EV_ASYNC_ENABLE 1602#if EV_ASYNC_ENABLE
1516 gotasync = 1; 1603 gotasync = 1;
1517#endif 1604#endif
1518 1605
1519 ev_ref (EV_A); 1606 ev_ref (EV_A);
1520 ev_io_stop (EV_A_ &pipeev); 1607 ev_io_stop (EV_A_ &pipe_w);
1521 1608
1522#if EV_USE_EVENTFD 1609#if EV_USE_EVENTFD
1523 if (evfd >= 0) 1610 if (evfd >= 0)
1524 close (evfd); 1611 close (evfd);
1525#endif 1612#endif
1530 close (evpipe [1]); 1617 close (evpipe [1]);
1531 } 1618 }
1532 1619
1533 evpipe_init (EV_A); 1620 evpipe_init (EV_A);
1534 /* now iterate over everything, in case we missed something */ 1621 /* now iterate over everything, in case we missed something */
1535 pipecb (EV_A_ &pipeev, EV_READ); 1622 pipecb (EV_A_ &pipe_w, EV_READ);
1536 } 1623 }
1537 1624
1538 postfork = 0; 1625 postfork = 0;
1539} 1626}
1540 1627
1565void 1652void
1566ev_loop_fork (EV_P) 1653ev_loop_fork (EV_P)
1567{ 1654{
1568 postfork = 1; /* must be in line with ev_default_fork */ 1655 postfork = 1; /* must be in line with ev_default_fork */
1569} 1656}
1657#endif /* multiplicity */
1570 1658
1571#if EV_VERIFY 1659#if EV_VERIFY
1572static void noinline 1660static void noinline
1573verify_watcher (EV_P_ W w) 1661verify_watcher (EV_P_ W w)
1574{ 1662{
1602 verify_watcher (EV_A_ ws [cnt]); 1690 verify_watcher (EV_A_ ws [cnt]);
1603 } 1691 }
1604} 1692}
1605#endif 1693#endif
1606 1694
1695#if EV_MINIMAL < 2
1607void 1696void
1608ev_loop_verify (EV_P) 1697ev_loop_verify (EV_P)
1609{ 1698{
1610#if EV_VERIFY 1699#if EV_VERIFY
1611 int i; 1700 int i;
1664 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1753 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) 1754 for (signum = signalmax; signum--; ) if (signals [signum].gotsig)
1666# endif 1755# endif
1667#endif 1756#endif
1668} 1757}
1669 1758#endif
1670#endif /* multiplicity */
1671 1759
1672#if EV_MULTIPLICITY 1760#if EV_MULTIPLICITY
1673struct ev_loop * 1761struct ev_loop *
1674ev_default_loop_init (unsigned int flags) 1762ev_default_loop_init (unsigned int flags)
1675#else 1763#else
1736ev_invoke (EV_P_ void *w, int revents) 1824ev_invoke (EV_P_ void *w, int revents)
1737{ 1825{
1738 EV_CB_INVOKE ((W)w, revents); 1826 EV_CB_INVOKE ((W)w, revents);
1739} 1827}
1740 1828
1741inline_speed void 1829void noinline
1742call_pending (EV_P) 1830ev_invoke_pending (EV_P)
1743{ 1831{
1744 int pri; 1832 int pri;
1745 1833
1746 for (pri = NUMPRI; pri--; ) 1834 for (pri = NUMPRI; pri--; )
1747 while (pendingcnt [pri]) 1835 while (pendingcnt [pri])
1748 { 1836 {
1749 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1837 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1750 1838
1751 if (expect_true (p->w))
1752 {
1753 /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/ 1839 /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/
1840 /* ^ this is no longer true, as pending_w could be here */
1754 1841
1755 p->w->pending = 0; 1842 p->w->pending = 0;
1756 EV_CB_INVOKE (p->w, p->events); 1843 EV_CB_INVOKE (p->w, p->events);
1757 EV_FREQUENT_CHECK; 1844 EV_FREQUENT_CHECK;
1758 }
1759 } 1845 }
1760} 1846}
1761 1847
1762#if EV_IDLE_ENABLE 1848#if EV_IDLE_ENABLE
1849/* make idle watchers pending. this handles the "call-idle */
1850/* only when higher priorities are idle" logic */
1763inline_size void 1851inline_size void
1764idle_reify (EV_P) 1852idle_reify (EV_P)
1765{ 1853{
1766 if (expect_false (idleall)) 1854 if (expect_false (idleall))
1767 { 1855 {
1780 } 1868 }
1781 } 1869 }
1782} 1870}
1783#endif 1871#endif
1784 1872
1873/* make timers pending */
1785inline_size void 1874inline_size void
1786timers_reify (EV_P) 1875timers_reify (EV_P)
1787{ 1876{
1788 EV_FREQUENT_CHECK; 1877 EV_FREQUENT_CHECK;
1789 1878
1818 feed_reverse_done (EV_A_ EV_TIMEOUT); 1907 feed_reverse_done (EV_A_ EV_TIMEOUT);
1819 } 1908 }
1820} 1909}
1821 1910
1822#if EV_PERIODIC_ENABLE 1911#if EV_PERIODIC_ENABLE
1912/* make periodics pending */
1823inline_size void 1913inline_size void
1824periodics_reify (EV_P) 1914periodics_reify (EV_P)
1825{ 1915{
1826 EV_FREQUENT_CHECK; 1916 EV_FREQUENT_CHECK;
1827 1917
1874 1964
1875 feed_reverse_done (EV_A_ EV_PERIODIC); 1965 feed_reverse_done (EV_A_ EV_PERIODIC);
1876 } 1966 }
1877} 1967}
1878 1968
1969/* simply recalculate all periodics */
1970/* TODO: maybe ensure that at leats one event happens when jumping forward? */
1879static void noinline 1971static void noinline
1880periodics_reschedule (EV_P) 1972periodics_reschedule (EV_P)
1881{ 1973{
1882 int i; 1974 int i;
1883 1975
1896 1988
1897 reheap (periodics, periodiccnt); 1989 reheap (periodics, periodiccnt);
1898} 1990}
1899#endif 1991#endif
1900 1992
1993/* adjust all timers by a given offset */
1901static void noinline 1994static void noinline
1902timers_reschedule (EV_P_ ev_tstamp adjust) 1995timers_reschedule (EV_P_ ev_tstamp adjust)
1903{ 1996{
1904 int i; 1997 int i;
1905 1998
1909 ANHE_w (*he)->at += adjust; 2002 ANHE_w (*he)->at += adjust;
1910 ANHE_at_cache (*he); 2003 ANHE_at_cache (*he);
1911 } 2004 }
1912} 2005}
1913 2006
2007/* fetch new monotonic and realtime times from the kernel */
2008/* also detetc if there was a timejump, and act accordingly */
1914inline_speed void 2009inline_speed void
1915time_update (EV_P_ ev_tstamp max_block) 2010time_update (EV_P_ ev_tstamp max_block)
1916{ 2011{
1917 int i;
1918
1919#if EV_USE_MONOTONIC 2012#if EV_USE_MONOTONIC
1920 if (expect_true (have_monotonic)) 2013 if (expect_true (have_monotonic))
1921 { 2014 {
2015 int i;
1922 ev_tstamp odiff = rtmn_diff; 2016 ev_tstamp odiff = rtmn_diff;
1923 2017
1924 mn_now = get_clock (); 2018 mn_now = get_clock ();
1925 2019
1926 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */ 2020 /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
1976 2070
1977 mn_now = ev_rt_now; 2071 mn_now = ev_rt_now;
1978 } 2072 }
1979} 2073}
1980 2074
1981static int loop_done;
1982
1983void 2075void
1984ev_loop (EV_P_ int flags) 2076ev_loop (EV_P_ int flags)
1985{ 2077{
2078#if EV_MINIMAL < 2
2079 ++loop_depth;
2080#endif
2081
1986 loop_done = EVUNLOOP_CANCEL; 2082 loop_done = EVUNLOOP_CANCEL;
1987 2083
1988 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 2084 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
1989 2085
1990 do 2086 do
1991 { 2087 {
1992#if EV_VERIFY >= 2 2088#if EV_VERIFY >= 2
1993 ev_loop_verify (EV_A); 2089 ev_loop_verify (EV_A);
2006 /* we might have forked, so queue fork handlers */ 2102 /* we might have forked, so queue fork handlers */
2007 if (expect_false (postfork)) 2103 if (expect_false (postfork))
2008 if (forkcnt) 2104 if (forkcnt)
2009 { 2105 {
2010 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 2106 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2011 call_pending (EV_A); 2107 EV_INVOKE_PENDING;
2012 } 2108 }
2013#endif 2109#endif
2014 2110
2015 /* queue prepare watchers (and execute them) */ 2111 /* queue prepare watchers (and execute them) */
2016 if (expect_false (preparecnt)) 2112 if (expect_false (preparecnt))
2017 { 2113 {
2018 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 2114 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2019 call_pending (EV_A); 2115 EV_INVOKE_PENDING;
2020 } 2116 }
2021 2117
2022 /* we might have forked, so reify kernel state if necessary */ 2118 /* we might have forked, so reify kernel state if necessary */
2023 if (expect_false (postfork)) 2119 if (expect_false (postfork))
2024 loop_fork (EV_A); 2120 loop_fork (EV_A);
2031 ev_tstamp waittime = 0.; 2127 ev_tstamp waittime = 0.;
2032 ev_tstamp sleeptime = 0.; 2128 ev_tstamp sleeptime = 0.;
2033 2129
2034 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2130 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
2035 { 2131 {
2132 /* remember old timestamp for io_blocktime calculation */
2133 ev_tstamp prev_mn_now = mn_now;
2134
2036 /* update time to cancel out callback processing overhead */ 2135 /* update time to cancel out callback processing overhead */
2037 time_update (EV_A_ 1e100); 2136 time_update (EV_A_ 1e100);
2038 2137
2039 waittime = MAX_BLOCKTIME; 2138 waittime = MAX_BLOCKTIME;
2040 2139
2050 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; 2149 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
2051 if (waittime > to) waittime = to; 2150 if (waittime > to) waittime = to;
2052 } 2151 }
2053#endif 2152#endif
2054 2153
2154 /* don't let timeouts decrease the waittime below timeout_blocktime */
2055 if (expect_false (waittime < timeout_blocktime)) 2155 if (expect_false (waittime < timeout_blocktime))
2056 waittime = timeout_blocktime; 2156 waittime = timeout_blocktime;
2057 2157
2058 sleeptime = waittime - backend_fudge; 2158 /* extra check because io_blocktime is commonly 0 */
2059
2060 if (expect_true (sleeptime > io_blocktime)) 2159 if (expect_false (io_blocktime))
2061 sleeptime = io_blocktime;
2062
2063 if (sleeptime)
2064 { 2160 {
2161 sleeptime = io_blocktime - (mn_now - prev_mn_now);
2162
2163 if (sleeptime > waittime - backend_fudge)
2164 sleeptime = waittime - backend_fudge;
2165
2166 if (expect_true (sleeptime > 0.))
2167 {
2065 ev_sleep (sleeptime); 2168 ev_sleep (sleeptime);
2066 waittime -= sleeptime; 2169 waittime -= sleeptime;
2170 }
2067 } 2171 }
2068 } 2172 }
2069 2173
2174#if EV_MINIMAL < 2
2070 ++loop_count; 2175 ++loop_count;
2176#endif
2071 backend_poll (EV_A_ waittime); 2177 backend_poll (EV_A_ waittime);
2072 2178
2073 /* update ev_rt_now, do magic */ 2179 /* update ev_rt_now, do magic */
2074 time_update (EV_A_ waittime + sleeptime); 2180 time_update (EV_A_ waittime + sleeptime);
2075 } 2181 }
2087 2193
2088 /* queue check watchers, to be executed first */ 2194 /* queue check watchers, to be executed first */
2089 if (expect_false (checkcnt)) 2195 if (expect_false (checkcnt))
2090 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 2196 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2091 2197
2092 call_pending (EV_A); 2198 EV_INVOKE_PENDING;
2093 } 2199 }
2094 while (expect_true ( 2200 while (expect_true (
2095 activecnt 2201 activecnt
2096 && !loop_done 2202 && !loop_done
2097 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2203 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK))
2098 )); 2204 ));
2099 2205
2100 if (loop_done == EVUNLOOP_ONE) 2206 if (loop_done == EVUNLOOP_ONE)
2101 loop_done = EVUNLOOP_CANCEL; 2207 loop_done = EVUNLOOP_CANCEL;
2208
2209#if EV_MINIMAL < 2
2210 --loop_depth;
2211#endif
2102} 2212}
2103 2213
2104void 2214void
2105ev_unloop (EV_P_ int how) 2215ev_unloop (EV_P_ int how)
2106{ 2216{
2137 ev_tstamp mn_prev = mn_now; 2247 ev_tstamp mn_prev = mn_now;
2138 2248
2139 ev_now_update (EV_A); 2249 ev_now_update (EV_A);
2140 timers_reschedule (EV_A_ mn_now - mn_prev); 2250 timers_reschedule (EV_A_ mn_now - mn_prev);
2141#if EV_PERIODIC_ENABLE 2251#if EV_PERIODIC_ENABLE
2252 /* TODO: really do this? */
2142 periodics_reschedule (EV_A); 2253 periodics_reschedule (EV_A);
2143#endif 2254#endif
2144} 2255}
2145 2256
2146/*****************************************************************************/ 2257/*****************************************************************************/
2258/* singly-linked list management, used when the expected list length is short */
2147 2259
2148inline_size void 2260inline_size void
2149wlist_add (WL *head, WL elem) 2261wlist_add (WL *head, WL elem)
2150{ 2262{
2151 elem->next = *head; 2263 elem->next = *head;
2165 2277
2166 head = &(*head)->next; 2278 head = &(*head)->next;
2167 } 2279 }
2168} 2280}
2169 2281
2282/* internal, faster, version of ev_clear_pending */
2170inline_speed void 2283inline_speed void
2171clear_pending (EV_P_ W w) 2284clear_pending (EV_P_ W w)
2172{ 2285{
2173 if (w->pending) 2286 if (w->pending)
2174 { 2287 {
2175 pendings [ABSPRI (w)][w->pending - 1].w = 0; 2288 pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w;
2176 w->pending = 0; 2289 w->pending = 0;
2177 } 2290 }
2178} 2291}
2179 2292
2180int 2293int
2184 int pending = w_->pending; 2297 int pending = w_->pending;
2185 2298
2186 if (expect_true (pending)) 2299 if (expect_true (pending))
2187 { 2300 {
2188 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1; 2301 ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
2302 p->w = (W)&pending_w;
2189 w_->pending = 0; 2303 w_->pending = 0;
2190 p->w = 0;
2191 return p->events; 2304 return p->events;
2192 } 2305 }
2193 else 2306 else
2194 return 0; 2307 return 0;
2195} 2308}
2196 2309
2197inline_size void 2310inline_size void
2198pri_adjust (EV_P_ W w) 2311pri_adjust (EV_P_ W w)
2199{ 2312{
2200 int pri = w->priority; 2313 int pri = ev_priority (w);
2201 pri = pri < EV_MINPRI ? EV_MINPRI : pri; 2314 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2202 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; 2315 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2203 w->priority = pri; 2316 ev_set_priority (w, pri);
2204} 2317}
2205 2318
2206inline_speed void 2319inline_speed void
2207ev_start (EV_P_ W w, int active) 2320ev_start (EV_P_ W w, int active)
2208{ 2321{
3201 } 3314 }
3202} 3315}
3203 3316
3204/*****************************************************************************/ 3317/*****************************************************************************/
3205 3318
3206#if 0 3319#if EV_WALK_ENABLE
3207void 3320void
3208ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) 3321ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w))
3209{ 3322{
3210 int i, j; 3323 int i, j;
3211 ev_watcher_list *wl, *wn; 3324 ev_watcher_list *wl, *wn;
3227#if EV_USE_INOTIFY 3340#if EV_USE_INOTIFY
3228 if (ev_cb ((ev_io *)wl) == infy_cb) 3341 if (ev_cb ((ev_io *)wl) == infy_cb)
3229 ; 3342 ;
3230 else 3343 else
3231#endif 3344#endif
3232 if ((ev_io *)wl != &pipeev) 3345 if ((ev_io *)wl != &pipe_w)
3233 if (types & EV_IO) 3346 if (types & EV_IO)
3234 cb (EV_A_ EV_IO, wl); 3347 cb (EV_A_ EV_IO, wl);
3235 3348
3236 wl = wn; 3349 wl = wn;
3237 } 3350 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines