pub struct TaskSpawner<T: Send + 'static> { /* private fields */ }Expand description
A cheap-to-clone handle for scheduling background tasks onto the
shared pool. Held by the shell and handed to the plugin through
InitContext, ProcessContext, and the editor’s PluginContext.
A spawner built with Self::new may run its handler concurrently
with itself for one instance (see the module’s Concurrency section);
Self::new_serialized runs it one at a time.
Implementations§
Source§impl<T: Send + 'static> TaskSpawner<T>
impl<T: Send + 'static> TaskSpawner<T>
Sourcepub fn new(run: impl Fn(T) + Send + Sync + 'static) -> Self
pub fn new(run: impl Fn(T) + Send + Sync + 'static) -> Self
Register an instance’s handler with the shared pool. run is the
monomorphized move |task| task.run(¶ms), built once
by the shell. The pool itself is not started until the first task
is actually scheduled, so constructing a spawner for a plugin that
never schedules costs only the (small) inbound queue.
The handler may run concurrently with itself for one instance; use
Self::new_serialized for a handler that isn’t reentrancy-safe.
Sourcepub fn new_serialized(run: impl Fn(T) + Send + Sync + 'static) -> Self
pub fn new_serialized(run: impl Fn(T) + Send + Sync + 'static) -> Self
Like Self::new, but the pool runs the handler for a given
instance one at a time (“one-slot” mode). The shell selects this
when the plugin’s BackgroundTask::SERIALIZED is true.
Sourcepub fn try_spawn(&self, task: T) -> Result<(), T>
pub fn try_spawn(&self, task: T) -> Result<(), T>
Enqueue a task, running it on the pool as soon as a worker is
free. Wait-free. Returns Err(task) if the inbound queue is full
(the audio thread decides what to do - drop, or coalesce via
Self::spawn_coalescing - rather than block).
§Errors
Returns the task back when the preallocated inbound queue is full.
Sourcepub fn spawn_coalescing(&self, task: T)
pub fn spawn_coalescing(&self, task: T)
Post a task into the single coalescing slot, replacing any
still-unrun target. Wait-free, never rejects. Only the newest
survives and the worker runs it at most once per drain, so a knob
sweep that outruns the handler collapses to one execution, not one
build per intermediate target. The displaced target drops on the
caller (the audio thread on the hot path), so a coalescing task
type should be cheap to drop - a small Copy request, not an
owned buffer.