336 template <
typename CallbackT,
typename = std::enable_if_t<!std::is_convertible_v<CallbackT, Schedule::RetCallback>>>
352 template <
typename CallbackT,
typename = std::enable_if_t<std::is_convertible_v<CallbackT, Schedule::RetCallback>>>
451 template <
class FunctionT,
class... ArgsT,
typename ResultT = std::invoke_result_t<FunctionT, ArgsT...>>
452 [[nodiscard]] std::future<ResultT>
invoke_task(FunctionT&& function, ArgsT&&... args);
469 template <
class FunctionT,
class... ArgsT,
typename ResultT = std::invoke_result_t<FunctionT, ArgsT...>>
522 static uint64_t get_current_nano_time();
524 bool add_timer(Timer* timer);
526 bool remove_timer(Timer* timer);
528 bool push_task(
Callback&& callback, uint16_t priority);
530 void push_normal_task(
Callback&& callback);
532 bool push_lockfree_task(
Callback&& callback);
534 void push_priority_task(
Callback&& callback, uint16_t priority);
538 bool process_normal_task(
bool block);
540 bool process_lockfree_task(
bool block);
542 bool process_priority_task(
bool block);
544 bool process_timer_task(int64_t& next_sleep_time);
547 std::unique_ptr<struct MessageLoopImpl> impl_;
556template <
typename CallbackT,
typename>
563 bool post_ret =
false;
571 post_ret =
post_task(std::move(wrapper_callback));
576 status.set_valid(
false);
582template <
typename CallbackT,
typename>
589 bool post_ret =
false;
597 post_ret =
post_task(std::move(wrapper_callback));
602 status.set_valid(
false);
608template <
class FunctionT,
class... ArgsT,
typename ResultT>
610 auto task = std::make_shared<std::packaged_task<ResultT()>>(
611 std::bind(std::forward<FunctionT>(function), std::forward<ArgsT>(args)...));
613 std::future<ResultT> res = task->get_future();
622template <
class FunctionT,
class... ArgsT,
typename ResultT>
625 auto task = std::make_shared<std::packaged_task<ResultT()>>(
626 std::bind(std::forward<FunctionT>(function), std::forward<ArgsT>(args)...));
628 std::future<ResultT> res = task->get_future();
MessageLoop(Type type)
Constructs a MessageLoop with the specified queue type.
void register_begin_handler(Callback &&callback)
Registers a callback invoked once when the loop thread starts.
bool spin_once(bool block=true)
Processes one batch of pending tasks and timers.
MessageLoop()
Constructs a MessageLoop with kNormalType queue.
const std::string & get_name() const
Returns the name set via set_name().
std::function< void()> Callback
Callback type for tasks and event handlers.
定义 message_loop.h:111
virtual void on_task_changed(Callback &&callback, uint32_t start_time)
Called before each task is executed.
bool async_run()
Starts the message loop on a new background thread (non-blocking).
bool wait_for_quit(int ms=Timer::kInfinite, bool check=true)
Waits until the loop has fully exited (after quit() was called).
bool spin()
Runs the loop continuously in a spin mode (blocking; no background thread).
bool wakeup()
Wakes the loop thread if it is sleeping (e.g., in kBlockStrategy).
void set_name(const std::string &name)
Sets a human-readable name for this loop (visible in profiling tools).
virtual bool is_in_same_thread() const
Returns true if the calling thread is the same as the loop thread.
bool post_task(Callback &&callback)
Posts a task to the queue for execution on the loop thread.
virtual void on_end()
Called from the loop thread just after the last task has been processed.
std::future< ResultT > invoke_task(FunctionT &&function, ArgsT &&... args)
Dispatches a callable to the loop thread and returns a std::future for the result.
定义 message_loop.h:609
Type
Queue implementation type.
定义 message_loop.h:119
@ kNormalType
Mutex-protected FIFO queue (default)
定义 message_loop.h:120
@ kPriorityType
Priority-ordered queue
定义 message_loop.h:122
@ kLockfreeType
Lock-free MPMC queue
定义 message_loop.h:121
std::future< ResultT > invoke_task_with_priority(FunctionT &&function, uint16_t priority, ArgsT &&... args)
Dispatches a callable with an explicit priority and returns a std::future.
定义 message_loop.h:623
virtual void on_idle()
Called from the loop thread each time the queue becomes empty.
Strategy get_strategy() const
Returns the current idle dispatch strategy.
Schedule::Status exec_task(const Schedule::Config &config, CallbackT &&callback)
Posts a scheduled task and returns a Schedule::Status for chaining callbacks.
定义 message_loop.h:557
Type get_type() const
Returns the queue type this loop was constructed with.
bool run()
Runs the message loop on the calling thread (blocking).
bool is_running() const
Returns true if the loop is currently running (started and not quit).
bool is_busy() const
Returns true if the loop is currently executing a task.
Priority
Pre-defined task priority levels for kPriorityType loops.
定义 message_loop.h:144
@ kNoPriority
No priority (FIFO order)
定义 message_loop.h:145
@ kTimerPriority
Used internally for timer callbacks
定义 message_loop.h:147
@ kLowestPriority
Lowest real priority
定义 message_loop.h:146
@ kHighestPriority
Highest available priority
定义 message_loop.h:149
@ kNormalPriority
Default task priority
定义 message_loop.h:148
virtual uint32_t get_max_elapsed_time() const
Returns the maximum allowed task execution time in milliseconds.
void set_strategy(Strategy strategy)
Changes the idle dispatch strategy.
virtual size_t get_max_timer_count() const
Returns the maximum number of timers that can be attached to this loop.
bool is_ready_to_quit() const
Returns true if quit() has been called and the loop is winding down.
bool post_task_with_priority(Callback &&callback, uint16_t priority)
Posts a task with an explicit priority (requires kPriorityType loop).
virtual size_t get_max_task_count() const
Returns the maximum queue depth.
void reset_lockfree_capacity()
Resets the lock-free queue to its initial capacity.
void register_idle_handler(Callback &&callback)
Registers a callback invoked each time the task queue becomes empty.
virtual ~MessageLoop()
Destructor. Calls quit(true) and waits for the background thread (if any).
size_t get_task_count() const
Returns the number of tasks currently in the queue.
Strategy
Idle strategy controlling CPU and latency trade-offs.
定义 message_loop.h:131
@ kOptimizationStrategy
Balance latency and CPU via yield
定义 message_loop.h:132
@ kBlockStrategy
Block on condition variable (lowest CPU)
定义 message_loop.h:134
@ kPopStrategy
Busy-poll (lowest latency, highest CPU)
定义 message_loop.h:133
bool wait_for_idle(int ms=Timer::kInfinite, bool check=true)
Waits until the task queue is drained.
virtual void on_task_timeout(Callback &&callback, uint32_t elapsed_time)
Called when a task's execution time exceeds get_max_elapsed_time().
void register_end_handler(Callback &&callback)
Registers a callback invoked once when the loop thread exits.
bool quit(bool force=false)
Requests the loop to exit cleanly.
virtual void on_begin()
Called from the loop thread just before the first task is processed.
RAII handle returned by exec_task() for a bool-returning callback task.
定义 schedule.h:249
RAII handle returned by exec_task() for a void-callback task.
定义 schedule.h:152
static bool call_once(class MessageLoop *message_loop, uint32_t interval_ms, Callback &&callback, uint16_t priority=0)
Posts a one-shot timer to a MessageLoop without creating a Timer object.
static constexpr int kInfinite
Sentinel loop count meaning repeat indefinitely.
定义 timer.h:91
#define VUNLIKELY(...)
Shorthand alias for VLINK_UNLIKELY. Hints that the expression is unlikely true.
定义 macros.h:302
#define VLINK_EXPORT
定义 macros.h:85
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
定义 macros.h:184
RAII task scheduling wrapper with delay, priority, timeouts and result chaining.
Scheduling parameters for a task posted via MessageLoop::exec_task().
定义 schedule.h:121
uint32_t delay_ms
Delay in ms before the task is posted.
定义 schedule.h:138
uint16_t priority
Dispatch priority (higher = sooner).
定义 schedule.h:139
static Status process(const Config &config, Callback &&callback, Callback &wrapper_callback)
Wraps a void callback in a Config envelope and produces a wrapper task.
std::function< void()> Callback
Callback type for void tasks and lifecycle hooks (schedule/execution timeout, else).
定义 schedule.h:102
static RetStatus process_with_ret(const Config &config, RetCallback &&callback, Callback &wrapper_callback)
Wraps a bool-returning callback in a Config envelope and produces a wrapper task.
Event-loop-driven periodic/one-shot timer with configurable priority.