fragile/
thread_id.rs

1use std::num::NonZeroUsize;
2use std::sync::atomic::{AtomicUsize, Ordering};
3
4fn next() -> NonZeroUsize {
5    static COUNTER: AtomicUsize = AtomicUsize::new(1);
6    NonZeroUsize::new(COUNTER.fetch_add(1, Ordering::SeqCst)).expect("more than usize::MAX threads")
7}
8
9pub(crate) fn get() -> NonZeroUsize {
10    thread_local!(static THREAD_ID: NonZeroUsize = next());
11    THREAD_ID.with(|&x| x)
12}