Skip to main content

Pool

Struct Pool 

Source
pub struct Pool<'p> { /* private fields */ }
Expand description

A wrapper around a host’s borrowed, owned or shared Rayon pool.

tcontract never creates threads beyond this pool. Its size is fixed at construction (Rayon pools cannot resize). The embedded mutex serializes SPMD broadcasts through this wrapper. Hosts sharing a raw pool must retain one wrapper and serialize potentially conflicting numerical work, rather than constructing independent wrappers with separate SPMD mutexes.

§Examples

let tp = rayon::ThreadPoolBuilder::new().num_threads(3).build().unwrap();
let pool = tcontract_exec::Pool::borrow(&tp);
assert_eq!(pool.size(), 3);
assert_eq!(pool.stats().entries, 0);

Implementations§

Source§

impl<'p> Pool<'p>

Source

pub fn borrow(pool: &'p ThreadPool) -> Self

Borrow a host pool.

Source

pub fn workspace(&self) -> &ArenaProvider

The storage this pool lends to every operation that runs on it.

Borrowed, not copied: one pool is one owner, and a driver that is given this provider returns its team sets to it on drop.

Source

pub fn trim_workspace(&self)

Release the pool’s idle workspace storage. Live leases and borrowed worker buffers survive; a worker parked on this pool keeps its slot until it is idle again.

Source

pub fn size(&self) -> usize

Number of workers.

Source

pub fn into_owned(self) -> Option<ThreadPool>

Give back an owned pool (None for a borrowed or shared one), for example so a C host can drop it and join its workers. A shared wrapper releases only its Arc reference; other owners keep the raw pool alive.

Source

pub fn stats(&self) -> PoolStats

Snapshot of the entry counters.

Source

pub fn reset_stats(&self)

Zero the counters.

Source§

impl Pool<'static>

Source

pub fn shared(pool: Arc<ThreadPool>) -> Self

Share the host’s existing Rayon pool without creating another worker team.

Retain this wrapper across calls and share it (for example through an Arc<Pool<'static>>) across host clones to reuse workspace and SPMD serialization. Other numerical libraries may borrow the host’s original raw pool, but their work must obey the host’s same resource-loan protocol.

§Examples
use std::sync::Arc;
let raw = Arc::new(rayon::ThreadPoolBuilder::new().num_threads(2).build().unwrap());
let pool = tcontract_exec::Pool::shared(Arc::clone(&raw));
assert_eq!(pool.size(), raw.current_num_threads());
Source

pub fn owned(pool: ThreadPool) -> Self

Take ownership of a pool, for hosts without one of their own (the C ABI’s tcontract_tapp_executor_create_rayon). Rust hosts normally Pool::borrow.

§Examples
let tp = rayon::ThreadPoolBuilder::new().num_threads(2).build().unwrap();
let pool = tcontract_exec::Pool::owned(tp);
assert_eq!(pool.size(), 2);

Trait Implementations§

Source§

impl Debug for Pool<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'p> !Freeze for Pool<'p>

§

impl<'p> !RefUnwindSafe for Pool<'p>

§

impl<'p> !UnwindSafe for Pool<'p>

§

impl<'p> Send for Pool<'p>

§

impl<'p> Sync for Pool<'p>

§

impl<'p> Unpin for Pool<'p>

§

impl<'p> UnsafeUnpin for Pool<'p>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.