diff options
| author | bors <bors@rust-lang.org> | 2022-10-15 22:49:30 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-10-15 22:49:30 +0000 |
| commit | ddc7fd983736bef0e610e02f5ef08955ecd562bb (patch) | |
| tree | 3576fb11f3b5836059c8081fb43f5caffe13c8cd /library/std/src/thread | |
| parent | b8c35ca26b191bb9a9ac669a4b3f4d3d52d97fb1 (diff) | |
| parent | 2d2c9e44939d44e03bd6b9922b584ff09fb513e6 (diff) | |
| download | rust-ddc7fd983736bef0e610e02f5ef08955ecd562bb.tar.gz rust-ddc7fd983736bef0e610e02f5ef08955ecd562bb.zip | |
Auto merge of #100579 - joboet:sync_mutex_everywhere, r=thomcc
std: use `sync::Mutex` for internal statics Since `sync::Mutex` is now `const`-constructible, it can be used for internal statics, removing the need for `sys_common::StaticMutex`. This adds some extra allocations on platforms which need to box their mutexes (currently SGX and some UNIX), but these will become unnecessary with the lock improvements tracked in #93740. I changed the program argument implementation on Hermit, it does not need `Mutex` but can use atomics like some UNIX systems (ping `@mkroening` `@stlankes).`
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 5d4a236bd2c..05023df1bb2 100644 --- a/library/std/src/thread/mod.rs +++ b/library/std/src/thread/mod.rs @@ -1128,24 +1128,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()) } } } |
