morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
morph::exec::MainThreadExecutor Class Reference

Single-thread executor intended for use on the calling (main) thread. More...

#include <executor.hpp>

Inheritance diagram for morph::exec::MainThreadExecutor:
morph::exec::IExecutor

Public Member Functions

void post (std::function< void()> task) override
 Enqueues task to be run on the next runFor() call.
void runFor (std::chrono::milliseconds timeout)
 Pumps the task queue for up to timeout.
bool runOnce ()
 Dequeues and runs at most one pending task, without blocking.
void drain ()
 Runs tasks until the queue is empty, with no wall-clock timeout.
Public Member Functions inherited from morph::exec::IExecutor
 IExecutor (const IExecutor &)=delete
IExecutor & operator= (const IExecutor &)=delete
 IExecutor (IExecutor &&)=delete
IExecutor & operator= (IExecutor &&)=delete
virtual bool isSerial () const noexcept
 Whether this executor runs one task at a time, each after the tasks posted before it: a strand, a GUI loop, a pumped MainThreadExecutor.
::core::async::IExecutor & coreExecutor () noexcept
 This executor as a core-cpp executor: the identity an core::async::ExecutorScope names.

Detailed Description

Single-thread executor intended for use on the calling (main) thread.

Tasks posted from other threads are collected and dispatched only when the caller invokes runFor(). Useful in tests or event loops that have no native dispatcher.

Definition at line 253 of file executor.hpp.

Member Function Documentation

◆ post()

void morph::exec::MainThreadExecutor::post ( std::function< void()> task)
inlineoverridevirtual

Enqueues task to be run on the next runFor() call.

Thread-safe. The task is not executed immediately.

Parameters
taskCallable to execute.

Implements morph::exec::IExecutor.

Definition at line 259 of file executor.hpp.

◆ runFor()

void morph::exec::MainThreadExecutor::runFor ( std::chrono::milliseconds timeout)
inline

Pumps the task queue for up to timeout.

Runs queued tasks one by one. It does not return early when the queue drains: while time remains before the deadline it blocks waiting for newly posted tasks, and only returns once the deadline is reached (with any still-queued tasks left for a subsequent call — this is a pump, not a flush). Exceptions thrown by tasks are logged and execution continues with the next task.

Must be called from the thread that "owns" this executor.

Parameters
timeoutMaximum wall-clock time to spend draining.

Definition at line 280 of file executor.hpp.

◆ runOnce()

bool morph::exec::MainThreadExecutor::runOnce ( )
inline

Dequeues and runs at most one pending task, without blocking.

Never waits for a task to appear: if the queue is empty, returns false immediately. Otherwise pops exactly one task, runs it, and returns true whether or not that task threw. Unlike runFor(), there is no wall-clock wait — this is the single-step primitive for callers (e.g. tests or external event loops) that want deterministic, one-task-at-a-time control instead of a timed pump.

Must be called from the thread that "owns" this executor.

Returns
true if a task was dequeued and run, false if the queue was empty.

Definition at line 307 of file executor.hpp.

◆ drain()

void morph::exec::MainThreadExecutor::drain ( )
inline

Runs tasks until the queue is empty, with no wall-clock timeout.

Repeatedly calls the same dequeue-and-invoke step as runOnce() until the queue reports empty, then returns. It does not wait for tasks posted after draining begins: a task that posts new work while running extends the drain (the new task is still queued and gets consumed), but drain() never blocks waiting for an externally posted task the way runFor() does — it simply stops once the queue is observed empty.

Must be called from the thread that "owns" this executor.

Definition at line 331 of file executor.hpp.


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