Skip to main content

Module thread_pump

Module thread_pump 

Source
Expand description

Thread affinity and cooperative main-thread pump for TaskManager.

Most DCC and game-engine host APIs are UI / main-thread pinned — calling them from a worker thread either throws or causes a segfault. This module adds a first-class ThreadAffinity concept to the [TaskManager] ecosystem and a cooperative MainThreadPump so the host can drain the main-thread work queue from its own idle callback.

§Design

  • Yield points are cooperative: a task must periodically check [TaskHandle::is_cancelled] or simply complete quickly. There is no pre-emption, mirroring the Go scheduler and Unity’s EditorApplication.
  • MainThreadPump::pump drains work items up to a wall-clock budget — it never blocks longer than that, keeping the host’s frame rate intact.

§DCC host integration examples

HostIdle callback
Mayacmds.scriptJob(idleEvent=pump_fn)
Houdinihdefereval.execute_deferred_after_waiting
3dsMaxpymxs.run_at_ui_idle
Blenderbpy.app.timers.register
Unity EditorEditorApplication.update
UnrealFTSTicker / AsyncTask(ENamedThreads::GameThread, …)

§Example

use ipckit::{MainThreadPump, ThreadAffinity, TaskManager, TaskBuilder};
use std::time::Duration;

let manager = TaskManager::new(Default::default());
let pump = MainThreadPump::new();

// Register a "main-thread" task
let handle = manager.create(
    TaskBuilder::new("update-ui", "ui")
        .affinity(ThreadAffinity::Main)
);

// Dispatch work to the main thread
pump.dispatch(move || {
    handle.start();
    handle.complete(serde_json::json!({"updated": true}));
});

// In the host idle callback:
let stats = pump.pump(Duration::from_millis(8));
assert_eq!(stats.processed, 1);

Structs§

MainThreadPump
Cooperative pump that drains main-thread work items on demand.
PumpStats
Statistics returned by a single MainThreadPump::pump call.

Enums§

ThreadAffinity
Controls which thread a task must execute on.