1#ifndef NEFORCE_CORE_ASYNC_IO_CONTEXT_HPP__
2#define NEFORCE_CORE_ASYNC_IO_CONTEXT_HPP__
25NEFORCE_BEGIN_NAMESPACE__
74#ifdef NEFORCE_PLATFORM_WINDOWS
207 friend class file_async;
216 bool operator>(
const timer_entry& other)
const {
return deadline_ms > other.deadline_ms; }
221 native_handle_type fd;
223 fd_callback callback;
227 atomic<int> running_{0};
229 atomic<bool> stopped_{
false};
231 atomic<size_t> outstanding_work_{0};
233 size_t next_timer_id_{1};
236 flat_unordered_map<native_handle_type, fd_info> fd_map_;
238 vector<timer_entry> timer_heap_;
240 mutable mutex timer_mutex_;
243 lock_free_queue<handler_type> external_queue_;
245 atomic<size_t> external_queue_count_{0};
247#ifdef NEFORCE_PLATFORM_WINDOWS
253 flat_unordered_map<native_handle_type, void*> fd_events_;
257 thread monitor_thread_;
259 atomic<bool> monitor_running_{
false};
262 flat_unordered_map<uintptr_t, file_completion_cb> file_completions_;
266 void register_file_completion(uintptr_t key, file_completion_cb cb);
267 void unregister_file_completion(uintptr_t key);
276 vector<thread> pool_threads_;
281 uint64_t next_timer_deadline()
const;
285 void process_timers(
size_t max_count = numeric_traits<size_t>::max());
292 size_t drain_handlers(
size_t max_count = 256);
304 executor(
const executor& other)
noexcept =
default;
305 executor& operator=(
const executor& other)
noexcept =
default;
315 NEFORCE_NODISCARD io_context&
context() const noexcept {
return *ctx_; }
320 bool operator==(
const executor& other)
const noexcept {
return ctx_ == other.ctx_; }
325 bool operator!=(
const executor& other)
const noexcept {
return ctx_ != other.ctx_; }
365 work& operator=(
const work&) =
delete;
372NEFORCE_END_NAMESPACE__
void execute(handler_type handler) const
在关联的 io_context 上投递 handler
io_context & context() const noexcept
获取关联的 io_context
bool operator!=(const executor &other) const noexcept
不等比较
bool operator==(const executor &other) const noexcept
相等比较 — 同一 io_context 的执行器相等
阻止 io_context::run() 提前退出的守卫
work(io_context &ctx)
构造函数 — 增加 io_context 的工作计数
void run_pool(size_t n)
多线程并发驱动事件循环
size_t run_one(int timeout_ms=-1)
等待并执行一个就绪的 handler
size_t poll()
执行所有已就绪的 handler 后立即返回
function< void()> timer_callback
定时器到期回调
uintptr_t native_handle_type
平台原生句柄类型
bool cancel_timer(size_t timer_id)
取消待执行的定时器
void remove_fd(native_handle_type fd)
取消 fd 的事件监控
void post(handler_type handler)
将 handler 投递到队列末尾
void mod_fd(native_handle_type fd, uint32_t events, bool edge_triggered=true)
修改已注册 fd 的事件掩码
size_t schedule_timer(uint64_t delay_ms, timer_callback handler)
调度一次性定时器
io_context()
构造 io_context
void restart()
重置停止标志,允许再次 run()
void add_fd(native_handle_type fd, uint32_t events, fd_callback cb, bool edge_triggered=true)
注册 fd 进行事件监控
function< void(int fd, uint32_t events, error_code ec)> fd_callback
fd 事件回调,参数为 (fd, events, error_code)
function< void()> handler_type
通用 handler 类型
executor get_executor() noexcept
获取默认执行器
void dispatch(handler_type handler)
若当前在 io_context 线程上则立即执行 handler,否则 post
bool stopped() const noexcept
检查是否已停止
unsigned int uint32_t
32位无符号整数类型
unsigned long uint64_t
64位无符号整数类型
constexpr uint32_t epoll_out
监控可写事件
constexpr uint32_t epoll_et
边沿触发模式
constexpr uint32_t epoll_in
监控可读事件
constexpr auto memory_order_release
释放内存顺序常量
constexpr auto memory_order_acquire
获取内存顺序常量
constexpr auto memory_order_relaxed
宽松内存顺序常量
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素