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

Comparing libev/ev.c (file contents):
Revision 1.374 by root, Sat Feb 26 15:21:01 2011 UTC vs.
Revision 1.378 by root, Mon Jun 13 09:52:36 2011 UTC

870 select (0, 0, 0, 0, &tv); 870 select (0, 0, 0, 0, &tv);
871#endif 871#endif
872 } 872 }
873} 873}
874 874
875inline_speed int
876ev_timeout_to_ms (ev_tstamp timeout)
877{
878 int ms = timeout * 1000. + .999999;
879
880 return expect_true (ms) ? ms : timeout < 1e-6 ? 0 : 1;
881}
882
883/*****************************************************************************/ 875/*****************************************************************************/
884 876
885#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */ 877#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
886 878
887/* find a suitable new size for the given array, */ 879/* find a suitable new size for the given array, */
1376inline_size void 1368inline_size void
1377evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1369evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1378{ 1370{
1379 if (!*flag) 1371 if (!*flag)
1380 { 1372 {
1381 int old_errno = errno; /* save errno because write might clobber it */
1382 char dummy;
1383
1384 *flag = 1; 1373 *flag = 1;
1385 1374
1375 pipe_write_skipped = 1;
1376
1377 if (pipe_write_wanted)
1378 {
1379 int old_errno = errno; /* save errno because write will clobber it */
1380 char dummy;
1381
1382 pipe_write_skipped = 0;
1383
1386#if EV_USE_EVENTFD 1384#if EV_USE_EVENTFD
1387 if (evfd >= 0) 1385 if (evfd >= 0)
1388 { 1386 {
1389 uint64_t counter = 1; 1387 uint64_t counter = 1;
1390 write (evfd, &counter, sizeof (uint64_t)); 1388 write (evfd, &counter, sizeof (uint64_t));
1389 }
1390 else
1391#endif
1392 {
1393 /* win32 people keep sending patches that change this write() to send() */
1394 /* and then run away. but send() is wrong, it wants a socket handle on win32 */
1395 /* so when you think this write should be a send instead, please find out */
1396 /* where your send() is from - it's definitely not the microsoft send, and */
1397 /* tell me. thank you. */
1398 write (evpipe [1], &dummy, 1);
1399 }
1400
1401 errno = old_errno;
1391 } 1402 }
1392 else
1393#endif
1394 /* win32 people keep sending patches that change this write() to send() */
1395 /* and then run away. but send() is wrong, it wants a socket handle on win32 */
1396 /* so when you think this write should be a send instead, please find out */
1397 /* where your send() is from - it's definitely not the microsoft send, and */
1398 /* tell me. thank you. */
1399 write (evpipe [1], &dummy, 1);
1400
1401 errno = old_errno;
1402 } 1403 }
1403} 1404}
1404 1405
1405/* called whenever the libev signal pipe */ 1406/* called whenever the libev signal pipe */
1406/* got some events (signal, async) */ 1407/* got some events (signal, async) */
1407static void 1408static void
1408pipecb (EV_P_ ev_io *iow, int revents) 1409pipecb (EV_P_ ev_io *iow, int revents)
1409{ 1410{
1410 int i; 1411 int i;
1411 1412
1413 if (revents & EV_READ)
1414 {
1412#if EV_USE_EVENTFD 1415#if EV_USE_EVENTFD
1413 if (evfd >= 0) 1416 if (evfd >= 0)
1414 { 1417 {
1415 uint64_t counter; 1418 uint64_t counter;
1416 read (evfd, &counter, sizeof (uint64_t)); 1419 read (evfd, &counter, sizeof (uint64_t));
1417 } 1420 }
1418 else 1421 else
1419#endif 1422#endif
1420 { 1423 {
1421 char dummy; 1424 char dummy;
1422 /* see discussion in evpipe_write when you think this read should be recv in win32 */ 1425 /* see discussion in evpipe_write when you think this read should be recv in win32 */
1423 read (evpipe [0], &dummy, 1); 1426 read (evpipe [0], &dummy, 1);
1427 }
1424 } 1428 }
1429
1430 pipe_write_skipped = 0;
1425 1431
1426#if EV_SIGNAL_ENABLE 1432#if EV_SIGNAL_ENABLE
1427 if (sig_pending) 1433 if (sig_pending)
1428 { 1434 {
1429 sig_pending = 0; 1435 sig_pending = 0;
1458 EV_P = signals [signum - 1].loop; 1464 EV_P = signals [signum - 1].loop;
1459 1465
1460 if (!EV_A) 1466 if (!EV_A)
1461 return; 1467 return;
1462#endif 1468#endif
1469
1470 evpipe_init (EV_A);
1463 1471
1464 signals [signum - 1].pending = 1; 1472 signals [signum - 1].pending = 1;
1465 evpipe_write (EV_A_ &sig_pending); 1473 evpipe_write (EV_A_ &sig_pending);
1466} 1474}
1467 1475
1765 if (!(flags & EVFLAG_NOENV) 1773 if (!(flags & EVFLAG_NOENV)
1766 && !enable_secure () 1774 && !enable_secure ()
1767 && getenv ("LIBEV_FLAGS")) 1775 && getenv ("LIBEV_FLAGS"))
1768 flags = atoi (getenv ("LIBEV_FLAGS")); 1776 flags = atoi (getenv ("LIBEV_FLAGS"));
1769 1777
1770 ev_rt_now = ev_time (); 1778 ev_rt_now = ev_time ();
1771 mn_now = get_clock (); 1779 mn_now = get_clock ();
1772 now_floor = mn_now; 1780 now_floor = mn_now;
1773 rtmn_diff = ev_rt_now - mn_now; 1781 rtmn_diff = ev_rt_now - mn_now;
1774#if EV_FEATURE_API 1782#if EV_FEATURE_API
1775 invoke_cb = ev_invoke_pending; 1783 invoke_cb = ev_invoke_pending;
1776#endif 1784#endif
1777 1785
1778 io_blocktime = 0.; 1786 io_blocktime = 0.;
1779 timeout_blocktime = 0.; 1787 timeout_blocktime = 0.;
1780 backend = 0; 1788 backend = 0;
1781 backend_fd = -1; 1789 backend_fd = -1;
1782 sig_pending = 0; 1790 sig_pending = 0;
1783#if EV_ASYNC_ENABLE 1791#if EV_ASYNC_ENABLE
1784 async_pending = 0; 1792 async_pending = 0;
1785#endif 1793#endif
1794 pipe_write_skipped = 0;
1795 pipe_write_wanted = 0;
1786#if EV_USE_INOTIFY 1796#if EV_USE_INOTIFY
1787 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; 1797 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
1788#endif 1798#endif
1789#if EV_USE_SIGNALFD 1799#if EV_USE_SIGNALFD
1790 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1; 1800 sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
1791#endif 1801#endif
1792 1802
1793 if (!(flags & EVBACKEND_MASK)) 1803 if (!(flags & EVBACKEND_MASK))
1794 flags |= ev_recommended_backends (); 1804 flags |= ev_recommended_backends ();
1795 1805
1960 infy_fork (EV_A); 1970 infy_fork (EV_A);
1961#endif 1971#endif
1962 1972
1963 if (ev_is_active (&pipe_w)) 1973 if (ev_is_active (&pipe_w))
1964 { 1974 {
1965 /* this "locks" the handlers against writing to the pipe */ 1975 /* pipe_write_wanted must be false now, so modifying fd vars should be safe */
1966 /* while we modify the fd vars */
1967 sig_pending = 1;
1968#if EV_ASYNC_ENABLE
1969 async_pending = 1;
1970#endif
1971 1976
1972 ev_ref (EV_A); 1977 ev_ref (EV_A);
1973 ev_io_stop (EV_A_ &pipe_w); 1978 ev_io_stop (EV_A_ &pipe_w);
1974 1979
1975#if EV_USE_EVENTFD 1980#if EV_USE_EVENTFD
2507 ev_tstamp prev_mn_now = mn_now; 2512 ev_tstamp prev_mn_now = mn_now;
2508 2513
2509 /* update time to cancel out callback processing overhead */ 2514 /* update time to cancel out callback processing overhead */
2510 time_update (EV_A_ 1e100); 2515 time_update (EV_A_ 1e100);
2511 2516
2517 /* from now on, we want a pipe-wake-up */
2518 pipe_write_wanted = 1;
2519
2512 if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt))) 2520 if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped)))
2513 { 2521 {
2514 waittime = MAX_BLOCKTIME; 2522 waittime = MAX_BLOCKTIME;
2515 2523
2516 if (timercnt) 2524 if (timercnt)
2517 { 2525 {
2518 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; 2526 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now;
2519 if (waittime > to) waittime = to; 2527 if (waittime > to) waittime = to;
2520 } 2528 }
2521 2529
2522#if EV_PERIODIC_ENABLE 2530#if EV_PERIODIC_ENABLE
2523 if (periodiccnt) 2531 if (periodiccnt)
2524 { 2532 {
2525 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge; 2533 ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now;
2526 if (waittime > to) waittime = to; 2534 if (waittime > to) waittime = to;
2527 } 2535 }
2528#endif 2536#endif
2529 2537
2530 /* don't let timeouts decrease the waittime below timeout_blocktime */ 2538 /* don't let timeouts decrease the waittime below timeout_blocktime */
2531 if (expect_false (waittime < timeout_blocktime)) 2539 if (expect_false (waittime < timeout_blocktime))
2532 waittime = timeout_blocktime; 2540 waittime = timeout_blocktime;
2541
2542 /* at this point, we NEED to wait, so we have to ensure */
2543 /* to pass a minimum nonzero value to the backend */
2544 if (expect_false (waittime < backend_mintime))
2545 waittime = backend_mintime;
2533 2546
2534 /* extra check because io_blocktime is commonly 0 */ 2547 /* extra check because io_blocktime is commonly 0 */
2535 if (expect_false (io_blocktime)) 2548 if (expect_false (io_blocktime))
2536 { 2549 {
2537 sleeptime = io_blocktime - (mn_now - prev_mn_now); 2550 sleeptime = io_blocktime - (mn_now - prev_mn_now);
2538 2551
2539 if (sleeptime > waittime - backend_fudge) 2552 if (sleeptime > waittime - backend_mintime)
2540 sleeptime = waittime - backend_fudge; 2553 sleeptime = waittime - backend_mintime;
2541 2554
2542 if (expect_true (sleeptime > 0.)) 2555 if (expect_true (sleeptime > 0.))
2543 { 2556 {
2544 ev_sleep (sleeptime); 2557 ev_sleep (sleeptime);
2545 waittime -= sleeptime; 2558 waittime -= sleeptime;
2551 ++loop_count; 2564 ++loop_count;
2552#endif 2565#endif
2553 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ 2566 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
2554 backend_poll (EV_A_ waittime); 2567 backend_poll (EV_A_ waittime);
2555 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ 2568 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
2569
2570 pipe_write_wanted = 0;
2571
2572 if (pipe_write_skipped)
2573 {
2574 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
2575 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
2576 }
2577
2556 2578
2557 /* update ev_rt_now, do magic */ 2579 /* update ev_rt_now, do magic */
2558 time_update (EV_A_ waittime + sleeptime); 2580 time_update (EV_A_ waittime + sleeptime);
2559 } 2581 }
2560 2582

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines