diff options
| author | joboet <jonasboettiger@icloud.com> | 2022-09-03 14:21:38 +0200 |
|---|---|---|
| committer | joboet <jonasboettiger@icloud.com> | 2022-10-13 12:55:14 +0200 |
| commit | 2d2c9e44939d44e03bd6b9922b584ff09fb513e6 (patch) | |
| tree | f055123aa87d53399723972db37c439ccba39999 /library/std/src/thread | |
| parent | fa0ca783f89a83046e6ce0383385ba5b28296435 (diff) | |
| download | rust-2d2c9e44939d44e03bd6b9922b584ff09fb513e6.tar.gz rust-2d2c9e44939d44e03bd6b9922b584ff09fb513e6.zip | |
std: use `sync::Mutex` for internal statics
Diffstat (limited to 'library/std/src/thread')
| -rw-r--r-- | library/std/src/thread/mod.rs | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/library/std/src/thread/mod.rs b/library/std/src/thread/mod.rs index 55110c44b6e..0a51e1a10f4 100644 --- a/library/std/src/thread/mod.rs +++ b/library/std/src/thread/mod.rs @@ -1118,24 +1118,21 @@ impl ThreadId { } } } else { - use crate::sys_common::mutex::StaticMutex; + use crate::sync::{Mutex, PoisonError}; - // It is UB to attempt to acquire this mutex reentrantly! - static GUARD: StaticMutex = StaticMutex::new(); - static mut COUNTER: u64 = 0; + static COUNTER: Mutex<u64> = Mutex::new(0); - unsafe { - let guard = GUARD.lock(); + let mut counter = COUNTER.lock().unwrap_or_else(PoisonError::into_inner); + let Some(id) = counter.checked_add(1) else { + // in case the panic handler ends up calling `ThreadId::new()`, + // avoid reentrant lock acquire. + drop(counter); + exhausted(); + }; - let Some(id) = COUNTER.checked_add(1) else { - drop(guard); // in case the panic handler ends up calling `ThreadId::new()`, avoid reentrant lock acquire. - exhausted(); - }; - - COUNTER = id; - drop(guard); - ThreadId(NonZeroU64::new(id).unwrap()) - } + *counter = id; + drop(counter); + ThreadId(NonZeroU64::new(id).unwrap()) } } } |
