[][src]Struct conquer_util::BoundedThreadLocal

pub struct BoundedThreadLocal<'s, T> { /* fields omitted */ }

A one-shot, lock-free, bounded per-value thread local storage.

Examples

use std::thread;
use std::sync::Arc;

use conquer_util::BoundedThreadLocal;

const THREADS: usize = 4;
let counter = Arc::new(BoundedThreadLocal::<usize>::new(THREADS));

let handles: Vec<_> = (0..THREADS)
    .map(|id| {
        let counter = Arc::clone(&counter);
        thread::spawn(move || {
            let mut token = counter.thread_token().unwrap();
            *token.get_mut() += id;
        })
    })
    .collect();

for handle in handles {
    handle.join().unwrap();
}

let mut counter = Arc::try_unwrap(counter).unwrap();

let sum: usize = counter.into_iter().sum();
assert_eq!(sum, (0..4).sum());

Implementations

impl<'s, T: Default> BoundedThreadLocal<'s, T>[src]

pub fn new(max_threads: usize) -> Self[src]

Creates a new Default initialized BoundedThreadLocal that internally allocates a buffer of max_size.

Panics

This method panics, if max_size is 0.

impl<'s, T> BoundedThreadLocal<'s, T>[src]

pub fn with_init(max_threads: usize, init: impl Fn() -> T) -> Self[src]

Creates a new BoundedThreadLocal that internally allocates a buffer of max_size and initializes each Local with init.

Panics

This method panics, if max_size is 0.

impl<'s, T> BoundedThreadLocal<'s, T>[src]

pub const unsafe fn with_buffer(buf: &'s [Local<T>]) -> Self[src]

Creates a new BoundedThreadLocal that is based on a separate buffer.

Safety

The given buf must be treated as if it were mutably, i.e. it must not be used or otherwise accessed during the lifetime of the BoundedThreadLocal that borrows it.

Examples

use conquer_util::{BoundedThreadLocal, Local};

static BUF: [Local<usize>; 4] =
    [Local::new(0), Local::new(0), Local::new(0), Local::new(0)];
static TLS: BoundedThreadLocal<usize> = unsafe { BoundedThreadLocal::with_buffer(&BUF) };
assert_eq!(TLS.thread_token().unwrap().get(), &0);

pub fn thread_token(&self) -> Result<Token<'_, '_, T>, BoundsError>[src]

Returns a thread local token to a unique instance of T.

The thread local instance will not be dropped, when the token itself is dropped and can e.g. be iterated afterwards.

Errors

This method fails, if the maximum number of tokens for this BoundedThreadLocal has already been acquired.

Examples

use conquer_util::BoundedThreadLocal;


let tls = BoundedThreadLocal::<usize>::new(1);
let mut token = tls.thread_token()?;
*token.get_mut() += 1;
assert_eq!(token.get(), &1);

pub fn try_iter(&self) -> Result<Iter<'_, '_, T>, ConcurrentAccessErr>[src]

Attempts to create an [Iter] over all Local instances.

Errors

Fails, if there are still outstanding thread tokens, that might concurrently access any of the thread local state instances.

Trait Implementations

impl<T, '_> Debug for BoundedThreadLocal<'_, T>[src]

impl<'s, T> IntoIterator for BoundedThreadLocal<'s, T>[src]

type Item = T

The type of the elements being iterated over.

type IntoIter = IntoIter<'s, T>

Which kind of iterator are we turning this into?

impl<T, '_> Send for BoundedThreadLocal<'_, T>[src]

impl<T, '_> Sync for BoundedThreadLocal<'_, T>[src]

Auto Trait Implementations

impl<'s, T> !RefUnwindSafe for BoundedThreadLocal<'s, T>

impl<'s, T> Unpin for BoundedThreadLocal<'s, T>

impl<'s, T> !UnwindSafe for BoundedThreadLocal<'s, T>

Blanket Implementations

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

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

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

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

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

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.

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.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,