Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
portals::Scheduler Class Reference

One UI executor; call dispatch only between frames, never during layout/paint. create captures its calling thread. Callbacks run there without the queue lock. No method mutates a widget or requests a frame: downstream callbacks own invalidation. Destruction must occur on UI, outside dispatch, after UI tokens; publishers may survive it. Callbacks must terminate promptly: budgets bound counts, not application callback wall time. More...

#include <scheduler.hpp>

Public Types

using ErrorSink = void (*)(void *, SchedulerError) noexcept
 UI error sink; borrowed context must outlive Scheduler, sink must not throw.

Public Member Functions

 ~Scheduler ()
 Stop/reclaim on owner; no joins; destroying from a callback is prohibited.
 Scheduler (const Scheduler &)=delete
Scheduler & operator= (const Scheduler &)=delete
std::expected< UiScope, SchedulerError > makeScope ()
 UI-only; closed/identity exhaustion are explicit.
template<class T, class Callback>
std::expected< SchedulerConnection< T >, SchedulerError > connect (const UiScope &scope, Callback &&callback, SchedulerLane lane=SchedulerLane::DATA, SchedulerOverflow overflow=SchedulerOverflow::REJECT)
 UI-only typed registration; copies/moves and owns callback, translating bad_alloc. Callback receives an independent snapshot owner. Foreign/closed scope, invalid enum, registration capacity or allocation failure rejects the operation without registration. REPLACE_LATEST is data-only and keeps at most one pending value per sender.
template<class Callback>
std::expected< Subscription, SchedulerError > at (const UiScope &scope, SchedulerTime deadline, Callback &&callback)
 Register a cancellable one-shot timer at an absolute clock millisecond. Due timers run in deadline/registration order, within timer_budget; no catch-up loss.
std::expected< SchedulerDispatch, SchedulerError > dispatch ()
 UI-only; nested dispatch rejected, callback exceptions contained.
std::expected< SchedulerWake, SchedulerError > waitFor (std::chrono::milliseconds timeout)
 UI-only bounded blocking wait; negative timeout invalid, dispatch reentry rejected. Real timeout is independent of injected time. Advance fake time then wake explicitly.
void requestStop () noexcept
 Worker-safe while this object lives; sender variant can outlive it.
std::expected< void, SchedulerError > stop ()
 UI-only close and reclamation, including self-stop.
SchedulerMetrics metrics () const
 Thread-safe copy, usable after stop.
Diagnostics * diagnostics () const noexcept
 Borrow the configured recorder; its lifetime is retained by this scheduler.

Static Public Member Functions

static std::expected< std::unique_ptr< Scheduler >, SchedulerError > create (SchedulerOptions options={}, SchedulerClock clock={}, ErrorSink sink=nullptr, void *sink_context=nullptr)
 Allocate bounded storage; invalid limits/no memory return errors without partial state. Allocation in caller expressions is outside this factory's boundary.

Detailed Description

One UI executor; call dispatch only between frames, never during layout/paint. create captures its calling thread. Callbacks run there without the queue lock. No method mutates a widget or requests a frame: downstream callbacks own invalidation. Destruction must occur on UI, outside dispatch, after UI tokens; publishers may survive it. Callbacks must terminate promptly: budgets bound counts, not application callback wall time.

Member Function Documentation

◆ at()

template<class Callback>
std::expected< Subscription, SchedulerError > portals::Scheduler::at ( const UiScope & scope,
SchedulerTime deadline,
Callback && callback )
inline

Register a cancellable one-shot timer at an absolute clock millisecond. Due timers run in deadline/registration order, within timer_budget; no catch-up loss.

Template Parameters
CallbackCopy-constructible callable storable in std::function and invocable as an lvalue without arguments; its return value is ignored.
Parameters
scopeLive scope belonging to this scheduler; closing it cancels the timer.
deadlineAbsolute millisecond deadline in the configured monotonic clock.
callbackCallable copied/moved into UI-owned registration storage.
Returns
Cancellation token, or WRONG_THREAD, WRONG_SCHEDULER, closed, WOULD_BLOCK, IDENTITY_EXHAUSTED, or NO_MEMORY.
Note
Exceptions other than std::bad_alloc propagate when materializing the callback.

◆ connect()

template<class T, class Callback>
std::expected< SchedulerConnection< T >, SchedulerError > portals::Scheduler::connect ( const UiScope & scope,
Callback && callback,
SchedulerLane lane = SchedulerLane::DATA,
SchedulerOverflow overflow = SchedulerOverflow::REJECT )
inline

UI-only typed registration; copies/moves and owns callback, translating bad_alloc. Callback receives an independent snapshot owner. Foreign/closed scope, invalid enum, registration capacity or allocation failure rejects the operation without registration. REPLACE_LATEST is data-only and keeps at most one pending value per sender.

Template Parameters
TTransitively immutable payload type; final ownership may be released on UI or a producer.
CallbackCopy-constructible callable storable in std::function and invocable as an lvalue with std::shared_ptr<const T>; its return value is ignored.
Parameters
scopeLive scope belonging to this scheduler; it controls callback lifetime.
callbackCallable copied/moved into UI-owned registration storage.
laneQueue whose capacity and dispatch budget apply.
overflowReject on capacity or replace this sender's pending data snapshot.
Returns
Subscription and publisher, or WRONG_THREAD, WRONG_SCHEDULER, closed, INVALID_ARGUMENT, WOULD_BLOCK, IDENTITY_EXHAUSTED, or NO_MEMORY.
Note
Exceptions other than std::bad_alloc propagate when materializing the callback.

◆ create()

std::expected< std::unique_ptr< Scheduler >, SchedulerError > portals::Scheduler::create ( SchedulerOptions options = {},
SchedulerClock clock = {},
ErrorSink sink = nullptr,
void * sink_context = nullptr )
static

Allocate bounded storage; invalid limits/no memory return errors without partial state. Allocation in caller expressions is outside this factory's boundary.

Parameters
optionsPositive queue, registration and dispatch limits; storage is reserved up front.
clockBorrowed monotonic clock; a null reader selects steady_clock.
sinkOptional nonthrowing callback-failure sink, invoked on the creating UI thread.
sink_contextBorrowed sink state that must outlive this scheduler.
Returns
New UI-owned scheduler, INVALID_ARGUMENT for invalid/unrepresentable limits, or NO_MEMORY.

◆ diagnostics()

Diagnostics * portals::Scheduler::diagnostics ( ) const
noexcept

Borrow the configured recorder; its lifetime is retained by this scheduler.

Returns
Recorder pointer, or null when diagnostics were not configured.

◆ dispatch()

std::expected< SchedulerDispatch, SchedulerError > portals::Scheduler::dispatch ( )

UI-only; nested dispatch rejected, callback exceptions contained.

Returns
Counts for this control/timer/data cycle, including caught callback failures and stop; WRONG_THREAD or REENTRANT_DISPATCH rejects before any callback starts.

◆ makeScope()

std::expected< UiScope, SchedulerError > portals::Scheduler::makeScope ( )

UI-only; closed/identity exhaustion are explicit.

Returns
New UI-owned scope, or WRONG_THREAD, closed, or IDENTITY_EXHAUSTED.

◆ metrics()

SchedulerMetrics portals::Scheduler::metrics ( ) const

Thread-safe copy, usable after stop.

Returns
Owning snapshot of both queue counters and aggregate dispatch counters.

◆ stop()

std::expected< void, SchedulerError > portals::Scheduler::stop ( )

UI-only close and reclamation, including self-stop.

Returns
Success after closure and UI reclamation, including repeated stop; WRONG_THREAD otherwise.

◆ waitFor()

std::expected< SchedulerWake, SchedulerError > portals::Scheduler::waitFor ( std::chrono::milliseconds timeout)

UI-only bounded blocking wait; negative timeout invalid, dispatch reentry rejected. Real timeout is independent of injected time. Advance fake time then wake explicitly.

Parameters
timeoutNonnegative maximum real elapsed wait; zero only checks readiness.
Returns
ready for queued/due work or explicit wake, stopped after stop, timeout after the wait, or WRONG_THREAD, REENTRANT_DISPATCH, or INVALID_ARGUMENT on rejection.

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