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>
impl<'s, T: Default> BoundedThreadLocal<'s, T>
Source§impl<'s, T> BoundedThreadLocal<'s, T>
impl<'s, T> BoundedThreadLocal<'s, T>
Source§impl<'s, T> BoundedThreadLocal<'s, T>
impl<'s, T> BoundedThreadLocal<'s, T>
Sourcepub const unsafe fn with_buffer(buf: &'s [Local<T>]) -> Self
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);
Sourcepub fn thread_token(&self) -> Result<Token<'_, '_, T>, BoundsError>
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);
Trait Implementations§
Source§impl<T> Debug for BoundedThreadLocal<'_, T>
impl<T> Debug for BoundedThreadLocal<'_, T>
Source§impl<'s, T> IntoIterator for BoundedThreadLocal<'s, T>
impl<'s, T> IntoIterator for BoundedThreadLocal<'s, T>
impl<T> Send for BoundedThreadLocal<'_, T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more