diff options
Diffstat (limited to 'src/thread_id.rs')
-rw-r--r-- | src/thread_id.rs | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/thread_id.rs b/src/thread_id.rs new file mode 100644 index 0000000..00468b2 --- /dev/null +++ b/src/thread_id.rs @@ -0,0 +1,12 @@ +use std::num::NonZeroUsize; +use std::sync::atomic::{AtomicUsize, Ordering}; + +fn next() -> NonZeroUsize { + static COUNTER: AtomicUsize = AtomicUsize::new(1); + NonZeroUsize::new(COUNTER.fetch_add(1, Ordering::SeqCst)).expect("more than usize::MAX threads") +} + +pub(crate) fn get() -> NonZeroUsize { + thread_local!(static THREAD_ID: NonZeroUsize = next()); + THREAD_ID.with(|&x| x) +} |