From 2a66973098dede36d4d4492d2ae82e6ec84981cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Tue, 31 Dec 2013 03:00:58 +0100 Subject: [PATCH] Refactor timer execution into its own function --- src/iomux_impl.c | 49 ++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/iomux_impl.c b/src/iomux_impl.c index 2902e92..d4b14ed 100644 --- a/src/iomux_impl.c +++ b/src/iomux_impl.c @@ -3,6 +3,7 @@ #include #include #include "ucore/clock.h" +#include "ucore/backtrace.h" #include "iomux_impl.h" @@ -65,42 +66,50 @@ static inline void future_to_interval(const struct timeval *future, } } +static int iomux_run_timers(struct IOMux *mux, struct timeval *timeout) +{ + struct timeval first_timer; + + if (uc_timers_first(&mux->timers, &first_timer) == 0) { + struct timeval now; + mux->uclock->now(mux->uclock, &now); + future_to_interval(&first_timer, &now, timeout); + /* TRACEF("now %d %d future %d %d interval %d %d\n", mux->now.tv_sec, mux->now.tv_usec, + first_timer.tv_sec, first_timer.tv_usec, + timeout.tv_sec, timeout.tv_usec); + */ + assert(timeout->tv_sec >= 0); + assert(timeout->tv_usec >= 0); + + return 1; + } + + return 9; +} + int iomux_run(struct IOMux *mux) { for (;;) { int rc; - struct timeval first_timer; struct timeval timeout; - struct timeval *timeoutp;; - int has_timers = 0; + struct timeval *timeoutp; - - if (uc_timers_first(&mux->timers, &first_timer) == 0) { - struct timeval now; - mux->uclock->now(mux->uclock, &now); - future_to_interval(&first_timer, &now, &timeout); + if (iomux_run_timers(mux, &timeout)) { timeoutp = &timeout; - /* TRACEF("now %d %d future %d %d interval %d %d\n", mux->now.tv_sec, mux->now.tv_usec, - first_timer.tv_sec, first_timer.tv_usec, - timeout.tv_sec, timeout.tv_usec); - */ - - assert(timeout.tv_sec >= 0); - assert(timeout.tv_usec >= 0); - - has_timers = 1; } else { - timeoutp = NULL; //no timeout + timeoutp = NULL; } + rc = mux->run_impl(mux, timeoutp); if (rc < 0) return rc; - if (rc == 0 && !has_timers) //no more events, ever + if (rc == 0 && timeoutp == NULL) //no more fd or timer events, ever return 0; } - return 0; + UC_NOT_REACED(); + return -44; } int iomux_timers_run(struct IOMux *mux)