-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rt: use internal ThreadId implementation (#5329)
The version provided by `std` has limitations, including no way to try to get a thread ID without panicking.
- Loading branch information
1 parent
048049f
commit c6552c5
Showing
5 changed files
with
66 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters