|
VLink 2.0.0
A high-performance communication middleware
|
Single-threaded event loop with three queue types, timer management and task scheduling. 更多...
#include <functional>#include <future>#include <limits>#include <memory>#include <string>#include <type_traits>#include <utility>#include "./schedule.h"#include "./timer.h"类 | |
| class | vlink::MessageLoop |
| Single-threaded serial task dispatcher with integrated timer support. 更多... | |
命名空间 | |
| namespace | vlink |
Single-threaded event loop with three queue types, timer management and task scheduling.
MessageLoop is the primary task dispatcher in VLink. It owns a task queue and an associated timer registry. Tasks posted via post_task() are executed serially on the loop's thread. Timers registered with Timer::attach() fire their callbacks as regular queue tasks.
Queue types:
| Type | Queue implementation | Max tasks | Notes |
|---|---|---|---|
kNormalType | Mutex-protected std::queue | 10000 | Default; no priority support |
kLockfreeType | MpmcQueue (lock-free MPMC) | 10000 | Fastest single-producer path |
kPriorityType | Priority queue | 10000 | Supports task priority levels |
Dispatch strategies:
| Strategy | Behaviour |
|---|---|
kOptimizationStrategy | Yields CPU when queue is empty; balances latency vs CPU usage |
kPopStrategy | Busy-waits by repeatedly polling the queue (lowest latency) |
kBlockStrategy | Blocks on a condition variable when the queue is empty (lowest CPU) |
Run modes:
run() – blocks the calling thread until quit() is called.async_run() – launches a new background thread and returns immediately.spin() – calls spin_once() in a loop; suitable for use in an existing event loop.spin_once() – processes one batch of pending tasks (optionally blocking).Task execution with scheduling: exec_task() wraps a callback in a Schedule::Config (delay, priority, timeouts) and posts it. It returns a Schedule::Status or Schedule::RetStatus that can be chained with on_then / on_else / on_catch / on_schedule_timeout callbacks.
kMaxTaskSize); posts beyond this fail silently.kMaxTimerSize).invoke_task() dispatches a callable and returns a std::future for the result. Blocking on the future from the same thread as the loop will deadlock.