diff --git a/a8/xtimer.cc b/a8/xtimer.cc index 824cc82..547fce3 100644 --- a/a8/xtimer.cc +++ b/a8/xtimer.cc @@ -119,7 +119,6 @@ namespace a8 { base_ = new xtvec_base(); base_->running_timer = nullptr; - base_->timer_tick = get_tick_count_func_(context_); INIT_LIST_HEAD(&base_->free_timer); base_->free_timer_num = 0; for (int j = 0; j < TVN_SIZE; j++) { @@ -143,9 +142,12 @@ namespace a8 void XTimer::Init(XGetTickCountFunc func, void* context, int gc_time, int cache_timer_num) { get_tick_count_func_ = func; - context_ = context_; + context_ = context; gc_time_ = gc_time; cache_timer_num_ = cache_timer_num; + #if 1 + base_->timer_tick = get_tick_count_func_(context_); + #endif AddRepeatTimer(gc_time_, a8::XParams().SetSender(this), &XTimer::GC_XTimerFunc); @@ -197,6 +199,14 @@ namespace a8 return timer; } + xtimer_list* XTimer::AddRepeatTimerAndAttach(int expire_time, a8::XParams param, a8::XTimerFunc timer_func, + list_head* attach_list) + { + xtimer_list* timer = AddRepeatTimer(expire_time, param, timer_func); + list_add_tail(&timer->attach_entry, attach_list); + return timer; + } + void XTimer::ModifyTimer(xtimer_list* timer, int expire_time) { DetachTimer(timer); @@ -218,6 +228,9 @@ namespace a8 void XTimer::DeleteTimer(xtimer_list* timer) { + if (base_->running_timer == timer) { + base_->running_timer = nullptr; + } if (timer->timer_after_func) { timer->timer_after_func(timer->param); } @@ -264,25 +277,27 @@ namespace a8 if (timer->timer_after_func) { timer->timer_after_func(timer->param); } - switch (timer->timer_type) { - case 1: - case 2: //循环类定时 fixed timer也是循环定时器 - { - if (timer->timer_type == 2) { - timer->fixed_timer_execute_times++; + if (base_->running_timer) { + switch (timer->timer_type) { + case 1: + case 2: //循环类定时 fixed timer也是循环定时器 + { + if (timer->timer_type == 2) { + timer->fixed_timer_execute_times++; + } + ModifyTimer(timer, timer->expire_time); } - ModifyTimer(timer, timer->expire_time); - } - break; - default: //deadline timer - { - DetachTimer(timer); - if (!list_empty(&timer->attach_entry)) { - list_del_init(&timer->attach_entry); + break; + default: //deadline timer + { + DetachTimer(timer); + if (!list_empty(&timer->attach_entry)) { + list_del_init(&timer->attach_entry); + } + AddToFreeList(timer); } - AddToFreeList(timer); + break; } - break; } } } diff --git a/a8/xtimer.h b/a8/xtimer.h index f398f74..76d08c8 100644 --- a/a8/xtimer.h +++ b/a8/xtimer.h @@ -15,11 +15,10 @@ namespace a8 class XTimer { - private: + public: XTimer(); ~XTimer(); - public: void Init(XGetTickCountFunc func, void* context, int gc_time, int cache_timer_num); void Update(); @@ -33,6 +32,8 @@ namespace a8 a8::XTimerAfterFunc timer_after_func = nullptr); //添加重复执行定时器(周期性定时器) xtimer_list* AddRepeatTimer(int expire_time, a8::XParams param, a8::XTimerFunc timer_func); + xtimer_list* AddRepeatTimerAndAttach(int expire_time, a8::XParams param, a8::XTimerFunc timer_func, + list_head* attach_list); //修改定时器参数 void ModifyTimer(xtimer_list* timer, int expire_time); //删除定时器