xtimer add destory handle
This commit is contained in:
parent
8932713766
commit
5e0a939a89
52
a8/xtimer.cc
52
a8/xtimer.cc
@ -32,6 +32,7 @@ struct xtvec_base {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct xtimer_list {
|
struct xtimer_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
|
int timer_type; // 0:deadline 1: repeattimer 2:fixedtimer outher:deadline
|
||||||
@ -45,6 +46,12 @@ struct xtimer_list {
|
|||||||
a8::XParams param;
|
a8::XParams param;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct XTimerDestoryHandleNode
|
||||||
|
{
|
||||||
|
struct list_head entry;
|
||||||
|
std::function<void(xtimer_list*)> cb;
|
||||||
|
};
|
||||||
|
|
||||||
static void InternalAddXTimer(struct xtvec_base *base, struct xtimer_list *timer)
|
static void InternalAddXTimer(struct xtvec_base *base, struct xtimer_list *timer)
|
||||||
{
|
{
|
||||||
long long expires = timer->expires;
|
long long expires = timer->expires;
|
||||||
@ -100,6 +107,7 @@ static inline void InitTimerList(xtvec_base* base, xtimer_list* timer, int timer
|
|||||||
long long expires, int expire_time, a8::XParams& param,
|
long long expires, int expire_time, a8::XParams& param,
|
||||||
a8::XTimerFunc timer_func, a8::XTimerAfterFunc timer_after_func)
|
a8::XTimerFunc timer_func, a8::XTimerAfterFunc timer_after_func)
|
||||||
{
|
{
|
||||||
|
INIT_LIST_HEAD(&timer->destory_handle_list);
|
||||||
INIT_LIST_HEAD(&timer->entry);
|
INIT_LIST_HEAD(&timer->entry);
|
||||||
INIT_LIST_HEAD(&timer->attach_entry);
|
INIT_LIST_HEAD(&timer->attach_entry);
|
||||||
timer->timer_type = timer_type;
|
timer->timer_type = timer_type;
|
||||||
@ -243,6 +251,14 @@ namespace a8
|
|||||||
if (!list_empty(&timer->attach_entry)) {
|
if (!list_empty(&timer->attach_entry)) {
|
||||||
list_del_init(&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);
|
AddToFreeList(timer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -318,6 +334,14 @@ namespace a8
|
|||||||
if (!list_empty(&timer->attach_entry)) {
|
if (!list_empty(&timer->attach_entry)) {
|
||||||
list_del_init(&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);
|
AddToFreeList(timer);
|
||||||
if (timer->timer_after_func) {
|
if (timer->timer_after_func) {
|
||||||
timer->timer_after_func(timer->param);
|
timer->timer_after_func(timer->param);
|
||||||
@ -361,6 +385,14 @@ namespace a8
|
|||||||
if (!list_empty(&timer->attach_entry)) {
|
if (!list_empty(&timer->attach_entry)) {
|
||||||
list_del_init(&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;
|
||||||
|
}
|
||||||
delete timer;
|
delete timer;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -391,4 +423,24 @@ namespace a8
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
XTimerDestoryHandle* XTimer::AddTimerDestoryHandle(xtimer_list* timer,
|
||||||
|
std::function<void(xtimer_list*)> cb)
|
||||||
|
{
|
||||||
|
XTimerDestoryHandleNode* node = new XTimerDestoryHandleNode;
|
||||||
|
node->cb = cb;
|
||||||
|
list_add_tail(&node->entry, &timer->destory_handle_list);
|
||||||
|
return &node->entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
void XTimer::RemoveTimerDestoryHandle(XTimerDestoryHandle* handle)
|
||||||
|
{
|
||||||
|
if (!handle || list_empty(handle)) {
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
XTimerDestoryHandleNode* node = list_entry(handle, XTimerDestoryHandleNode, entry);
|
||||||
|
list_del_init(&node->entry);
|
||||||
|
delete node;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,12 +6,12 @@
|
|||||||
|
|
||||||
struct xtimer_list;
|
struct xtimer_list;
|
||||||
struct xtvec_base;
|
struct xtvec_base;
|
||||||
|
|
||||||
namespace a8
|
namespace a8
|
||||||
{
|
{
|
||||||
typedef void (*XTimerFunc)(const a8::XParams& param);
|
typedef void (*XTimerFunc)(const a8::XParams& param);
|
||||||
typedef void (*XTimerAfterFunc)(const a8::XParams& param);
|
typedef void (*XTimerAfterFunc)(const a8::XParams& param);
|
||||||
typedef long long (*XGetTickCountFunc)(void*);
|
typedef long long (*XGetTickCountFunc)(void*);
|
||||||
|
typedef struct list_head XTimerDestoryHandle;
|
||||||
|
|
||||||
class XTimer
|
class XTimer
|
||||||
{
|
{
|
||||||
@ -48,6 +48,11 @@ namespace a8
|
|||||||
long long GetRemainTime(xtimer_list* timer);
|
long long GetRemainTime(xtimer_list* timer);
|
||||||
//获取当前正在运行的定时器
|
//获取当前正在运行的定时器
|
||||||
xtimer_list* GetRunningTimer();
|
xtimer_list* GetRunningTimer();
|
||||||
|
//添加定时器销毁事件
|
||||||
|
XTimerDestoryHandle* AddTimerDestoryHandle(xtimer_list* timer,
|
||||||
|
std::function<void(xtimer_list*)> cb);
|
||||||
|
//移除定时器销毁事件
|
||||||
|
void RemoveTimerDestoryHandle(XTimerDestoryHandle* handle);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void UpdateTimer();
|
void UpdateTimer();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user