Struct BoundedThreadLocal

Source
pub struct BoundedThreadLocal<'s, T> { /* private fields */ }
Expand description

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§

Source§

impl<'s, T: Default> BoundedThreadLocal<'s, T>

Source

pub fn new(max_threads: usize) -> Self

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

§Panics

This method panics, if max_size is 0.

Source§

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

Source

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

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.

Source§

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

Source

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

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

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

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

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

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§

Source§

impl<T> Debug for BoundedThreadLocal<'_, T>

Source§

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

Formats the value using the given formatter. Read more
Source§

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

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<'s, 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> Send for BoundedThreadLocal<'_, T>

Source§

impl<T> Sync for BoundedThreadLocal<'_, T>

Auto Trait Implementations§

§

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

§

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§

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, 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.
Source§

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

Source§

fn vzip(self) -> V