1
This commit is contained in:
parent
c75e00fffd
commit
5ac8b9be91
145
a8/xtimer.cc
145
a8/xtimer.cc
@ -2,6 +2,12 @@
|
|||||||
|
|
||||||
#include <a8/xtimer.h>
|
#include <a8/xtimer.h>
|
||||||
|
|
||||||
|
enum TimerType_e
|
||||||
|
{
|
||||||
|
kTimeOutTimer = 0,
|
||||||
|
kIntervalTimer = 1
|
||||||
|
};
|
||||||
|
|
||||||
#define CONFIG_BASE_SMALL 0
|
#define CONFIG_BASE_SMALL 0
|
||||||
#define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
|
#define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
|
||||||
#define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
|
#define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
|
||||||
@ -35,10 +41,9 @@ struct xtimer_list {
|
|||||||
struct list_head destory_handle_list;
|
struct list_head destory_handle_list;
|
||||||
struct list_head entry;
|
struct list_head entry;
|
||||||
struct list_head attach_entry;
|
struct list_head attach_entry;
|
||||||
int timer_type; // 0:deadline 1: repeattimer 2:fixedtimer outher:deadline
|
TimerType_e timer_type;
|
||||||
long long expires;
|
long long expires;
|
||||||
int expire_time;
|
int expire_time;
|
||||||
int fixed_timer_execute_times;
|
|
||||||
struct xtvec_base *base;
|
struct xtvec_base *base;
|
||||||
|
|
||||||
a8::TimerCb cb;
|
a8::TimerCb cb;
|
||||||
@ -123,9 +128,7 @@ namespace a8
|
|||||||
|
|
||||||
~XTimerImpl()
|
~XTimerImpl()
|
||||||
{
|
{
|
||||||
#if 0
|
ClearTimer();
|
||||||
Clear();
|
|
||||||
#endif
|
|
||||||
A8_SAFE_DELETE(base_);
|
A8_SAFE_DELETE(base_);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,56 +170,37 @@ namespace a8
|
|||||||
void InternalSetTimeout(int expire_time, TimerCb cb, Attacher* attacher, XTimerWp* wp)
|
void InternalSetTimeout(int expire_time, TimerCb cb, Attacher* attacher, XTimerWp* wp)
|
||||||
{
|
{
|
||||||
xtimer_list *timer = NewTimerList();
|
xtimer_list *timer = NewTimerList();
|
||||||
InitTimerList(base_,
|
InitTimerList(timer,
|
||||||
timer,
|
kTimeOutTimer,
|
||||||
0,
|
|
||||||
get_tick_count_func_(context_) + expire_time,
|
get_tick_count_func_(context_) + expire_time,
|
||||||
expire_time,
|
expire_time,
|
||||||
cb
|
cb
|
||||||
);
|
);
|
||||||
InternalModifyTime(timer, expire_time);
|
InternalModifyTime(timer, expire_time);
|
||||||
|
if (wp) {
|
||||||
#if 0
|
AddTimerWp(timer, *wp);
|
||||||
std::shared_ptr<XTimerPtr> timer_ptr = std::make_shared<XTimerPtr>();
|
}
|
||||||
timer_ptr->timer = timer;
|
|
||||||
std::weak_ptr<XTimerPtr> result = timer_ptr;
|
|
||||||
AddTimerDestoryHandle
|
|
||||||
(result,
|
|
||||||
[timer_ptr] (xtimer_list* timer)
|
|
||||||
{
|
|
||||||
timer_ptr->timer = nullptr;
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void InternalSetInterval(int expire_time, TimerCb cb, Attacher* attacher, XTimerWp* wp)
|
void InternalSetInterval(int expire_time, TimerCb cb, Attacher* attacher, XTimerWp* wp)
|
||||||
{
|
{
|
||||||
xtimer_list *timer = NewTimerList();
|
xtimer_list *timer = NewTimerList();
|
||||||
InitTimerList(base_,
|
InitTimerList(timer,
|
||||||
timer,
|
kIntervalTimer,
|
||||||
1,
|
|
||||||
get_tick_count_func_(context_) + expire_time,
|
get_tick_count_func_(context_) + expire_time,
|
||||||
expire_time,
|
expire_time,
|
||||||
cb);
|
cb);
|
||||||
InternalModifyTime(timer, expire_time);
|
InternalModifyTime(timer, expire_time);
|
||||||
|
|
||||||
if (wp) {
|
if (wp) {
|
||||||
std::shared_ptr<XTimerPtr> timer_ptr = std::make_shared<XTimerPtr>();
|
AddTimerWp(timer, *wp);
|
||||||
timer_ptr->timer = timer;
|
|
||||||
*wp = timer_ptr;
|
|
||||||
AddTimerDestoryHandle
|
|
||||||
(*wp,
|
|
||||||
[timer_ptr] (xtimer_list* timer)
|
|
||||||
{
|
|
||||||
timer_ptr->timer = nullptr;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FireEvent(XTimerWp& timer_wp, a8::Args* args)
|
void InternalFireEvent(xtimer_list* timer, int event, a8::Args* args)
|
||||||
{
|
{
|
||||||
|
if (timer->cb) {
|
||||||
|
timer->cb(event, args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void InternalDelete(xtimer_list* timer)
|
void InternalDelete(xtimer_list* timer)
|
||||||
@ -249,26 +233,12 @@ namespace a8
|
|||||||
{
|
{
|
||||||
DetachTimer(timer);
|
DetachTimer(timer);
|
||||||
timer->expire_time = expire_time;
|
timer->expire_time = expire_time;
|
||||||
if (timer->timer_type == 2) {
|
timer->expires = get_tick_count_func_(context_) + expire_time;
|
||||||
long long tick = get_tick_count_func_(context_);
|
|
||||||
long long today_passed_seconds = time(nullptr) - a8::GetDaySeconds(time(nullptr), 0);
|
|
||||||
timer->expires = (tick - today_passed_seconds * 1000) + expire_time;
|
|
||||||
if (timer->fixed_timer_execute_times > 0) {
|
|
||||||
if (timer->expires <= tick) { //已过期
|
|
||||||
timer->expires += 1000 * 3600 * 24;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}else {
|
|
||||||
timer->expires = get_tick_count_func_(context_) + expire_time;
|
|
||||||
}
|
|
||||||
InternalAddXTimer(base_, timer);
|
InternalAddXTimer(base_, timer);
|
||||||
}
|
}
|
||||||
|
|
||||||
long long InternalGetRemainTime(xtimer_list* timer)
|
long long InternalGetRemainTime(xtimer_list* timer)
|
||||||
{
|
{
|
||||||
if (!timer) {
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
long long remain_time = timer->expires - get_tick_count_func_(context_);
|
long long remain_time = timer->expires - get_tick_count_func_(context_);
|
||||||
return std::max(remain_time, (long long)0);
|
return std::max(remain_time, (long long)0);
|
||||||
}
|
}
|
||||||
@ -285,31 +255,6 @@ namespace a8
|
|||||||
|
|
||||||
void DestoryAttacher(Attacher* attacher)
|
void DestoryAttacher(Attacher* attacher)
|
||||||
{
|
{
|
||||||
#if 0
|
|
||||||
xtimer_list* timer = timer_ptr.lock()->timer;
|
|
||||||
if (!timer) {
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
if (base_->running_timer == timer) {
|
|
||||||
base_->running_timer = nullptr;
|
|
||||||
}
|
|
||||||
if (timer->cb) {
|
|
||||||
timer->cb(TIMER_DESTORY_EVNET, nullptr);
|
|
||||||
}
|
|
||||||
DetachTimer(timer);
|
|
||||||
if (!list_empty(&timer->attach_entry)) {
|
|
||||||
list_del_init(&timer->attach_entry);
|
|
||||||
}
|
|
||||||
while (!list_empty(&timer->destory_handle_list)) {
|
|
||||||
XTimerDestoryHandleNode* node = list_first_entry(&timer->destory_handle_list,
|
|
||||||
XTimerDestoryHandleNode,
|
|
||||||
entry);
|
|
||||||
list_del_init(&node->entry);
|
|
||||||
node->cb(timer);
|
|
||||||
delete node;
|
|
||||||
}
|
|
||||||
AddToFreeList(timer);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpdateTimer()
|
void UpdateTimer()
|
||||||
@ -337,18 +282,7 @@ namespace a8
|
|||||||
}
|
}
|
||||||
if (base_->running_timer) {
|
if (base_->running_timer) {
|
||||||
switch (timer->timer_type) {
|
switch (timer->timer_type) {
|
||||||
case 1:
|
case kTimeOutTimer:
|
||||||
case 2: //循环类定时 fixed timer也是循环定时器
|
|
||||||
{
|
|
||||||
if (timer->timer_type == 2) {
|
|
||||||
timer->fixed_timer_execute_times++;
|
|
||||||
}
|
|
||||||
#if 0
|
|
||||||
ModifyTimer(timer, timer->expire_time);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default: //deadline timer
|
|
||||||
{
|
{
|
||||||
DetachTimer(timer);
|
DetachTimer(timer);
|
||||||
if (!list_empty(&timer->attach_entry)) {
|
if (!list_empty(&timer->attach_entry)) {
|
||||||
@ -368,6 +302,10 @@ namespace a8
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -377,7 +315,7 @@ namespace a8
|
|||||||
|
|
||||||
void DeleteCurrentTimer()
|
void DeleteCurrentTimer()
|
||||||
{
|
{
|
||||||
|
InternalDelete(base_->running_timer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -388,6 +326,19 @@ namespace a8
|
|||||||
base_->free_timer_num++;
|
base_->free_timer_num++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AddTimerWp(xtimer_list* timer, XTimerWp& wp)
|
||||||
|
{
|
||||||
|
std::shared_ptr<XTimerPtr> timer_ptr = std::make_shared<XTimerPtr>();
|
||||||
|
timer_ptr->timer = timer;
|
||||||
|
wp = timer_ptr;
|
||||||
|
AddTimerDestoryHandle
|
||||||
|
(wp,
|
||||||
|
[timer_ptr] (xtimer_list* timer)
|
||||||
|
{
|
||||||
|
timer_ptr->timer = nullptr;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
xtimer_list* NewTimerList()
|
xtimer_list* NewTimerList()
|
||||||
{
|
{
|
||||||
if (!list_empty(&base_->free_timer)) {
|
if (!list_empty(&base_->free_timer)) {
|
||||||
@ -455,8 +406,10 @@ namespace a8
|
|||||||
delete node;
|
delete node;
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitTimerList(xtvec_base* base, xtimer_list* timer, int timer_type,
|
void InitTimerList(xtimer_list* timer,
|
||||||
long long expires, int expire_time,
|
TimerType_e timer_type,
|
||||||
|
long long expires,
|
||||||
|
int expire_time,
|
||||||
a8::TimerCb cb)
|
a8::TimerCb cb)
|
||||||
{
|
{
|
||||||
INIT_LIST_HEAD(&timer->destory_handle_list);
|
INIT_LIST_HEAD(&timer->destory_handle_list);
|
||||||
@ -465,8 +418,7 @@ namespace a8
|
|||||||
timer->timer_type = timer_type;
|
timer->timer_type = timer_type;
|
||||||
timer->expires = expires;
|
timer->expires = expires;
|
||||||
timer->expire_time = expire_time;
|
timer->expire_time = expire_time;
|
||||||
timer->fixed_timer_execute_times = 0;
|
timer->base = base_;
|
||||||
timer->base = base;
|
|
||||||
timer->cb = std::move(cb);
|
timer->cb = std::move(cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -555,9 +507,12 @@ namespace a8
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void XTimer::FireEvent(XTimerWp& timer_wp, a8::Args* args)
|
void XTimer::FireEvent(XTimerWp& timer_wp, int event, a8::Args* args)
|
||||||
{
|
{
|
||||||
impl_->FireEvent(timer_wp, args);
|
if (timer_wp.expired()) {
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
impl_->InternalFireEvent(timer_wp.lock()->timer, event, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
void XTimer::ModifyTime(XTimerWp& timer_wp, int expire_time)
|
void XTimer::ModifyTime(XTimerWp& timer_wp, int expire_time)
|
||||||
|
@ -25,7 +25,7 @@ namespace a8
|
|||||||
XTimerWp SetIntervalWp(int expire_time, TimerCb cb);
|
XTimerWp SetIntervalWp(int expire_time, TimerCb cb);
|
||||||
XTimerWp SetIntervalWpEx(int expire_time, TimerCb cb, Attacher* attacher);
|
XTimerWp SetIntervalWpEx(int expire_time, TimerCb cb, Attacher* attacher);
|
||||||
|
|
||||||
void FireEvent(XTimerWp& timer_wp, a8::Args* args);
|
void FireEvent(XTimerWp& timer_wp, int event, a8::Args* args);
|
||||||
void ModifyTime(XTimerWp& timer_wp, int expire_time);
|
void ModifyTime(XTimerWp& timer_wp, int expire_time);
|
||||||
void Delete(XTimerWp& timer_wp);
|
void Delete(XTimerWp& timer_wp);
|
||||||
long long GetRemainTime(XTimerWp& timer_wp);
|
long long GetRemainTime(XTimerWp& timer_wp);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user