Skip to content

Commit b183ef2

Browse files
authored
Rollup merge of #77648 - fusion-engineering-forks:static-mutex, r=dtolnay
Static mutex is static StaticMutex is only ever used with as a static (as the name already suggests). So it doesn't have to be generic over a lifetime, but can simply assume 'static. This 'static lifetime guarantees the object is never moved, so this is no longer a manually checked requirement for unsafe calls to lock(). @rustbot modify labels: +T-libs +A-concurrency +C-cleanup
2 parents 085399f + 44a2af3 commit b183ef2

File tree

3 files changed

+8
-14
lines changed

3 files changed

+8
-14
lines changed

library/std/src/sys/unix/os.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ pub unsafe fn environ() -> *mut *const *const c_char {
470470
&mut environ
471471
}
472472

473-
pub unsafe fn env_lock() -> StaticMutexGuard<'static> {
473+
pub unsafe fn env_lock() -> StaticMutexGuard {
474474
// It is UB to attempt to acquire this mutex reentrantly!
475475
static ENV_LOCK: StaticMutex = StaticMutex::new();
476476
ENV_LOCK.lock()

library/std/src/sys/vxworks/os.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ pub unsafe fn environ() -> *mut *const *const c_char {
212212
&mut environ
213213
}
214214

215-
pub unsafe fn env_lock() -> StaticMutexGuard<'static> {
215+
pub unsafe fn env_lock() -> StaticMutexGuard {
216216
// It is UB to attempt to acquire this mutex reentrantly!
217217
static ENV_LOCK: StaticMutex = StaticMutex::new();
218218
ENV_LOCK.lock()

library/std/src/sys_common/mutex.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use crate::sys::mutex as imp;
33
/// An OS-based mutual exclusion lock, meant for use in static variables.
44
///
55
/// This mutex has a const constructor ([`StaticMutex::new`]), does not
6-
/// implement `Drop` to cleanup resources, and causes UB when moved or used
7-
/// reentrantly.
6+
/// implement `Drop` to cleanup resources, and causes UB when used reentrantly.
87
///
98
/// This mutex does not implement poisoning.
109
///
@@ -16,31 +15,26 @@ unsafe impl Sync for StaticMutex {}
1615

1716
impl StaticMutex {
1817
/// Creates a new mutex for use.
19-
///
20-
/// Behavior is undefined if the mutex is moved after it is
21-
/// first used with any of the functions below.
22-
/// Also, the behavior is undefined if this mutex is ever used reentrantly,
23-
/// i.e., `lock` is called by the thread currently holding the lock.
2418
pub const fn new() -> Self {
2519
Self(imp::Mutex::new())
2620
}
2721

2822
/// Calls raw_lock() and then returns an RAII guard to guarantee the mutex
2923
/// will be unlocked.
3024
///
31-
/// It is undefined behaviour to call this function while locked, or if the
32-
/// mutex has been moved since the last time this was called.
25+
/// It is undefined behaviour to call this function while locked by the
26+
/// same thread.
3327
#[inline]
34-
pub unsafe fn lock(&self) -> StaticMutexGuard<'_> {
28+
pub unsafe fn lock(&'static self) -> StaticMutexGuard {
3529
self.0.lock();
3630
StaticMutexGuard(&self.0)
3731
}
3832
}
3933

4034
#[must_use]
41-
pub struct StaticMutexGuard<'a>(&'a imp::Mutex);
35+
pub struct StaticMutexGuard(&'static imp::Mutex);
4236

43-
impl Drop for StaticMutexGuard<'_> {
37+
impl Drop for StaticMutexGuard {
4438
#[inline]
4539
fn drop(&mut self) {
4640
unsafe {

0 commit comments

Comments
 (0)