Refactor timer execution into its own function
This commit is contained in:
+29
-20
@@ -3,6 +3,7 @@
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#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)
|
||||
|
||||
Reference in New Issue
Block a user