Skip to content

Commit 44a2af3

Browse files
committed
Remove lifetime from StaticMutex and assume '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().
1 parent 5875657 commit 44a2af3

File tree

3 files changed

+7
-14
lines changed

3 files changed

+7
-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

+5-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,11 +15,6 @@ 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
}
@@ -29,19 +23,18 @@ impl StaticMutex {
2923
/// will be unlocked.
3024
///
3125
/// It is undefined behaviour to call this function while locked by the
32-
/// same thread, or if the mutex has been moved since the last time this
33-
/// was called.
26+
/// same thread.
3427
#[inline]
35-
pub unsafe fn lock(&self) -> StaticMutexGuard<'_> {
28+
pub unsafe fn lock(&'static self) -> StaticMutexGuard {
3629
self.0.lock();
3730
StaticMutexGuard(&self.0)
3831
}
3932
}
4033

4134
#[must_use]
42-
pub struct StaticMutexGuard<'a>(&'a imp::Mutex);
35+
pub struct StaticMutexGuard(&'static imp::Mutex);
4336

44-
impl Drop for StaticMutexGuard<'_> {
37+
impl Drop for StaticMutexGuard {
4538
#[inline]
4639
fn drop(&mut self) {
4740
unsafe {

0 commit comments

Comments
 (0)