Skip to main content

ThreadLocalCtx

Struct ThreadLocalCtx 

Source
pub struct ThreadLocalCtx<T, F> { /* private fields */ }
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§

Source§

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

Source

pub fn new(inner: F) -> Self

Create a new TlsCtx

§Examples

Creating a thread-local byte buffer:

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

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

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(); });
Source

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

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§

Source§

impl<T, F> IntoIterator for ThreadLocalCtx<T, F>

Consume the context and retrieve all created items.

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = ThreadNodeIterator<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

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

Source§

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<T, F> UnwindSafe for ThreadLocalCtx<T, F>

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 = Infallible

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

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

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.