]> git.llucax.com Git - software/libev.git/blobdiff - ev.c
*** empty log message ***
[software/libev.git] / ev.c
diff --git a/ev.c b/ev.c
index 3f9fe2bdd5c7f055c5ebe850f9a7ed700d6d1b03..14213df8f039858fdcd18a9e79d0ff779d127b70 100644 (file)
--- a/ev.c
+++ b/ev.c
@@ -56,6 +56,14 @@ extern "C" {
 #  endif
 # endif
 
 #  endif
 # endif
 
+# ifndef EV_USE_NANOSLEEP
+#  if HAVE_NANOSLEEP
+#   define EV_USE_NANOSLEEP 1
+#  else
+#   define EV_USE_NANOSLEEP 0
+#  endif
+# endif
+
 # ifndef EV_USE_SELECT
 #  if HAVE_SELECT && HAVE_SYS_SELECT_H
 #   define EV_USE_SELECT 1
 # ifndef EV_USE_SELECT
 #  if HAVE_SELECT && HAVE_SYS_SELECT_H
 #   define EV_USE_SELECT 1
@@ -148,6 +156,10 @@ extern "C" {
 # define EV_USE_REALTIME 0
 #endif
 
 # define EV_USE_REALTIME 0
 #endif
 
+#ifndef EV_USE_NANOSLEEP
+# define EV_USE_NANOSLEEP 0
+#endif
+
 #ifndef EV_USE_SELECT
 # define EV_USE_SELECT 1
 #endif
 #ifndef EV_USE_SELECT
 # define EV_USE_SELECT 1
 #endif
@@ -209,6 +221,12 @@ extern "C" {
 # define EV_USE_INOTIFY 0
 #endif
 
 # define EV_USE_INOTIFY 0
 #endif
 
+#if !EV_USE_NANOSLEEP
+# ifndef _WIN32
+#  include <sys/select.h>
+# endif
+#endif
+
 #if EV_USE_INOTIFY
 # include <sys/inotify.h>
 #endif
 #if EV_USE_INOTIFY
 # include <sys/inotify.h>
 #endif
@@ -410,6 +428,33 @@ ev_now (EV_P)
 }
 #endif
 
 }
 #endif
 
+void
+ev_sleep (ev_tstamp delay)
+{
+  if (delay > 0.)
+    {
+#if EV_USE_NANOSLEEP
+      struct timespec ts;
+
+      ts.tv_sec  = (time_t)delay;
+      ts.tv_nsec = (long)((delay - (ev_tstamp)(ts.tv_sec)) * 1e9);
+
+      nanosleep (&ts, 0);
+#elif defined(_WIN32)
+      Sleep (delay * 1e3);
+#else
+      struct timeval tv;
+
+      tv.tv_sec  = (time_t)delay;
+      tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
+
+      select (0, 0, 0, 0, &tv);
+#endif
+    }
+}
+
+/*****************************************************************************/
+
 int inline_size
 array_nextsize (int elem, int cur, int cnt)
 {
 int inline_size
 array_nextsize (int elem, int cur, int cnt)
 {
@@ -944,6 +989,18 @@ ev_loop_count (EV_P)
   return loop_count;
 }
 
   return loop_count;
 }
 
+void
+ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
+{
+  io_blocktime = interval;
+}
+
+void
+ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
+{
+  timeout_blocktime = interval;
+}
+
 static void noinline
 loop_init (EV_P_ unsigned int flags)
 {
 static void noinline
 loop_init (EV_P_ unsigned int flags)
 {
@@ -962,6 +1019,9 @@ loop_init (EV_P_ unsigned int flags)
       now_floor = mn_now;
       rtmn_diff = ev_rt_now - mn_now;
 
       now_floor = mn_now;
       rtmn_diff = ev_rt_now - mn_now;
 
+      io_blocktime      = 0.;
+      timeout_blocktime = 0.;
+
       /* pid check not overridable via env */
 #ifndef _WIN32
       if (flags & EVFLAG_FORKCHECK)
       /* pid check not overridable via env */
 #ifndef _WIN32
       if (flags & EVFLAG_FORKCHECK)
@@ -1458,39 +1518,50 @@ ev_loop (EV_P_ int flags)
 
       /* calculate blocking time */
       {
 
       /* calculate blocking time */
       {
-        ev_tstamp block;
+        ev_tstamp waittime  = 0.;
+        ev_tstamp sleeptime = 0.;
 
 
-        if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
-          block = 0.; /* do not block at all */
-        else
+        if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt)))
           {
             /* update time to cancel out callback processing overhead */
             time_update (EV_A_ 1e100);
 
           {
             /* update time to cancel out callback processing overhead */
             time_update (EV_A_ 1e100);
 
-            block = MAX_BLOCKTIME;
+            waittime = MAX_BLOCKTIME;
 
             if (timercnt)
               {
                 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
 
             if (timercnt)
               {
                 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
-                if (block > to) block = to;
+                if (waittime > to) waittime = to;
               }
 
 #if EV_PERIODIC_ENABLE
             if (periodiccnt)
               {
                 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
               }
 
 #if EV_PERIODIC_ENABLE
             if (periodiccnt)
               {
                 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
-                if (block > to) block = to;
+                if (waittime > to) waittime = to;
               }
 #endif
 
               }
 #endif
 
-            if (expect_false (block < 0.)) block = 0.;
+            if (expect_false (waittime < timeout_blocktime))
+              waittime = timeout_blocktime;
+
+            sleeptime = waittime - backend_fudge;
+
+            if (expect_true (sleeptime > io_blocktime))
+              sleeptime = io_blocktime;
+
+            if (sleeptime)
+              {
+                ev_sleep (sleeptime);
+                waittime -= sleeptime;
+              }
           }
 
         ++loop_count;
           }
 
         ++loop_count;
-        backend_poll (EV_A_ block);
+        backend_poll (EV_A_ waittime);
 
         /* update ev_rt_now, do magic */
 
         /* update ev_rt_now, do magic */
-        time_update (EV_A_ block);
+        time_update (EV_A_ waittime + sleeptime);
       }
 
       /* queue pending timers and reschedule them */
       }
 
       /* queue pending timers and reschedule them */