1
This commit is contained in:
parent
ffcbe9e7bf
commit
c75e00fffd
@ -267,7 +267,7 @@ namespace a8
|
||||
client->connect_timer_attacher.ClearTimerList();
|
||||
context->xtimer.SetTimeoutEx
|
||||
(
|
||||
param.param1,
|
||||
param.param1.GetInt(),
|
||||
[client] (int event, const a8::Args* args)
|
||||
{
|
||||
if (a8::TIMER_EXEC_EVENT == event) {
|
||||
|
149
a8/xtimer.cc
149
a8/xtimer.cc
@ -136,7 +136,7 @@ namespace a8
|
||||
gc_time_ = gc_time;
|
||||
cache_timer_num_ = cache_timer_num;
|
||||
base_->timer_tick = get_tick_count_func_(context_);
|
||||
SetInterval
|
||||
InternalSetInterval
|
||||
(gc_time_,
|
||||
[this] (int event, const a8::Args* args)
|
||||
{
|
||||
@ -152,7 +152,9 @@ namespace a8
|
||||
++i;
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
nullptr,
|
||||
nullptr);
|
||||
}
|
||||
|
||||
void Update()
|
||||
@ -162,17 +164,17 @@ namespace a8
|
||||
}
|
||||
}
|
||||
|
||||
void InternalSetTimeout(int time, TimerCb cb, Attacher* attacher, XTimerWp* wp)
|
||||
void InternalSetTimeout(int expire_time, TimerCb cb, Attacher* attacher, XTimerWp* wp)
|
||||
{
|
||||
xtimer_list *timer = ::NewTimerList(base_);
|
||||
xtimer_list *timer = NewTimerList();
|
||||
InitTimerList(base_,
|
||||
timer,
|
||||
0,
|
||||
get_tick_count_func_(context_) + expire_time,
|
||||
expire_time,
|
||||
timer_cb
|
||||
cb
|
||||
);
|
||||
ModifyTimerTimer(timer, expire_time);
|
||||
InternalModifyTime(timer, expire_time);
|
||||
|
||||
#if 0
|
||||
std::shared_ptr<XTimerPtr> timer_ptr = std::make_shared<XTimerPtr>();
|
||||
@ -188,29 +190,28 @@ namespace a8
|
||||
#endif
|
||||
}
|
||||
|
||||
void InternalSetInterval(int time, TimerCb cb, Attacher* attacher, XTimerWp* wp)
|
||||
void InternalSetInterval(int expire_time, TimerCb cb, Attacher* attacher, XTimerWp* wp)
|
||||
{
|
||||
xtimer_list *timer = ::NewTimerList(base_);
|
||||
xtimer_list *timer = NewTimerList();
|
||||
InitTimerList(base_,
|
||||
timer,
|
||||
1,
|
||||
get_tick_count_func_(context_) + expire_time,
|
||||
expire_time,
|
||||
timer_cb);
|
||||
#if 0
|
||||
ModifyTimer(timer, expire_time);
|
||||
#endif
|
||||
cb);
|
||||
InternalModifyTime(timer, expire_time);
|
||||
|
||||
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;
|
||||
if (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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void FireEvent(XTimerWp& timer_wp, a8::Args* args)
|
||||
@ -218,28 +219,8 @@ namespace a8
|
||||
|
||||
}
|
||||
|
||||
void ModifyTime(XTimerWp& timer_wp, int expire_time)
|
||||
void InternalDelete(xtimer_list* timer)
|
||||
{
|
||||
DetachTimer(timer);
|
||||
timer->expire_time = expire_time;
|
||||
if (timer->timer_type == 2) {
|
||||
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);
|
||||
}
|
||||
|
||||
void Delete(XTimerWp& timer_wp)
|
||||
{
|
||||
xtimer_list* timer = timer_ptr.lock()->timer;
|
||||
if (!timer) {
|
||||
abort();
|
||||
}
|
||||
@ -261,12 +242,30 @@ namespace a8
|
||||
node->cb(timer);
|
||||
delete node;
|
||||
}
|
||||
AddToFreeList(base_, timer);
|
||||
AddToFreeList(timer);
|
||||
}
|
||||
|
||||
long long GetRemainTime(XTimerWp& timer_wp)
|
||||
void InternalModifyTime(xtimer_list* timer, int expire_time)
|
||||
{
|
||||
DetachTimer(timer);
|
||||
timer->expire_time = expire_time;
|
||||
if (timer->timer_type == 2) {
|
||||
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);
|
||||
}
|
||||
|
||||
long long InternalGetRemainTime(xtimer_list* timer)
|
||||
{
|
||||
xtimer_list* timer = timer_ptr.lock()->timer;
|
||||
if (!timer) {
|
||||
abort();
|
||||
}
|
||||
@ -309,11 +308,11 @@ namespace a8
|
||||
node->cb(timer);
|
||||
delete node;
|
||||
}
|
||||
AddToFreeList(base_, timer);
|
||||
AddToFreeList(timer);
|
||||
#endif
|
||||
}
|
||||
|
||||
void XTimer::UpdateTimer()
|
||||
void UpdateTimer()
|
||||
{
|
||||
struct xtvec_base *base = base_;
|
||||
while (get_tick_count_func_(context_) >= base->timer_tick) {
|
||||
@ -363,7 +362,7 @@ namespace a8
|
||||
node->cb(timer);
|
||||
delete node;
|
||||
}
|
||||
AddToFreeList(base_, timer);
|
||||
AddToFreeList(timer);
|
||||
if (timer->cb) {
|
||||
timer->cb(TIMER_DELETE_EVENT, nullptr);
|
||||
}
|
||||
@ -389,7 +388,7 @@ namespace a8
|
||||
base_->free_timer_num++;
|
||||
}
|
||||
|
||||
xtimer_list* NewTimerList(xtvec_base* base_)
|
||||
xtimer_list* NewTimerList()
|
||||
{
|
||||
if (!list_empty(&base_->free_timer)) {
|
||||
xtimer_list* timer = list_first_entry(&base_->free_timer, struct xtimer_list,entry);
|
||||
@ -404,7 +403,7 @@ namespace a8
|
||||
void ClearTimer()
|
||||
{
|
||||
auto free_timers =
|
||||
[] (list_head* head)
|
||||
[this] (list_head* head)
|
||||
{
|
||||
while (!list_empty(head)) {
|
||||
struct xtimer_list *timer;
|
||||
@ -507,51 +506,52 @@ namespace a8
|
||||
impl_->Update();
|
||||
}
|
||||
|
||||
void XTimer::SetTimeout(int time, TimerCb cb)
|
||||
void XTimer::SetTimeout(int expire_time, TimerCb cb)
|
||||
{
|
||||
impl_->InternalSetTimeout(time, cb, nullptr, nullptr);
|
||||
impl_->InternalSetTimeout(expire_time, cb, nullptr, nullptr);
|
||||
}
|
||||
|
||||
void XTimer::SetTimeoutEx(int time, TimerCb cb, Attacher* attacher)
|
||||
void XTimer::SetTimeoutEx(int expire_time, TimerCb cb, Attacher* attacher)
|
||||
{
|
||||
impl_->InternalSetTimeout(time, cb, attacher, nullptr);
|
||||
|
||||
impl_->InternalSetTimeout(expire_time, cb, attacher, nullptr);
|
||||
}
|
||||
|
||||
XTimerWp XTimer::SetTimeoutWp(int time, TimerCb cb)
|
||||
XTimerWp XTimer::SetTimeoutWp(int expire_time, TimerCb cb)
|
||||
{
|
||||
XTimerWp result;
|
||||
impl_->InternalSetTimeout(time, cb, nullptr, &result);
|
||||
impl_->InternalSetTimeout(expire_time, cb, nullptr, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
XTimerWp XTimer::SetTimeoutWpEx(int time, TimerCb cb, Attacher* attacher)
|
||||
XTimerWp XTimer::SetTimeoutWpEx(int expire_time, TimerCb cb, Attacher* attacher)
|
||||
{
|
||||
XTimerWp result;
|
||||
impl_->InternalSetTimeout(time, cb, attacherk, &result);
|
||||
impl_->InternalSetTimeout(expire_time, cb, attacher, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
void XTimer::SetInterval(int time, a8::TimerCb cb)
|
||||
void XTimer::SetInterval(int expire_time, a8::TimerCb cb)
|
||||
{
|
||||
impl_->InternalSetInterval(time, cb, nullptr, nullptr);
|
||||
impl_->InternalSetInterval(expire_time, cb, nullptr, nullptr);
|
||||
}
|
||||
|
||||
void XTimer::SetIntervalEx(int time, a8::TimerCb cb, Attacher* attacher)
|
||||
void XTimer::SetIntervalEx(int expire_time, a8::TimerCb cb, Attacher* attacher)
|
||||
{
|
||||
impl_->InternalSetInterval(time, cb, attacker, nullptr);
|
||||
impl_->InternalSetInterval(expire_time, cb, attacher, nullptr);
|
||||
}
|
||||
|
||||
XTimerWp XTimer::SetIntervalWp(int time, TimerCb cb)
|
||||
XTimerWp XTimer::SetIntervalWp(int expire_time, TimerCb cb)
|
||||
{
|
||||
XTimerWp result;
|
||||
impl_->InternalSetInterval(time, cb, nullptr, &result);
|
||||
impl_->InternalSetInterval(expire_time, cb, nullptr, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
XTimerWp XTimer::SetIntervalWpEx(int time, TimerCb cb, Attacher* attacher)
|
||||
XTimerWp XTimer::SetIntervalWpEx(int expire_time, TimerCb cb, Attacher* attacher)
|
||||
{
|
||||
XTimerWp result;
|
||||
impl_->InternalSetInterval(time, cb, attacherk, &result);
|
||||
impl_->InternalSetInterval(expire_time, cb, attacher, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -562,17 +562,26 @@ namespace a8
|
||||
|
||||
void XTimer::ModifyTime(XTimerWp& timer_wp, int expire_time)
|
||||
{
|
||||
impl_->ModifyTime(timr_wp, expire_time);
|
||||
if (timer_wp.expired()) {
|
||||
abort();
|
||||
}
|
||||
impl_->InternalModifyTime(timer_wp.lock()->timer, expire_time);
|
||||
}
|
||||
|
||||
void XTimer::Delete(XTimerWp& timer_wp)
|
||||
{
|
||||
impl_->Delete(timer_wp);
|
||||
if (timer_wp.expired()) {
|
||||
abort();
|
||||
}
|
||||
impl_->InternalDelete(timer_wp.lock()->timer);
|
||||
}
|
||||
|
||||
long long XTimer::GetRemainTime(XTimerWp& timer_wp)
|
||||
{
|
||||
return impl_->GetRemainTime(timer_wp);
|
||||
if (timer_wp.expired()) {
|
||||
abort();
|
||||
}
|
||||
return impl_->InternalGetRemainTime(timer_wp.lock()->timer);
|
||||
}
|
||||
|
||||
bool XTimer::IsRunning()
|
||||
|
16
a8/xtimer.h
16
a8/xtimer.h
@ -15,15 +15,15 @@ namespace a8
|
||||
void Init(XGetTickCountFunc func, void* context, int gc_time, int cache_timer_num);
|
||||
void Update();
|
||||
|
||||
inline void SetTimeout(int time, TimerCb cb);
|
||||
inline void SetTimeoutEx(int time, TimerCb cb, Attacher* attacher);
|
||||
inline XTimerWp SetTimeoutWp(int time, TimerCb cb);
|
||||
inline XTimerWp SetTimeoutWpEx(int time, TimerCb cb, Attacher* attacher);
|
||||
void SetTimeout(int expire_time, TimerCb cb);
|
||||
void SetTimeoutEx(int expire_time, TimerCb cb, Attacher* attacher);
|
||||
XTimerWp SetTimeoutWp(int expire_time, TimerCb cb);
|
||||
XTimerWp SetTimeoutWpEx(int expire_time, TimerCb cb, Attacher* attacher);
|
||||
|
||||
inline void SetInterval(int time, a8::TimerCb cb);
|
||||
inline void SetIntervalEx(int time, a8::TimerCb cb, Attacher* attacher);
|
||||
inline XTimerWp SetIntervalWp(int time, TimerCb cb);
|
||||
inline XTimerWp SetIntervalWpEx(int time, TimerCb cb, Attacher* attacher);
|
||||
void SetInterval(int expire_time, a8::TimerCb cb);
|
||||
void SetIntervalEx(int expire_time, a8::TimerCb cb, Attacher* attacher);
|
||||
XTimerWp SetIntervalWp(int expire_time, TimerCb cb);
|
||||
XTimerWp SetIntervalWpEx(int expire_time, TimerCb cb, Attacher* attacher);
|
||||
|
||||
void FireEvent(XTimerWp& timer_wp, a8::Args* args);
|
||||
void ModifyTime(XTimerWp& timer_wp, int expire_time);
|
||||
|
Loading…
x
Reference in New Issue
Block a user