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

Comparing libev/ev.c (file contents):
Revision 1.192 by root, Fri Dec 21 07:55:29 2007 UTC vs.
Revision 1.193 by root, Sat Dec 22 05:47:58 2007 UTC

51# ifndef EV_USE_MONOTONIC 51# ifndef EV_USE_MONOTONIC
52# define EV_USE_MONOTONIC 0 52# define EV_USE_MONOTONIC 0
53# endif 53# endif
54# ifndef EV_USE_REALTIME 54# ifndef EV_USE_REALTIME
55# define EV_USE_REALTIME 0 55# define EV_USE_REALTIME 0
56# endif
57# endif
58
59# ifndef EV_USE_NANOSLEEP
60# if HAVE_NANOSLEEP
61# define EV_USE_NANOSLEEP 1
62# else
63# define EV_USE_NANOSLEEP 0
56# endif 64# endif
57# endif 65# endif
58 66
59# ifndef EV_USE_SELECT 67# ifndef EV_USE_SELECT
60# if HAVE_SELECT && HAVE_SYS_SELECT_H 68# if HAVE_SELECT && HAVE_SYS_SELECT_H
146 154
147#ifndef EV_USE_REALTIME 155#ifndef EV_USE_REALTIME
148# define EV_USE_REALTIME 0 156# define EV_USE_REALTIME 0
149#endif 157#endif
150 158
159#ifndef EV_USE_NANOSLEEP
160# define EV_USE_NANOSLEEP 0
161#endif
162
151#ifndef EV_USE_SELECT 163#ifndef EV_USE_SELECT
152# define EV_USE_SELECT 1 164# define EV_USE_SELECT 1
153#endif 165#endif
154 166
155#ifndef EV_USE_POLL 167#ifndef EV_USE_POLL
205#endif 217#endif
206 218
207#if !EV_STAT_ENABLE 219#if !EV_STAT_ENABLE
208# undef EV_USE_INOTIFY 220# undef EV_USE_INOTIFY
209# define EV_USE_INOTIFY 0 221# define EV_USE_INOTIFY 0
222#endif
223
224#if !EV_USE_NANOSLEEP
225# ifndef _WIN32
226# include <sys/select.h>
227# endif
210#endif 228#endif
211 229
212#if EV_USE_INOTIFY 230#if EV_USE_INOTIFY
213# include <sys/inotify.h> 231# include <sys/inotify.h>
214#endif 232#endif
408{ 426{
409 return ev_rt_now; 427 return ev_rt_now;
410} 428}
411#endif 429#endif
412 430
431void
432ev_sleep (ev_tstamp delay)
433{
434 if (delay > 0.)
435 {
436#if EV_USE_NANOSLEEP
437 struct timespec ts;
438
439 ts.tv_sec = (time_t)delay;
440 ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9);
441
442 nanosleep (&ts, 0);
443#elif defined(_WIN32)
444 Sleep (delay * 1e3);
445#else
446 struct timeval tv;
447
448 tv.tv_sec = (time_t)delay;
449 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
450
451 select (0, 0, 0, 0, &tv);
452#endif
453 }
454}
455
456/*****************************************************************************/
457
413int inline_size 458int inline_size
414array_nextsize (int elem, int cur, int cnt) 459array_nextsize (int elem, int cur, int cnt)
415{ 460{
416 int ncur = cur + 1; 461 int ncur = cur + 1;
417 462
942ev_loop_count (EV_P) 987ev_loop_count (EV_P)
943{ 988{
944 return loop_count; 989 return loop_count;
945} 990}
946 991
992void
993ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
994{
995 io_blocktime = interval;
996}
997
998void
999ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1000{
1001 timeout_blocktime = interval;
1002}
1003
947static void noinline 1004static void noinline
948loop_init (EV_P_ unsigned int flags) 1005loop_init (EV_P_ unsigned int flags)
949{ 1006{
950 if (!backend) 1007 if (!backend)
951 { 1008 {
959 1016
960 ev_rt_now = ev_time (); 1017 ev_rt_now = ev_time ();
961 mn_now = get_clock (); 1018 mn_now = get_clock ();
962 now_floor = mn_now; 1019 now_floor = mn_now;
963 rtmn_diff = ev_rt_now - mn_now; 1020 rtmn_diff = ev_rt_now - mn_now;
1021
1022 io_blocktime = 0.;
1023 timeout_blocktime = 0.;
964 1024
965 /* pid check not overridable via env */ 1025 /* pid check not overridable via env */
966#ifndef _WIN32 1026#ifndef _WIN32
967 if (flags & EVFLAG_FORKCHECK) 1027 if (flags & EVFLAG_FORKCHECK)
968 curpid = getpid (); 1028 curpid = getpid ();
1456 /* update fd-related kernel structures */ 1516 /* update fd-related kernel structures */
1457 fd_reify (EV_A); 1517 fd_reify (EV_A);
1458 1518
1459 /* calculate blocking time */ 1519 /* calculate blocking time */
1460 { 1520 {
1461 ev_tstamp block; 1521 ev_tstamp waittime = 0.;
1522 ev_tstamp sleeptime = 0.;
1462 1523
1463 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt)) 1524 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
1464 block = 0.; /* do not block at all */
1465 else
1466 { 1525 {
1467 /* update time to cancel out callback processing overhead */ 1526 /* update time to cancel out callback processing overhead */
1468 time_update (EV_A_ 1e100); 1527 time_update (EV_A_ 1e100);
1469 1528
1470 block = MAX_BLOCKTIME; 1529 waittime = MAX_BLOCKTIME;
1471 1530
1472 if (timercnt) 1531 if (timercnt)
1473 { 1532 {
1474 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1533 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1475 if (block > to) block = to; 1534 if (waittime > to) waittime = to;
1476 } 1535 }
1477 1536
1478#if EV_PERIODIC_ENABLE 1537#if EV_PERIODIC_ENABLE
1479 if (periodiccnt) 1538 if (periodiccnt)
1480 { 1539 {
1481 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1540 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
1482 if (block > to) block = to; 1541 if (waittime > to) waittime = to;
1483 } 1542 }
1484#endif 1543#endif
1485 1544
1486 if (expect_false (block < 0.)) block = 0.; 1545 if (expect_false (waittime < timeout_blocktime))
1546 waittime = timeout_blocktime;
1547
1548 sleeptime = waittime - backend_fudge;
1549
1550 if (expect_true (sleeptime > io_blocktime))
1551 sleeptime = io_blocktime;
1552
1553 if (sleeptime)
1554 {
1555 ev_sleep (sleeptime);
1556 waittime -= sleeptime;
1557 }
1487 } 1558 }
1488 1559
1489 ++loop_count; 1560 ++loop_count;
1490 backend_poll (EV_A_ block); 1561 backend_poll (EV_A_ waittime);
1491 1562
1492 /* update ev_rt_now, do magic */ 1563 /* update ev_rt_now, do magic */
1493 time_update (EV_A_ block); 1564 time_update (EV_A_ waittime + sleeptime);
1494 } 1565 }
1495 1566
1496 /* queue pending timers and reschedule them */ 1567 /* queue pending timers and reschedule them */
1497 timers_reify (EV_A); /* relative timers called last */ 1568 timers_reify (EV_A); /* relative timers called last */
1498#if EV_PERIODIC_ENABLE 1569#if EV_PERIODIC_ENABLE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines