pub struct ReentrantDispatch { /* private fields */ }Expand description
Reentrancy-safe dispatcher for affinity-pinned threads.
Attach one of these to GracefulIpcChannel so that callers can safely
dispatch work back to the “main” (or any named) thread without deadlocking
when the caller itself is the affinity thread.
The pattern mirrors Swift’s MainActor.assumeIsolated and C#’s
SynchronizationContext.Send:
- Caller is affinity thread → execute
finline, return immediately. - Caller is any other thread → push
fto the queue, block until the affinity thread drains it viaReentrantDispatch::pump.
Implementations§
Source§impl ReentrantDispatch
impl ReentrantDispatch
Sourcepub fn bind_current_thread(&self)
pub fn bind_current_thread(&self)
Bind the current thread as the affinity thread.
Must be called once from the thread that will handle dispatched
work (e.g. the DCC idle callback, Unity EditorApplication.update,
Unreal FTSTicker, etc.).
Calling this again from a different thread replaces the binding.
Sourcepub fn is_affinity_thread(&self) -> bool
pub fn is_affinity_thread(&self) -> bool
Returns true if the calling thread is the bound affinity thread.
Sourcepub fn submit_reentrant<F, R>(&self, f: F) -> Result<R>
pub fn submit_reentrant<F, R>(&self, f: F) -> Result<R>
Submit f to the affinity thread in a reentrancy-safe way.
- If the current thread is the affinity thread,
fis called inline and its return value is propagated immediately — no queue, no blocking. - Otherwise,
fis pushed into the work queue and the calling thread blocks until the affinity thread processes it viapump.
§Errors
Returns Err(IpcError::Closed) if the affinity thread has been
dropped (the receiver end of the queue is gone).
Returns Err(IpcError::Other(...)) if f itself returns an error.
Sourcepub fn pump(&self, budget: Duration) -> usize
pub fn pump(&self, budget: Duration) -> usize
Drain at most one “budget” worth of pending work items on the current (affinity) thread.
Call this from your host’s idle callback:
// Maya scriptJob idleEvent / Unity EditorApplication.update / …
dispatch.pump(std::time::Duration::from_millis(8));Returns the number of items processed.
Sourcepub fn pending_count(&self) -> usize
pub fn pending_count(&self) -> usize
Number of items currently waiting in the queue.
Trait Implementations§
Source§impl Clone for ReentrantDispatch
impl Clone for ReentrantDispatch
Source§fn clone(&self) -> ReentrantDispatch
fn clone(&self) -> ReentrantDispatch
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more