uloop: allow specifying a timeout for uloop_run()
authorFelix Fietkau <nbd@nbd.name>
Thu, 1 Jun 2017 09:24:37 +0000 (11:24 +0200)
committerFelix Fietkau <nbd@nbd.name>
Thu, 1 Jun 2017 09:24:44 +0000 (11:24 +0200)
This can be useful for cleanup with pending timers, or for hooking into
existing code that does not use uloop

Signed-off-by: Felix Fietkau <nbd@nbd.name>
uloop.c
uloop.h

diff --git a/uloop.c b/uloop.c
index 26fef32bd67b1818617e3b88ecaf34292badf0e5..f5032418ed6697bf5035cbe3cdedf1c3ff85880b 100644 (file)
--- a/uloop.c
+++ b/uloop.c
@@ -520,8 +520,9 @@ bool uloop_cancelling(void)
        return uloop_run_depth > 0 && uloop_cancelled;
 }
 
-int uloop_run(void)
+int uloop_run_timeout(int timeout)
 {
+       int next_time = 0;
        struct timeval tv;
 
        /*
@@ -545,7 +546,11 @@ int uloop_run(void)
                        break;
 
                uloop_gettime(&tv);
-               uloop_run_events(uloop_get_next_timeout(&tv));
+
+               next_time = uloop_get_next_timeout(&tv);
+               if (timeout > 0 && next_time < timeout)
+                       timeout = next_time;
+               uloop_run_events(timeout);
        }
 
        if (!--uloop_run_depth)
diff --git a/uloop.h b/uloop.h
index 5ab9a5f1196f8525111baeca8e5880edbc32b31f..36084f520e2ac8570967f1c98474874f9fba0af9 100644 (file)
--- a/uloop.h
+++ b/uloop.h
@@ -105,7 +105,11 @@ static inline void uloop_end(void)
 }
 
 int uloop_init(void);
-int uloop_run(void);
+int uloop_run_timeout(int timeout);
+static inline int uloop_run(void)
+{
+       return uloop_run_timeout(-1);
+}
 void uloop_done(void);
 
 #endif