diff --git a/a8/timer_attacher.h b/a8/timer_attacher.h index 978e90e..64c99c1 100644 --- a/a8/timer_attacher.h +++ b/a8/timer_attacher.h @@ -28,6 +28,7 @@ namespace a8 list_head timer_list_; friend class XTimer; + friend class XTimerImpl; }; } diff --git a/a8/xtimer.cc b/a8/xtimer.cc index 62b95cc..8fa17d9 100644 --- a/a8/xtimer.cc +++ b/a8/xtimer.cc @@ -1,6 +1,7 @@ #include #include +#include enum TimerType_e { @@ -92,8 +93,6 @@ static int Cascade(struct xtvec_base *base, struct xtvec *tv, int index) namespace a8 { - typedef struct list_head XTimerDestoryHandle; - struct XTimerPtr { xtimer_list* timer = nullptr; @@ -109,8 +108,9 @@ namespace a8 { public: - XTimerImpl() + XTimerImpl(XTimer* owner) { + owner_ = owner; base_ = new xtvec_base(); base_->running_timer = nullptr; INIT_LIST_HEAD(&base_->free_timer); @@ -176,6 +176,12 @@ namespace a8 expire_time, cb ); + if (attacher) { + if (attacher->owner_ != owner_) { + abort(); + } + list_add_tail(&timer->attach_entry, &attacher->timer_list_); + } InternalModifyTime(timer, expire_time); if (wp) { AddTimerWp(timer, *wp); @@ -190,6 +196,12 @@ namespace a8 get_tick_count_func_(context_) + expire_time, expire_time, cb); + if (attacher) { + if (attacher->owner_ != owner_) { + abort(); + } + list_add_tail(&timer->attach_entry, &attacher->timer_list_); + } InternalModifyTime(timer, expire_time); if (wp) { AddTimerWp(timer, *wp); @@ -260,11 +272,22 @@ namespace a8 void ClearAttacher(Attacher* attacher) { - + struct list_head* pos = nullptr; + struct list_head* n = nullptr; + list_for_each_safe(pos, n, &attacher->timer_list_) { + xtimer_list* tmp_timer = list_entry(pos, struct xtimer_list, attach_entry); + InternalDelete(tmp_timer, false, true); + } } void DestoryAttacher(Attacher* attacher) { + struct list_head* pos = nullptr; + struct list_head* n = nullptr; + list_for_each_safe(pos, n, &attacher->timer_list_) { + xtimer_list* tmp_timer = list_entry(pos, struct xtimer_list, attach_entry); + InternalDelete(tmp_timer, true, true); + } } void UpdateTimer() @@ -369,24 +392,12 @@ namespace a8 free_timers(&base_->free_timer); } - XTimerDestoryHandle* AddTimerDestoryHandle(std::weak_ptr& timer_ptr, - std::function cb) + void AddTimerDestoryHandle(std::weak_ptr& timer_ptr, + std::function cb) { XTimerDestoryHandleNode* node = new XTimerDestoryHandleNode; - node->cb = cb; + node->cb = std::move(cb); list_add_tail(&node->entry, &timer_ptr.lock()->timer->destory_handle_list); - return &node->entry; - } - - void RemoveTimerDestoryHandle(XTimerDestoryHandle* handle) - { - if (!handle || list_empty(handle)) { - abort(); - } - - XTimerDestoryHandleNode* node = list_entry(handle, XTimerDestoryHandleNode, entry); - list_del_init(&node->entry); - delete node; } void InitTimerList(xtimer_list* timer, @@ -414,6 +425,7 @@ namespace a8 } private: + XTimer* owner_ = nullptr; xtvec_base* base_ = nullptr; XGetTickCountFunc get_tick_count_func_ = nullptr; void* context_ = nullptr; @@ -423,7 +435,7 @@ namespace a8 XTimer::XTimer() { - impl_ = new XTimerImpl(); + impl_ = new XTimerImpl(this); } XTimer::~XTimer() @@ -529,11 +541,17 @@ namespace a8 void XTimer::ClearAttacher(Attacher* attacher) { + if (attacher->owner_ != this) { + abort(); + } impl_->ClearAttacher(attacher); } void XTimer::DestoryAttacher(Attacher* attacher) { + if (attacher->owner_ != this) { + abort(); + } impl_->DestoryAttacher(attacher); }