Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static mutex is static #77648

Merged
merged 2 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/std/src/sys/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ pub unsafe fn environ() -> *mut *const *const c_char {
&mut environ
}

pub unsafe fn env_lock() -> StaticMutexGuard<'static> {
pub unsafe fn env_lock() -> StaticMutexGuard {
// It is UB to attempt to acquire this mutex reentrantly!
static ENV_LOCK: StaticMutex = StaticMutex::new();
ENV_LOCK.lock()
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/vxworks/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ pub unsafe fn environ() -> *mut *const *const c_char {
&mut environ
}

pub unsafe fn env_lock() -> StaticMutexGuard<'static> {
pub unsafe fn env_lock() -> StaticMutexGuard {
// It is UB to attempt to acquire this mutex reentrantly!
static ENV_LOCK: StaticMutex = StaticMutex::new();
ENV_LOCK.lock()
Expand Down
18 changes: 6 additions & 12 deletions library/std/src/sys_common/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use crate::sys::mutex as imp;
/// An OS-based mutual exclusion lock, meant for use in static variables.
///
/// This mutex has a const constructor ([`StaticMutex::new`]), does not
/// implement `Drop` to cleanup resources, and causes UB when moved or used
/// reentrantly.
/// implement `Drop` to cleanup resources, and causes UB when used reentrantly.
///
/// This mutex does not implement poisoning.
///
Expand All @@ -16,31 +15,26 @@ unsafe impl Sync for StaticMutex {}

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

/// Calls raw_lock() and then returns an RAII guard to guarantee the mutex
/// will be unlocked.
///
/// It is undefined behaviour to call this function while locked, or if the
/// mutex has been moved since the last time this was called.
/// It is undefined behaviour to call this function while locked by the
/// same thread.
#[inline]
pub unsafe fn lock(&self) -> StaticMutexGuard<'_> {
pub unsafe fn lock(&'static self) -> StaticMutexGuard {
self.0.lock();
StaticMutexGuard(&self.0)
}
}

#[must_use]
pub struct StaticMutexGuard<'a>(&'a imp::Mutex);
pub struct StaticMutexGuard(&'static imp::Mutex);

impl Drop for StaticMutexGuard<'_> {
impl Drop for StaticMutexGuard {
#[inline]
fn drop(&mut self) {
unsafe {
Expand Down