Skip to main content

ReentrantDispatch

Struct ReentrantDispatch 

Source
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 f inline, return immediately.
  • Caller is any other thread → push f to the queue, block until the affinity thread drains it via ReentrantDispatch::pump.

Implementations§

Source§

impl ReentrantDispatch

Source

pub fn new() -> Self

Create a new dispatch queue.

Source

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.

Source

pub fn is_affinity_thread(&self) -> bool

Returns true if the calling thread is the bound affinity thread.

Source

pub fn submit_reentrant<F, R>(&self, f: F) -> Result<R>
where F: FnOnce() -> R + Send + 'static, R: Send + 'static,

Submit f to the affinity thread in a reentrancy-safe way.

  • If the current thread is the affinity thread, f is called inline and its return value is propagated immediately — no queue, no blocking.
  • Otherwise, f is pushed into the work queue and the calling thread blocks until the affinity thread processes it via pump.
§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.

Source

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.

Source

pub fn pending_count(&self) -> usize

Number of items currently waiting in the queue.

Trait Implementations§

Source§

impl Clone for ReentrantDispatch

Source§

fn clone(&self) -> ReentrantDispatch

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for ReentrantDispatch

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoMetered for T

Source§

fn metered(self, metrics: Arc<ChannelMetrics>) -> MeteredSender<Self>

Wrap this sender with metrics tracking.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithMetrics for T

Source§

fn with_metrics(self) -> MeteredWrapper<Self>

Wrap this channel with metrics tracking.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more