… | |
… | |
133 | # else |
133 | # else |
134 | # define EV_USE_INOTIFY 0 |
134 | # define EV_USE_INOTIFY 0 |
135 | # endif |
135 | # endif |
136 | # endif |
136 | # endif |
137 | |
137 | |
|
|
138 | # ifndef EV_USE_SIGNALFD |
|
|
139 | # if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H |
|
|
140 | # define EV_USE_SIGNALFD 1 |
|
|
141 | # else |
|
|
142 | # define EV_USE_SIGNALFD 0 |
|
|
143 | # endif |
|
|
144 | # endif |
|
|
145 | |
138 | # ifndef EV_USE_EVENTFD |
146 | # ifndef EV_USE_EVENTFD |
139 | # if HAVE_EVENTFD |
147 | # if HAVE_EVENTFD |
140 | # define EV_USE_EVENTFD 1 |
148 | # define EV_USE_EVENTFD 1 |
141 | # else |
149 | # else |
142 | # define EV_USE_EVENTFD 0 |
150 | # define EV_USE_EVENTFD 0 |
… | |
… | |
263 | #ifndef EV_USE_EVENTFD |
271 | #ifndef EV_USE_EVENTFD |
264 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) |
272 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)) |
265 | # define EV_USE_EVENTFD 1 |
273 | # define EV_USE_EVENTFD 1 |
266 | # else |
274 | # else |
267 | # define EV_USE_EVENTFD 0 |
275 | # define EV_USE_EVENTFD 0 |
|
|
276 | # endif |
|
|
277 | #endif |
|
|
278 | |
|
|
279 | #ifndef EV_USE_SIGNALFD |
|
|
280 | # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9)) |
|
|
281 | # define EV_USE_SIGNALFD 1 |
|
|
282 | # else |
|
|
283 | # define EV_USE_SIGNALFD 0 |
268 | # endif |
284 | # endif |
269 | #endif |
285 | #endif |
270 | |
286 | |
271 | #if 0 /* debugging */ |
287 | #if 0 /* debugging */ |
272 | # define EV_VERIFY 3 |
288 | # define EV_VERIFY 3 |
… | |
… | |
339 | #endif |
355 | #endif |
340 | |
356 | |
341 | #if EV_USE_EVENTFD |
357 | #if EV_USE_EVENTFD |
342 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
358 | /* our minimum requirement is glibc 2.7 which has the stub, but not the header */ |
343 | # include <stdint.h> |
359 | # include <stdint.h> |
|
|
360 | # ifndef EFD_NONBLOCK |
|
|
361 | # define EFD_NONBLOCK O_NONBLOCK |
|
|
362 | # endif |
|
|
363 | # ifndef EFD_CLOEXEC |
|
|
364 | # define EFD_CLOEXEC O_CLOEXEC |
|
|
365 | # endif |
344 | # ifdef __cplusplus |
366 | # ifdef __cplusplus |
345 | extern "C" { |
367 | extern "C" { |
346 | # endif |
368 | # endif |
347 | int eventfd (unsigned int initval, int flags); |
369 | int eventfd (unsigned int initval, int flags); |
348 | # ifdef __cplusplus |
370 | # ifdef __cplusplus |
349 | } |
371 | } |
350 | # endif |
372 | # endif |
|
|
373 | #endif |
|
|
374 | |
|
|
375 | #if EV_USE_SIGNALFD |
|
|
376 | # include <sys/signalfd.h> |
351 | #endif |
377 | #endif |
352 | |
378 | |
353 | /**/ |
379 | /**/ |
354 | |
380 | |
355 | #if EV_VERIFY >= 3 |
381 | #if EV_VERIFY >= 3 |
… | |
… | |
490 | #define ev_malloc(size) ev_realloc (0, (size)) |
516 | #define ev_malloc(size) ev_realloc (0, (size)) |
491 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
517 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
492 | |
518 | |
493 | /*****************************************************************************/ |
519 | /*****************************************************************************/ |
494 | |
520 | |
|
|
521 | /* set in reify when reification needed */ |
|
|
522 | #define EV_ANFD_REIFY 1 |
|
|
523 | |
495 | /* file descriptor info structure */ |
524 | /* file descriptor info structure */ |
496 | typedef struct |
525 | typedef struct |
497 | { |
526 | { |
498 | WL head; |
527 | WL head; |
499 | unsigned char events; /* the events watched for */ |
528 | unsigned char events; /* the events watched for */ |
500 | unsigned char reify; /* flag set when this ANFD needs reification */ |
529 | unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */ |
501 | unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ |
530 | unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ |
502 | unsigned char unused; |
531 | unsigned char unused; |
503 | #if EV_USE_EPOLL |
532 | #if EV_USE_EPOLL |
504 | unsigned int egen; /* generation counter to counter epoll bugs */ |
533 | unsigned int egen; /* generation counter to counter epoll bugs */ |
505 | #endif |
534 | #endif |
… | |
… | |
567 | |
596 | |
568 | static int ev_default_loop_ptr; |
597 | static int ev_default_loop_ptr; |
569 | |
598 | |
570 | #endif |
599 | #endif |
571 | |
600 | |
|
|
601 | #if EV_MINIMAL < 2 |
|
|
602 | # define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A) |
|
|
603 | # define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A) |
|
|
604 | # define EV_INVOKE_PENDING invoke_cb (EV_A) |
|
|
605 | #else |
|
|
606 | # define EV_RELEASE_CB (void)0 |
|
|
607 | # define EV_ACQUIRE_CB (void)0 |
|
|
608 | # define EV_INVOKE_PENDING ev_invoke_pending (EV_A) |
|
|
609 | #endif |
|
|
610 | |
|
|
611 | #define EVUNLOOP_RECURSE 0x80 |
|
|
612 | |
572 | /*****************************************************************************/ |
613 | /*****************************************************************************/ |
573 | |
614 | |
574 | #ifndef EV_HAVE_EV_TIME |
615 | #ifndef EV_HAVE_EV_TIME |
575 | ev_tstamp |
616 | ev_tstamp |
576 | ev_time (void) |
617 | ev_time (void) |
… | |
… | |
632 | |
673 | |
633 | tv.tv_sec = (time_t)delay; |
674 | tv.tv_sec = (time_t)delay; |
634 | tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); |
675 | tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); |
635 | |
676 | |
636 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
677 | /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */ |
637 | /* somehting not guaranteed by newer posix versions, but guaranteed */ |
678 | /* something not guaranteed by newer posix versions, but guaranteed */ |
638 | /* by older ones */ |
679 | /* by older ones */ |
639 | select (0, 0, 0, 0, &tv); |
680 | select (0, 0, 0, 0, &tv); |
640 | #endif |
681 | #endif |
641 | } |
682 | } |
642 | } |
683 | } |
… | |
… | |
750 | } |
791 | } |
751 | |
792 | |
752 | /*****************************************************************************/ |
793 | /*****************************************************************************/ |
753 | |
794 | |
754 | inline_speed void |
795 | inline_speed void |
755 | fd_event (EV_P_ int fd, int revents) |
796 | fd_event_nc (EV_P_ int fd, int revents) |
756 | { |
797 | { |
757 | ANFD *anfd = anfds + fd; |
798 | ANFD *anfd = anfds + fd; |
758 | ev_io *w; |
799 | ev_io *w; |
759 | |
800 | |
760 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
801 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
… | |
… | |
764 | if (ev) |
805 | if (ev) |
765 | ev_feed_event (EV_A_ (W)w, ev); |
806 | ev_feed_event (EV_A_ (W)w, ev); |
766 | } |
807 | } |
767 | } |
808 | } |
768 | |
809 | |
|
|
810 | /* do not submit kernel events for fds that have reify set */ |
|
|
811 | /* because that means they changed while we were polling for new events */ |
|
|
812 | inline_speed void |
|
|
813 | fd_event (EV_P_ int fd, int revents) |
|
|
814 | { |
|
|
815 | ANFD *anfd = anfds + fd; |
|
|
816 | |
|
|
817 | if (expect_true (!anfd->reify)) |
|
|
818 | fd_event_nc (EV_A_ fd, revents); |
|
|
819 | } |
|
|
820 | |
769 | void |
821 | void |
770 | ev_feed_fd_event (EV_P_ int fd, int revents) |
822 | ev_feed_fd_event (EV_P_ int fd, int revents) |
771 | { |
823 | { |
772 | if (fd >= 0 && fd < anfdmax) |
824 | if (fd >= 0 && fd < anfdmax) |
773 | fd_event (EV_A_ fd, revents); |
825 | fd_event_nc (EV_A_ fd, revents); |
774 | } |
826 | } |
775 | |
827 | |
776 | /* make sure the external fd watch events are in-sync */ |
828 | /* make sure the external fd watch events are in-sync */ |
777 | /* with the kernel/libev internal state */ |
829 | /* with the kernel/libev internal state */ |
778 | inline_size void |
830 | inline_size void |
… | |
… | |
893 | for (fd = 0; fd < anfdmax; ++fd) |
945 | for (fd = 0; fd < anfdmax; ++fd) |
894 | if (anfds [fd].events) |
946 | if (anfds [fd].events) |
895 | { |
947 | { |
896 | anfds [fd].events = 0; |
948 | anfds [fd].events = 0; |
897 | anfds [fd].emask = 0; |
949 | anfds [fd].emask = 0; |
898 | fd_change (EV_A_ fd, EV__IOFDSET | 1); |
950 | fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY); |
899 | } |
951 | } |
900 | } |
952 | } |
901 | |
953 | |
902 | /*****************************************************************************/ |
954 | /*****************************************************************************/ |
903 | |
955 | |
… | |
… | |
1078 | evpipe_init (EV_P) |
1130 | evpipe_init (EV_P) |
1079 | { |
1131 | { |
1080 | if (!ev_is_active (&pipe_w)) |
1132 | if (!ev_is_active (&pipe_w)) |
1081 | { |
1133 | { |
1082 | #if EV_USE_EVENTFD |
1134 | #if EV_USE_EVENTFD |
|
|
1135 | evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); |
|
|
1136 | if (evfd < 0 && errno == EINVAL) |
1083 | if ((evfd = eventfd (0, 0)) >= 0) |
1137 | evfd = eventfd (0, 0); |
|
|
1138 | |
|
|
1139 | if (evfd >= 0) |
1084 | { |
1140 | { |
1085 | evpipe [0] = -1; |
1141 | evpipe [0] = -1; |
1086 | fd_intern (evfd); |
1142 | fd_intern (evfd); /* doing it twice doesn't hurt */ |
1087 | ev_io_set (&pipe_w, evfd, EV_READ); |
1143 | ev_io_set (&pipe_w, evfd, EV_READ); |
1088 | } |
1144 | } |
1089 | else |
1145 | else |
1090 | #endif |
1146 | #endif |
1091 | { |
1147 | { |
… | |
… | |
1203 | signals [signum].gotsig = 0; |
1259 | signals [signum].gotsig = 0; |
1204 | |
1260 | |
1205 | for (w = signals [signum].head; w; w = w->next) |
1261 | for (w = signals [signum].head; w; w = w->next) |
1206 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
1262 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
1207 | } |
1263 | } |
|
|
1264 | |
|
|
1265 | #if EV_USE_SIGNALFD |
|
|
1266 | static void |
|
|
1267 | sigfdcb (EV_P_ ev_io *iow, int revents) |
|
|
1268 | { |
|
|
1269 | struct signalfd_siginfo si[4], *sip; |
|
|
1270 | |
|
|
1271 | for (;;) |
|
|
1272 | { |
|
|
1273 | ssize_t res = read (sigfd, si, sizeof (si)); |
|
|
1274 | |
|
|
1275 | /* not ISO-C, as res might be -1, but works with SuS */ |
|
|
1276 | for (sip = si; (char *)sip < (char *)si + res; ++sip) |
|
|
1277 | ev_feed_signal_event (EV_A_ sip->ssi_signo); |
|
|
1278 | |
|
|
1279 | if (res < (ssize_t)sizeof (si)) |
|
|
1280 | break; |
|
|
1281 | } |
|
|
1282 | } |
|
|
1283 | #endif |
1208 | |
1284 | |
1209 | /*****************************************************************************/ |
1285 | /*****************************************************************************/ |
1210 | |
1286 | |
1211 | static WL childs [EV_PID_HASHSIZE]; |
1287 | static WL childs [EV_PID_HASHSIZE]; |
1212 | |
1288 | |
… | |
… | |
1357 | ev_backend (EV_P) |
1433 | ev_backend (EV_P) |
1358 | { |
1434 | { |
1359 | return backend; |
1435 | return backend; |
1360 | } |
1436 | } |
1361 | |
1437 | |
|
|
1438 | #if EV_MINIMAL < 2 |
1362 | unsigned int |
1439 | unsigned int |
1363 | ev_loop_count (EV_P) |
1440 | ev_loop_count (EV_P) |
1364 | { |
1441 | { |
1365 | return loop_count; |
1442 | return loop_count; |
1366 | } |
1443 | } |
… | |
… | |
1380 | void |
1457 | void |
1381 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
1458 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
1382 | { |
1459 | { |
1383 | timeout_blocktime = interval; |
1460 | timeout_blocktime = interval; |
1384 | } |
1461 | } |
|
|
1462 | |
|
|
1463 | void |
|
|
1464 | ev_set_userdata (EV_P_ void *data) |
|
|
1465 | { |
|
|
1466 | userdata = data; |
|
|
1467 | } |
|
|
1468 | |
|
|
1469 | void * |
|
|
1470 | ev_userdata (EV_P) |
|
|
1471 | { |
|
|
1472 | return userdata; |
|
|
1473 | } |
|
|
1474 | |
|
|
1475 | void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) |
|
|
1476 | { |
|
|
1477 | invoke_cb = invoke_pending_cb; |
|
|
1478 | } |
|
|
1479 | |
|
|
1480 | void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) |
|
|
1481 | { |
|
|
1482 | release_cb = release; |
|
|
1483 | acquire_cb = acquire; |
|
|
1484 | } |
|
|
1485 | #endif |
1385 | |
1486 | |
1386 | /* initialise a loop structure, must be zero-initialised */ |
1487 | /* initialise a loop structure, must be zero-initialised */ |
1387 | static void noinline |
1488 | static void noinline |
1388 | loop_init (EV_P_ unsigned int flags) |
1489 | loop_init (EV_P_ unsigned int flags) |
1389 | { |
1490 | { |
… | |
… | |
1411 | |
1512 | |
1412 | ev_rt_now = ev_time (); |
1513 | ev_rt_now = ev_time (); |
1413 | mn_now = get_clock (); |
1514 | mn_now = get_clock (); |
1414 | now_floor = mn_now; |
1515 | now_floor = mn_now; |
1415 | rtmn_diff = ev_rt_now - mn_now; |
1516 | rtmn_diff = ev_rt_now - mn_now; |
|
|
1517 | #if EV_MINIMAL < 2 |
|
|
1518 | invoke_cb = ev_invoke_pending; |
|
|
1519 | #endif |
1416 | |
1520 | |
1417 | io_blocktime = 0.; |
1521 | io_blocktime = 0.; |
1418 | timeout_blocktime = 0.; |
1522 | timeout_blocktime = 0.; |
1419 | backend = 0; |
1523 | backend = 0; |
1420 | backend_fd = -1; |
1524 | backend_fd = -1; |
1421 | gotasync = 0; |
1525 | gotasync = 0; |
1422 | #if EV_USE_INOTIFY |
1526 | #if EV_USE_INOTIFY |
1423 | fs_fd = -2; |
1527 | fs_fd = -2; |
1424 | #endif |
1528 | #endif |
|
|
1529 | #if EV_USE_SIGNALFD |
|
|
1530 | sigfd = -2; |
|
|
1531 | #endif |
1425 | |
1532 | |
1426 | /* pid check not overridable via env */ |
1533 | /* pid check not overridable via env */ |
1427 | #ifndef _WIN32 |
1534 | #ifndef _WIN32 |
1428 | if (flags & EVFLAG_FORKCHECK) |
1535 | if (flags & EVFLAG_FORKCHECK) |
1429 | curpid = getpid (); |
1536 | curpid = getpid (); |
… | |
… | |
1466 | { |
1573 | { |
1467 | int i; |
1574 | int i; |
1468 | |
1575 | |
1469 | if (ev_is_active (&pipe_w)) |
1576 | if (ev_is_active (&pipe_w)) |
1470 | { |
1577 | { |
1471 | ev_ref (EV_A); /* signal watcher */ |
1578 | /*ev_ref (EV_A);*/ |
1472 | ev_io_stop (EV_A_ &pipe_w); |
1579 | /*ev_io_stop (EV_A_ &pipe_w);*/ |
1473 | |
1580 | |
1474 | #if EV_USE_EVENTFD |
1581 | #if EV_USE_EVENTFD |
1475 | if (evfd >= 0) |
1582 | if (evfd >= 0) |
1476 | close (evfd); |
1583 | close (evfd); |
1477 | #endif |
1584 | #endif |
… | |
… | |
1480 | { |
1587 | { |
1481 | close (evpipe [0]); |
1588 | close (evpipe [0]); |
1482 | close (evpipe [1]); |
1589 | close (evpipe [1]); |
1483 | } |
1590 | } |
1484 | } |
1591 | } |
|
|
1592 | |
|
|
1593 | #if EV_USE_SIGNALFD |
|
|
1594 | if (ev_is_active (&sigfd_w)) |
|
|
1595 | { |
|
|
1596 | /*ev_ref (EV_A);*/ |
|
|
1597 | /*ev_io_stop (EV_A_ &sigfd_w);*/ |
|
|
1598 | |
|
|
1599 | close (sigfd); |
|
|
1600 | } |
|
|
1601 | #endif |
1485 | |
1602 | |
1486 | #if EV_USE_INOTIFY |
1603 | #if EV_USE_INOTIFY |
1487 | if (fs_fd >= 0) |
1604 | if (fs_fd >= 0) |
1488 | close (fs_fd); |
1605 | close (fs_fd); |
1489 | #endif |
1606 | #endif |
… | |
… | |
1593 | ev_loop_new (unsigned int flags) |
1710 | ev_loop_new (unsigned int flags) |
1594 | { |
1711 | { |
1595 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
1712 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
1596 | |
1713 | |
1597 | memset (loop, 0, sizeof (struct ev_loop)); |
1714 | memset (loop, 0, sizeof (struct ev_loop)); |
1598 | |
|
|
1599 | loop_init (EV_A_ flags); |
1715 | loop_init (EV_A_ flags); |
1600 | |
1716 | |
1601 | if (ev_backend (EV_A)) |
1717 | if (ev_backend (EV_A)) |
1602 | return loop; |
1718 | return loop; |
1603 | |
1719 | |
… | |
… | |
1614 | void |
1730 | void |
1615 | ev_loop_fork (EV_P) |
1731 | ev_loop_fork (EV_P) |
1616 | { |
1732 | { |
1617 | postfork = 1; /* must be in line with ev_default_fork */ |
1733 | postfork = 1; /* must be in line with ev_default_fork */ |
1618 | } |
1734 | } |
|
|
1735 | #endif /* multiplicity */ |
1619 | |
1736 | |
1620 | #if EV_VERIFY |
1737 | #if EV_VERIFY |
1621 | static void noinline |
1738 | static void noinline |
1622 | verify_watcher (EV_P_ W w) |
1739 | verify_watcher (EV_P_ W w) |
1623 | { |
1740 | { |
… | |
… | |
1651 | verify_watcher (EV_A_ ws [cnt]); |
1768 | verify_watcher (EV_A_ ws [cnt]); |
1652 | } |
1769 | } |
1653 | } |
1770 | } |
1654 | #endif |
1771 | #endif |
1655 | |
1772 | |
|
|
1773 | #if EV_MINIMAL < 2 |
1656 | void |
1774 | void |
1657 | ev_loop_verify (EV_P) |
1775 | ev_loop_verify (EV_P) |
1658 | { |
1776 | { |
1659 | #if EV_VERIFY |
1777 | #if EV_VERIFY |
1660 | int i; |
1778 | int i; |
… | |
… | |
1713 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
1831 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
1714 | for (signum = signalmax; signum--; ) if (signals [signum].gotsig) |
1832 | for (signum = signalmax; signum--; ) if (signals [signum].gotsig) |
1715 | # endif |
1833 | # endif |
1716 | #endif |
1834 | #endif |
1717 | } |
1835 | } |
1718 | |
1836 | #endif |
1719 | #endif /* multiplicity */ |
|
|
1720 | |
1837 | |
1721 | #if EV_MULTIPLICITY |
1838 | #if EV_MULTIPLICITY |
1722 | struct ev_loop * |
1839 | struct ev_loop * |
1723 | ev_default_loop_init (unsigned int flags) |
1840 | ev_default_loop_init (unsigned int flags) |
1724 | #else |
1841 | #else |
… | |
… | |
1785 | ev_invoke (EV_P_ void *w, int revents) |
1902 | ev_invoke (EV_P_ void *w, int revents) |
1786 | { |
1903 | { |
1787 | EV_CB_INVOKE ((W)w, revents); |
1904 | EV_CB_INVOKE ((W)w, revents); |
1788 | } |
1905 | } |
1789 | |
1906 | |
1790 | inline_speed void |
1907 | unsigned int |
1791 | call_pending (EV_P) |
1908 | ev_pending_count (EV_P) |
|
|
1909 | { |
|
|
1910 | int pri; |
|
|
1911 | unsigned int count = 0; |
|
|
1912 | |
|
|
1913 | for (pri = NUMPRI; pri--; ) |
|
|
1914 | count += pendingcnt [pri]; |
|
|
1915 | |
|
|
1916 | return count; |
|
|
1917 | } |
|
|
1918 | |
|
|
1919 | void noinline |
|
|
1920 | ev_invoke_pending (EV_P) |
1792 | { |
1921 | { |
1793 | int pri; |
1922 | int pri; |
1794 | |
1923 | |
1795 | for (pri = NUMPRI; pri--; ) |
1924 | for (pri = NUMPRI; pri--; ) |
1796 | while (pendingcnt [pri]) |
1925 | while (pendingcnt [pri]) |
… | |
… | |
2034 | } |
2163 | } |
2035 | |
2164 | |
2036 | void |
2165 | void |
2037 | ev_loop (EV_P_ int flags) |
2166 | ev_loop (EV_P_ int flags) |
2038 | { |
2167 | { |
|
|
2168 | #if EV_MINIMAL < 2 |
2039 | ++loop_depth; |
2169 | ++loop_depth; |
|
|
2170 | #endif |
|
|
2171 | |
|
|
2172 | assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE)); |
2040 | |
2173 | |
2041 | loop_done = EVUNLOOP_CANCEL; |
2174 | loop_done = EVUNLOOP_CANCEL; |
2042 | |
2175 | |
2043 | call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ |
2176 | EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */ |
2044 | |
2177 | |
2045 | do |
2178 | do |
2046 | { |
2179 | { |
2047 | #if EV_VERIFY >= 2 |
2180 | #if EV_VERIFY >= 2 |
2048 | ev_loop_verify (EV_A); |
2181 | ev_loop_verify (EV_A); |
… | |
… | |
2061 | /* we might have forked, so queue fork handlers */ |
2194 | /* we might have forked, so queue fork handlers */ |
2062 | if (expect_false (postfork)) |
2195 | if (expect_false (postfork)) |
2063 | if (forkcnt) |
2196 | if (forkcnt) |
2064 | { |
2197 | { |
2065 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
2198 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
2066 | call_pending (EV_A); |
2199 | EV_INVOKE_PENDING; |
2067 | } |
2200 | } |
2068 | #endif |
2201 | #endif |
2069 | |
2202 | |
2070 | /* queue prepare watchers (and execute them) */ |
2203 | /* queue prepare watchers (and execute them) */ |
2071 | if (expect_false (preparecnt)) |
2204 | if (expect_false (preparecnt)) |
2072 | { |
2205 | { |
2073 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
2206 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
2074 | call_pending (EV_A); |
2207 | EV_INVOKE_PENDING; |
2075 | } |
2208 | } |
|
|
2209 | |
|
|
2210 | if (expect_false (loop_done)) |
|
|
2211 | break; |
2076 | |
2212 | |
2077 | /* we might have forked, so reify kernel state if necessary */ |
2213 | /* we might have forked, so reify kernel state if necessary */ |
2078 | if (expect_false (postfork)) |
2214 | if (expect_false (postfork)) |
2079 | loop_fork (EV_A); |
2215 | loop_fork (EV_A); |
2080 | |
2216 | |
… | |
… | |
2128 | waittime -= sleeptime; |
2264 | waittime -= sleeptime; |
2129 | } |
2265 | } |
2130 | } |
2266 | } |
2131 | } |
2267 | } |
2132 | |
2268 | |
|
|
2269 | #if EV_MINIMAL < 2 |
2133 | ++loop_count; |
2270 | ++loop_count; |
|
|
2271 | #endif |
|
|
2272 | assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */ |
2134 | backend_poll (EV_A_ waittime); |
2273 | backend_poll (EV_A_ waittime); |
|
|
2274 | assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */ |
2135 | |
2275 | |
2136 | /* update ev_rt_now, do magic */ |
2276 | /* update ev_rt_now, do magic */ |
2137 | time_update (EV_A_ waittime + sleeptime); |
2277 | time_update (EV_A_ waittime + sleeptime); |
2138 | } |
2278 | } |
2139 | |
2279 | |
… | |
… | |
2150 | |
2290 | |
2151 | /* queue check watchers, to be executed first */ |
2291 | /* queue check watchers, to be executed first */ |
2152 | if (expect_false (checkcnt)) |
2292 | if (expect_false (checkcnt)) |
2153 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
2293 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
2154 | |
2294 | |
2155 | call_pending (EV_A); |
2295 | EV_INVOKE_PENDING; |
2156 | } |
2296 | } |
2157 | while (expect_true ( |
2297 | while (expect_true ( |
2158 | activecnt |
2298 | activecnt |
2159 | && !loop_done |
2299 | && !loop_done |
2160 | && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) |
2300 | && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) |
2161 | )); |
2301 | )); |
2162 | |
2302 | |
2163 | if (loop_done == EVUNLOOP_ONE) |
2303 | if (loop_done == EVUNLOOP_ONE) |
2164 | loop_done = EVUNLOOP_CANCEL; |
2304 | loop_done = EVUNLOOP_CANCEL; |
2165 | |
2305 | |
|
|
2306 | #if EV_MINIMAL < 2 |
2166 | --loop_depth; |
2307 | --loop_depth; |
|
|
2308 | #endif |
2167 | } |
2309 | } |
2168 | |
2310 | |
2169 | void |
2311 | void |
2170 | ev_unloop (EV_P_ int how) |
2312 | ev_unloop (EV_P_ int how) |
2171 | { |
2313 | { |
… | |
… | |
2303 | |
2445 | |
2304 | ev_start (EV_A_ (W)w, 1); |
2446 | ev_start (EV_A_ (W)w, 1); |
2305 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
2447 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
2306 | wlist_add (&anfds[fd].head, (WL)w); |
2448 | wlist_add (&anfds[fd].head, (WL)w); |
2307 | |
2449 | |
2308 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | 1); |
2450 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); |
2309 | w->events &= ~EV__IOFDSET; |
2451 | w->events &= ~EV__IOFDSET; |
2310 | |
2452 | |
2311 | EV_FREQUENT_CHECK; |
2453 | EV_FREQUENT_CHECK; |
2312 | } |
2454 | } |
2313 | |
2455 | |
… | |
… | |
2407 | } |
2549 | } |
2408 | |
2550 | |
2409 | EV_FREQUENT_CHECK; |
2551 | EV_FREQUENT_CHECK; |
2410 | } |
2552 | } |
2411 | |
2553 | |
|
|
2554 | ev_tstamp |
|
|
2555 | ev_timer_remaining (EV_P_ ev_timer *w) |
|
|
2556 | { |
|
|
2557 | return ev_at (w) - (ev_is_active (w) ? mn_now : 0.); |
|
|
2558 | } |
|
|
2559 | |
2412 | #if EV_PERIODIC_ENABLE |
2560 | #if EV_PERIODIC_ENABLE |
2413 | void noinline |
2561 | void noinline |
2414 | ev_periodic_start (EV_P_ ev_periodic *w) |
2562 | ev_periodic_start (EV_P_ ev_periodic *w) |
2415 | { |
2563 | { |
2416 | if (expect_false (ev_is_active (w))) |
2564 | if (expect_false (ev_is_active (w))) |
… | |
… | |
2491 | if (expect_false (ev_is_active (w))) |
2639 | if (expect_false (ev_is_active (w))) |
2492 | return; |
2640 | return; |
2493 | |
2641 | |
2494 | assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0)); |
2642 | assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0)); |
2495 | |
2643 | |
|
|
2644 | EV_FREQUENT_CHECK; |
|
|
2645 | |
|
|
2646 | #if EV_USE_SIGNALFD |
|
|
2647 | if (sigfd == -2) |
|
|
2648 | { |
|
|
2649 | sigfd = signalfd (-1, &sigfd_set, SFD_NONBLOCK | SFD_CLOEXEC); |
|
|
2650 | if (sigfd < 0 && errno == EINVAL) |
|
|
2651 | sigfd = signalfd (-1, &sigfd_set, 0); /* retry without flags */ |
|
|
2652 | |
|
|
2653 | if (sigfd >= 0) |
|
|
2654 | { |
|
|
2655 | fd_intern (sigfd); /* doing it twice will not hurt */ |
|
|
2656 | |
|
|
2657 | sigemptyset (&sigfd_set); |
|
|
2658 | |
|
|
2659 | ev_io_init (&sigfd_w, sigfdcb, sigfd, EV_READ); |
|
|
2660 | ev_set_priority (&sigfd_w, EV_MAXPRI); |
|
|
2661 | ev_io_start (EV_A_ &sigfd_w); |
|
|
2662 | ev_unref (EV_A); /* signalfd watcher should not keep loop alive */ |
|
|
2663 | } |
|
|
2664 | } |
|
|
2665 | |
|
|
2666 | if (sigfd >= 0) |
|
|
2667 | { |
|
|
2668 | /* TODO: check .head */ |
|
|
2669 | sigaddset (&sigfd_set, w->signum); |
|
|
2670 | sigprocmask (SIG_BLOCK, &sigfd_set, 0); |
|
|
2671 | |
|
|
2672 | signalfd (sigfd, &sigfd_set, 0); |
|
|
2673 | } |
|
|
2674 | else |
|
|
2675 | #endif |
2496 | evpipe_init (EV_A); |
2676 | evpipe_init (EV_A); |
2497 | |
|
|
2498 | EV_FREQUENT_CHECK; |
|
|
2499 | |
2677 | |
2500 | { |
2678 | { |
2501 | #ifndef _WIN32 |
2679 | #ifndef _WIN32 |
2502 | sigset_t full, prev; |
2680 | sigset_t full, prev; |
2503 | sigfillset (&full); |
2681 | sigfillset (&full); |
… | |
… | |
2505 | #endif |
2683 | #endif |
2506 | |
2684 | |
2507 | array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero); |
2685 | array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero); |
2508 | |
2686 | |
2509 | #ifndef _WIN32 |
2687 | #ifndef _WIN32 |
|
|
2688 | if (sigfd < 0)/*TODO*/ |
|
|
2689 | sigdelset (&prev, w->signum); |
2510 | sigprocmask (SIG_SETMASK, &prev, 0); |
2690 | sigprocmask (SIG_SETMASK, &prev, 0); |
2511 | #endif |
2691 | #endif |
2512 | } |
2692 | } |
2513 | |
2693 | |
2514 | ev_start (EV_A_ (W)w, 1); |
2694 | ev_start (EV_A_ (W)w, 1); |
… | |
… | |
2517 | if (!((WL)w)->next) |
2697 | if (!((WL)w)->next) |
2518 | { |
2698 | { |
2519 | #if _WIN32 |
2699 | #if _WIN32 |
2520 | signal (w->signum, ev_sighandler); |
2700 | signal (w->signum, ev_sighandler); |
2521 | #else |
2701 | #else |
|
|
2702 | if (sigfd < 0) /*TODO*/ |
|
|
2703 | { |
2522 | struct sigaction sa; |
2704 | struct sigaction sa = { }; |
2523 | sa.sa_handler = ev_sighandler; |
2705 | sa.sa_handler = ev_sighandler; |
2524 | sigfillset (&sa.sa_mask); |
2706 | sigfillset (&sa.sa_mask); |
2525 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2707 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2526 | sigaction (w->signum, &sa, 0); |
2708 | sigaction (w->signum, &sa, 0); |
|
|
2709 | } |
2527 | #endif |
2710 | #endif |
2528 | } |
2711 | } |
2529 | |
2712 | |
2530 | EV_FREQUENT_CHECK; |
2713 | EV_FREQUENT_CHECK; |
2531 | } |
2714 | } |
… | |
… | |
2541 | |
2724 | |
2542 | wlist_del (&signals [w->signum - 1].head, (WL)w); |
2725 | wlist_del (&signals [w->signum - 1].head, (WL)w); |
2543 | ev_stop (EV_A_ (W)w); |
2726 | ev_stop (EV_A_ (W)w); |
2544 | |
2727 | |
2545 | if (!signals [w->signum - 1].head) |
2728 | if (!signals [w->signum - 1].head) |
|
|
2729 | #if EV_USE_SIGNALFD |
|
|
2730 | if (sigfd >= 0) |
|
|
2731 | { |
|
|
2732 | sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D |
|
|
2733 | sigdelset (&sigfd_set, w->signum); |
|
|
2734 | signalfd (sigfd, &sigfd_set, 0); |
|
|
2735 | sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D |
|
|
2736 | /*TODO: maybe unblock signal? */ |
|
|
2737 | } |
|
|
2738 | else |
|
|
2739 | #endif |
2546 | signal (w->signum, SIG_DFL); |
2740 | signal (w->signum, SIG_DFL); |
2547 | |
2741 | |
2548 | EV_FREQUENT_CHECK; |
2742 | EV_FREQUENT_CHECK; |
2549 | } |
2743 | } |
2550 | |
2744 | |
2551 | void |
2745 | void |