Refactor timer execution into its own function

This commit is contained in:
Nils O. Selåsdal
2013-12-31 03:00:58 +01:00
parent ca2b2be2ee
commit 2a66973098
+29 -20
View File
@@ -3,6 +3,7 @@
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include "ucore/clock.h" #include "ucore/clock.h"
#include "ucore/backtrace.h"
#include "iomux_impl.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) int iomux_run(struct IOMux *mux)
{ {
for (;;) { for (;;) {
int rc; int rc;
struct timeval first_timer;
struct timeval timeout; struct timeval timeout;
struct timeval *timeoutp;; struct timeval *timeoutp;
int has_timers = 0;
if (iomux_run_timers(mux, &timeout)) {
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);
timeoutp = &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 { } else {
timeoutp = NULL; //no timeout timeoutp = NULL;
} }
rc = mux->run_impl(mux, timeoutp); rc = mux->run_impl(mux, timeoutp);
if (rc < 0) if (rc < 0)
return rc; 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;
} }
return 0; UC_NOT_REACED();
return -44;
} }
int iomux_timers_run(struct IOMux *mux) int iomux_timers_run(struct IOMux *mux)