This commit is contained in:
aozhiwei 2022-12-14 12:18:10 +08:00
parent 1c1903fab1
commit a2c41b1b7e
2 changed files with 39 additions and 20 deletions

View File

@ -28,6 +28,7 @@ namespace a8
list_head timer_list_; list_head timer_list_;
friend class XTimer; friend class XTimer;
friend class XTimerImpl;
}; };
} }

View File

@ -1,6 +1,7 @@
#include <a8/a8.h> #include <a8/a8.h>
#include <a8/xtimer.h> #include <a8/xtimer.h>
#include <a8/timer_attacher.h>
enum TimerType_e enum TimerType_e
{ {
@ -92,8 +93,6 @@ static int Cascade(struct xtvec_base *base, struct xtvec *tv, int index)
namespace a8 namespace a8
{ {
typedef struct list_head XTimerDestoryHandle;
struct XTimerPtr struct XTimerPtr
{ {
xtimer_list* timer = nullptr; xtimer_list* timer = nullptr;
@ -109,8 +108,9 @@ namespace a8
{ {
public: public:
XTimerImpl() XTimerImpl(XTimer* owner)
{ {
owner_ = owner;
base_ = new xtvec_base(); base_ = new xtvec_base();
base_->running_timer = nullptr; base_->running_timer = nullptr;
INIT_LIST_HEAD(&base_->free_timer); INIT_LIST_HEAD(&base_->free_timer);
@ -176,6 +176,12 @@ namespace a8
expire_time, expire_time,
cb cb
); );
if (attacher) {
if (attacher->owner_ != owner_) {
abort();
}
list_add_tail(&timer->attach_entry, &attacher->timer_list_);
}
InternalModifyTime(timer, expire_time); InternalModifyTime(timer, expire_time);
if (wp) { if (wp) {
AddTimerWp(timer, *wp); AddTimerWp(timer, *wp);
@ -190,6 +196,12 @@ namespace a8
get_tick_count_func_(context_) + expire_time, get_tick_count_func_(context_) + expire_time,
expire_time, expire_time,
cb); cb);
if (attacher) {
if (attacher->owner_ != owner_) {
abort();
}
list_add_tail(&timer->attach_entry, &attacher->timer_list_);
}
InternalModifyTime(timer, expire_time); InternalModifyTime(timer, expire_time);
if (wp) { if (wp) {
AddTimerWp(timer, *wp); AddTimerWp(timer, *wp);
@ -260,11 +272,22 @@ namespace a8
void ClearAttacher(Attacher* attacher) 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) 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() void UpdateTimer()
@ -369,24 +392,12 @@ namespace a8
free_timers(&base_->free_timer); free_timers(&base_->free_timer);
} }
XTimerDestoryHandle* AddTimerDestoryHandle(std::weak_ptr<XTimerPtr>& timer_ptr, void AddTimerDestoryHandle(std::weak_ptr<XTimerPtr>& timer_ptr,
std::function<void(xtimer_list*)> cb) std::function<void(xtimer_list*)> cb)
{ {
XTimerDestoryHandleNode* node = new XTimerDestoryHandleNode; XTimerDestoryHandleNode* node = new XTimerDestoryHandleNode;
node->cb = cb; node->cb = std::move(cb);
list_add_tail(&node->entry, &timer_ptr.lock()->timer->destory_handle_list); 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, void InitTimerList(xtimer_list* timer,
@ -414,6 +425,7 @@ namespace a8
} }
private: private:
XTimer* owner_ = nullptr;
xtvec_base* base_ = nullptr; xtvec_base* base_ = nullptr;
XGetTickCountFunc get_tick_count_func_ = nullptr; XGetTickCountFunc get_tick_count_func_ = nullptr;
void* context_ = nullptr; void* context_ = nullptr;
@ -423,7 +435,7 @@ namespace a8
XTimer::XTimer() XTimer::XTimer()
{ {
impl_ = new XTimerImpl(); impl_ = new XTimerImpl(this);
} }
XTimer::~XTimer() XTimer::~XTimer()
@ -529,11 +541,17 @@ namespace a8
void XTimer::ClearAttacher(Attacher* attacher) void XTimer::ClearAttacher(Attacher* attacher)
{ {
if (attacher->owner_ != this) {
abort();
}
impl_->ClearAttacher(attacher); impl_->ClearAttacher(attacher);
} }
void XTimer::DestoryAttacher(Attacher* attacher) void XTimer::DestoryAttacher(Attacher* attacher)
{ {
if (attacher->owner_ != this) {
abort();
}
impl_->DestoryAttacher(attacher); impl_->DestoryAttacher(attacher);
} }