mirror of
https://gitee.com/fasiondog/hikyuu.git
synced 2024-12-02 20:08:26 +08:00
TimerManager增加移除定时任务操作
This commit is contained in:
parent
8410e99d6a
commit
9faf8d35d0
@ -124,7 +124,7 @@ public:
|
|||||||
|
|
||||||
// 清除已无效的 timer
|
// 清除已无效的 timer
|
||||||
for (auto id : invalid_timers) {
|
for (auto id : invalid_timers) {
|
||||||
removeTimer(id);
|
_removeTimer(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
@ -162,10 +162,11 @@ public:
|
|||||||
* @param delay 间隔时间,需大于 TimeDelta(0)
|
* @param delay 间隔时间,需大于 TimeDelta(0)
|
||||||
* @param f 待执行的延迟任务
|
* @param f 待执行的延迟任务
|
||||||
* @param args 任务具体参数
|
* @param args 任务具体参数
|
||||||
|
* @return timer id
|
||||||
*/
|
*/
|
||||||
template <typename F, typename... Args>
|
template <typename F, typename... Args>
|
||||||
void addFunc(Datetime start_date, Datetime end_date, TimeDelta start_time, TimeDelta end_time,
|
int addFunc(Datetime start_date, Datetime end_date, TimeDelta start_time, TimeDelta end_time,
|
||||||
int repeat_num, TimeDelta duration, F&& f, Args&&... args) {
|
int repeat_num, TimeDelta duration, F&& f, Args&&... args) {
|
||||||
HKU_CHECK(!start_date.isNull(), "Invalid start_date!");
|
HKU_CHECK(!start_date.isNull(), "Invalid start_date!");
|
||||||
HKU_CHECK(!end_date.isNull(), "Invalid end_date!");
|
HKU_CHECK(!end_date.isNull(), "Invalid end_date!");
|
||||||
Datetime start = start_date.startOfDay();
|
Datetime start = start_date.startOfDay();
|
||||||
@ -191,15 +192,15 @@ public:
|
|||||||
* @param delay 间隔时间,需大于 TimeDelta(0)
|
* @param delay 间隔时间,需大于 TimeDelta(0)
|
||||||
* @param f 待执行的延迟任务
|
* @param f 待执行的延迟任务
|
||||||
* @param args 任务具体参数
|
* @param args 任务具体参数
|
||||||
* @return true 成功 | false 失败
|
* @return timer id
|
||||||
*/
|
*/
|
||||||
template <typename F, typename... Args>
|
template <typename F, typename... Args>
|
||||||
void addDurationFunc(int repeat_num, TimeDelta duration, F&& f, Args&&... args) {
|
int addDurationFunc(int repeat_num, TimeDelta duration, F&& f, Args&&... args) {
|
||||||
HKU_CHECK(repeat_num > 0, "Invalid repeat_num: {}, must > 0", repeat_num);
|
HKU_CHECK(repeat_num > 0, "Invalid repeat_num: {}, must > 0", repeat_num);
|
||||||
HKU_CHECK(duration > TimeDelta(), "Invalid duration: {}, must > TimeDelta(0)!",
|
HKU_CHECK(duration > TimeDelta(), "Invalid duration: {}, must > TimeDelta(0)!",
|
||||||
duration.repr());
|
duration.repr());
|
||||||
_addFunc(Datetime::min(), Datetime::max(), TimeDelta(), TimeDelta(), repeat_num, duration,
|
return _addFunc(Datetime::min(), Datetime::max(), TimeDelta(), TimeDelta(), repeat_num,
|
||||||
std::forward<F>(f), std::forward<Args>(args)...);
|
duration, std::forward<F>(f), std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -209,12 +210,13 @@ public:
|
|||||||
* @param delay 延迟时间,需大于 TimeDelta(0)
|
* @param delay 延迟时间,需大于 TimeDelta(0)
|
||||||
* @param f 待执行的延迟任务
|
* @param f 待执行的延迟任务
|
||||||
* @param args 任务具体参数
|
* @param args 任务具体参数
|
||||||
|
* @return timer id
|
||||||
*/
|
*/
|
||||||
template <typename F, typename... Args>
|
template <typename F, typename... Args>
|
||||||
void addDelayFunc(TimeDelta delay, F&& f, Args&&... args) {
|
int addDelayFunc(TimeDelta delay, F&& f, Args&&... args) {
|
||||||
HKU_CHECK(delay > TimeDelta(), "Invalid delay: {}, must > TimeDelta(0)!", delay);
|
HKU_CHECK(delay > TimeDelta(), "Invalid delay: {}, must > TimeDelta(0)!", delay);
|
||||||
_addFunc(Datetime::min(), Datetime::max(), TimeDelta(), TimeDelta(), 1, delay,
|
return _addFunc(Datetime::min(), Datetime::max(), TimeDelta(), TimeDelta(), 1, delay,
|
||||||
std::forward<F>(f), std::forward<Args>(args)...);
|
std::forward<F>(f), std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -222,15 +224,16 @@ public:
|
|||||||
* @tparam F 任务类型
|
* @tparam F 任务类型
|
||||||
* @tparam Args 任务参数
|
* @tparam Args 任务参数
|
||||||
* @param time_point 指定的运行时刻(包含具体的日、时、分、秒...)
|
* @param time_point 指定的运行时刻(包含具体的日、时、分、秒...)
|
||||||
|
* @return timer id
|
||||||
*/
|
*/
|
||||||
template <typename F, typename... Args>
|
template <typename F, typename... Args>
|
||||||
void addFuncAtTime(Datetime time_point, F&& f, Args&&... args) {
|
int addFuncAtTime(Datetime time_point, F&& f, Args&&... args) {
|
||||||
Datetime now = Datetime::now();
|
Datetime now = Datetime::now();
|
||||||
HKU_CHECK(time_point > now, "You want run at {}, but now is {}", time_point, now);
|
HKU_CHECK(time_point > now, "You want run at {}, but now is {}", time_point, now);
|
||||||
Datetime point_date = time_point.startOfDay();
|
Datetime point_date = time_point.startOfDay();
|
||||||
TimeDelta point = time_point - point_date;
|
TimeDelta point = time_point - point_date;
|
||||||
_addFunc(time_point.startOfDay(), Datetime::max(), TimeDelta(-1), point, 1, TimeDelta(),
|
return _addFunc(time_point.startOfDay(), Datetime::max(), TimeDelta(-1), point, 1,
|
||||||
std::forward<F>(f), std::forward<Args>(args)...);
|
TimeDelta(), std::forward<F>(f), std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -240,10 +243,11 @@ public:
|
|||||||
* @param start_date 允许执行的开始日期
|
* @param start_date 允许执行的开始日期
|
||||||
* @param end_date 允许执行的结束日期
|
* @param end_date 允许执行的结束日期
|
||||||
* @param time 指定运行的日内时刻
|
* @param time 指定运行的日内时刻
|
||||||
|
* @return timer id
|
||||||
*/
|
*/
|
||||||
template <typename F, typename... Args>
|
template <typename F, typename... Args>
|
||||||
void addFuncAtTimeEveryDay(Datetime start_date, Datetime end_date, TimeDelta time, F&& f,
|
int addFuncAtTimeEveryDay(Datetime start_date, Datetime end_date, TimeDelta time, F&& f,
|
||||||
Args&&... args) {
|
Args&&... args) {
|
||||||
HKU_CHECK(!start_date.isNull() && !end_date.isNull(),
|
HKU_CHECK(!start_date.isNull() && !end_date.isNull(),
|
||||||
"Invalid start_date({}) or end_date({})!", start_date, end_date);
|
"Invalid start_date({}) or end_date({})!", start_date, end_date);
|
||||||
HKU_CHECK(time >= TimeDelta() && time <= TimeDelta(0, 23, 59, 59, 999, 999),
|
HKU_CHECK(time >= TimeDelta() && time <= TimeDelta(0, 23, 59, 59, 999, 999),
|
||||||
@ -251,9 +255,9 @@ public:
|
|||||||
Datetime start = start_date.startOfDay();
|
Datetime start = start_date.startOfDay();
|
||||||
Datetime end = end_date.startOfDay();
|
Datetime end = end_date.startOfDay();
|
||||||
HKU_CHECK(end >= start, "Invalid range of date! ({} - {})", start, end);
|
HKU_CHECK(end >= start, "Invalid range of date! ({} - {})", start, end);
|
||||||
_addFunc(Datetime::min(), Datetime::max(), TimeDelta(-1), time,
|
return _addFunc(Datetime::min(), Datetime::max(), TimeDelta(-1), time,
|
||||||
std::numeric_limits<int>::max(), TimeDelta(), std::forward<F>(f),
|
std::numeric_limits<int>::max(), TimeDelta(), std::forward<F>(f),
|
||||||
std::forward<Args>(args)...);
|
std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -261,15 +265,28 @@ public:
|
|||||||
* @tparam F 任务类型
|
* @tparam F 任务类型
|
||||||
* @tparam Args 任务参数
|
* @tparam Args 任务参数
|
||||||
* @param time 指定运行的日内时刻
|
* @param time 指定运行的日内时刻
|
||||||
|
* @return timer id
|
||||||
*/
|
*/
|
||||||
template <typename F, typename... Args>
|
template <typename F, typename... Args>
|
||||||
void addFuncAtTimeEveryDay(TimeDelta time, F&& f, Args&&... args) {
|
int addFuncAtTimeEveryDay(TimeDelta time, F&& f, Args&&... args) {
|
||||||
addFuncAtTimeEveryDay(Datetime::min(), Datetime::max(), time, std::forward<F>(f),
|
return addFuncAtTimeEveryDay(Datetime::min(), Datetime::max(), time, std::forward<F>(f),
|
||||||
std::forward<Args>(args)...);
|
std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 移除定时任务
|
||||||
|
* @param timerid 定时器id
|
||||||
|
*/
|
||||||
|
void removeTimer(int timerid) {
|
||||||
|
std::unique_lock<std::mutex> lock(m_mutex);
|
||||||
|
auto iter = m_timers.find(timerid);
|
||||||
|
if (iter != m_timers.end()) {
|
||||||
|
iter->second->m_repeat_num = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void removeTimer(int id) {
|
void _removeTimer(int id) {
|
||||||
delete m_timers[id];
|
delete m_timers[id];
|
||||||
m_timers.erase(id);
|
m_timers.erase(id);
|
||||||
}
|
}
|
||||||
@ -308,7 +325,7 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (timer->m_repeat_num <= 0) {
|
if (timer->m_repeat_num <= 0) {
|
||||||
removeTimer(s.m_timer_id);
|
_removeTimer(s.m_timer_id);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -317,7 +334,7 @@ private:
|
|||||||
: s.m_time_point + TimeDelta(1);
|
: s.m_time_point + TimeDelta(1);
|
||||||
if (timer->m_end_date != Datetime::max() &&
|
if (timer->m_end_date != Datetime::max() &&
|
||||||
s.m_time_point > timer->m_end_date + timer->m_end_time) {
|
s.m_time_point > timer->m_end_date + timer->m_end_time) {
|
||||||
removeTimer(s.m_timer_id);
|
_removeTimer(s.m_timer_id);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -384,8 +401,8 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <typename F, typename... Args>
|
template <typename F, typename... Args>
|
||||||
void _addFunc(Datetime start_date, Datetime end_date, TimeDelta start_time, TimeDelta end_time,
|
int _addFunc(Datetime start_date, Datetime end_date, TimeDelta start_time, TimeDelta end_time,
|
||||||
int repeat_num, TimeDelta duration, F&& f, Args&&... args) {
|
int repeat_num, TimeDelta duration, F&& f, Args&&... args) {
|
||||||
Datetime now = Datetime::now();
|
Datetime now = Datetime::now();
|
||||||
Datetime today = now.startOfDay();
|
Datetime today = now.startOfDay();
|
||||||
HKU_CHECK(end_date >= today, "Invalid end_date {}, because today is {}", end_date, today);
|
HKU_CHECK(end_date >= today, "Invalid end_date {}, because today is {}", end_date, today);
|
||||||
@ -453,6 +470,7 @@ private:
|
|||||||
m_queue.push(s);
|
m_queue.push(s);
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
m_cond.notify_all();
|
m_cond.notify_all();
|
||||||
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Loading…
Reference in New Issue
Block a user