1#ifndef NEFORCE_CORE_ASYNC_TIMER_HPP__
2#define NEFORCE_CORE_ASYNC_TIMER_HPP__
18NEFORCE_BEGIN_NAMESPACE__
40template <
typename Clock>
45 using duration =
typename clock_type::duration;
66 node(
const node&) =
default;
67 node& operator=(
const node&) =
default;
68 node(node&&) =
default;
69 node& operator=(node&&) =
default;
75 if (expire < other.expire) {
78 if (expire > other.expire) {
106 while (!stopped_.
load()) {
109 if (nodes_.
empty()) {
111 if (stopped_.load()) {
117 while (!nodes_.empty() && nodes_.begin()->expire <= now) {
118 auto it = nodes_.begin();
119 node current_node = *it;
121 node_map_.erase(current_node.id);
124 if (!stopped_.load()) {
125 current_node.handler();
128 now = clock_type::now();
131 if (!nodes_.empty()) {
132 time_point next_expire = nodes_.begin()->expire;
133 cv_.wait_until(lock, next_expire);
145 thread_ =
thread(&timer_scheduler::run,
this);
152 stopped_.store(
true);
154 if (thread_.joinable()) {
174 token id = next_id_++;
176 const bool is_earliest = nodes_.empty() || expire < nodes_.begin()->expire;
178 node new_node(expire,
id, _NEFORCE
move(handler));
179 auto result = nodes_.insert(new_node);
180 node_map_[
id] = result.first;
200 auto it_map = node_map_.find(
id);
201 if (it_map == node_map_.end()) {
205 const bool is_earliest = (it_map->second == nodes_.begin());
206 nodes_.erase(it_map->second);
207 node_map_.erase(it_map);
233 NEFORCE_NODISCARD
size_t size()
const {
235 return nodes_.size();
247template <
typename Clock>
276 scheduler_(other.scheduler_),
277 task_id_(other.task_id_),
278 expire_(other.expire_) {
291 task_id_ = other.task_id_;
292 expire_ = other.expire_;
306 expire_ = expiry_time;
336 NEFORCE_NODISCARD
bool is_active()
const {
return task_id_ != 0; }
346 template <
typename WaitHandler>
359 scheduler_.cancel(task_id_);
379NEFORCE_END_NAMESPACE__
void expires_from_now(const int64_t ms)
设置从当前时间开始的毫秒数
NEFORCE_NODISCARD time_point expiry() const
获取到期时间点
void async_wait(WaitHandler &&handler)
异步等待定时器到期
void expires_after(const duration &expiry_duration)
设置相对到期时间
void expires_at(const time_point &expiry_time)
设置绝对到期时间
NEFORCE_NODISCARD bool is_active() const
检查定时器是否活跃(有待执行的任务)
typename timer_scheduler< Clock >::token token
任务标识符类型
typename timer_scheduler< Clock >::handler_type handler_type
回调函数类型
typename clock_type::duration duration
时长类型
basic_timer & operator=(basic_timer &&other) noexcept
移动赋值运算符
basic_timer(basic_timer &&other) noexcept
移动构造函数
~basic_timer()
析构函数,自动取消未完成的任务
typename clock_type::time_point time_point
时间点类型
void wait(unique_lock< mutex > &lock)
无限期等待
NEFORCE_NODISCARD bool empty() const noexcept
检查是否为空
typename clock_type::time_point time_point
时间点类型
timer_scheduler()
构造函数,启动调度线程
~timer_scheduler()
析构函数,停止调度线程并等待其结束
typename clock_type::duration duration
时长类型
NEFORCE_NODISCARD size_t size() const
获取当前待处理的任务数量
bool cancel(token id)
取消定时任务
void cancel_all()
取消所有定时任务
token add_task(time_point expire, handler_type &&handler)
添加定时任务
function< void()> handler_type
回调函数类型
NEFORCE_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
NEFORCE_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
basic_timer< system_clock > system_timer
基于系统时钟的定时器
basic_timer< steady_clock > steady_timer
基于稳定时钟的定时器
long long int64_t
64位有符号整数类型
duration< int64_t, milli > milliseconds
毫秒持续时间
NEFORCE_NODISCARD constexpr bool operator<(const normal_iterator< LeftIter > &lhs, const normal_iterator< RightIter > &rhs) noexcept
小于比较运算符
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
NEFORCE_ALWAYS_INLINE_INLINE thread::id id() noexcept
获取当前线程标识符
T load(const memory_order mo=memory_order_seq_cst) const noexcept
原子加载操作
static time_point now() noexcept
获取当前时间点