Convert UCTimers to use UCoreClock
This commit is contained in:
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
struct UCTimer;
|
struct UCTimer;
|
||||||
struct UCTimers;
|
struct UCTimers;
|
||||||
|
struct UCoreClock;
|
||||||
|
|
||||||
//internal timer states
|
//internal timer states
|
||||||
enum {
|
enum {
|
||||||
@@ -62,6 +63,7 @@ struct UCTimer {
|
|||||||
/** Timer engine. Must be initialized onnce.*/
|
/** Timer engine. Must be initialized onnce.*/
|
||||||
struct UCTimers {
|
struct UCTimers {
|
||||||
RBTree timer_tree;
|
RBTree timer_tree;
|
||||||
|
struct UCoreClock *uclock;
|
||||||
size_t seq; //used to generate unique timers
|
size_t seq; //used to generate unique timers
|
||||||
TAILQ_HEAD(, UCTimer) ready_list; //pending timers to be fired while inside uc_timers_run
|
TAILQ_HEAD(, UCTimer) ready_list; //pending timers to be fired while inside uc_timers_run
|
||||||
};
|
};
|
||||||
@@ -73,6 +75,12 @@ struct UCTimers {
|
|||||||
*/
|
*/
|
||||||
void uc_timers_init(struct UCTimers *t);
|
void uc_timers_init(struct UCTimers *t);
|
||||||
|
|
||||||
|
/** Initialize a timer engine for use, with a user supplied clock.
|
||||||
|
*
|
||||||
|
* @param t UCTimers struct to initialize
|
||||||
|
*/
|
||||||
|
void uc_timers_init_ex(struct UCTimers *t, struct UCoreClock *uclock);
|
||||||
|
|
||||||
/** Add a (one-shot) timer to the timer engine.
|
/** Add a (one-shot) timer to the timer engine.
|
||||||
* User fills in the callback and any cookie members of the UCTimer. The callback
|
* User fills in the callback and any cookie members of the UCTimer. The callback
|
||||||
* will be called when the timer expires, and the timer is removed from the engine
|
* will be called when the timer expires, and the timer is removed from the engine
|
||||||
@@ -127,7 +135,7 @@ int uc_timer_running(const struct UCTimer *timer);
|
|||||||
* @return the number of timers fired, or negative if an error occured(presently no errors
|
* @return the number of timers fired, or negative if an error occured(presently no errors
|
||||||
* are possible and the return value is always >= 0
|
* are possible and the return value is always >= 0
|
||||||
**/
|
**/
|
||||||
int uc_timers_run(struct UCTimers *timers, const struct timeval *now);
|
int uc_timers_run(struct UCTimers *timers);
|
||||||
|
|
||||||
//uc_timers_destroy(struct UCTimers *timers) is currently missing..
|
//uc_timers_destroy(struct UCTimers *timers) is currently missing..
|
||||||
|
|
||||||
|
|||||||
+2
-4
@@ -74,10 +74,9 @@ int iomux_run(struct IOMux *mux)
|
|||||||
if (uc_timers_first(&mux->timers, &first_timer) == 0) {
|
if (uc_timers_first(&mux->timers, &first_timer) == 0) {
|
||||||
future_to_interval(&first_timer, &mux->now, &timeout);
|
future_to_interval(&first_timer, &mux->now, &timeout);
|
||||||
timeoutp = &timeout;
|
timeoutp = &timeout;
|
||||||
/* fprintf(stdout, "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);
|
||||||
fflush(stdout);
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
assert(timeout.tv_sec >= 0);
|
assert(timeout.tv_sec >= 0);
|
||||||
@@ -101,9 +100,8 @@ int iomux_run(struct IOMux *mux)
|
|||||||
int iomux_timers_run(struct IOMux *mux)
|
int iomux_timers_run(struct IOMux *mux)
|
||||||
{
|
{
|
||||||
int event_cnt;
|
int event_cnt;
|
||||||
gettimeofday(&mux->now, NULL);
|
|
||||||
|
|
||||||
event_cnt = uc_timers_run(&mux->timers, &mux->now);
|
event_cnt = uc_timers_run(&mux->timers);
|
||||||
assert(event_cnt >= 0);
|
assert(event_cnt >= 0);
|
||||||
|
|
||||||
return event_cnt;
|
return event_cnt;
|
||||||
|
|||||||
+13
-4
@@ -1,5 +1,6 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <ucore/ucore_timers.h>
|
#include <ucore/ucore_timers.h>
|
||||||
|
#include <ucore/ucore_clock.h>
|
||||||
|
|
||||||
|
|
||||||
static int timers_cmp(const void *a_, const void *b_)
|
static int timers_cmp(const void *a_, const void *b_)
|
||||||
@@ -35,9 +36,15 @@ static void uc_timer_real_add(struct UCTimers *timers, struct UCTimer *timer)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void uc_timers_init(struct UCTimers *t)
|
void uc_timers_init(struct UCTimers *t)
|
||||||
|
{
|
||||||
|
uc_timers_init_ex(t, &ucore_timeofday_clock);
|
||||||
|
}
|
||||||
|
|
||||||
|
void uc_timers_init_ex(struct UCTimers *t, struct UCoreClock *uclock)
|
||||||
{
|
{
|
||||||
t->timer_tree.node = NULL;
|
t->timer_tree.node = NULL;
|
||||||
t->timer_tree.compare = timers_cmp;
|
t->timer_tree.compare = timers_cmp;
|
||||||
|
t->uclock = uclock;
|
||||||
t->seq = 0;
|
t->seq = 0;
|
||||||
TAILQ_INIT(&t->ready_list);
|
TAILQ_INIT(&t->ready_list);
|
||||||
}
|
}
|
||||||
@@ -54,7 +61,7 @@ void uc_timer_add(struct UCTimers *timers, struct UCTimer *timer, int sec, int u
|
|||||||
tmp.tv_sec = sec;
|
tmp.tv_sec = sec;
|
||||||
tmp.tv_usec = usec;
|
tmp.tv_usec = usec;
|
||||||
|
|
||||||
gettimeofday(&now, NULL);
|
timers->uclock->now(timers->uclock, &now);
|
||||||
timeradd(&now, &tmp, &timer->timeout);
|
timeradd(&now, &tmp, &timer->timeout);
|
||||||
uc_timer_real_add(timers, timer);
|
uc_timer_real_add(timers, timer);
|
||||||
}
|
}
|
||||||
@@ -108,10 +115,13 @@ int uc_timer_running(const struct UCTimer *timer)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int uc_timers_run(struct UCTimers *timers, const struct timeval *now)
|
int uc_timers_run(struct UCTimers *timers)
|
||||||
{
|
{
|
||||||
RBNode *node;
|
RBNode *node;
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
|
struct timeval now;
|
||||||
|
|
||||||
|
timers->uclock->now(timers->uclock, &now);
|
||||||
|
|
||||||
/* First find all the expired nodes, place them in a linked list.
|
/* First find all the expired nodes, place them in a linked list.
|
||||||
* Callbacks cannot be performed here, as the callback might alter
|
* Callbacks cannot be performed here, as the callback might alter
|
||||||
@@ -122,7 +132,7 @@ int uc_timers_run(struct UCTimers *timers, const struct timeval *now)
|
|||||||
for (node = uc_rb_first(&timers->timer_tree); node ; node = uc_rb_next(node)) {
|
for (node = uc_rb_first(&timers->timer_tree); node ; node = uc_rb_next(node)) {
|
||||||
struct UCTimer *t = node->data;
|
struct UCTimer *t = node->data;
|
||||||
//Note, timercmp does not work for >=
|
//Note, timercmp does not work for >=
|
||||||
if (!timercmp(now, &t->timeout, <)) {
|
if (!timercmp(&now, &t->timeout, <)) {
|
||||||
TAILQ_INSERT_TAIL(&timers->ready_list, t, ready_entry);
|
TAILQ_INSERT_TAIL(&timers->ready_list, t, ready_entry);
|
||||||
t->state = UC_TIMER_PENDING; //so uc_timer_remove knows
|
t->state = UC_TIMER_PENDING; //so uc_timer_remove knows
|
||||||
//it have to remove it from the ready_list
|
//it have to remove it from the ready_list
|
||||||
@@ -151,7 +161,6 @@ int uc_timers_run(struct UCTimers *timers, const struct timeval *now)
|
|||||||
cnt++;
|
cnt++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return cnt;
|
return cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-3
@@ -69,9 +69,7 @@ int main(int argc, char *argv[])
|
|||||||
uc_timer_add(&timers, &my_struct2.timer, 10, 0);
|
uc_timer_add(&timers, &my_struct2.timer, 10, 0);
|
||||||
|
|
||||||
for(;;) {
|
for(;;) {
|
||||||
struct timeval now;
|
uc_timers_run(&timers);
|
||||||
gettimeofday(&now, NULL);
|
|
||||||
uc_timers_run(&timers, &now);
|
|
||||||
#ifndef FULL_SPEED
|
#ifndef FULL_SPEED
|
||||||
usleep(1000);
|
usleep(1000);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user