Refactor timer execution into its own function
This commit is contained in:
+26
-17
@@ -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,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int iomux_run(struct IOMux *mux)
|
static int iomux_run_timers(struct IOMux *mux, struct timeval *timeout)
|
||||||
{
|
{
|
||||||
for (;;) {
|
|
||||||
int rc;
|
|
||||||
struct timeval first_timer;
|
struct timeval first_timer;
|
||||||
struct timeval timeout;
|
|
||||||
struct timeval *timeoutp;;
|
|
||||||
int has_timers = 0;
|
|
||||||
|
|
||||||
|
|
||||||
if (uc_timers_first(&mux->timers, &first_timer) == 0) {
|
if (uc_timers_first(&mux->timers, &first_timer) == 0) {
|
||||||
struct timeval now;
|
struct timeval now;
|
||||||
mux->uclock->now(mux->uclock, &now);
|
mux->uclock->now(mux->uclock, &now);
|
||||||
future_to_interval(&first_timer, &now, &timeout);
|
future_to_interval(&first_timer, &now, timeout);
|
||||||
timeoutp = &timeout;
|
|
||||||
/* TRACEF("now %d %d future %d %d interval %d %d\n", mux->now.tv_sec, mux->now.tv_usec,
|
/* 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,
|
first_timer.tv_sec, first_timer.tv_usec,
|
||||||
timeout.tv_sec, timeout.tv_usec);
|
timeout.tv_sec, timeout.tv_usec);
|
||||||
*/
|
*/
|
||||||
|
assert(timeout->tv_sec >= 0);
|
||||||
|
assert(timeout->tv_usec >= 0);
|
||||||
|
|
||||||
assert(timeout.tv_sec >= 0);
|
return 1;
|
||||||
assert(timeout.tv_usec >= 0);
|
|
||||||
|
|
||||||
has_timers = 1;
|
|
||||||
} else {
|
|
||||||
timeoutp = NULL; //no timeout
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 9;
|
||||||
|
}
|
||||||
|
|
||||||
|
int iomux_run(struct IOMux *mux)
|
||||||
|
{
|
||||||
|
for (;;) {
|
||||||
|
int rc;
|
||||||
|
struct timeval timeout;
|
||||||
|
struct timeval *timeoutp;
|
||||||
|
|
||||||
|
if (iomux_run_timers(mux, &timeout)) {
|
||||||
|
timeoutp = &timeout;
|
||||||
|
} else {
|
||||||
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user