FerroUI/ docs

Search guides, controls, Rust APIs, and XAML.

    GitHub ↗

    Dispatcher

    struct · ferroui-base · v0.5.0

    struct Dispatcher
    

    Provides services for managing work items on a thread.

    Threading

    The object model is single-threaded and Rc-based; the dispatcher is the one entry point that can be used from other threads. A dispatcher is therefore handed out as Arc<Dispatcher> and is Send + Sync, and its API is split in two:

    • post, invoke, invoke_async and their variants can be called from any thread. Their callbacks (and results) must be Send.
    • post_local, invoke_local, invoke_async_local and their variants accept callbacks that are not Send (they may capture Rcs, Refs, ...). They can only be called on the dispatcher thread, which is checked at run time: calling them from another thread panics. The callback is kept in thread-local storage and never leaves the thread.

    Timers (DispatcherTimer), the shutdown and unhandled-exception events and everything else that takes non-Send handlers is likewise bound to the dispatcher thread. So is the platform implementation (IDispatcherImpl): other threads only ever use its IDispatcherSignal.

    Async

    The dispatcher also runs futures, which takes the place of the synchronization context of the reference implementation: it is a schedule per priority of the executor of ferroui-system (task_schedule). spawn_local, spawn_local_with_priority and spawn run a future on the dispatcher thread, polling it from jobs queued at a fixed priority, and give its ferroui_system::task::JoinHandle; start_local polls it for the first time inside the call. The deprecated invoke_async_task and DispatcherTaskScheduler do the same and give a DispatcherTask. resume, yield_now and await_with_priority change the priority at which an async block continues. No async runtime is involved.

    Panics in callbacks

    What the reference implementation does with exceptions is done here with panics: a panic in a posted callback goes through unhandled_exception_filter and unhandled_exception and is re-raised on the dispatcher thread unless a handler marks it as handled; a panic in an invoke/invoke_async callback is captured and re-raised to whoever waits for the operation. On targets that abort on panic none of this applies.

    View source

    Members

    supports_run_loops

    fn supports_run_loops(self: &Self) -> bool
    

    Whether the platform implementation can run nested event loops. When this is false, push_frame and main_loop are not available.

    Source

    check_access

    fn check_access(self: &Self) -> bool
    

    Checks that the current thread is the dispatcher thread.

    Source

    verify_access

    fn verify_access(self: &Self)
    

    Checks that the current thread is the dispatcher thread and panics if not.

    Source

    thread

    fn thread(self: &Self) -> ThreadId
    

    The thread this dispatcher belongs to.

    Source

    unhandled_exception

    fn unhandled_exception(self: &Self, handler: impl Fn(&DispatcherUnhandledExceptionEventArgs<'a>) -> () + 'static) -> Rc<dyn IDisposable>
    

    Occurs when a callback posted with post or post_local panics and the panic is not caught.

    A handler can mark the panic as handled, which stops it from being re-raised on the dispatcher thread. Handlers must be written with care to avoid secondary panics.

    Callbacks run through invoke/invoke_async do not raise this event: their panic is delivered to whoever waits for the operation.

    Panics

    Panics when called from a thread other than the dispatcher thread.

    Source

    unhandled_exception_filter

    fn unhandled_exception_filter(self: &Self, handler: impl Fn(&DispatcherUnhandledExceptionFilterEventArgs<'a>) -> () + 'static) -> Rc<dyn IDisposable>
    

    Occurs first when a posted callback panics, to decide whether unhandled_exception is raised at all.

    The filter runs regardless of whether there are unhandled_exception handlers. Setting request_catch to false skips the unhandled_exception event.

    Panics

    Panics when called from a thread other than the dispatcher thread.

    Source

    invoke

    fn invoke<R: Send + 'static>(self: &Self, callback: impl FnOnce() -> R + Send + 'static) -> Result<R, Canceled>
    

    Executes the specified callback synchronously on the thread that the dispatcher was created on, at DispatcherPriority::SEND.

    Returns Err when the operation was aborted before it ran (for instance because the dispatcher has shut down).

    Panics

    Re-raises the panic of the callback.

    Source

    invoke_with_priority

    fn invoke_with_priority<R: Send + 'static>(self: &Self, callback: impl FnOnce() -> R + Send + 'static, priority: DispatcherPriority) -> Result<R, Canceled>
    

    Executes the specified callback synchronously with the specified priority on the thread that the dispatcher was created on.

    Source

    invoke_with_cancellation

    fn invoke_with_cancellation<R: Send + 'static>(self: &Self, callback: impl FnOnce() -> R + Send + 'static, priority: DispatcherPriority, cancellation_token: &CancellationToken) -> Result<R, Canceled>
    

    Executes the specified callback synchronously with the specified priority on the thread that the dispatcher was created on.

    cancellation_token cancels the operation as long as it has not started. Returns Err when the token is already canceled.

    Source

    invoke_with_timeout

    fn invoke_with_timeout<R: Send + 'static>(self: &Self, callback: impl FnOnce() -> R + Send + 'static, priority: DispatcherPriority, cancellation_token: &CancellationToken, timeout: Duration) -> Result<R, DispatcherInvokeError>
    

    Executes the specified callback synchronously with the specified priority on the thread that the dispatcher was created on, giving up when the operation has not started within timeout.

    The timeout only applies to the time the operation spends in the queue: once it has started, the call waits for it to complete. When the operation is still pending after timeout it is aborted and DispatcherInvokeError::Timeout is returned.

    On the dispatcher thread the queue is pumped while waiting and the timeout is measured with the dispatcher clock.

    Source

    invoke_local

    fn invoke_local<R: 'static>(self: &Self, callback: impl FnOnce() -> R + 'static) -> Result<R, Canceled>
    

    Like invoke for callbacks that are not Send.

    Panics

    Panics when called from a thread other than the dispatcher thread.

    Source

    invoke_local_with_priority

    fn invoke_local_with_priority<R: 'static>(self: &Self, callback: impl FnOnce() -> R + 'static, priority: DispatcherPriority) -> Result<R, Canceled>
    

    Like invoke_with_priority for callbacks that are not Send.

    Panics

    Panics when called from a thread other than the dispatcher thread.

    Source

    invoke_local_with_cancellation

    fn invoke_local_with_cancellation<R: 'static>(self: &Self, callback: impl FnOnce() -> R + 'static, priority: DispatcherPriority, cancellation_token: &CancellationToken) -> Result<R, Canceled>
    

    Like invoke_with_cancellation for callbacks that are not Send.

    Panics

    Panics when called from a thread other than the dispatcher thread.

    Source

    invoke_local_with_timeout

    fn invoke_local_with_timeout<R: 'static>(self: &Self, callback: impl FnOnce() -> R + 'static, priority: DispatcherPriority, cancellation_token: &CancellationToken, timeout: Duration) -> Result<R, DispatcherInvokeError>
    

    Like invoke_with_timeout for callbacks that are not Send.

    Panics

    Panics when called from a thread other than the dispatcher thread.

    Source

    invoke_async

    fn invoke_async<R: Send + 'static>(self: &Self, callback: impl FnOnce() -> R + Send + 'static) -> DispatcherOperation<R>
    

    Executes the specified callback asynchronously on the thread that the dispatcher was created on, at DispatcherPriority::DEFAULT.

    The returned operation can be awaited, waited for, aborted and re-prioritized.

    Source

    invoke_async_with_priority

    fn invoke_async_with_priority<R: Send + 'static>(self: &Self, callback: impl FnOnce() -> R + Send + 'static, priority: DispatcherPriority) -> DispatcherOperation<R>
    

    Executes the specified callback asynchronously with the specified priority on the thread that the dispatcher was created on.

    Source

    invoke_async_with_cancellation

    fn invoke_async_with_cancellation<R: Send + 'static>(self: &Self, callback: impl FnOnce() -> R + Send + 'static, priority: DispatcherPriority, cancellation_token: &CancellationToken) -> DispatcherOperation<R>
    

    Executes the specified callback asynchronously with the specified priority on the thread that the dispatcher was created on.

    cancellation_token aborts the operation as long as it has not started.

    Source

    invoke_async_local

    fn invoke_async_local<R: 'static>(self: &Self, callback: impl FnOnce() -> R + 'static) -> DispatcherOperation<R>
    

    Like invoke_async for callbacks that are not Send.

    Panics

    Panics when called from a thread other than the dispatcher thread.

    Source

    invoke_async_local_with_priority

    fn invoke_async_local_with_priority<R: 'static>(self: &Self, callback: impl FnOnce() -> R + 'static, priority: DispatcherPriority) -> DispatcherOperation<R>
    

    Like invoke_async_with_priority for callbacks that are not Send.

    Panics

    Panics when called from a thread other than the dispatcher thread.

    Source

    invoke_async_local_with_cancellation

    fn invoke_async_local_with_cancellation<R: 'static>(self: &Self, callback: impl FnOnce() -> R + 'static, priority: DispatcherPriority, cancellation_token: &CancellationToken) -> DispatcherOperation<R>
    

    Like invoke_async_with_cancellation for callbacks that are not Send.

    Panics

    Panics when called from a thread other than the dispatcher thread.

    Source

    invoke_async_task

    fn invoke_async_task<F, Fut>(self: &Self, callback: F) -> DispatcherTask<<Fut as >::Output>
    where F: FnOnce() -> Fut + Send + 'static,
          Fut: Future + 'static,
          <Fut as >::Output: Send + 'static
    

    Executes the specified callback on the dispatcher thread and then runs the future it returns there, at DispatcherPriority::DEFAULT.

    The returned task completes when the future has completed; see invoke_async_task_with_priority.

    Deprecated: spawn does the same and gives a JoinHandle.

    Deprecated since 0.5.0. use Dispatcher::spawn, which gives a JoinHandle (a panic of the future is JoinError::Panicked, a value)

    Source

    invoke_async_task_with_priority

    fn invoke_async_task_with_priority<F, Fut>(self: &Self, callback: F, priority: DispatcherPriority) -> DispatcherTask<<Fut as >::Output>
    where F: FnOnce() -> Fut + Send + 'static,
          Fut: Future + 'static,
          <Fut as >::Output: Send + 'static
    

    Executes the specified callback with the specified priority on the dispatcher thread and then runs the future it returns there.

    The callback can come from any thread; the future it creates never leaves the dispatcher thread and does not have to be Send. The future is polled for the first time right after the callback, inside the same job, and afterwards from jobs of the same priority.

    The returned task completes when the future has completed. It is canceled when the operation is aborted before the callback ran or the dispatcher shuts down before the future is done, and it fails with the panic of the callback or the future.

    The task is a task of the dispatcher (spawn_with_priority), with a handle that keeps the contract of DispatcherTask.

    Deprecated: spawn_with_priority does the same and gives a JoinHandle.

    Deprecated since 0.5.0. use Dispatcher::spawn_with_priority, which gives a JoinHandle (a panic of the future is JoinError::Panicked, a value)

    Source

    invoke_async_task_local

    fn invoke_async_task_local<F, Fut>(self: &Self, callback: F) -> DispatcherTask<<Fut as >::Output>
    where F: FnOnce() -> Fut + 'static,
          Fut: Future + 'static,
          <Fut as >::Output: 'static
    

    Like invoke_async_task for callbacks and results that are not Send.

    Panics

    Panics when called from a thread other than the dispatcher thread.

    Deprecated: spawn_local_with_priority(async move { callback().await }, DispatcherPriority::DEFAULT) does the same (the callback runs in the first poll, a job at that priority) and gives a JoinHandle.

    Deprecated since 0.5.0. use Dispatcher::spawn_local_with_priority(async move { callback().await }, DispatcherPriority::DEFAULT), which gives a JoinHandle

    Source

    invoke_async_task_local_with_priority

    fn invoke_async_task_local_with_priority<F, Fut>(self: &Self, callback: F, priority: DispatcherPriority) -> DispatcherTask<<Fut as >::Output>
    where F: FnOnce() -> Fut + 'static,
          Fut: Future + 'static,
          <Fut as >::Output: 'static
    

    Like invoke_async_task_with_priority for callbacks and results that are not Send.

    Panics

    Panics when called from a thread other than the dispatcher thread.

    Deprecated: spawn_local_with_priority(async move { callback().await }, priority) does the same and gives a JoinHandle.

    Deprecated since 0.5.0. use Dispatcher::spawn_local_with_priority(async move { callback().await }, priority), which gives a JoinHandle

    Source

    post

    fn post(self: &Self, action: impl FnOnce() -> () + Send + 'static, priority: DispatcherPriority)
    

    Posts an action that will be invoked on the dispatcher thread.

    Can be called from any thread. A panic raised by the action goes through the unhandled-exception events of the dispatcher.

    Source

    post_local

    fn post_local(self: &Self, action: impl FnOnce() -> () + 'static, priority: DispatcherPriority)
    

    Posts an action that is not Send to be invoked on the dispatcher thread.

    Panics

    Panics when called from a thread other than the dispatcher thread.

    Source

    await_with_priority

    fn await_with_priority<F: Future>(self: &Self, task: F, priority: DispatcherPriority) -> DispatcherPriorityTaskAwaitable<F>
    

    Returns a future that awaits task and then resumes the awaiting code from a job of this dispatcher with the given priority, yielding the output of task.

    Source

    resume

    fn resume(self: &Self) -> DispatcherPriorityAwaitable
    

    Returns a future that resumes on this dispatcher's thread at DispatcherPriority::BACKGROUND when awaited.

    Source

    resume_with_priority

    fn resume_with_priority(self: &Self, priority: DispatcherPriority) -> DispatcherPriorityAwaitable
    

    Returns a future that resumes on this dispatcher's thread at the given priority when awaited.

    Source

    yield_now

    fn yield_now() -> DispatcherPriorityAwaitable
    

    Returns a future that yields to the current dispatcher, letting it process other work, and resumes at DispatcherPriority::BACKGROUND.

    Source

    yield_with_priority

    fn yield_with_priority(priority: DispatcherPriority) -> DispatcherPriorityAwaitable
    

    Returns a future that yields to the current dispatcher and resumes at the given priority.

    Source

    shutdown_started

    fn shutdown_started(self: &Self, handler: impl Fn(&Arc<Dispatcher>) -> () + 'static) -> Rc<dyn IDisposable>
    

    Raised when the dispatcher is shutting down.

    Panics

    Panics when called from a thread other than the dispatcher thread.

    Source

    shutdown_finished

    fn shutdown_finished(self: &Self, handler: impl Fn(&Arc<Dispatcher>) -> () + 'static) -> Rc<dyn IDisposable>
    

    Raised when the dispatcher is shut down.

    Panics

    Panics when called from a thread other than the dispatcher thread.

    Source

    push_frame

    fn push_frame(self: &Self, frame: &Arc<DispatcherFrame>)
    

    Push an execution frame.

    Panics

    Panics when the platform implementation does not support run loops (see supports_run_loops), when the dispatcher has shut down, when processing is disabled and when called from another thread.

    Source

    main_loop

    fn main_loop(self: &Self, cancellation_token: &CancellationToken)
    

    Runs the dispatcher's main loop until cancellation_token is canceled.

    Panics

    See push_frame.

    Source

    exit_all_frames

    fn exit_all_frames(self: &Self)
    

    Requests that all nested frames exit.

    Source

    begin_invoke_shutdown

    fn begin_invoke_shutdown(self: &Self, priority: DispatcherPriority)
    

    Begins the process of shutting down the dispatcher.

    Source

    invoke_shutdown

    fn invoke_shutdown(self: &Self)
    

    Initiates the shutdown process of the dispatcher synchronously.

    Source

    disable_processing

    fn disable_processing(self: &Self) -> DispatcherProcessingDisabled
    

    Disable the event processing of the dispatcher.

    This is an advanced method intended to eliminate the chance of unrelated reentrancy. While processing is disabled no one is allowed to push a frame and no job processing is permitted. Processing is enabled again when the returned value is disposed or dropped.

    Source

    task_schedule

    fn task_schedule(self: &Self, priority: DispatcherPriority) -> Arc<dyn Schedule>
    

    The schedule of the tasks of this dispatcher at priority: every poll of a task given to it is a job posted at that priority, and the dispatcher aborts the task when it shuts down; work that is not Send (Schedule::schedule_local) is a job posted with post_local. It is the current schedule of the jobs of that priority (ferroui_system::task::current_schedule); on the dispatcher thread there is one schedule per priority.

    For ferroui_system::task::spawn_local_on, on the dispatcher thread; with FirstPoll::Now the future runs up to its first pending await inside the spawning call. A task given to it on another thread is aborted at once.

    Source

    spawn_local

    fn spawn_local<F>(self: &Self, future: F) -> JoinHandle<<F as >::Output>
    where F: Future + 'static,
          <F as >::Output: 'static
    

    Runs future on this dispatcher at the priority of the job that calls this (DispatcherPriority::DEFAULT outside of a job), and gives its handle; see spawn_local_with_priority.

    Panics

    When called from a thread other than the dispatcher thread.

    Source

    spawn_local_with_priority

    fn spawn_local_with_priority<F>(self: &Self, future: F, priority: DispatcherPriority) -> JoinHandle<<F as >::Output>
    where F: Future + 'static,
          <F as >::Output: 'static
    

    Runs future, which need not be Send, on this dispatcher, and gives its handle. Its first poll is a job posted at priority, and so is every poll that a wake queues, from any thread. The handle is a future of the output, or of a JoinError: canceled when the task is aborted or the dispatcher shuts down first, panicked when the future panics. Dropping the handle detaches the task, which keeps running.

    Panics

    When called from a thread other than the dispatcher thread.

    Source

    spawn

    fn spawn<C, F>(self: &Self, create: C) -> JoinHandle<<F as >::Output>
    where C: FnOnce() -> F + Send + 'static,
          F: Future + 'static,
          <F as >::Output: Send + 'static
    

    Runs the future that create makes on this dispatcher at DispatcherPriority::DEFAULT, from any thread; see spawn_with_priority.

    Source

    spawn_with_priority

    fn spawn_with_priority<C, F>(self: &Self, create: C, priority: DispatcherPriority) -> JoinHandle<<F as >::Output>
    where C: FnOnce() -> F + Send + 'static,
          F: Future + 'static,
          <F as >::Output: Send + 'static
    

    Runs the future that create makes on this dispatcher, and gives its handle; callable from any thread. create is Send and runs in a job posted at priority; the future it makes need not be Send, never leaves the dispatcher thread, and is polled for the first time inside that job, then from jobs of the same priority (what the deprecated invoke_async_task_with_priority does, with a JoinHandle). A panic of create faults the task like a panic of the future; the task is canceled when the dispatcher shuts down before the job runs.

    Source

    start_local

    fn start_local<F>(self: &Self, future: F) -> JoinHandle<<F as >::Output>
    where F: Future + 'static,
          <F as >::Output: 'static
    

    Starts future on this dispatcher at the priority of the job that calls this (DispatcherPriority::DEFAULT outside of a job), and gives its handle; see start_local_with_priority.

    Panics

    When called from a thread other than the dispatcher thread.

    Source

    start_local_with_priority

    fn start_local_with_priority<F>(self: &Self, future: F, priority: DispatcherPriority) -> JoinHandle<<F as >::Output>
    where F: Future + 'static,
          <F as >::Output: 'static
    

    Starts future, which need not be Send, on this dispatcher the way calling an asynchronous method does, and gives its handle: the future is polled for the first time before this returns (FirstPoll::Now), so it runs up to its first pending await inside the call (and the handle is finished when there is none); every later poll is a job posted at priority by a wake, from any thread. Otherwise the task is the one of spawn_local_with_priority: a panic of the future is JoinError::Panicked, an abort or the shutdown of the dispatcher is JoinError::Canceled, and dropping the handle detaches the task.

    What DispatcherTaskScheduler::start_local (deprecated) did, with a JoinHandle.

    Panics

    When called from a thread other than the dispatcher thread.

    Source

    start_local_on_result_with_priority

    fn start_local_on_result_with_priority<F, T>(self: &Self, future: F, priority: DispatcherPriority) -> JoinHandle<T>
    where F: Future<Output = Result<T, ferroui_system::task::JoinError>> + 'static,
          T: 'static
    

    start_local_with_priority for a future whose failure is a value: an Err of the future fails the handle with that JoinError, as a panic of the future does, but without unwinding, so the task fails the same way in a build that aborts on panic (#567).

    Panics

    When called from a thread other than the dispatcher thread.

    Source

    run_jobs

    fn run_jobs(self: &Self, priority: Option<DispatcherPriority>)
    

    Force-runs all dispatcher operations ignoring any pending OS events, use with caution.

    None runs every active job.

    Source

    has_jobs_with_priority

    fn has_jobs_with_priority(self: &Self, priority: DispatcherPriority) -> bool
    

    Whether a job with at least the given priority is queued.

    Source

    current_dispatcher

    fn current_dispatcher() -> Arc<Dispatcher>
    

    The dispatcher of the calling thread. It is created when the thread does not have one yet and then lives until the thread exits.

    Source

    from_thread

    fn from_thread(thread: ThreadId) -> Option<Arc<Dispatcher>>
    

    The dispatcher of thread, if that thread has one.

    Source

    ui_thread

    fn ui_thread() -> Arc<Dispatcher>
    

    Gets the dispatcher for the UI thread: the first dispatcher that was created in the process.

    Control and library authors are encouraged to use current_dispatcher and the dispatcher of the object at hand instead.

    Source

    now

    fn now(self: &Self) -> i64
    

    The current time of the platform implementation, in milliseconds.

    Dispatcher thread only; a dispatcher that has been reset reports 0.

    Source

    current_priority

    fn current_priority() -> Option<DispatcherPriority>
    

    The priority of the dispatcher job that runs on the calling thread (None outside of a job): what asynchronous code started by the job continues at. Also the priority set with set_current_priority for the code that runs outside of a job, and the priority of a nested frame between its jobs (DispatcherPriority::NORMAL).

    Source

    set_current_priority

    fn set_current_priority(priority: Option<DispatcherPriority>) -> Option<DispatcherPriority>
    

    Sets the priority of the code that runs on the calling thread outside of a dispatcher job, and gives the previous one: the dispatcher of the thread at that priority becomes the current schedule of the thread (ferroui_system::task::current_schedule), which the tasks spawned there and the reactive libraries continue in; None clears both. What the application does when it starts (at DispatcherPriority::NORMAL, unless the thread has a priority), as the context that the upstream application installs; the jobs of the dispatcher set their own priority while they run, and restore this one.

    A priority creates the dispatcher of the calling thread when it has none.

    Source

    to_task_scheduler

    fn to_task_scheduler(self: &Self) -> DispatcherTaskScheduler
    

    Gets a scheduler which runs futures on this dispatcher. The priority is the one of the job that calls this (Dispatcher::current_priority), DispatcherPriority::DEFAULT outside of a job.

    Deprecated: Dispatcher::spawn_local, Dispatcher::start_local and Dispatcher::spawn_with_priority at that priority give a JoinHandle.

    Deprecated since 0.5.0. use Dispatcher::spawn_local, start_local or spawn_with_priority, which give a JoinHandle

    Source

    to_task_scheduler_with_priority

    fn to_task_scheduler_with_priority(self: &Self, priority: DispatcherPriority) -> DispatcherTaskScheduler
    

    Gets a scheduler which runs futures on this dispatcher with the specified priority.

    Deprecated: Dispatcher::spawn_local_with_priority, Dispatcher::start_local_with_priority and Dispatcher::spawn_with_priority give a JoinHandle.

    Deprecated since 0.5.0. use Dispatcher::spawn_local_with_priority, start_local_with_priority or spawn_with_priority, which give a JoinHandle

    Source

    Trait implementations

    • Send
    • Sync
    • Freeze
    • UnsafeUnpin
    • Unpin
    • UnwindSafe
    • RefUnwindSafe
    • ClassDefaults
    • Borrow<T>
    • BorrowMut<T>
    • Into<U>
    • From<T>
    • TryInto<U>
    • TryFrom<U>
    • Any
    • ErasedDestructor
    • IntoEither
    • IDispatcher
    • Debug

    Availability

    Extracted for aarch64-apple-darwin. Cargo features are listed in the crate index.