1
This commit is contained in:
parent
358c62df57
commit
e4a9fab0ef
13
a8/ioloop.cc
13
a8/ioloop.cc
@ -265,19 +265,20 @@ namespace a8
|
|||||||
AsyncTcpClient* client = (AsyncTcpClient*)param.sender.GetUserData();
|
AsyncTcpClient* client = (AsyncTcpClient*)param.sender.GetUserData();
|
||||||
client->DoAsyncConnect();
|
client->DoAsyncConnect();
|
||||||
client->connect_timer_attacher.ClearTimerList();
|
client->connect_timer_attacher.ClearTimerList();
|
||||||
context->xtimer.AddDeadLineTimerAndAttach(
|
context->xtimer.AddDeadLineTimer
|
||||||
|
(
|
||||||
param.param1,
|
param.param1,
|
||||||
a8::XParams()
|
a8::Args({}),
|
||||||
.SetSender(client),
|
[client] (int event, const a8::Args& args, const a8::Args* user_args)
|
||||||
[] (const a8::XParams& param)
|
|
||||||
{
|
{
|
||||||
AsyncTcpClient* client = (AsyncTcpClient*)param.sender.GetUserData();
|
if (a8::TIMER_EXEC_EVENT == event) {
|
||||||
client->DoAsyncClose();
|
client->DoAsyncClose();
|
||||||
if (client->on_error) {
|
if (client->on_error) {
|
||||||
client->on_error(client, 111);
|
client->on_error(client, 111);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
&client->connect_timer_attacher.timer_list_
|
&client->connect_timer_attacher
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,12 +40,14 @@ namespace a8
|
|||||||
|
|
||||||
void XTimerAttacher::ClearTimerList()
|
void XTimerAttacher::ClearTimerList()
|
||||||
{
|
{
|
||||||
|
#if 0
|
||||||
struct list_head* pos = nullptr;
|
struct list_head* pos = nullptr;
|
||||||
struct list_head* n = nullptr;
|
struct list_head* n = nullptr;
|
||||||
list_for_each_safe(pos, n, &timer_list_){
|
list_for_each_safe(pos, n, &timer_list_){
|
||||||
xtimer_list* tmp_timer = xtimer->GetTimerByAttach(pos);
|
xtimer_list* tmp_timer = xtimer->GetTimerByAttach(pos);
|
||||||
xtimer->DeleteTimer(tmp_timer, is_destory_);
|
xtimer->DeleteTimer(tmp_timer, is_destory_);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ namespace a8
|
|||||||
const int INVALID_FD = -1;
|
const int INVALID_FD = -1;
|
||||||
const int INVALID_SOCKET = -1;
|
const int INVALID_SOCKET = -1;
|
||||||
const int INVALID_SOCKET_HANDLE = 0;
|
const int INVALID_SOCKET_HANDLE = 0;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
145
a8/xtimer.cc
145
a8/xtimer.cc
@ -41,9 +41,8 @@ struct xtimer_list {
|
|||||||
int fixed_timer_execute_times;
|
int fixed_timer_execute_times;
|
||||||
struct xtvec_base *base;
|
struct xtvec_base *base;
|
||||||
|
|
||||||
a8::XTimerFunc timer_func;
|
a8::TimerCbProc cb;
|
||||||
a8::XTimerAfterFunc timer_after_func;
|
a8::Args args;
|
||||||
a8::XParams param;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct XTimerDestoryHandleNode
|
struct XTimerDestoryHandleNode
|
||||||
@ -104,8 +103,8 @@ static inline int DetachTimer(struct xtimer_list *timer)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static inline void InitTimerList(xtvec_base* base, xtimer_list* timer, int timer_type,
|
static inline void InitTimerList(xtvec_base* base, xtimer_list* timer, int timer_type,
|
||||||
long long expires, int expire_time, a8::XParams& param,
|
long long expires, int expire_time,
|
||||||
a8::XTimerFunc timer_func, a8::XTimerAfterFunc timer_after_func)
|
a8::Args args, a8::TimerCbProc cb)
|
||||||
{
|
{
|
||||||
INIT_LIST_HEAD(&timer->destory_handle_list);
|
INIT_LIST_HEAD(&timer->destory_handle_list);
|
||||||
INIT_LIST_HEAD(&timer->entry);
|
INIT_LIST_HEAD(&timer->entry);
|
||||||
@ -115,9 +114,8 @@ static inline void InitTimerList(xtvec_base* base, xtimer_list* timer, int timer
|
|||||||
timer->expire_time = expire_time;
|
timer->expire_time = expire_time;
|
||||||
timer->fixed_timer_execute_times = 0;
|
timer->fixed_timer_execute_times = 0;
|
||||||
timer->base = base;
|
timer->base = base;
|
||||||
timer->timer_func = timer_func;
|
timer->cb = std::move(cb);
|
||||||
timer->timer_after_func = timer_after_func;
|
timer->args = std::move(args);
|
||||||
timer->param = param;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace a8
|
namespace a8
|
||||||
@ -158,12 +156,25 @@ namespace a8
|
|||||||
context_ = context;
|
context_ = context;
|
||||||
gc_time_ = gc_time;
|
gc_time_ = gc_time;
|
||||||
cache_timer_num_ = cache_timer_num;
|
cache_timer_num_ = cache_timer_num;
|
||||||
#if 1
|
|
||||||
base_->timer_tick = get_tick_count_func_(context_);
|
base_->timer_tick = get_tick_count_func_(context_);
|
||||||
#endif
|
AddRepeatTimer
|
||||||
AddRepeatTimer(gc_time_,
|
(gc_time_,
|
||||||
a8::XParams().SetSender(this),
|
a8::Args({}),
|
||||||
&XTimer::GC_XTimerFunc);
|
[this] (int event, const a8::Args& args, const a8::Args* user_args)
|
||||||
|
{
|
||||||
|
if (a8::TIMER_EXEC_EVENT == event) {
|
||||||
|
int i = 0;
|
||||||
|
while (!list_empty(&base_->free_timer) &&
|
||||||
|
base_->free_timer_num > cache_timer_num_ && i < 1000) {
|
||||||
|
xtimer_list* timer = list_first_entry(&base_->free_timer, struct xtimer_list, entry);
|
||||||
|
list_del_init(&timer->entry);
|
||||||
|
delete timer;
|
||||||
|
|
||||||
|
base_->free_timer_num--;
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void XTimer::Update()
|
void XTimer::Update()
|
||||||
@ -173,6 +184,98 @@ namespace a8
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if 1
|
||||||
|
|
||||||
|
void XTimer::AddDeadLineTimer(
|
||||||
|
int expire_time,
|
||||||
|
a8::Args args,
|
||||||
|
a8::TimerCbProc timer_cb,
|
||||||
|
a8::XTimerAttacher* attacher
|
||||||
|
)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
std::weak_ptr<XTimerPtr> XTimer::AddDeadLineTimerEx(
|
||||||
|
int expire_time,
|
||||||
|
a8::Args args,
|
||||||
|
a8::TimerCbProc timer_cb,
|
||||||
|
a8::XTimerAttacher* attacher
|
||||||
|
)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void XTimer::AddRepeatTimer(
|
||||||
|
int expire_time,
|
||||||
|
a8::Args args,
|
||||||
|
a8::TimerCbProc timer_cb,
|
||||||
|
a8::XTimerAttacher* attacher)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
std::weak_ptr<XTimerPtr> XTimer::AddRepeatTimerEx(
|
||||||
|
int expire_time,
|
||||||
|
a8::Args args,
|
||||||
|
a8::TimerCbProc timer_cb,
|
||||||
|
a8::XTimerAttacher* attacher)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void XTimer::FireUserEvent(std::weak_ptr<XTimerPtr>& timer_ptr, a8::Args& args)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void XTimer::ModifyTimer(std::weak_ptr<XTimerPtr>& timer_ptr, int expire_time)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void XTimer::DeleteTimer(std::weak_ptr<XTimerPtr>& timer_ptr)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
long long XTimer::GetRemainTime(std::weak_ptr<XTimerPtr>& timer_ptr)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void XTimer::DestoryTimer(std::weak_ptr<XTimerPtr>& timer_ptr)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
xtimer_list* XTimer::GetTimerByAttach(list_head* attach_entry)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
xtimer_list* XTimer::GetRunningTimer()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
XTimerDestoryHandle* XTimer::AddTimerDestoryHandle(std::weak_ptr<XTimerPtr>& timer_ptr,
|
||||||
|
std::function<void(xtimer_list*)> cb)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void XTimer::RemoveTimerDestoryHandle(XTimerDestoryHandle* handle)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void XTimer::DeleteRunningTimer()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
xtimer_list* XTimer::AddDeadLineTimer(int expire_time, a8::XParams param, a8::XTimerFunc timer_func,
|
xtimer_list* XTimer::AddDeadLineTimer(int expire_time, a8::XParams param, a8::XTimerFunc timer_func,
|
||||||
a8::XTimerAfterFunc timer_after_func)
|
a8::XTimerAfterFunc timer_after_func)
|
||||||
{
|
{
|
||||||
@ -428,21 +531,6 @@ namespace a8
|
|||||||
free_timers(&base_->free_timer);
|
free_timers(&base_->free_timer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void XTimer::GC_XTimerFunc(const a8::XParams& param)
|
|
||||||
{
|
|
||||||
XTimer* timer = (XTimer*)param.sender.GetUserData();
|
|
||||||
int i = 0;
|
|
||||||
while (!list_empty(&timer->base_->free_timer) &&
|
|
||||||
timer->base_->free_timer_num > timer->cache_timer_num_ && i < 1000) {
|
|
||||||
xtimer_list* timer1 = list_first_entry(&timer->base_->free_timer, struct xtimer_list, entry);
|
|
||||||
list_del_init(&timer1->entry);
|
|
||||||
delete timer1;
|
|
||||||
|
|
||||||
timer->base_->free_timer_num--;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
XTimerDestoryHandle* XTimer::AddTimerDestoryHandle(xtimer_list* timer,
|
XTimerDestoryHandle* XTimer::AddTimerDestoryHandle(xtimer_list* timer,
|
||||||
std::function<void(xtimer_list*)> cb)
|
std::function<void(xtimer_list*)> cb)
|
||||||
{
|
{
|
||||||
@ -482,5 +570,6 @@ namespace a8
|
|||||||
DeleteTimer(ptr.lock()->timer);
|
DeleteTimer(ptr.lock()->timer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
77
a8/xtimer.h
77
a8/xtimer.h
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* 仿照linux2.6内核定时器实现
|
* linux2.6内核定时器实现
|
||||||
*/
|
*/
|
||||||
#ifndef A8_XTIMER_H
|
#ifndef A8_XTIMER_H
|
||||||
#define A8_XTIMER_H
|
#define A8_XTIMER_H
|
||||||
@ -8,8 +8,14 @@ struct xtimer_list;
|
|||||||
struct xtvec_base;
|
struct xtvec_base;
|
||||||
namespace a8
|
namespace a8
|
||||||
{
|
{
|
||||||
typedef void (*XTimerFunc)(const a8::XParams& param);
|
|
||||||
typedef void (*XTimerAfterFunc)(const a8::XParams& param, bool is_destory);
|
const int TIMER_EXEC_EVENT = 1;
|
||||||
|
const int TIMER_DELETE_EVNET = 2;
|
||||||
|
const int TIMER_DESTORY_EVNET = 3;
|
||||||
|
const int TIMER_USER_EVNET = 66;
|
||||||
|
|
||||||
|
typedef std::function<void(int, const a8::Args&, const a8::Args*)> TimerCbProc;
|
||||||
|
|
||||||
typedef long long (*XGetTickCountFunc)(void*);
|
typedef long long (*XGetTickCountFunc)(void*);
|
||||||
typedef struct list_head XTimerDestoryHandle;
|
typedef struct list_head XTimerDestoryHandle;
|
||||||
struct XTimerPtr;
|
struct XTimerPtr;
|
||||||
@ -25,48 +31,63 @@ namespace a8
|
|||||||
void Update();
|
void Update();
|
||||||
|
|
||||||
//添加截止时间定时器(一次性定时器)
|
//添加截止时间定时器(一次性定时器)
|
||||||
xtimer_list* AddDeadLineTimer(int expire_time, a8::XParams param, a8::XTimerFunc timer_func,
|
void AddDeadLineTimer(
|
||||||
a8::XTimerAfterFunc timer_after_func = nullptr);
|
int expire_time,
|
||||||
xtimer_list* AddDeadLineTimerAndAttach(int expire_time, a8::XParams,
|
a8::Args args,
|
||||||
a8::XTimerFunc timer_func,
|
a8::TimerCbProc timer_cb,
|
||||||
list_head* attach_list,
|
a8::XTimerAttacher* attacher = nullptr
|
||||||
a8::XTimerAfterFunc timer_after_func = nullptr);
|
);
|
||||||
|
|
||||||
|
std::weak_ptr<XTimerPtr> AddDeadLineTimerEx(
|
||||||
|
int expire_time,
|
||||||
|
a8::Args args,
|
||||||
|
a8::TimerCbProc timer_cb,
|
||||||
|
a8::XTimerAttacher* attacher = nullptr
|
||||||
|
);
|
||||||
|
|
||||||
//添加重复执行定时器(周期性定时器)
|
//添加重复执行定时器(周期性定时器)
|
||||||
xtimer_list* AddRepeatTimer(int expire_time, a8::XParams param, a8::XTimerFunc timer_func,
|
void AddRepeatTimer(
|
||||||
a8::XTimerAfterFunc timer_after_func = nullptr);
|
int expire_time,
|
||||||
xtimer_list* AddRepeatTimerAndAttach(int expire_time, a8::XParams param, a8::XTimerFunc timer_func,
|
a8::Args args,
|
||||||
list_head* attach_list,
|
a8::TimerCbProc timer_cb,
|
||||||
a8::XTimerAfterFunc timer_after_func = nullptr);
|
a8::XTimerAttacher* attacher = nullptr);
|
||||||
|
|
||||||
|
std::weak_ptr<XTimerPtr> AddRepeatTimerEx(
|
||||||
|
int expire_time,
|
||||||
|
a8::Args args,
|
||||||
|
a8::TimerCbProc timer_cb,
|
||||||
|
a8::XTimerAttacher* attacher = nullptr);
|
||||||
|
|
||||||
|
//发送用户事件
|
||||||
|
void FireUserEvent(std::weak_ptr<XTimerPtr>& timer_ptr, a8::Args& args);
|
||||||
|
|
||||||
//修改定时器参数
|
//修改定时器参数
|
||||||
void ModifyTimer(xtimer_list* timer, int expire_time);
|
void ModifyTimer(std::weak_ptr<XTimerPtr>& timer_ptr, int expire_time);
|
||||||
void ModifyTimer(std::weak_ptr<XTimerPtr>& ptr, int expire_time);
|
|
||||||
//删除定时器
|
//删除定时器
|
||||||
void DeleteTimer(xtimer_list* timer, bool is_destory = false);
|
void DeleteTimer(std::weak_ptr<XTimerPtr>& timer_ptr);
|
||||||
|
//获取定时器剩余时间
|
||||||
|
long long GetRemainTime(std::weak_ptr<XTimerPtr>& timer_ptr);
|
||||||
|
//删除当前运行中定时器
|
||||||
|
void DeleteRunningTimer();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
//销毁定时器
|
||||||
|
void DestoryTimer(std::weak_ptr<XTimerPtr>& timer_ptr);
|
||||||
//通过关联的list_head获取定时器对象
|
//通过关联的list_head获取定时器对象
|
||||||
xtimer_list* GetTimerByAttach(list_head* attach_entry);
|
xtimer_list* GetTimerByAttach(list_head* attach_entry);
|
||||||
//获取定时器关联参数
|
|
||||||
a8::XParams* MutableParams(xtimer_list* timer);
|
|
||||||
a8::XParams* MutableParams(std::weak_ptr<XTimerPtr>& ptr);
|
|
||||||
//获取定时器剩余时间
|
|
||||||
long long GetRemainTime(xtimer_list* timer);
|
|
||||||
//获取当前正在运行的定时器
|
//获取当前正在运行的定时器
|
||||||
xtimer_list* GetRunningTimer();
|
xtimer_list* GetRunningTimer();
|
||||||
//添加定时器销毁事件
|
//添加定时器销毁事件
|
||||||
XTimerDestoryHandle* AddTimerDestoryHandle(xtimer_list* timer,
|
XTimerDestoryHandle* AddTimerDestoryHandle(std::weak_ptr<XTimerPtr>& timer_ptr,
|
||||||
std::function<void(xtimer_list*)> cb);
|
std::function<void(xtimer_list*)> cb);
|
||||||
//移除定时器销毁事件
|
//移除定时器销毁事件
|
||||||
void RemoveTimerDestoryHandle(XTimerDestoryHandle* handle);
|
void RemoveTimerDestoryHandle(XTimerDestoryHandle* handle);
|
||||||
//获取定时器弱引用
|
|
||||||
std::weak_ptr<XTimerPtr> GetTimerPtr(xtimer_list* timer);
|
|
||||||
//删除弱引用指向的定时器
|
|
||||||
void DeleteTimer(std::weak_ptr<XTimerPtr>& ptr);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void UpdateTimer();
|
void UpdateTimer();
|
||||||
xtimer_list* NewTimerList();
|
xtimer_list* NewTimerList();
|
||||||
void AddToFreeList(xtimer_list* timer);
|
void AddToFreeList(xtimer_list* timer);
|
||||||
void Clear();
|
void Clear();
|
||||||
static void GC_XTimerFunc(const a8::XParams& param);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
xtvec_base* base_ = nullptr;
|
xtvec_base* base_ = nullptr;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user