Aria 2.0.0
C++23 MVVM framework (C++20 minimum) — reactive, coroutine-first, ABI-layered
Loading...
Searching...
No Matches
aria::async::MainThreadExecutor Class Reference

Main-thread executor — queues callables for later execution on the thread that "owns" the executor (typically the application's main thread or a test thread). More...

#include <executor.hpp>

Inheritance diagram for aria::async::MainThreadExecutor:
[legend]

Public Member Functions

void post (std::function< void()> fn) override
 Legacy / canonical executor entry point.
aria::SchedulerCaps caps () const noexcept override
 Main-thread executor: safe in both reactive roles, plus Pumpable (drain/pump_until/run_one) and MainThread (owner-thread affinity is enforced after first pump).
bool is_main_thread () const noexcept override
 True iff the calling thread is the scheduler's "main" thread.
std::size_t drain ()
 Run callables.
template<typename Pred>
bool pump_until (Pred predicate, std::chrono::milliseconds timeout=std::chrono::seconds{2})
 Pump until predicate() returns true OR timeout elapses, then return.
void run_one ()
 Run exactly one callable, blocking the owner thread until one is available.
bool is_owner_thread () const noexcept
 True if called from the thread that owns this executor (or if no owner has been bound yet).
std::size_t pending () const noexcept
void clear () noexcept
 Drop all pending callables without running them. Owner-thread-only.
Public Member Functions inherited from aria::async::IExecutor
 ~IExecutor () override=default
void schedule (std::function< void()> fn) override
 Submit fn for execution "soon". Defines Caps::Post.
virtual bool is_safe_graph_executor () const noexcept
 True iff this executor is safe to use as the graph-thread (UI) executor.
virtual bool is_safe_worker_executor () const noexcept
 True iff this executor can host worker tasks.
Public Member Functions inherited from aria::IScheduler
virtual ~IScheduler ()=default
virtual void schedule_after (std::chrono::milliseconds delay, std::function< void()> fn)
 Submit fn for execution after delay.

Detailed Description

Main-thread executor — queues callables for later execution on the thread that "owns" the executor (typically the application's main thread or a test thread).

MainThreadExecutor is the canonical graph-thread executor for any scenario that mixes a thread-pool worker with reactive Property writes. The owner thread is established lazily by the first call to drain(), pump_until() or pump_one(); subsequent attempts to pump from a different thread trip a debug assert (and a runtime throw in Release).

Usage (test):

MainThreadExecutor ui; // declared on the test thread ThreadPoolExecutor pool{4};

AsyncCommand<int, int> cmd{ui, pool, ...}; cmd.execute(7); // Pump until the coroutine has marshalled its Property writes // back to the graph thread. REQUIRE(ui.pump_until([&]{ return !cmd.is_executing.get(); }));

Usage (console / headless app):

MainThreadExecutor main_loop; set_main_executor(main_loop); // ... wire up your ViewModels, Commands, etc. ... while (running) main_loop.run_one(); // blocks until next post

Thread-safety:

Member Function Documentation

◆ post()

void aria::async::MainThreadExecutor::post ( std::function< void()> fn)
inlineoverridevirtual

Legacy / canonical executor entry point.

Implementations override this; the unified IScheduler::schedule(fn) is wired to it.

Implements aria::async::IExecutor.

◆ caps()

aria::SchedulerCaps aria::async::MainThreadExecutor::caps ( ) const
inlinenodiscardoverridevirtualnoexcept

Main-thread executor: safe in both reactive roles, plus Pumpable (drain/pump_until/run_one) and MainThread (owner-thread affinity is enforced after first pump).

Reimplemented from aria::async::IExecutor.

◆ is_main_thread()

bool aria::async::MainThreadExecutor::is_main_thread ( ) const
inlinenodiscardoverridevirtualnoexcept

True iff the calling thread is the scheduler's "main" thread.

Default returns false; implementations advertising Caps::MainThread SHOULD override with a meaningful answer.

Reimplemented from aria::IScheduler.

◆ drain()

std::size_t aria::async::MainThreadExecutor::drain ( )
inline

Run callables.

Drains recursively: any task that posts further callables on this executor (typical of coroutine resumption that schedules a follow-up on the same thread) will be picked up in the same call. Returns the total number of callables executed.

Owner-thread-only.

◆ pump_until()

template<typename Pred>
bool aria::async::MainThreadExecutor::pump_until ( Pred predicate,
std::chrono::milliseconds timeout = std::chrono::seconds{2} )
inline

Pump until predicate() returns true OR timeout elapses, then return.

Blocks the owner thread on a condition variable when the queue is empty — no spin sleeping. Returns true iff predicate became true within the deadline.

Owner-thread-only.

◆ run_one()

void aria::async::MainThreadExecutor::run_one ( )
inline

Run exactly one callable, blocking the owner thread until one is available.

Suitable as the body of a console app's main loop.

Owner-thread-only.

◆ is_owner_thread()

bool aria::async::MainThreadExecutor::is_owner_thread ( ) const
inlinenodiscardnoexcept

True if called from the thread that owns this executor (or if no owner has been bound yet).

◆ pending()

std::size_t aria::async::MainThreadExecutor::pending ( ) const
inlinenodiscardnoexcept

◆ clear()

void aria::async::MainThreadExecutor::clear ( )
inlinenoexcept

Drop all pending callables without running them. Owner-thread-only.


The documentation for this class was generated from the following file: