Skip to main content

Pool

Trait Pool 

Source
pub trait Pool: Sync {
    // Required methods
    fn workers(&self) -> usize;
    fn broadcast(&self, copies: usize, job: &(dyn Fn() + Sync));
}
Expand description

Threads a host lends the kernel, where std::thread cannot start one.

Once set with set_pool, every parallel stage runs its workers through Pool::broadcast instead of starting threads of its own.

The pool’s workers must not need the thread that calls into the kernel to make progress: that thread blocks inside Pool::broadcast until every copy has returned. A browser’s main thread cannot block, so a browser host calls the kernel from a worker of its own.

A rayon-backed host:

struct Rayon;
impl ogeom_core::parallel::Pool for Rayon {
    fn workers(&self) -> usize {
        rayon::current_num_threads()
    }
    fn broadcast(&self, copies: usize, job: &(dyn Fn() + Sync)) {
        rayon::scope(|s| {
            for _ in 0..copies {
                s.spawn(|_| job());
            }
        });
    }
}

Required Methods§

Source

fn workers(&self) -> usize

How many workers the pool runs at once.

Source

fn broadcast(&self, copies: usize, job: &(dyn Fn() + Sync))

Run job on copies workers at once and return when every copy has returned.

Each copy takes items until none are left, so a copy that starts late or never costs time, not results: whatever the copies leave, the caller computes after broadcast returns. A copy does not unwind into the pool: a panic in a stage is caught in the copy and resumed on the caller once broadcast returns.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§