1 | /* |
1 | /* |
2 | * libev event processing core, watcher management |
2 | * libev event processing core, watcher management |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
… | |
… | |
591 | if (syserr_cb) |
591 | if (syserr_cb) |
592 | syserr_cb (msg); |
592 | syserr_cb (msg); |
593 | else |
593 | else |
594 | { |
594 | { |
595 | #if EV_AVOID_STDIO |
595 | #if EV_AVOID_STDIO |
596 | const char *err = strerror (errno); |
|
|
597 | |
|
|
598 | ev_printerr (msg); |
596 | ev_printerr (msg); |
599 | ev_printerr (": "); |
597 | ev_printerr (": "); |
600 | ev_printerr (err); |
598 | ev_printerr (strerror (errno)); |
601 | ev_printerr ("\n"); |
599 | ev_printerr ("\n"); |
602 | #else |
600 | #else |
603 | perror (msg); |
601 | perror (msg); |
604 | #endif |
602 | #endif |
605 | abort (); |
603 | abort (); |
… | |
… | |
639 | ptr = alloc (ptr, size); |
637 | ptr = alloc (ptr, size); |
640 | |
638 | |
641 | if (!ptr && size) |
639 | if (!ptr && size) |
642 | { |
640 | { |
643 | #if EV_AVOID_STDIO |
641 | #if EV_AVOID_STDIO |
644 | ev_printerr ("libev: memory allocation failed, aborting.\n"); |
642 | ev_printerr ("(libev) memory allocation failed, aborting.\n"); |
645 | #else |
643 | #else |
646 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
644 | fprintf (stderr, "(libev) cannot allocate %ld bytes, aborting.", size); |
647 | #endif |
645 | #endif |
648 | abort (); |
646 | abort (); |
649 | } |
647 | } |
650 | |
648 | |
651 | return ptr; |
649 | return ptr; |
… | |
… | |
1371 | #endif |
1369 | #endif |
1372 | } |
1370 | } |
1373 | |
1371 | |
1374 | /*****************************************************************************/ |
1372 | /*****************************************************************************/ |
1375 | |
1373 | |
|
|
1374 | void |
|
|
1375 | ev_feed_signal (int signum) |
|
|
1376 | { |
|
|
1377 | #if EV_MULTIPLICITY |
|
|
1378 | EV_P = signals [signum - 1].loop; |
|
|
1379 | |
|
|
1380 | if (!EV_A) |
|
|
1381 | return; |
|
|
1382 | #endif |
|
|
1383 | |
|
|
1384 | signals [signum - 1].pending = 1; |
|
|
1385 | evpipe_write (EV_A_ &sig_pending); |
|
|
1386 | } |
|
|
1387 | |
1376 | static void |
1388 | static void |
1377 | ev_sighandler (int signum) |
1389 | ev_sighandler (int signum) |
1378 | { |
1390 | { |
1379 | #if EV_MULTIPLICITY |
|
|
1380 | EV_P = signals [signum - 1].loop; |
|
|
1381 | #endif |
|
|
1382 | |
|
|
1383 | #ifdef _WIN32 |
1391 | #ifdef _WIN32 |
1384 | signal (signum, ev_sighandler); |
1392 | signal (signum, ev_sighandler); |
1385 | #endif |
1393 | #endif |
1386 | |
1394 | |
1387 | signals [signum - 1].pending = 1; |
1395 | ev_feed_signal (signum); |
1388 | evpipe_write (EV_A_ &sig_pending); |
|
|
1389 | } |
1396 | } |
1390 | |
1397 | |
1391 | void noinline |
1398 | void noinline |
1392 | ev_feed_signal_event (EV_P_ int signum) |
1399 | ev_feed_signal_event (EV_P_ int signum) |
1393 | { |
1400 | { |
… | |
… | |
1645 | static void noinline |
1652 | static void noinline |
1646 | loop_init (EV_P_ unsigned int flags) |
1653 | loop_init (EV_P_ unsigned int flags) |
1647 | { |
1654 | { |
1648 | if (!backend) |
1655 | if (!backend) |
1649 | { |
1656 | { |
|
|
1657 | origflags = flags; |
|
|
1658 | |
1650 | #if EV_USE_REALTIME |
1659 | #if EV_USE_REALTIME |
1651 | if (!have_realtime) |
1660 | if (!have_realtime) |
1652 | { |
1661 | { |
1653 | struct timespec ts; |
1662 | struct timespec ts; |
1654 | |
1663 | |
… | |
… | |
1699 | #endif |
1708 | #endif |
1700 | #if EV_USE_SIGNALFD |
1709 | #if EV_USE_SIGNALFD |
1701 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1710 | sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; |
1702 | #endif |
1711 | #endif |
1703 | |
1712 | |
1704 | if (!(flags & 0x0000ffffU)) |
1713 | if (!(flags & EVBACKEND_MASK)) |
1705 | flags |= ev_recommended_backends (); |
1714 | flags |= ev_recommended_backends (); |
1706 | |
1715 | |
1707 | #if EV_USE_IOCP |
1716 | #if EV_USE_IOCP |
1708 | if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags); |
1717 | if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags); |
1709 | #endif |
1718 | #endif |
… | |
… | |
1735 | /* free up a loop structure */ |
1744 | /* free up a loop structure */ |
1736 | void |
1745 | void |
1737 | ev_loop_destroy (EV_P) |
1746 | ev_loop_destroy (EV_P) |
1738 | { |
1747 | { |
1739 | int i; |
1748 | int i; |
|
|
1749 | |
|
|
1750 | #if EV_MULTIPLICITY |
|
|
1751 | /* mimic free (0) */ |
|
|
1752 | if (!EV_A) |
|
|
1753 | return; |
|
|
1754 | #endif |
|
|
1755 | |
|
|
1756 | #if EV_CLEANUP_ENABLE |
|
|
1757 | /* queue cleanup watchers (and execute them) */ |
|
|
1758 | if (expect_false (cleanupcnt)) |
|
|
1759 | { |
|
|
1760 | queue_events (EV_A_ (W *)cleanups, cleanupcnt, EV_CLEANUP); |
|
|
1761 | EV_INVOKE_PENDING; |
|
|
1762 | } |
|
|
1763 | #endif |
1740 | |
1764 | |
1741 | #if EV_CHILD_ENABLE |
1765 | #if EV_CHILD_ENABLE |
1742 | if (ev_is_active (&childev)) |
1766 | if (ev_is_active (&childev)) |
1743 | { |
1767 | { |
1744 | ev_ref (EV_A); /* child watcher */ |
1768 | ev_ref (EV_A); /* child watcher */ |
… | |
… | |
1812 | #if EV_PERIODIC_ENABLE |
1836 | #if EV_PERIODIC_ENABLE |
1813 | array_free (periodic, EMPTY); |
1837 | array_free (periodic, EMPTY); |
1814 | #endif |
1838 | #endif |
1815 | #if EV_FORK_ENABLE |
1839 | #if EV_FORK_ENABLE |
1816 | array_free (fork, EMPTY); |
1840 | array_free (fork, EMPTY); |
|
|
1841 | #endif |
|
|
1842 | #if EV_CLEANUP_ENABLE |
|
|
1843 | array_free (cleanup, EMPTY); |
1817 | #endif |
1844 | #endif |
1818 | array_free (prepare, EMPTY); |
1845 | array_free (prepare, EMPTY); |
1819 | array_free (check, EMPTY); |
1846 | array_free (check, EMPTY); |
1820 | #if EV_ASYNC_ENABLE |
1847 | #if EV_ASYNC_ENABLE |
1821 | array_free (async, EMPTY); |
1848 | array_free (async, EMPTY); |
… | |
… | |
1983 | } |
2010 | } |
1984 | |
2011 | |
1985 | #if EV_FORK_ENABLE |
2012 | #if EV_FORK_ENABLE |
1986 | assert (forkmax >= forkcnt); |
2013 | assert (forkmax >= forkcnt); |
1987 | array_verify (EV_A_ (W *)forks, forkcnt); |
2014 | array_verify (EV_A_ (W *)forks, forkcnt); |
|
|
2015 | #endif |
|
|
2016 | |
|
|
2017 | #if EV_CLEANUP_ENABLE |
|
|
2018 | assert (cleanupmax >= cleanupcnt); |
|
|
2019 | array_verify (EV_A_ (W *)cleanups, cleanupcnt); |
1988 | #endif |
2020 | #endif |
1989 | |
2021 | |
1990 | #if EV_ASYNC_ENABLE |
2022 | #if EV_ASYNC_ENABLE |
1991 | assert (asyncmax >= asynccnt); |
2023 | assert (asyncmax >= asynccnt); |
1992 | array_verify (EV_A_ (W *)asyncs, asynccnt); |
2024 | array_verify (EV_A_ (W *)asyncs, asynccnt); |
… | |
… | |
2858 | sa.sa_handler = ev_sighandler; |
2890 | sa.sa_handler = ev_sighandler; |
2859 | sigfillset (&sa.sa_mask); |
2891 | sigfillset (&sa.sa_mask); |
2860 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2892 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
2861 | sigaction (w->signum, &sa, 0); |
2893 | sigaction (w->signum, &sa, 0); |
2862 | |
2894 | |
|
|
2895 | if (origflags & EVFLAG_NOSIGMASK) |
|
|
2896 | { |
2863 | sigemptyset (&sa.sa_mask); |
2897 | sigemptyset (&sa.sa_mask); |
2864 | sigaddset (&sa.sa_mask, w->signum); |
2898 | sigaddset (&sa.sa_mask, w->signum); |
2865 | sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); |
2899 | sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); |
|
|
2900 | } |
2866 | #endif |
2901 | #endif |
2867 | } |
2902 | } |
2868 | |
2903 | |
2869 | EV_FREQUENT_CHECK; |
2904 | EV_FREQUENT_CHECK; |
2870 | } |
2905 | } |
… | |
… | |
3562 | |
3597 | |
3563 | EV_FREQUENT_CHECK; |
3598 | EV_FREQUENT_CHECK; |
3564 | } |
3599 | } |
3565 | #endif |
3600 | #endif |
3566 | |
3601 | |
|
|
3602 | #if EV_CLEANUP_ENABLE |
|
|
3603 | void |
|
|
3604 | ev_cleanup_start (EV_P_ ev_cleanup *w) |
|
|
3605 | { |
|
|
3606 | if (expect_false (ev_is_active (w))) |
|
|
3607 | return; |
|
|
3608 | |
|
|
3609 | EV_FREQUENT_CHECK; |
|
|
3610 | |
|
|
3611 | ev_start (EV_A_ (W)w, ++cleanupcnt); |
|
|
3612 | array_needsize (ev_cleanup *, cleanups, cleanupmax, cleanupcnt, EMPTY2); |
|
|
3613 | cleanups [cleanupcnt - 1] = w; |
|
|
3614 | |
|
|
3615 | /* cleanup watchers should never keep a refcount on the loop */ |
|
|
3616 | ev_unref (EV_A); |
|
|
3617 | EV_FREQUENT_CHECK; |
|
|
3618 | } |
|
|
3619 | |
|
|
3620 | void |
|
|
3621 | ev_cleanup_stop (EV_P_ ev_cleanup *w) |
|
|
3622 | { |
|
|
3623 | clear_pending (EV_A_ (W)w); |
|
|
3624 | if (expect_false (!ev_is_active (w))) |
|
|
3625 | return; |
|
|
3626 | |
|
|
3627 | EV_FREQUENT_CHECK; |
|
|
3628 | ev_ref (EV_A); |
|
|
3629 | |
|
|
3630 | { |
|
|
3631 | int active = ev_active (w); |
|
|
3632 | |
|
|
3633 | cleanups [active - 1] = cleanups [--cleanupcnt]; |
|
|
3634 | ev_active (cleanups [active - 1]) = active; |
|
|
3635 | } |
|
|
3636 | |
|
|
3637 | ev_stop (EV_A_ (W)w); |
|
|
3638 | |
|
|
3639 | EV_FREQUENT_CHECK; |
|
|
3640 | } |
|
|
3641 | #endif |
|
|
3642 | |
3567 | #if EV_ASYNC_ENABLE |
3643 | #if EV_ASYNC_ENABLE |
3568 | void |
3644 | void |
3569 | ev_async_start (EV_P_ ev_async *w) |
3645 | ev_async_start (EV_P_ ev_async *w) |
3570 | { |
3646 | { |
3571 | if (expect_false (ev_is_active (w))) |
3647 | if (expect_false (ev_is_active (w))) |