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

Comparing libev/ev.c (file contents):
Revision 1.185 by root, Fri Dec 14 18:22:30 2007 UTC vs.
Revision 1.206 by root, Fri Jan 25 15:45:08 2008 UTC

2 * libev event processing core, watcher management 2 * libev event processing core, watcher management
3 * 3 *
4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 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 7 * Redistribution and use in source and binary forms, with or without modifica-
8 * modification, are permitted provided that the following conditions are 8 * tion, are permitted provided that the following conditions are met:
9 * met: 9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
25 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26 * OF THE POSSIBILITY OF SUCH DAMAGE.
10 * 27 *
11 * * Redistributions of source code must retain the above copyright 28 * Alternatively, the contents of this file may be used under the terms of
12 * notice, this list of conditions and the following disclaimer. 29 * the GNU General Public License ("GPL") version 2 or any later version,
13 * 30 * in which case the provisions of the GPL are applicable instead of
14 * * Redistributions in binary form must reproduce the above 31 * the above. If you wish to allow the use of your version of this file
15 * copyright notice, this list of conditions and the following 32 * only under the terms of the GPL and not to allow others to use your
16 * disclaimer in the documentation and/or other materials provided 33 * version of this file under the BSD license, indicate your decision
17 * with the distribution. 34 * by deleting the provisions above and replace them with the notice
18 * 35 * and other provisions required by the GPL. If you do not delete the
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 36 * provisions above, a recipient may use your version of this file under
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 37 * either the BSD or the GPL.
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 38 */
31 39
32#ifdef __cplusplus 40#ifdef __cplusplus
33extern "C" { 41extern "C" {
34#endif 42#endif
51# ifndef EV_USE_MONOTONIC 59# ifndef EV_USE_MONOTONIC
52# define EV_USE_MONOTONIC 0 60# define EV_USE_MONOTONIC 0
53# endif 61# endif
54# ifndef EV_USE_REALTIME 62# ifndef EV_USE_REALTIME
55# define EV_USE_REALTIME 0 63# define EV_USE_REALTIME 0
64# endif
65# endif
66
67# ifndef EV_USE_NANOSLEEP
68# if HAVE_NANOSLEEP
69# define EV_USE_NANOSLEEP 1
70# else
71# define EV_USE_NANOSLEEP 0
56# endif 72# endif
57# endif 73# endif
58 74
59# ifndef EV_USE_SELECT 75# ifndef EV_USE_SELECT
60# if HAVE_SELECT && HAVE_SYS_SELECT_H 76# if HAVE_SELECT && HAVE_SYS_SELECT_H
146 162
147#ifndef EV_USE_REALTIME 163#ifndef EV_USE_REALTIME
148# define EV_USE_REALTIME 0 164# define EV_USE_REALTIME 0
149#endif 165#endif
150 166
167#ifndef EV_USE_NANOSLEEP
168# define EV_USE_NANOSLEEP 0
169#endif
170
151#ifndef EV_USE_SELECT 171#ifndef EV_USE_SELECT
152# define EV_USE_SELECT 1 172# define EV_USE_SELECT 1
153#endif 173#endif
154 174
155#ifndef EV_USE_POLL 175#ifndef EV_USE_POLL
205#endif 225#endif
206 226
207#if !EV_STAT_ENABLE 227#if !EV_STAT_ENABLE
208# undef EV_USE_INOTIFY 228# undef EV_USE_INOTIFY
209# define EV_USE_INOTIFY 0 229# define EV_USE_INOTIFY 0
230#endif
231
232#if !EV_USE_NANOSLEEP
233# ifndef _WIN32
234# include <sys/select.h>
235# endif
210#endif 236#endif
211 237
212#if EV_USE_INOTIFY 238#if EV_USE_INOTIFY
213# include <sys/inotify.h> 239# include <sys/inotify.h>
214#endif 240#endif
262 288
263typedef ev_watcher *W; 289typedef ev_watcher *W;
264typedef ev_watcher_list *WL; 290typedef ev_watcher_list *WL;
265typedef ev_watcher_time *WT; 291typedef ev_watcher_time *WT;
266 292
293#if EV_USE_MONOTONIC
294/* sig_atomic_t is used to avoid per-thread variables or locking but still */
295/* giving it a reasonably high chance of working on typical architetcures */
267static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 296static sig_atomic_t have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
297#endif
268 298
269#ifdef _WIN32 299#ifdef _WIN32
270# include "ev_win32.c" 300# include "ev_win32.c"
271#endif 301#endif
272 302
408{ 438{
409 return ev_rt_now; 439 return ev_rt_now;
410} 440}
411#endif 441#endif
412 442
443void
444ev_sleep (ev_tstamp delay)
445{
446 if (delay > 0.)
447 {
448#if EV_USE_NANOSLEEP
449 struct timespec ts;
450
451 ts.tv_sec = (time_t)delay;
452 ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9);
453
454 nanosleep (&ts, 0);
455#elif defined(_WIN32)
456 Sleep (delay * 1e3);
457#else
458 struct timeval tv;
459
460 tv.tv_sec = (time_t)delay;
461 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
462
463 select (0, 0, 0, 0, &tv);
464#endif
465 }
466}
467
468/*****************************************************************************/
469
413int inline_size 470int inline_size
414array_nextsize (int elem, int cur, int cnt) 471array_nextsize (int elem, int cur, int cnt)
415{ 472{
416 int ncur = cur + 1; 473 int ncur = cur + 1;
417 474
543 600
544#if EV_SELECT_IS_WINSOCKET 601#if EV_SELECT_IS_WINSOCKET
545 if (events) 602 if (events)
546 { 603 {
547 unsigned long argp; 604 unsigned long argp;
605 #ifdef EV_FD_TO_WIN32_HANDLE
606 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
607 #else
548 anfd->handle = _get_osfhandle (fd); 608 anfd->handle = _get_osfhandle (fd);
609 #endif
549 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); 610 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
550 } 611 }
551#endif 612#endif
552 613
553 { 614 {
806 867
807#ifndef _WIN32 868#ifndef _WIN32
808 869
809static ev_signal childev; 870static ev_signal childev;
810 871
872#ifndef WIFCONTINUED
873# define WIFCONTINUED(status) 0
874#endif
875
811void inline_speed 876void inline_speed
812child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) 877child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status)
813{ 878{
814 ev_child *w; 879 ev_child *w;
880 int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
815 881
816 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 882 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
883 {
817 if (w->pid == pid || !w->pid) 884 if ((w->pid == pid || !w->pid)
885 && (!traced || (w->flags & 1)))
818 { 886 {
819 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */ 887 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */
820 w->rpid = pid; 888 w->rpid = pid;
821 w->rstatus = status; 889 w->rstatus = status;
822 ev_feed_event (EV_A_ (W)w, EV_CHILD); 890 ev_feed_event (EV_A_ (W)w, EV_CHILD);
823 } 891 }
892 }
824} 893}
825 894
826#ifndef WCONTINUED 895#ifndef WCONTINUED
827# define WCONTINUED 0 896# define WCONTINUED 0
828#endif 897#endif
925} 994}
926 995
927unsigned int 996unsigned int
928ev_embeddable_backends (void) 997ev_embeddable_backends (void)
929{ 998{
930 return EVBACKEND_EPOLL 999 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
931 | EVBACKEND_KQUEUE 1000
932 | EVBACKEND_PORT; 1001 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
1002 /* please fix it and tell me how to detect the fix */
1003 flags &= ~EVBACKEND_EPOLL;
1004
1005 return flags;
933} 1006}
934 1007
935unsigned int 1008unsigned int
936ev_backend (EV_P) 1009ev_backend (EV_P)
937{ 1010{
940 1013
941unsigned int 1014unsigned int
942ev_loop_count (EV_P) 1015ev_loop_count (EV_P)
943{ 1016{
944 return loop_count; 1017 return loop_count;
1018}
1019
1020void
1021ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
1022{
1023 io_blocktime = interval;
1024}
1025
1026void
1027ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1028{
1029 timeout_blocktime = interval;
945} 1030}
946 1031
947static void noinline 1032static void noinline
948loop_init (EV_P_ unsigned int flags) 1033loop_init (EV_P_ unsigned int flags)
949{ 1034{
960 ev_rt_now = ev_time (); 1045 ev_rt_now = ev_time ();
961 mn_now = get_clock (); 1046 mn_now = get_clock ();
962 now_floor = mn_now; 1047 now_floor = mn_now;
963 rtmn_diff = ev_rt_now - mn_now; 1048 rtmn_diff = ev_rt_now - mn_now;
964 1049
1050 io_blocktime = 0.;
1051 timeout_blocktime = 0.;
1052
965 /* pid check not overridable via env */ 1053 /* pid check not overridable via env */
966#ifndef _WIN32 1054#ifndef _WIN32
967 if (flags & EVFLAG_FORKCHECK) 1055 if (flags & EVFLAG_FORKCHECK)
968 curpid = getpid (); 1056 curpid = getpid ();
969#endif 1057#endif
1037 array_free (pending, [i]); 1125 array_free (pending, [i]);
1038#if EV_IDLE_ENABLE 1126#if EV_IDLE_ENABLE
1039 array_free (idle, [i]); 1127 array_free (idle, [i]);
1040#endif 1128#endif
1041 } 1129 }
1130
1131 ev_free (anfds); anfdmax = 0;
1042 1132
1043 /* have to use the microsoft-never-gets-it-right macro */ 1133 /* have to use the microsoft-never-gets-it-right macro */
1044 array_free (fdchange, EMPTY); 1134 array_free (fdchange, EMPTY);
1045 array_free (timer, EMPTY); 1135 array_free (timer, EMPTY);
1046#if EV_PERIODIC_ENABLE 1136#if EV_PERIODIC_ENABLE
1047 array_free (periodic, EMPTY); 1137 array_free (periodic, EMPTY);
1048#endif 1138#endif
1139#if EV_FORK_ENABLE
1140 array_free (fork, EMPTY);
1141#endif
1049 array_free (prepare, EMPTY); 1142 array_free (prepare, EMPTY);
1050 array_free (check, EMPTY); 1143 array_free (check, EMPTY);
1051 1144
1052 backend = 0; 1145 backend = 0;
1053} 1146}
1081 1174
1082 while (pipe (sigpipe)) 1175 while (pipe (sigpipe))
1083 syserr ("(libev) error creating pipe"); 1176 syserr ("(libev) error creating pipe");
1084 1177
1085 siginit (EV_A); 1178 siginit (EV_A);
1179 sigcb (EV_A_ &sigev, EV_READ);
1086 } 1180 }
1087 1181
1088 postfork = 0; 1182 postfork = 0;
1089} 1183}
1090 1184
1112} 1206}
1113 1207
1114void 1208void
1115ev_loop_fork (EV_P) 1209ev_loop_fork (EV_P)
1116{ 1210{
1117 postfork = 1; 1211 postfork = 1; /* must be in line with ev_default_fork */
1118} 1212}
1119 1213
1120#endif 1214#endif
1121 1215
1122#if EV_MULTIPLICITY 1216#if EV_MULTIPLICITY
1186#if EV_MULTIPLICITY 1280#if EV_MULTIPLICITY
1187 struct ev_loop *loop = ev_default_loop_ptr; 1281 struct ev_loop *loop = ev_default_loop_ptr;
1188#endif 1282#endif
1189 1283
1190 if (backend) 1284 if (backend)
1191 postfork = 1; 1285 postfork = 1; /* must be in line with ev_loop_fork */
1192} 1286}
1193 1287
1194/*****************************************************************************/ 1288/*****************************************************************************/
1195 1289
1196void 1290void
1451 /* update fd-related kernel structures */ 1545 /* update fd-related kernel structures */
1452 fd_reify (EV_A); 1546 fd_reify (EV_A);
1453 1547
1454 /* calculate blocking time */ 1548 /* calculate blocking time */
1455 { 1549 {
1456 ev_tstamp block; 1550 ev_tstamp waittime = 0.;
1551 ev_tstamp sleeptime = 0.;
1457 1552
1458 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt)) 1553 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
1459 block = 0.; /* do not block at all */
1460 else
1461 { 1554 {
1462 /* update time to cancel out callback processing overhead */ 1555 /* update time to cancel out callback processing overhead */
1463 time_update (EV_A_ 1e100); 1556 time_update (EV_A_ 1e100);
1464 1557
1465 block = MAX_BLOCKTIME; 1558 waittime = MAX_BLOCKTIME;
1466 1559
1467 if (timercnt) 1560 if (timercnt)
1468 { 1561 {
1469 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1562 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1470 if (block > to) block = to; 1563 if (waittime > to) waittime = to;
1471 } 1564 }
1472 1565
1473#if EV_PERIODIC_ENABLE 1566#if EV_PERIODIC_ENABLE
1474 if (periodiccnt) 1567 if (periodiccnt)
1475 { 1568 {
1476 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1569 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
1477 if (block > to) block = to; 1570 if (waittime > to) waittime = to;
1478 } 1571 }
1479#endif 1572#endif
1480 1573
1481 if (expect_false (block < 0.)) block = 0.; 1574 if (expect_false (waittime < timeout_blocktime))
1575 waittime = timeout_blocktime;
1576
1577 sleeptime = waittime - backend_fudge;
1578
1579 if (expect_true (sleeptime > io_blocktime))
1580 sleeptime = io_blocktime;
1581
1582 if (sleeptime)
1583 {
1584 ev_sleep (sleeptime);
1585 waittime -= sleeptime;
1586 }
1482 } 1587 }
1483 1588
1484 ++loop_count; 1589 ++loop_count;
1485 backend_poll (EV_A_ block); 1590 backend_poll (EV_A_ waittime);
1486 1591
1487 /* update ev_rt_now, do magic */ 1592 /* update ev_rt_now, do magic */
1488 time_update (EV_A_ block); 1593 time_update (EV_A_ waittime + sleeptime);
1489 } 1594 }
1490 1595
1491 /* queue pending timers and reschedule them */ 1596 /* queue pending timers and reschedule them */
1492 timers_reify (EV_A); /* relative timers called last */ 1597 timers_reify (EV_A); /* relative timers called last */
1493#if EV_PERIODIC_ENABLE 1598#if EV_PERIODIC_ENABLE
2177 2282
2178#if EV_EMBED_ENABLE 2283#if EV_EMBED_ENABLE
2179void noinline 2284void noinline
2180ev_embed_sweep (EV_P_ ev_embed *w) 2285ev_embed_sweep (EV_P_ ev_embed *w)
2181{ 2286{
2182 ev_loop (w->loop, EVLOOP_NONBLOCK); 2287 ev_loop (w->other, EVLOOP_NONBLOCK);
2183} 2288}
2184 2289
2185static void 2290static void
2186embed_cb (EV_P_ ev_io *io, int revents) 2291embed_io_cb (EV_P_ ev_io *io, int revents)
2187{ 2292{
2188 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); 2293 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
2189 2294
2190 if (ev_cb (w)) 2295 if (ev_cb (w))
2191 ev_feed_event (EV_A_ (W)w, EV_EMBED); 2296 ev_feed_event (EV_A_ (W)w, EV_EMBED);
2192 else 2297 else
2193 ev_embed_sweep (loop, w); 2298 ev_loop (w->other, EVLOOP_NONBLOCK);
2194} 2299}
2300
2301static void
2302embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents)
2303{
2304 ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare));
2305
2306 {
2307 struct ev_loop *loop = w->other;
2308
2309 while (fdchangecnt)
2310 {
2311 fd_reify (EV_A);
2312 ev_loop (EV_A_ EVLOOP_NONBLOCK);
2313 }
2314 }
2315}
2316
2317#if 0
2318static void
2319embed_idle_cb (EV_P_ ev_idle *idle, int revents)
2320{
2321 ev_idle_stop (EV_A_ idle);
2322}
2323#endif
2195 2324
2196void 2325void
2197ev_embed_start (EV_P_ ev_embed *w) 2326ev_embed_start (EV_P_ ev_embed *w)
2198{ 2327{
2199 if (expect_false (ev_is_active (w))) 2328 if (expect_false (ev_is_active (w)))
2200 return; 2329 return;
2201 2330
2202 { 2331 {
2203 struct ev_loop *loop = w->loop; 2332 struct ev_loop *loop = w->other;
2204 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); 2333 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
2205 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); 2334 ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ);
2206 } 2335 }
2207 2336
2208 ev_set_priority (&w->io, ev_priority (w)); 2337 ev_set_priority (&w->io, ev_priority (w));
2209 ev_io_start (EV_A_ &w->io); 2338 ev_io_start (EV_A_ &w->io);
2210 2339
2340 ev_prepare_init (&w->prepare, embed_prepare_cb);
2341 ev_set_priority (&w->prepare, EV_MINPRI);
2342 ev_prepare_start (EV_A_ &w->prepare);
2343
2344 /*ev_idle_init (&w->idle, e,bed_idle_cb);*/
2345
2211 ev_start (EV_A_ (W)w, 1); 2346 ev_start (EV_A_ (W)w, 1);
2212} 2347}
2213 2348
2214void 2349void
2215ev_embed_stop (EV_P_ ev_embed *w) 2350ev_embed_stop (EV_P_ ev_embed *w)
2217 clear_pending (EV_A_ (W)w); 2352 clear_pending (EV_A_ (W)w);
2218 if (expect_false (!ev_is_active (w))) 2353 if (expect_false (!ev_is_active (w)))
2219 return; 2354 return;
2220 2355
2221 ev_io_stop (EV_A_ &w->io); 2356 ev_io_stop (EV_A_ &w->io);
2357 ev_prepare_stop (EV_A_ &w->prepare);
2222 2358
2223 ev_stop (EV_A_ (W)w); 2359 ev_stop (EV_A_ (W)w);
2224} 2360}
2225#endif 2361#endif
2226 2362
2315 ev_timer_set (&once->to, timeout, 0.); 2451 ev_timer_set (&once->to, timeout, 0.);
2316 ev_timer_start (EV_A_ &once->to); 2452 ev_timer_start (EV_A_ &once->to);
2317 } 2453 }
2318} 2454}
2319 2455
2456#if EV_MULTIPLICITY
2457 #include "ev_wrap.h"
2458#endif
2459
2320#ifdef __cplusplus 2460#ifdef __cplusplus
2321} 2461}
2322#endif 2462#endif
2323 2463

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines