1
Fork 0

std: use sync::Mutex for internal statics

This commit is contained in:
joboet 2022-09-03 14:21:38 +02:00
parent fa0ca783f8
commit 2d2c9e4493
No known key found for this signature in database
GPG key ID: 704E0149B0194B3C
7 changed files with 47 additions and 123 deletions

View file

@ -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())
}
}
}