… | |
… | |
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 |
… | |
… | |
391 | # define inline_speed static noinline |
417 | # define inline_speed static noinline |
392 | #else |
418 | #else |
393 | # define inline_speed static inline |
419 | # define inline_speed static inline |
394 | #endif |
420 | #endif |
395 | |
421 | |
396 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
422 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
423 | |
|
|
424 | #if EV_MINPRI == EV_MAXPRI |
|
|
425 | # define ABSPRI(w) (((W)w), 0) |
|
|
426 | #else |
397 | #define ABSPRI(w) (((W)w)->priority - EV_MINPRI) |
427 | # define ABSPRI(w) (((W)w)->priority - EV_MINPRI) |
|
|
428 | #endif |
398 | |
429 | |
399 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
430 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
400 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
431 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
401 | |
432 | |
402 | typedef ev_watcher *W; |
433 | typedef ev_watcher *W; |
… | |
… | |
485 | #define ev_malloc(size) ev_realloc (0, (size)) |
516 | #define ev_malloc(size) ev_realloc (0, (size)) |
486 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
517 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
487 | |
518 | |
488 | /*****************************************************************************/ |
519 | /*****************************************************************************/ |
489 | |
520 | |
|
|
521 | /* set in reify when reification needed */ |
|
|
522 | #define EV_ANFD_REIFY 1 |
|
|
523 | |
490 | /* file descriptor info structure */ |
524 | /* file descriptor info structure */ |
491 | typedef struct |
525 | typedef struct |
492 | { |
526 | { |
493 | WL head; |
527 | WL head; |
494 | unsigned char events; /* the events watched for */ |
528 | unsigned char events; /* the events watched for */ |
495 | 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) */ |
496 | 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 */ |
497 | unsigned char unused; |
531 | unsigned char unused; |
498 | #if EV_USE_EPOLL |
532 | #if EV_USE_EPOLL |
499 | unsigned int egen; /* generation counter to counter epoll bugs */ |
533 | unsigned int egen; /* generation counter to counter epoll bugs */ |
500 | #endif |
534 | #endif |
… | |
… | |
562 | |
596 | |
563 | static int ev_default_loop_ptr; |
597 | static int ev_default_loop_ptr; |
564 | |
598 | |
565 | #endif |
599 | #endif |
566 | |
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 | |
567 | /*****************************************************************************/ |
613 | /*****************************************************************************/ |
568 | |
614 | |
569 | #ifndef EV_HAVE_EV_TIME |
615 | #ifndef EV_HAVE_EV_TIME |
570 | ev_tstamp |
616 | ev_tstamp |
571 | ev_time (void) |
617 | ev_time (void) |
… | |
… | |
627 | |
673 | |
628 | tv.tv_sec = (time_t)delay; |
674 | tv.tv_sec = (time_t)delay; |
629 | tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); |
675 | tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); |
630 | |
676 | |
631 | /* 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 */ |
632 | /* somehting not guaranteed by newer posix versions, but guaranteed */ |
678 | /* something not guaranteed by newer posix versions, but guaranteed */ |
633 | /* by older ones */ |
679 | /* by older ones */ |
634 | select (0, 0, 0, 0, &tv); |
680 | select (0, 0, 0, 0, &tv); |
635 | #endif |
681 | #endif |
636 | } |
682 | } |
637 | } |
683 | } |
… | |
… | |
745 | } |
791 | } |
746 | |
792 | |
747 | /*****************************************************************************/ |
793 | /*****************************************************************************/ |
748 | |
794 | |
749 | inline_speed void |
795 | inline_speed void |
750 | fd_event (EV_P_ int fd, int revents) |
796 | fd_event_nc (EV_P_ int fd, int revents) |
751 | { |
797 | { |
752 | ANFD *anfd = anfds + fd; |
798 | ANFD *anfd = anfds + fd; |
753 | ev_io *w; |
799 | ev_io *w; |
754 | |
800 | |
755 | 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) |
… | |
… | |
759 | if (ev) |
805 | if (ev) |
760 | ev_feed_event (EV_A_ (W)w, ev); |
806 | ev_feed_event (EV_A_ (W)w, ev); |
761 | } |
807 | } |
762 | } |
808 | } |
763 | |
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 | |
764 | void |
821 | void |
765 | ev_feed_fd_event (EV_P_ int fd, int revents) |
822 | ev_feed_fd_event (EV_P_ int fd, int revents) |
766 | { |
823 | { |
767 | if (fd >= 0 && fd < anfdmax) |
824 | if (fd >= 0 && fd < anfdmax) |
768 | fd_event (EV_A_ fd, revents); |
825 | fd_event_nc (EV_A_ fd, revents); |
769 | } |
826 | } |
770 | |
827 | |
771 | /* make sure the external fd watch events are in-sync */ |
828 | /* make sure the external fd watch events are in-sync */ |
772 | /* with the kernel/libev internal state */ |
829 | /* with the kernel/libev internal state */ |
773 | inline_size void |
830 | inline_size void |
… | |
… | |
888 | for (fd = 0; fd < anfdmax; ++fd) |
945 | for (fd = 0; fd < anfdmax; ++fd) |
889 | if (anfds [fd].events) |
946 | if (anfds [fd].events) |
890 | { |
947 | { |
891 | anfds [fd].events = 0; |
948 | anfds [fd].events = 0; |
892 | anfds [fd].emask = 0; |
949 | anfds [fd].emask = 0; |
893 | fd_change (EV_A_ fd, EV__IOFDSET | 1); |
950 | fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY); |
894 | } |
951 | } |
895 | } |
952 | } |
896 | |
953 | |
897 | /*****************************************************************************/ |
954 | /*****************************************************************************/ |
898 | |
955 | |
… | |
… | |
1073 | evpipe_init (EV_P) |
1130 | evpipe_init (EV_P) |
1074 | { |
1131 | { |
1075 | if (!ev_is_active (&pipe_w)) |
1132 | if (!ev_is_active (&pipe_w)) |
1076 | { |
1133 | { |
1077 | #if EV_USE_EVENTFD |
1134 | #if EV_USE_EVENTFD |
|
|
1135 | evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); |
|
|
1136 | if (evfd < 0 && errno == EINVAL) |
1078 | if ((evfd = eventfd (0, 0)) >= 0) |
1137 | evfd = eventfd (0, 0); |
|
|
1138 | |
|
|
1139 | if (evfd >= 0) |
1079 | { |
1140 | { |
1080 | evpipe [0] = -1; |
1141 | evpipe [0] = -1; |
1081 | fd_intern (evfd); |
1142 | fd_intern (evfd); /* doing it twice doesn't hurt */ |
1082 | ev_io_set (&pipe_w, evfd, EV_READ); |
1143 | ev_io_set (&pipe_w, evfd, EV_READ); |
1083 | } |
1144 | } |
1084 | else |
1145 | else |
1085 | #endif |
1146 | #endif |
1086 | { |
1147 | { |
… | |
… | |
1198 | signals [signum].gotsig = 0; |
1259 | signals [signum].gotsig = 0; |
1199 | |
1260 | |
1200 | for (w = signals [signum].head; w; w = w->next) |
1261 | for (w = signals [signum].head; w; w = w->next) |
1201 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
1262 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
1202 | } |
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 |
1203 | |
1284 | |
1204 | /*****************************************************************************/ |
1285 | /*****************************************************************************/ |
1205 | |
1286 | |
1206 | static WL childs [EV_PID_HASHSIZE]; |
1287 | static WL childs [EV_PID_HASHSIZE]; |
1207 | |
1288 | |
… | |
… | |
1352 | ev_backend (EV_P) |
1433 | ev_backend (EV_P) |
1353 | { |
1434 | { |
1354 | return backend; |
1435 | return backend; |
1355 | } |
1436 | } |
1356 | |
1437 | |
|
|
1438 | #if EV_MINIMAL < 2 |
1357 | unsigned int |
1439 | unsigned int |
1358 | ev_loop_count (EV_P) |
1440 | ev_loop_count (EV_P) |
1359 | { |
1441 | { |
1360 | return loop_count; |
1442 | return loop_count; |
1361 | } |
1443 | } |
1362 | |
1444 | |
|
|
1445 | unsigned int |
|
|
1446 | ev_loop_depth (EV_P) |
|
|
1447 | { |
|
|
1448 | return loop_depth; |
|
|
1449 | } |
|
|
1450 | |
1363 | void |
1451 | void |
1364 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) |
1452 | ev_set_io_collect_interval (EV_P_ ev_tstamp interval) |
1365 | { |
1453 | { |
1366 | io_blocktime = interval; |
1454 | io_blocktime = interval; |
1367 | } |
1455 | } |
… | |
… | |
1369 | void |
1457 | void |
1370 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
1458 | ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) |
1371 | { |
1459 | { |
1372 | timeout_blocktime = interval; |
1460 | timeout_blocktime = interval; |
1373 | } |
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 |
1374 | |
1486 | |
1375 | /* initialise a loop structure, must be zero-initialised */ |
1487 | /* initialise a loop structure, must be zero-initialised */ |
1376 | static void noinline |
1488 | static void noinline |
1377 | loop_init (EV_P_ unsigned int flags) |
1489 | loop_init (EV_P_ unsigned int flags) |
1378 | { |
1490 | { |
… | |
… | |
1400 | |
1512 | |
1401 | ev_rt_now = ev_time (); |
1513 | ev_rt_now = ev_time (); |
1402 | mn_now = get_clock (); |
1514 | mn_now = get_clock (); |
1403 | now_floor = mn_now; |
1515 | now_floor = mn_now; |
1404 | 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 |
1405 | |
1520 | |
1406 | io_blocktime = 0.; |
1521 | io_blocktime = 0.; |
1407 | timeout_blocktime = 0.; |
1522 | timeout_blocktime = 0.; |
1408 | backend = 0; |
1523 | backend = 0; |
1409 | backend_fd = -1; |
1524 | backend_fd = -1; |
1410 | gotasync = 0; |
1525 | gotasync = 0; |
1411 | #if EV_USE_INOTIFY |
1526 | #if EV_USE_INOTIFY |
1412 | fs_fd = -2; |
1527 | fs_fd = -2; |
1413 | #endif |
1528 | #endif |
|
|
1529 | #if EV_USE_SIGNALFD |
|
|
1530 | sigfd = -2; |
|
|
1531 | #endif |
1414 | |
1532 | |
1415 | /* pid check not overridable via env */ |
1533 | /* pid check not overridable via env */ |
1416 | #ifndef _WIN32 |
1534 | #ifndef _WIN32 |
1417 | if (flags & EVFLAG_FORKCHECK) |
1535 | if (flags & EVFLAG_FORKCHECK) |
1418 | curpid = getpid (); |
1536 | curpid = getpid (); |
… | |
… | |
1455 | { |
1573 | { |
1456 | int i; |
1574 | int i; |
1457 | |
1575 | |
1458 | if (ev_is_active (&pipe_w)) |
1576 | if (ev_is_active (&pipe_w)) |
1459 | { |
1577 | { |
1460 | ev_ref (EV_A); /* signal watcher */ |
1578 | /*ev_ref (EV_A);*/ |
1461 | ev_io_stop (EV_A_ &pipe_w); |
1579 | /*ev_io_stop (EV_A_ &pipe_w);*/ |
1462 | |
1580 | |
1463 | #if EV_USE_EVENTFD |
1581 | #if EV_USE_EVENTFD |
1464 | if (evfd >= 0) |
1582 | if (evfd >= 0) |
1465 | close (evfd); |
1583 | close (evfd); |
1466 | #endif |
1584 | #endif |
… | |
… | |
1469 | { |
1587 | { |
1470 | close (evpipe [0]); |
1588 | close (evpipe [0]); |
1471 | close (evpipe [1]); |
1589 | close (evpipe [1]); |
1472 | } |
1590 | } |
1473 | } |
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 |
1474 | |
1602 | |
1475 | #if EV_USE_INOTIFY |
1603 | #if EV_USE_INOTIFY |
1476 | if (fs_fd >= 0) |
1604 | if (fs_fd >= 0) |
1477 | close (fs_fd); |
1605 | close (fs_fd); |
1478 | #endif |
1606 | #endif |
… | |
… | |
1582 | ev_loop_new (unsigned int flags) |
1710 | ev_loop_new (unsigned int flags) |
1583 | { |
1711 | { |
1584 | 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)); |
1585 | |
1713 | |
1586 | memset (loop, 0, sizeof (struct ev_loop)); |
1714 | memset (loop, 0, sizeof (struct ev_loop)); |
1587 | |
|
|
1588 | loop_init (EV_A_ flags); |
1715 | loop_init (EV_A_ flags); |
1589 | |
1716 | |
1590 | if (ev_backend (EV_A)) |
1717 | if (ev_backend (EV_A)) |
1591 | return loop; |
1718 | return loop; |
1592 | |
1719 | |
… | |
… | |
1603 | void |
1730 | void |
1604 | ev_loop_fork (EV_P) |
1731 | ev_loop_fork (EV_P) |
1605 | { |
1732 | { |
1606 | postfork = 1; /* must be in line with ev_default_fork */ |
1733 | postfork = 1; /* must be in line with ev_default_fork */ |
1607 | } |
1734 | } |
|
|
1735 | #endif /* multiplicity */ |
1608 | |
1736 | |
1609 | #if EV_VERIFY |
1737 | #if EV_VERIFY |
1610 | static void noinline |
1738 | static void noinline |
1611 | verify_watcher (EV_P_ W w) |
1739 | verify_watcher (EV_P_ W w) |
1612 | { |
1740 | { |
… | |
… | |
1640 | verify_watcher (EV_A_ ws [cnt]); |
1768 | verify_watcher (EV_A_ ws [cnt]); |
1641 | } |
1769 | } |
1642 | } |
1770 | } |
1643 | #endif |
1771 | #endif |
1644 | |
1772 | |
|
|
1773 | #if EV_MINIMAL < 2 |
1645 | void |
1774 | void |
1646 | ev_loop_verify (EV_P) |
1775 | ev_loop_verify (EV_P) |
1647 | { |
1776 | { |
1648 | #if EV_VERIFY |
1777 | #if EV_VERIFY |
1649 | int i; |
1778 | int i; |
… | |
… | |
1702 | 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) |
1703 | for (signum = signalmax; signum--; ) if (signals [signum].gotsig) |
1832 | for (signum = signalmax; signum--; ) if (signals [signum].gotsig) |
1704 | # endif |
1833 | # endif |
1705 | #endif |
1834 | #endif |
1706 | } |
1835 | } |
1707 | |
1836 | #endif |
1708 | #endif /* multiplicity */ |
|
|
1709 | |
1837 | |
1710 | #if EV_MULTIPLICITY |
1838 | #if EV_MULTIPLICITY |
1711 | struct ev_loop * |
1839 | struct ev_loop * |
1712 | ev_default_loop_init (unsigned int flags) |
1840 | ev_default_loop_init (unsigned int flags) |
1713 | #else |
1841 | #else |
… | |
… | |
1774 | ev_invoke (EV_P_ void *w, int revents) |
1902 | ev_invoke (EV_P_ void *w, int revents) |
1775 | { |
1903 | { |
1776 | EV_CB_INVOKE ((W)w, revents); |
1904 | EV_CB_INVOKE ((W)w, revents); |
1777 | } |
1905 | } |
1778 | |
1906 | |
1779 | inline_speed void |
1907 | unsigned int |
1780 | 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) |
1781 | { |
1921 | { |
1782 | int pri; |
1922 | int pri; |
1783 | |
1923 | |
1784 | for (pri = NUMPRI; pri--; ) |
1924 | for (pri = NUMPRI; pri--; ) |
1785 | while (pendingcnt [pri]) |
1925 | while (pendingcnt [pri]) |
… | |
… | |
2020 | |
2160 | |
2021 | mn_now = ev_rt_now; |
2161 | mn_now = ev_rt_now; |
2022 | } |
2162 | } |
2023 | } |
2163 | } |
2024 | |
2164 | |
2025 | static int loop_done; |
|
|
2026 | |
|
|
2027 | void |
2165 | void |
2028 | ev_loop (EV_P_ int flags) |
2166 | ev_loop (EV_P_ int flags) |
2029 | { |
2167 | { |
|
|
2168 | #if EV_MINIMAL < 2 |
|
|
2169 | ++loop_depth; |
|
|
2170 | #endif |
|
|
2171 | |
|
|
2172 | assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE)); |
|
|
2173 | |
2030 | loop_done = EVUNLOOP_CANCEL; |
2174 | loop_done = EVUNLOOP_CANCEL; |
2031 | |
2175 | |
2032 | 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 */ |
2033 | |
2177 | |
2034 | do |
2178 | do |
2035 | { |
2179 | { |
2036 | #if EV_VERIFY >= 2 |
2180 | #if EV_VERIFY >= 2 |
2037 | ev_loop_verify (EV_A); |
2181 | ev_loop_verify (EV_A); |
… | |
… | |
2050 | /* we might have forked, so queue fork handlers */ |
2194 | /* we might have forked, so queue fork handlers */ |
2051 | if (expect_false (postfork)) |
2195 | if (expect_false (postfork)) |
2052 | if (forkcnt) |
2196 | if (forkcnt) |
2053 | { |
2197 | { |
2054 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
2198 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
2055 | call_pending (EV_A); |
2199 | EV_INVOKE_PENDING; |
2056 | } |
2200 | } |
2057 | #endif |
2201 | #endif |
2058 | |
2202 | |
2059 | /* queue prepare watchers (and execute them) */ |
2203 | /* queue prepare watchers (and execute them) */ |
2060 | if (expect_false (preparecnt)) |
2204 | if (expect_false (preparecnt)) |
2061 | { |
2205 | { |
2062 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
2206 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
2063 | call_pending (EV_A); |
2207 | EV_INVOKE_PENDING; |
2064 | } |
2208 | } |
|
|
2209 | |
|
|
2210 | if (expect_false (loop_done)) |
|
|
2211 | break; |
2065 | |
2212 | |
2066 | /* we might have forked, so reify kernel state if necessary */ |
2213 | /* we might have forked, so reify kernel state if necessary */ |
2067 | if (expect_false (postfork)) |
2214 | if (expect_false (postfork)) |
2068 | loop_fork (EV_A); |
2215 | loop_fork (EV_A); |
2069 | |
2216 | |
… | |
… | |
2117 | waittime -= sleeptime; |
2264 | waittime -= sleeptime; |
2118 | } |
2265 | } |
2119 | } |
2266 | } |
2120 | } |
2267 | } |
2121 | |
2268 | |
|
|
2269 | #if EV_MINIMAL < 2 |
2122 | ++loop_count; |
2270 | ++loop_count; |
|
|
2271 | #endif |
|
|
2272 | assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */ |
2123 | backend_poll (EV_A_ waittime); |
2273 | backend_poll (EV_A_ waittime); |
|
|
2274 | assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */ |
2124 | |
2275 | |
2125 | /* update ev_rt_now, do magic */ |
2276 | /* update ev_rt_now, do magic */ |
2126 | time_update (EV_A_ waittime + sleeptime); |
2277 | time_update (EV_A_ waittime + sleeptime); |
2127 | } |
2278 | } |
2128 | |
2279 | |
… | |
… | |
2139 | |
2290 | |
2140 | /* queue check watchers, to be executed first */ |
2291 | /* queue check watchers, to be executed first */ |
2141 | if (expect_false (checkcnt)) |
2292 | if (expect_false (checkcnt)) |
2142 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
2293 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
2143 | |
2294 | |
2144 | call_pending (EV_A); |
2295 | EV_INVOKE_PENDING; |
2145 | } |
2296 | } |
2146 | while (expect_true ( |
2297 | while (expect_true ( |
2147 | activecnt |
2298 | activecnt |
2148 | && !loop_done |
2299 | && !loop_done |
2149 | && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) |
2300 | && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) |
2150 | )); |
2301 | )); |
2151 | |
2302 | |
2152 | if (loop_done == EVUNLOOP_ONE) |
2303 | if (loop_done == EVUNLOOP_ONE) |
2153 | loop_done = EVUNLOOP_CANCEL; |
2304 | loop_done = EVUNLOOP_CANCEL; |
|
|
2305 | |
|
|
2306 | #if EV_MINIMAL < 2 |
|
|
2307 | --loop_depth; |
|
|
2308 | #endif |
2154 | } |
2309 | } |
2155 | |
2310 | |
2156 | void |
2311 | void |
2157 | ev_unloop (EV_P_ int how) |
2312 | ev_unloop (EV_P_ int how) |
2158 | { |
2313 | { |
… | |
… | |
2250 | } |
2405 | } |
2251 | |
2406 | |
2252 | inline_size void |
2407 | inline_size void |
2253 | pri_adjust (EV_P_ W w) |
2408 | pri_adjust (EV_P_ W w) |
2254 | { |
2409 | { |
2255 | int pri = w->priority; |
2410 | int pri = ev_priority (w); |
2256 | pri = pri < EV_MINPRI ? EV_MINPRI : pri; |
2411 | pri = pri < EV_MINPRI ? EV_MINPRI : pri; |
2257 | pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; |
2412 | pri = pri > EV_MAXPRI ? EV_MAXPRI : pri; |
2258 | w->priority = pri; |
2413 | ev_set_priority (w, pri); |
2259 | } |
2414 | } |
2260 | |
2415 | |
2261 | inline_speed void |
2416 | inline_speed void |
2262 | ev_start (EV_P_ W w, int active) |
2417 | ev_start (EV_P_ W w, int active) |
2263 | { |
2418 | { |
… | |
… | |
2290 | |
2445 | |
2291 | ev_start (EV_A_ (W)w, 1); |
2446 | ev_start (EV_A_ (W)w, 1); |
2292 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
2447 | array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); |
2293 | wlist_add (&anfds[fd].head, (WL)w); |
2448 | wlist_add (&anfds[fd].head, (WL)w); |
2294 | |
2449 | |
2295 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | 1); |
2450 | fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY); |
2296 | w->events &= ~EV__IOFDSET; |
2451 | w->events &= ~EV__IOFDSET; |
2297 | |
2452 | |
2298 | EV_FREQUENT_CHECK; |
2453 | EV_FREQUENT_CHECK; |
2299 | } |
2454 | } |
2300 | |
2455 | |
… | |
… | |
2394 | } |
2549 | } |
2395 | |
2550 | |
2396 | EV_FREQUENT_CHECK; |
2551 | EV_FREQUENT_CHECK; |
2397 | } |
2552 | } |
2398 | |
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 | |
2399 | #if EV_PERIODIC_ENABLE |
2560 | #if EV_PERIODIC_ENABLE |
2400 | void noinline |
2561 | void noinline |
2401 | ev_periodic_start (EV_P_ ev_periodic *w) |
2562 | ev_periodic_start (EV_P_ ev_periodic *w) |
2402 | { |
2563 | { |
2403 | if (expect_false (ev_is_active (w))) |
2564 | if (expect_false (ev_is_active (w))) |
… | |
… | |
2478 | if (expect_false (ev_is_active (w))) |
2639 | if (expect_false (ev_is_active (w))) |
2479 | return; |
2640 | return; |
2480 | |
2641 | |
2481 | 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)); |
2482 | |
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 |
2483 | evpipe_init (EV_A); |
2676 | evpipe_init (EV_A); |
2484 | |
|
|
2485 | EV_FREQUENT_CHECK; |
|
|
2486 | |
2677 | |
2487 | { |
2678 | { |
2488 | #ifndef _WIN32 |
2679 | #ifndef _WIN32 |
2489 | sigset_t full, prev; |
2680 | sigset_t full, prev; |
2490 | sigfillset (&full); |
2681 | sigfillset (&full); |
… | |
… | |
2492 | #endif |
2683 | #endif |
2493 | |
2684 | |
2494 | array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero); |
2685 | array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero); |
2495 | |
2686 | |
2496 | #ifndef _WIN32 |
2687 | #ifndef _WIN32 |
|
|
2688 | if (sigfd < 0)/*TODO*/ |
|
|
2689 | sigdelset (&prev, w->signum); |
2497 | sigprocmask (SIG_SETMASK, &prev, 0); |
2690 | sigprocmask (SIG_SETMASK, &prev, 0); |
2498 | #endif |
2691 | #endif |
2499 | } |
2692 | } |
2500 | |
2693 | |
2501 | ev_start (EV_A_ (W)w, 1); |
2694 | ev_start (EV_A_ (W)w, 1); |
… | |
… | |
2504 | if (!((WL)w)->next) |
2697 | if (!((WL)w)->next) |
2505 | { |
2698 | { |
2506 | #if _WIN32 |
2699 | #if _WIN32 |
2507 | signal (w->signum, ev_sighandler); |
2700 | signal (w->signum, ev_sighandler); |
2508 | #else |
2701 | #else |
|
|
2702 | if (sigfd < 0) /*TODO*/ |
|
|
2703 | { |
2509 | struct sigaction sa; |
2704 | struct sigaction sa = { }; |
2510 | sa.sa_handler = ev_sighandler; |
2705 | sa.sa_handler = ev_sighandler; |
2511 | sigfillset (&sa.sa_mask); |
2706 | sigfillset (&sa.sa_mask); |
2512 | 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 */ |
2513 | sigaction (w->signum, &sa, 0); |
2708 | sigaction (w->signum, &sa, 0); |
|
|
2709 | } |
2514 | #endif |
2710 | #endif |
2515 | } |
2711 | } |
2516 | |
2712 | |
2517 | EV_FREQUENT_CHECK; |
2713 | EV_FREQUENT_CHECK; |
2518 | } |
2714 | } |
… | |
… | |
2528 | |
2724 | |
2529 | wlist_del (&signals [w->signum - 1].head, (WL)w); |
2725 | wlist_del (&signals [w->signum - 1].head, (WL)w); |
2530 | ev_stop (EV_A_ (W)w); |
2726 | ev_stop (EV_A_ (W)w); |
2531 | |
2727 | |
2532 | 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 |
2533 | signal (w->signum, SIG_DFL); |
2740 | signal (w->signum, SIG_DFL); |
2534 | |
2741 | |
2535 | EV_FREQUENT_CHECK; |
2742 | EV_FREQUENT_CHECK; |
2536 | } |
2743 | } |
2537 | |
2744 | |
2538 | void |
2745 | void |