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

Comparing libev/ev.c (file contents):
Revision 1.357 by root, Sat Oct 23 22:25:44 2010 UTC vs.
Revision 1.368 by root, Mon Jan 17 12:11:11 2011 UTC

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 *
530 530
531static unsigned int noinline 531static unsigned int noinline
532ev_linux_version (void) 532ev_linux_version (void)
533{ 533{
534#ifdef __linux 534#ifdef __linux
535 unsigned int v = 0;
535 struct utsname buf; 536 struct utsname buf;
536 unsigned int v;
537 int i; 537 int i;
538 char *p = buf.release; 538 char *p = buf.release;
539 539
540 if (uname (&buf)) 540 if (uname (&buf))
541 return 0; 541 return 0;
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;
815 /* by older ones */ 813 /* by older ones */
816 EV_TV_SET (tv, delay); 814 EV_TV_SET (tv, delay);
817 select (0, 0, 0, 0, &tv); 815 select (0, 0, 0, 0, &tv);
818#endif 816#endif
819 } 817 }
818}
819
820inline_speed int
821ev_timeout_to_ms (ev_tstamp timeout)
822{
823 int ms = timeout * 1000. + .999999;
824
825 return expect_true (ms) ? ms : timeout < 1e-6 ? 0 : 1;
820} 826}
821 827
822/*****************************************************************************/ 828/*****************************************************************************/
823 829
824#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ 830#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
1371#endif 1377#endif
1372} 1378}
1373 1379
1374/*****************************************************************************/ 1380/*****************************************************************************/
1375 1381
1382void
1383ev_feed_signal (int signum)
1384{
1385#if EV_MULTIPLICITY
1386 EV_P = signals [signum - 1].loop;
1387
1388 if (!EV_A)
1389 return;
1390#endif
1391
1392 signals [signum - 1].pending = 1;
1393 evpipe_write (EV_A_ &sig_pending);
1394}
1395
1376static void 1396static void
1377ev_sighandler (int signum) 1397ev_sighandler (int signum)
1378{ 1398{
1379#if EV_MULTIPLICITY
1380 EV_P = signals [signum - 1].loop;
1381#endif
1382
1383#ifdef _WIN32 1399#ifdef _WIN32
1384 signal (signum, ev_sighandler); 1400 signal (signum, ev_sighandler);
1385#endif 1401#endif
1386 1402
1387 signals [signum - 1].pending = 1; 1403 ev_feed_signal (signum);
1388 evpipe_write (EV_A_ &sig_pending);
1389} 1404}
1390 1405
1391void noinline 1406void noinline
1392ev_feed_signal_event (EV_P_ int signum) 1407ev_feed_signal_event (EV_P_ int signum)
1393{ 1408{
1645static void noinline 1660static void noinline
1646loop_init (EV_P_ unsigned int flags) 1661loop_init (EV_P_ unsigned int flags)
1647{ 1662{
1648 if (!backend) 1663 if (!backend)
1649 { 1664 {
1665 origflags = flags;
1666
1650#if EV_USE_REALTIME 1667#if EV_USE_REALTIME
1651 if (!have_realtime) 1668 if (!have_realtime)
1652 { 1669 {
1653 struct timespec ts; 1670 struct timespec ts;
1654 1671
1699#endif 1716#endif
1700#if EV_USE_SIGNALFD 1717#if EV_USE_SIGNALFD
1701 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; 1718 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
1702#endif 1719#endif
1703 1720
1704 if (!(flags & 0x0000ffffU)) 1721 if (!(flags & EVBACKEND_MASK))
1705 flags |= ev_recommended_backends (); 1722 flags |= ev_recommended_backends ();
1706 1723
1707#if EV_USE_IOCP 1724#if EV_USE_IOCP
1708 if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags); 1725 if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags);
1709#endif 1726#endif
1731#endif 1748#endif
1732 } 1749 }
1733} 1750}
1734 1751
1735/* free up a loop structure */ 1752/* free up a loop structure */
1736static void noinline 1753void
1737loop_destroy (EV_P) 1754ev_loop_destroy (EV_P)
1738{ 1755{
1739 int i; 1756 int i;
1757
1758#if EV_MULTIPLICITY
1759 /* mimic free (0) */
1760 if (!EV_A)
1761 return;
1762#endif
1763
1764#if EV_CLEANUP_ENABLE
1765 /* queue cleanup watchers (and execute them) */
1766 if (expect_false (cleanupcnt))
1767 {
1768 queue_events (EV_A_ (W *)cleanups, cleanupcnt, EV_CLEANUP);
1769 EV_INVOKE_PENDING;
1770 }
1771#endif
1772
1773#if EV_CHILD_ENABLE
1774 if (ev_is_active (&childev))
1775 {
1776 ev_ref (EV_A); /* child watcher */
1777 ev_signal_stop (EV_A_ &childev);
1778 }
1779#endif
1740 1780
1741 if (ev_is_active (&pipe_w)) 1781 if (ev_is_active (&pipe_w))
1742 { 1782 {
1743 /*ev_ref (EV_A);*/ 1783 /*ev_ref (EV_A);*/
1744 /*ev_io_stop (EV_A_ &pipe_w);*/ 1784 /*ev_io_stop (EV_A_ &pipe_w);*/
1805 array_free (periodic, EMPTY); 1845 array_free (periodic, EMPTY);
1806#endif 1846#endif
1807#if EV_FORK_ENABLE 1847#if EV_FORK_ENABLE
1808 array_free (fork, EMPTY); 1848 array_free (fork, EMPTY);
1809#endif 1849#endif
1850#if EV_CLEANUP_ENABLE
1851 array_free (cleanup, EMPTY);
1852#endif
1810 array_free (prepare, EMPTY); 1853 array_free (prepare, EMPTY);
1811 array_free (check, EMPTY); 1854 array_free (check, EMPTY);
1812#if EV_ASYNC_ENABLE 1855#if EV_ASYNC_ENABLE
1813 array_free (async, EMPTY); 1856 array_free (async, EMPTY);
1814#endif 1857#endif
1815 1858
1816 backend = 0; 1859 backend = 0;
1860
1861#if EV_MULTIPLICITY
1862 if (ev_is_default_loop (EV_A))
1863#endif
1864 ev_default_loop_ptr = 0;
1865#if EV_MULTIPLICITY
1866 else
1867 ev_free (EV_A);
1868#endif
1817} 1869}
1818 1870
1819#if EV_USE_INOTIFY 1871#if EV_USE_INOTIFY
1820inline_size void infy_fork (EV_P); 1872inline_size void infy_fork (EV_P);
1821#endif 1873#endif
1880 loop_init (EV_A_ flags); 1932 loop_init (EV_A_ flags);
1881 1933
1882 if (ev_backend (EV_A)) 1934 if (ev_backend (EV_A))
1883 return EV_A; 1935 return EV_A;
1884 1936
1937 ev_free (EV_A);
1885 return 0; 1938 return 0;
1886} 1939}
1887 1940
1888void
1889ev_loop_destroy (EV_P)
1890{
1891 loop_destroy (EV_A);
1892 ev_free (loop);
1893}
1894
1895void
1896ev_loop_fork (EV_P)
1897{
1898 postfork = 1; /* must be in line with ev_default_fork */
1899}
1900#endif /* multiplicity */ 1941#endif /* multiplicity */
1901 1942
1902#if EV_VERIFY 1943#if EV_VERIFY
1903static void noinline 1944static void noinline
1904verify_watcher (EV_P_ W w) 1945verify_watcher (EV_P_ W w)
1979#if EV_FORK_ENABLE 2020#if EV_FORK_ENABLE
1980 assert (forkmax >= forkcnt); 2021 assert (forkmax >= forkcnt);
1981 array_verify (EV_A_ (W *)forks, forkcnt); 2022 array_verify (EV_A_ (W *)forks, forkcnt);
1982#endif 2023#endif
1983 2024
2025#if EV_CLEANUP_ENABLE
2026 assert (cleanupmax >= cleanupcnt);
2027 array_verify (EV_A_ (W *)cleanups, cleanupcnt);
2028#endif
2029
1984#if EV_ASYNC_ENABLE 2030#if EV_ASYNC_ENABLE
1985 assert (asyncmax >= asynccnt); 2031 assert (asyncmax >= asynccnt);
1986 array_verify (EV_A_ (W *)asyncs, asynccnt); 2032 array_verify (EV_A_ (W *)asyncs, asynccnt);
1987#endif 2033#endif
1988 2034
2006} 2052}
2007#endif 2053#endif
2008 2054
2009#if EV_MULTIPLICITY 2055#if EV_MULTIPLICITY
2010struct ev_loop * 2056struct ev_loop *
2011ev_default_loop_init (unsigned int flags)
2012#else 2057#else
2013int 2058int
2059#endif
2014ev_default_loop (unsigned int flags) 2060ev_default_loop (unsigned int flags)
2015#endif
2016{ 2061{
2017 if (!ev_default_loop_ptr) 2062 if (!ev_default_loop_ptr)
2018 { 2063 {
2019#if EV_MULTIPLICITY 2064#if EV_MULTIPLICITY
2020 EV_P = ev_default_loop_ptr = &default_loop_struct; 2065 EV_P = ev_default_loop_ptr = &default_loop_struct;
2039 2084
2040 return ev_default_loop_ptr; 2085 return ev_default_loop_ptr;
2041} 2086}
2042 2087
2043void 2088void
2044ev_default_destroy (void) 2089ev_loop_fork (EV_P)
2045{ 2090{
2046#if EV_MULTIPLICITY
2047 EV_P = ev_default_loop_ptr;
2048#endif
2049
2050 ev_default_loop_ptr = 0;
2051
2052#if EV_CHILD_ENABLE
2053 ev_ref (EV_A); /* child watcher */
2054 ev_signal_stop (EV_A_ &childev);
2055#endif
2056
2057 loop_destroy (EV_A);
2058}
2059
2060void
2061ev_default_fork (void)
2062{
2063#if EV_MULTIPLICITY
2064 EV_P = ev_default_loop_ptr;
2065#endif
2066
2067 postfork = 1; /* must be in line with ev_loop_fork */ 2091 postfork = 1; /* must be in line with ev_default_fork */
2068} 2092}
2069 2093
2070/*****************************************************************************/ 2094/*****************************************************************************/
2071 2095
2072void 2096void
2094 2118
2095 for (pri = NUMPRI; pri--; ) 2119 for (pri = NUMPRI; pri--; )
2096 while (pendingcnt [pri]) 2120 while (pendingcnt [pri])
2097 { 2121 {
2098 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 2122 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
2099
2100 /*assert (("libev: non-pending watcher on pending list", p->w->pending));*/
2101 /* ^ this is no longer true, as pending_w could be here */
2102 2123
2103 p->w->pending = 0; 2124 p->w->pending = 0;
2104 EV_CB_INVOKE (p->w, p->events); 2125 EV_CB_INVOKE (p->w, p->events);
2105 EV_FREQUENT_CHECK; 2126 EV_FREQUENT_CHECK;
2106 } 2127 }
2874 sa.sa_handler = ev_sighandler; 2895 sa.sa_handler = ev_sighandler;
2875 sigfillset (&sa.sa_mask); 2896 sigfillset (&sa.sa_mask);
2876 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 2897 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
2877 sigaction (w->signum, &sa, 0); 2898 sigaction (w->signum, &sa, 0);
2878 2899
2900 if (origflags & EVFLAG_NOSIGMASK)
2901 {
2879 sigemptyset (&sa.sa_mask); 2902 sigemptyset (&sa.sa_mask);
2880 sigaddset (&sa.sa_mask, w->signum); 2903 sigaddset (&sa.sa_mask, w->signum);
2881 sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0); 2904 sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0);
2905 }
2882#endif 2906#endif
2883 } 2907 }
2884 2908
2885 EV_FREQUENT_CHECK; 2909 EV_FREQUENT_CHECK;
2886} 2910}
3578 3602
3579 EV_FREQUENT_CHECK; 3603 EV_FREQUENT_CHECK;
3580} 3604}
3581#endif 3605#endif
3582 3606
3607#if EV_CLEANUP_ENABLE
3608void
3609ev_cleanup_start (EV_P_ ev_cleanup *w)
3610{
3611 if (expect_false (ev_is_active (w)))
3612 return;
3613
3614 EV_FREQUENT_CHECK;
3615
3616 ev_start (EV_A_ (W)w, ++cleanupcnt);
3617 array_needsize (ev_cleanup *, cleanups, cleanupmax, cleanupcnt, EMPTY2);
3618 cleanups [cleanupcnt - 1] = w;
3619
3620 /* cleanup watchers should never keep a refcount on the loop */
3621 ev_unref (EV_A);
3622 EV_FREQUENT_CHECK;
3623}
3624
3625void
3626ev_cleanup_stop (EV_P_ ev_cleanup *w)
3627{
3628 clear_pending (EV_A_ (W)w);
3629 if (expect_false (!ev_is_active (w)))
3630 return;
3631
3632 EV_FREQUENT_CHECK;
3633 ev_ref (EV_A);
3634
3635 {
3636 int active = ev_active (w);
3637
3638 cleanups [active - 1] = cleanups [--cleanupcnt];
3639 ev_active (cleanups [active - 1]) = active;
3640 }
3641
3642 ev_stop (EV_A_ (W)w);
3643
3644 EV_FREQUENT_CHECK;
3645}
3646#endif
3647
3583#if EV_ASYNC_ENABLE 3648#if EV_ASYNC_ENABLE
3584void 3649void
3585ev_async_start (EV_P_ ev_async *w) 3650ev_async_start (EV_P_ ev_async *w)
3586{ 3651{
3587 if (expect_false (ev_is_active (w))) 3652 if (expect_false (ev_is_active (w)))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines