VLink  2.0.0
A high-performance communication middleware
vlink::MessageLoop类 参考

Serial task dispatcher with selectable queue backend and bounded timer registry. 更多...

#include <message_loop.h>

类 vlink::MessageLoop 继承关系图:
vlink::MessageLoop 的协作图:

struct  AliveState
 Lifetime gate shared with cross-thread observers. 更多...
 

Public 类型

enum  Type : uint8_t { kNormalType = 0 , kLockfreeType = 1 , kPriorityType = 2 }
 Internal queue implementation type. 更多...
 
enum  Strategy : uint8_t { kOptimizationStrategy = 0 , kPopStrategy = 1 , kBlockStrategy = 2 }
 Back-pressure strategy applied when the bounded queue is at capacity. 更多...
 
enum  Priority : uint16_t {
  kNoPriority = 0 , kLowestPriority = 1 , kTimerPriority = 50 , kNormalPriority = 100 ,
  kHighestPriority = std::numeric_limits<uint16_t>::max()
}
 Built-in priority levels for kPriorityType loops; higher values dispatch first. 更多...
 
using Callback = MoveFunction< void()>
 Callback type for tasks and event handlers. 更多...
 

Public 成员函数

 MessageLoop ()
 Constructs a loop with the default kNormalType queue. 更多...
 
 MessageLoop (Type type)
 Constructs a loop with the given queue type. 更多...
 
virtual ~MessageLoop ()
 Destructor; requests quit and joins the dispatcher thread if needed. 更多...
 
void set_name (const std::string &name)
 Sets a human-readable name visible to profiling tools. 更多...
 
const std::string & get_name () const
 Returns the loop name set via set_name. 更多...
 
Type get_type () const
 Returns the queue type this loop was constructed with. 更多...
 
Strategy get_strategy () const
 Returns the active back-pressure strategy. 更多...
 
void set_strategy (Strategy strategy)
 Replaces the back-pressure strategy. 更多...
 
void register_begin_handler (Callback &&callback)
 Registers a callback fired once at loop thread startup. 更多...
 
void register_end_handler (Callback &&callback)
 Registers a callback fired once when the loop thread exits. 更多...
 
void register_idle_handler (Callback &&callback)
 Registers a callback fired every time the queue becomes empty. 更多...
 
bool run ()
 Runs the loop on the calling thread until quit is requested. 更多...
 
bool async_run ()
 Starts the loop on a new background thread. 更多...
 
bool spin ()
 Alias of run blocking the calling thread. 更多...
 
bool spin_once (bool block=true)
 Processes one batch of pending tasks and timers on the calling thread. 更多...
 
bool quit (bool force=false)
 Requests the loop to exit. 更多...
 
bool wait_for_quit (int ms=Timer::kInfinite, bool check=true)
 Waits until the loop has fully exited. 更多...
 
bool post_task (Callback &&callback)
 Posts a task for execution on the loop thread. 更多...
 
TaskHandle post_task_handle (Callback &&callback, const PostTaskOptions &options={})
 Tracked variant of post_task returning a TaskHandle. 更多...
 
bool post_task_with_priority (Callback &&callback, uint16_t priority)
 Posts a task with an explicit priority on a kPriorityType loop. 更多...
 
TaskHandle post_task_with_priority_handle (Callback &&callback, uint16_t priority, const PostTaskOptions &options={})
 Tracked variant of post_task_with_priority returning a TaskHandle. 更多...
 
template<typename CallbackT , typename = std::enable_if_t<!std::is_convertible_v<CallbackT, Schedule::RetCallback>>>
Schedule::Status exec_task (const Schedule::Config &config, CallbackT &&callback)
 Schedules a void-returning callable and returns a chainable Schedule::Status. 更多...
 
template<typename CallbackT , typename = std::enable_if_t<std::is_convertible_v<CallbackT, Schedule::RetCallback>>>
Schedule::RetStatus exec_task (const Schedule::Config &config, CallbackT &&callback)
 Schedules a bool-returning callable and returns a chainable Schedule::RetStatus. 更多...
 
bool wakeup ()
 Wakes the loop thread if it is suspended in its idle wait. 更多...
 
void reset_lockfree_capacity ()
 Recreates the lock-free queue, clearing all queued tasks and counters. 更多...
 
bool is_running () const
 Reports whether the loop is currently running. 更多...
 
bool is_ready_to_quit () const
 Reports whether quit has been requested and the loop is winding down. 更多...
 
bool is_busy () const
 Reports whether the loop is currently executing a task. 更多...
 
size_t get_task_count () const
 Returns the current pending task count. 更多...
 
virtual bool wait_for_idle (int ms=Timer::kInfinite, bool check=true)
 Waits until the loop has drained its queue and is not executing a task. 更多...
 
virtual size_t get_max_task_count () const
 Returns the maximum queue depth. 更多...
 
virtual size_t get_max_timer_count () const
 Returns the maximum number of timers that can be attached. 更多...
 
virtual uint32_t get_max_elapsed_time () const
 Returns the maximum allowed task execution time in milliseconds. 更多...
 
virtual bool is_in_same_thread () const
 Reports whether the calling thread is owned by this loop. 更多...
 
std::shared_ptr< AliveStateget_alive_state () const
 Returns the shared lifetime flag used by cross-thread bridges. 更多...
 
template<class FunctionT , class... ArgsT, typename ResultT = std::invoke_result_t<FunctionT, ArgsT...>>
std::future< ResultT > invoke_task (FunctionT &&function, ArgsT &&... args)
 Dispatches a callable to the loop thread and returns a std::future for the result. 更多...
 
template<class FunctionT , class... ArgsT, typename ResultT = std::invoke_result_t<FunctionT, ArgsT...>>
std::future< ResultT > invoke_task_with_priority (FunctionT &&function, uint16_t priority, ArgsT &&... args)
 Priority variant of invoke_task; requires a kPriorityType loop. 更多...
 

Protected 成员函数

virtual void on_begin ()
 Hook invoked once on the loop thread before the first task runs. 更多...
 
virtual void on_end ()
 Hook invoked once on the loop thread after the last task runs. 更多...
 
virtual void on_idle ()
 Hook invoked on the loop thread each time the queue becomes empty. 更多...
 
virtual void on_task_changed (Callback &&callback, uint32_t start_time)
 Dispatches a ready task on the loop thread. 更多...
 
virtual void on_task_timeout (Callback &&callback, uint32_t elapsed_time)
 Hook invoked when a task exceeds get_max_elapsed_time(). 更多...
 

详细描述

Serial task dispatcher with selectable queue backend and bounded timer registry.

Owns a thread that pulls tasks from a bounded queue and fires timer callbacks. Other threads may post via post_task, post_task_with_priority, invoke_task or exec_task from any context. The class is the building block of MultiLoop and the engine target of GraphTask::execute.

成员类型定义说明

◆ Callback

Callback type for tasks and event handlers.

Move-only so move-only targets such as std::packaged_task or lambdas holding std::unique_ptr can be posted directly without a shared trampoline.

成员枚举类型说明

◆ Priority

Built-in priority levels for kPriorityType loops; higher values dispatch first.

枚举值
kNoPriority 

FIFO sentinel.

kLowestPriority 

Lowest real priority.

kTimerPriority 

Reserved for timer callbacks.

kNormalPriority 

Default user-task priority.

kHighestPriority 

Highest priority.

◆ Strategy

Back-pressure strategy applied when the bounded queue is at capacity.

Idle dispatch is unconditionally cv-driven; this enum only affects the push side.

枚举值
kOptimizationStrategy 

Retry up to ten times, then drop one eligible task and push.

kPopStrategy 

Drop one eligible task immediately and push.

kBlockStrategy 

Retry indefinitely until space appears.

◆ Type

enum vlink::MessageLoop::Type : uint8_t

Internal queue implementation type.

枚举值
kNormalType 

Mutex-protected FIFO queue (default).

kLockfreeType 

Lock-free MPMC queue.

kPriorityType 

Priority-ordered queue with droppable / protected split.

构造及析构函数说明

◆ MessageLoop() [1/2]

vlink::MessageLoop::MessageLoop ( )

Constructs a loop with the default kNormalType queue.

◆ MessageLoop() [2/2]

vlink::MessageLoop::MessageLoop ( Type  type)
explicit

Constructs a loop with the given queue type.

参数
typeQueue implementation.

◆ ~MessageLoop()

virtual vlink::MessageLoop::~MessageLoop ( )
virtual

Destructor; requests quit and joins the dispatcher thread if needed.

成员函数说明

◆ async_run()

bool vlink::MessageLoop::async_run ( )

Starts the loop on a new background thread.

返回
true when the thread was started; false when the loop is already running.

◆ exec_task() [1/2]

template<typename CallbackT , typename >
Schedule::Status vlink::MessageLoop::exec_task ( const Schedule::Config config,
CallbackT &&  callback 
)

Schedules a void-returning callable and returns a chainable Schedule::Status.

Schedule::Config carries the delay, priority and timeout fields. The returned status supports on_catch / on_schedule_timeout / on_execution_timeout chaining. Posting is deferred until the handle is committed (when the temporary handle is destroyed at the end of the fluent expression), so every continuation is registered before the task runs. A caller that stores the handle instead of consuming it inline must call Schedule::Status::dispatch() to post the task and learn whether it was accepted.

模板参数
CallbackTCallable returning void.
参数
configScheduling envelope.
callbackCallable to schedule.
返回
Chainable status object.
函数调用图:

◆ exec_task() [2/2]

template<typename CallbackT , typename >
Schedule::RetStatus vlink::MessageLoop::exec_task ( const Schedule::Config config,
CallbackT &&  callback 
)

Schedules a bool-returning callable and returns a chainable Schedule::RetStatus.

on_then fires when the callable returns true; on_else fires when it returns false. Posting is deferred until the handle is committed (destroyed at the end of the fluent expression), guaranteeing every continuation is registered first; a stored handle must call Schedule::Status::dispatch() to post it.

模板参数
CallbackTCallable returning bool.
参数
configScheduling envelope.
callbackCallable to schedule.
返回
Chainable status object.
函数调用图:

◆ get_alive_state()

std::shared_ptr<AliveState> vlink::MessageLoop::get_alive_state ( ) const

Returns the shared lifetime flag used by cross-thread bridges.

The returned AliveState outlives this loop. Adapters that need to post continuations back to this loop should lock mtx, re-check alive, and only call back while still holding the lock.

返回
Shared handle; never null while the loop object is alive.

◆ get_max_elapsed_time()

virtual uint32_t vlink::MessageLoop::get_max_elapsed_time ( ) const
virtual

Returns the maximum allowed task execution time in milliseconds.

Tasks exceeding this duration trigger on_task_timeout. Zero disables the check.

返回
Maximum execution time in milliseconds.

vlink::ProxyServer, vlink::ProxyAPI, vlink::DiscoveryViewer , 以及 vlink::DiscoveryReporter 重载.

◆ get_max_task_count()

virtual size_t vlink::MessageLoop::get_max_task_count ( ) const
virtual

Returns the maximum queue depth.

返回
kMaxTaskSize (10000) by default.

vlink::ProxyServer, vlink::ProxyAPI, vlink::VDBWriter, vlink::VDBReader, vlink::VCAPWriter, vlink::VCAPReader, vlink::DiscoveryViewer , 以及 vlink::DiscoveryReporter 重载.

◆ get_max_timer_count()

virtual size_t vlink::MessageLoop::get_max_timer_count ( ) const
virtual

Returns the maximum number of timers that can be attached.

返回
kMaxTimerSize (100) by default.

◆ get_name()

const std::string& vlink::MessageLoop::get_name ( ) const

Returns the loop name set via set_name.

返回
Reference to the stored name.

◆ get_strategy()

Strategy vlink::MessageLoop::get_strategy ( ) const

Returns the active back-pressure strategy.

返回
Current strategy.

◆ get_task_count()

size_t vlink::MessageLoop::get_task_count ( ) const

Returns the current pending task count.

返回
Number of tasks waiting in the queue.

◆ get_type()

Type vlink::MessageLoop::get_type ( ) const

Returns the queue type this loop was constructed with.

返回
Queue type.

◆ invoke_task()

template<class FunctionT , class... ArgsT, typename ResultT >
std::future< ResultT > vlink::MessageLoop::invoke_task ( FunctionT &&  function,
ArgsT &&...  args 
)
inline

Dispatches a callable to the loop thread and returns a std::future for the result.

Thread-safe. When posting fails the returned future becomes ready with std::future_error / broken_promise.

警告
Do not call .get() on the future from the loop's own thread; doing so deadlocks.
模板参数
FunctionTCallable type.
ArgsTArgument types.
ResultTReturn type (deduced).
参数
functionCallable to dispatch.
argsArguments forwarded to function.
返回
Future that becomes ready after the callable completes.
函数调用图:

◆ invoke_task_with_priority()

template<class FunctionT , class... ArgsT, typename ResultT >
std::future< ResultT > vlink::MessageLoop::invoke_task_with_priority ( FunctionT &&  function,
uint16_t  priority,
ArgsT &&...  args 
)
inline

Priority variant of invoke_task; requires a kPriorityType loop.

模板参数
FunctionTCallable type.
ArgsTArgument types.
ResultTReturn type (deduced).
参数
functionCallable to dispatch.
priorityDispatch priority.
argsArguments forwarded to function.
返回
Future that becomes ready after the callable completes.
函数调用图:

◆ is_busy()

bool vlink::MessageLoop::is_busy ( ) const

Reports whether the loop is currently executing a task.

返回
true while a callback is on the call stack inside the loop.

◆ is_in_same_thread()

virtual bool vlink::MessageLoop::is_in_same_thread ( ) const
virtual

Reports whether the calling thread is owned by this loop.

Detects callbacks that re-enter the loop synchronously. For MultiLoop this also covers worker threads of the underlying pool.

返回
true when called from a thread belonging to this loop.

vlink::MultiLoop 重载.

◆ is_ready_to_quit()

bool vlink::MessageLoop::is_ready_to_quit ( ) const

Reports whether quit has been requested and the loop is winding down.

返回
true once quit has been observed.

◆ is_running()

bool vlink::MessageLoop::is_running ( ) const

Reports whether the loop is currently running.

返回
true between a successful start and the corresponding quit.

◆ on_begin()

virtual void vlink::MessageLoop::on_begin ( )
protectedvirtual

Hook invoked once on the loop thread before the first task runs.

Subclasses override to perform per-thread initialisation.

vlink::ProxyServer, vlink::ProxyAPI, vlink::VDBWriter, vlink::VDBReader, vlink::VCAPWriter, vlink::VCAPReader, vlink::DiscoveryViewer, vlink::DiscoveryReporter , 以及 vlink::MultiLoop 重载.

◆ on_end()

virtual void vlink::MessageLoop::on_end ( )
protectedvirtual

Hook invoked once on the loop thread after the last task runs.

Subclasses override to perform per-thread cleanup.

vlink::ProxyServer, vlink::ProxyAPI, vlink::VDBWriter, vlink::VDBReader, vlink::VCAPWriter, vlink::VCAPReader, vlink::DiscoveryViewer, vlink::DiscoveryReporter , 以及 vlink::MultiLoop 重载.

◆ on_idle()

virtual void vlink::MessageLoop::on_idle ( )
protectedvirtual

Hook invoked on the loop thread each time the queue becomes empty.

Subclasses override to perform idle bookkeeping.

◆ on_task_changed()

virtual void vlink::MessageLoop::on_task_changed ( Callback &&  callback,
uint32_t  start_time 
)
protectedvirtual

Dispatches a ready task on the loop thread.

The default implementation invokes callback inline. Subclasses such as MultiLoop override to redirect the call to a worker pool; overrides that do not forward must invoke callback themselves or the task is silently dropped.

参数
callbackTask to dispatch.
start_timeMillisecond steady_clock timestamp captured at enqueue time, or 0 when elapsed tracking is disabled.

vlink::MultiLoop 重载.

◆ on_task_timeout()

virtual void vlink::MessageLoop::on_task_timeout ( Callback &&  callback,
uint32_t  elapsed_time 
)
protectedvirtual

Hook invoked when a task exceeds get_max_elapsed_time().

参数
callbackTask that timed out.
elapsed_timeActual execution time in milliseconds.

◆ post_task()

bool vlink::MessageLoop::post_task ( Callback &&  callback)

Posts a task for execution on the loop thread.

Thread-safe. Returns false when the loop is quitting or the configured Strategy cannot make room for the new task.

参数
callbackTask to post.
返回
true when the task was enqueued.
这是这个函数的调用关系图:

◆ post_task_handle()

TaskHandle vlink::MessageLoop::post_task_handle ( Callback &&  callback,
const PostTaskOptions options = {} 
)

Tracked variant of post_task returning a TaskHandle.

参数
callbackTask to post.
optionsOptional overflow / drop / cancellation policy.
返回
Handle bound to the posted task; valid even when posting was rejected.

◆ post_task_with_priority()

bool vlink::MessageLoop::post_task_with_priority ( Callback &&  callback,
uint16_t  priority 
)

Posts a task with an explicit priority on a kPriorityType loop.

Higher priority values dispatch first. Non-priority loops reject the call and return false.

参数
callbackTask to post.
priorityDispatch priority in [kLowestPriority, kHighestPriority].
返回
true when the task was enqueued.
这是这个函数的调用关系图:

◆ post_task_with_priority_handle()

TaskHandle vlink::MessageLoop::post_task_with_priority_handle ( Callback &&  callback,
uint16_t  priority,
const PostTaskOptions options = {} 
)

Tracked variant of post_task_with_priority returning a TaskHandle.

参数
callbackTask to post.
priorityDispatch priority.
optionsOptional overflow / drop / cancellation policy.
返回
Handle bound to the posted task.

◆ quit()

bool vlink::MessageLoop::quit ( bool  force = false)

Requests the loop to exit.

With force == false the current batch finishes and the loop exits afterwards. With force == true the in-flight batch is also aborted. Tasks queued after the request are rejected. Returns false when quit had already been called (only meaningful when force is false).

参数
forceWhen true, also discards the in-flight batch. Default: false.
返回
true when the quit signal was accepted.

◆ register_begin_handler()

void vlink::MessageLoop::register_begin_handler ( Callback &&  callback)

Registers a callback fired once at loop thread startup.

Must be registered before run / async_run; later calls are ignored.

参数
callbackStartup handler.

◆ register_end_handler()

void vlink::MessageLoop::register_end_handler ( Callback &&  callback)

Registers a callback fired once when the loop thread exits.

Must be registered before run / async_run; later calls are ignored.

参数
callbackShutdown handler.

◆ register_idle_handler()

void vlink::MessageLoop::register_idle_handler ( Callback &&  callback)

Registers a callback fired every time the queue becomes empty.

Must be registered before run / async_run; later calls are ignored.

参数
callbackIdle handler.

◆ reset_lockfree_capacity()

void vlink::MessageLoop::reset_lockfree_capacity ( )

Recreates the lock-free queue, clearing all queued tasks and counters.

Applies only to kLockfreeType loops; on other types the call is a no-op. Must be invoked while the loop is stopped; calls made while it is running are logged and skipped.

◆ run()

bool vlink::MessageLoop::run ( )

Runs the loop on the calling thread until quit is requested.

返回
true after a normal exit; false when the loop is already running.

◆ set_name()

void vlink::MessageLoop::set_name ( const std::string &  name)

Sets a human-readable name visible to profiling tools.

参数
nameLoop name.

◆ set_strategy()

void vlink::MessageLoop::set_strategy ( Strategy  strategy)

Replaces the back-pressure strategy.

参数
strategyNew strategy; takes effect on the next full-queue push.

◆ spin()

bool vlink::MessageLoop::spin ( )

Alias of run blocking the calling thread.

返回
Same return value as run.

◆ spin_once()

bool vlink::MessageLoop::spin_once ( bool  block = true)

Processes one batch of pending tasks and timers on the calling thread.

参数
blockWhen true (default) blocks if the queue is empty; when false returns immediately.
返回
true after a normal processing cycle; false when quitting or the call came from a thread that does not own the loop and is unrelated to run / async_run.

◆ wait_for_idle()

virtual bool vlink::MessageLoop::wait_for_idle ( int  ms = Timer::kInfinite,
bool  check = true 
)
virtual

Waits until the loop has drained its queue and is not executing a task.

参数
msMaximum wait in milliseconds; Timer::kInfinite means no limit.
checkWhen true (default) rejects calls from the loop's own thread.
返回
true when the idle condition was reached within ms.

vlink::MultiLoop 重载.

◆ wait_for_quit()

bool vlink::MessageLoop::wait_for_quit ( int  ms = Timer::kInfinite,
bool  check = true 
)

Waits until the loop has fully exited.

参数
msMaximum wait in milliseconds; Timer::kInfinite means no limit.
checkWhen true (default) rejects calls from the loop's own thread.
返回
true if the loop exited before the timeout.

◆ wakeup()

bool vlink::MessageLoop::wakeup ( )

Wakes the loop thread if it is suspended in its idle wait.

Concurrent calls coalesce; only the first one after the previous wakeup actually signals.

返回
true when a wakeup is pending (newly issued or already in flight).

该类的文档由以下文件生成: