Skip to main content

BackgroundTasks

Trait BackgroundTasks 

Source
pub trait BackgroundTasks {
    type Params: Params;
    type Task: Send + 'static;

    // Required method
    fn run_task(task: Self::Task, params: &Self::Params);
}
Expand description

Opt-in managed background work. A plugin implements this in addition to its leaf trait to declare a Send task type and a handler the framework runs on a shared background-thread pool, then wires it in with the tasks: key on the truce::plugin! macro. Nothing changes for a plugin that doesn’t implement it.

run_task runs off the audio thread and reaches shared state through params (its #[skip] channels / atomics), exactly like the editor: it must never touch DspState, which is audio-thread-exclusive. Feedback to the audio thread stays the plugin’s job through those #[skip] channels.

Keep handlers short and non-blocking. The pool is shared by every truce plugin in the host and small (available_parallelism() - 1 threads, as few as one), so a handler that blocks on I/O (reading a sample off disk) or waits on a lock stalls background work for every other instance too, not just its own. Allocation and CPU-bound bursts are fine - that is what the pool is for. For work that genuinely blocks or runs long, give the plugin its own thread with AudioTap::spawn_worker rather than the shared pool.

Schedule tasks with ctx.tasks::<Task>() from process (wait-free), the editor’s PluginContext, or the InitContext passed to init.

ⓘ
impl BackgroundTasks for Reverb {
    type Params = ReverbParams;
    type Task = Rebuild;
    fn run_task(task: Rebuild, params: &ReverbParams) {
        let graph = build_graph(task.sample_rate, task.time_s);
        let _ = params.ready.force_push(graph);   // #[skip] handoff
    }
}

Required Associated Types§

Source

type Params: Params

The plugin’s parameter struct; must match the leaf trait’s type Params.

Source

type Task: Send + 'static

A unit of off-thread work. Send because the pool moves it across threads; 'static because the worker outlives any block.

Required Methods§

Source

fn run_task(task: Self::Task, params: &Self::Params)

Run one task on the pool. See the trait docs for the contract.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§