From 368fd2645878edadc72c60948d1f19c6769751d6 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 1 Jun 2017 11:24:37 +0200 Subject: [PATCH] uloop: allow specifying a timeout for uloop_run() 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 --- uloop.c | 9 +++++++-- uloop.h | 6 +++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/uloop.c b/uloop.c index 26fef32..f503241 100644 --- 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 5ab9a5f..36084f5 100644 --- 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 -- 2.25.1