[][src]Struct conquer_util::BoundedThreadLocal

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

A lock-free bounded per-value thread local storage.

Methods

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

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

Creates a new Default initialized BoundedThreadLocal that borrows the specified buffer.

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_buffer_and_init(
    buffer: &'s [Local<T>],
    init: impl Fn() -> T
) -> Self
[src]

Creates a new BoundedThreadLocal that borrows the specified buffer and initializes each Local with init.

If the the buffer has previously used by a BoundedThreadLocal, the previous values are dropped upon initialization.

Examples

use conquer_util::{BoundedThreadLocal, Local};

let buf: [Local<i32>; 3] = [Local::new(), Local::new(), Local::new()];
let tls = BoundedThreadLocal::with_buffer_and_init(&buf, || -1);

assert_eq!(tls.thread_token().unwrap().get(), &-1);
assert_eq!(tls.thread_token().unwrap().get(), &-1);
assert_eq!(tls.thread_token().unwrap().get(), &-1);
assert!(tls.thread_token().is_err());

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.

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::new(1);
let mut token = tls.thread_token()?;
token.update(|local| *local = 1);
assert_eq!(token.get(), &1);

Important traits for IterMut<'s, 'tls, T>
pub fn iter(&mut self) -> IterMut<'s, '_, T>[src]

Creates an IterMut over all Local instances.

The iterator itself yields immutable items but the method itself requires a mutable reference in order to ensure there can be no concurrent accesses by other threads during the iteration.

Examples

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

use conquer_util::BoundedThreadLocal;

const THREADS: usize = 4;
let counter = Arc::new(BoundedThreadLocal::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.update(|curr| *curr = id)
        })
    })
    .collect();

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

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

let sum: usize = counter.iter().map(|c| *c).sum();
assert_eq!(sum, (0..4).sum());

Trait Implementations

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]

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

Auto Trait Implementations

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

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

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

Blanket Implementations

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

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

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

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<T> BorrowMut<T> for T where
    T: ?Sized
[src]

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

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

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