aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/thread_id.rs
blob: ef3928979634f053f9a2ebe46533e5c8a995773c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use std::num::NonZeroU64;

#[derive(Eq, PartialEq, Clone, Copy, Hash, Debug)]
pub(crate) struct ThreadId(NonZeroU64);

impl ThreadId {
    pub(crate) fn next() -> Self {
        use crate::loom::sync::atomic::{Ordering::Relaxed, StaticAtomicU64};

        static NEXT_ID: StaticAtomicU64 = StaticAtomicU64::new(0);

        let mut last = NEXT_ID.load(Relaxed);
        loop {
            let id = match last.checked_add(1) {
                Some(id) => id,
                None => exhausted(),
            };

            match NEXT_ID.compare_exchange_weak(last, id, Relaxed, Relaxed) {
                Ok(_) => return ThreadId(NonZeroU64::new(id).unwrap()),
                Err(id) => last = id,
            }
        }
    }
}

#[cold]
#[allow(dead_code)]
fn exhausted() -> ! {
    panic!("failed to generate unique thread ID: bitspace exhausted")
}