Struct rayon_tlsctx::ThreadLocalCtx[][src]

pub struct ThreadLocalCtx<T, F> { /* fields omitted */ }
Expand description

A thread local storage container for Rayon jobs

This context can be used to efficiently clone inner, only when it’s necessary.

Implementations

impl<T, F: Fn() -> T> ThreadLocalCtx<T, F>[src]

pub fn new(inner: F) -> Self[src]

Create a new TlsCtx

Examples

Creating a thread-local byte buffer:

use rayon_tlsctx::ThreadLocalCtx;
let ctx = ThreadLocalCtx::new(Vec::<u8>::new);

pub fn new_locked(inner: F) -> ThreadLocalCtx<T, impl Fn() -> T>[src]

Create a new TlsCtx.

This context utilises a lock for cloning values, making it usable for non-sync types.

Cloning an initialised buffer for each thread:

use rayon_tlsctx::ThreadLocalCtx;
use rayon::iter::*;
use std::cell::Cell;

let mut buf: Vec<u16> = (0..!0).collect();
buf.shuffle(&mut rng);

let buf = (buf, Cell::new(0));

// Must use new_locked, because cloning a cell across threads is not allowed
let ctx = ThreadLocalCtx::new_locked(move || buf.clone());

(0..16).into_par_iter().for_each(|_| unsafe { ctx.get(); });

pub unsafe fn get(&self) -> ThreadLocalMut<'_, T>[src]

Get a thread local context reference with dynamically checked borrow rules

Remarks

It is advised to manually drop the borrowed context if it is to be reborrowed within inner scope. This is because chances of heap allocations significantly increase.

Safety

Only one thread pool should use this scope (throughout the duration of its lifetime). The thread pool size can not grow midway through.

Examples

use rayon_tlsctx::ThreadLocalCtx;
use rayon::iter::*;

const NUM_COPIES: usize = 16;

let mut buf: Vec<u16> = (0..!0).collect();

// Create a thread local context with value 0.
let ctx = ThreadLocalCtx::new(|| 0);

// Sum the buffer `NUM_COPIES` times and accumulate the results
// into the threaded pool of counts. Note that the counts may be
// Unevenly distributed.
(0..NUM_COPIES)
    .into_par_iter()
    .flat_map(|_| buf.par_iter())
    .for_each(|i| {
        let mut cnt = unsafe { ctx.get() };
        *cnt += *i as usize;
    });

let buf_sum = buf.into_iter().fold(0, |acc, i| acc + i as usize);

// What matters is that the final sum matches the expected value.
assert_eq!(ctx.into_iter().sum::<usize>(), buf_sum * NUM_COPIES);

Trait Implementations

impl<T, F> IntoIterator for ThreadLocalCtx<T, F>[src]

Consume the context and retrieve all created items.

type Item = T

The type of the elements being iterated over.

type IntoIter = ThreadNodeIterator<T>

Which kind of iterator are we turning this into?

fn into_iter(self) -> Self::IntoIter[src]

Creates an iterator from a value. Read more

impl<T, F: Send + Sync> Send for ThreadLocalCtx<T, F>[src]

impl<T, F: Send + Sync> Sync for ThreadLocalCtx<T, F>[src]

Auto Trait Implementations

impl<T, F> !RefUnwindSafe for ThreadLocalCtx<T, F>

impl<T, F> Unpin for ThreadLocalCtx<T, F> where
    F: Unpin,
    T: Unpin

impl<T, F> UnwindSafe for ThreadLocalCtx<T, F> where
    F: UnwindSafe,
    T: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Pointable for T

pub const ALIGN: usize

The alignment of pointer.

type Init = T

The type for initializers.

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

Initializes a with the given initializer. Read more

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

Dereferences the given pointer. Read more

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

Mutably dereferences the given pointer. Read more

pub unsafe fn drop(ptr: usize)

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

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.