From c62c8cb82d18ee36bc47e12e5722d51cc011f133 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 6 Apr 2022 12:49:46 +0200 Subject: [PATCH 1/9] Add current_thread_unique_ptr() in std::sys_common. --- library/std/src/sys_common/thread_info.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/library/std/src/sys_common/thread_info.rs b/library/std/src/sys_common/thread_info.rs index 38c9e50009af5..cd570dca0ff57 100644 --- a/library/std/src/sys_common/thread_info.rs +++ b/library/std/src/sys_common/thread_info.rs @@ -30,6 +30,13 @@ impl ThreadInfo { } } +/// Get an address that is unique per running thread. +/// +/// This can be used as a non-null usize-sized ID. +pub fn current_thread_unique_ptr() -> usize { + THREAD_INFO.with(|info| <*const _>::addr(info)) +} + pub fn current_thread() -> Option { ThreadInfo::with(|info| info.thread.clone()) } From bd61bec67d23e11a37a19a3a554753419c734947 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 6 Apr 2022 12:50:02 +0200 Subject: [PATCH 2/9] Add futex-based ReentrantMutex on Linux. --- library/std/src/sys/unix/locks/futex.rs | 88 ++++++++++++++++++++++++- library/std/src/sys/unix/locks/mod.rs | 6 +- 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/library/std/src/sys/unix/locks/futex.rs b/library/std/src/sys/unix/locks/futex.rs index 630351d0dc278..f49fbda0d82bd 100644 --- a/library/std/src/sys/unix/locks/futex.rs +++ b/library/std/src/sys/unix/locks/futex.rs @@ -1,8 +1,10 @@ +use crate::cell::UnsafeCell; use crate::sync::atomic::{ - AtomicI32, + AtomicI32, AtomicUsize, Ordering::{Acquire, Relaxed, Release}, }; use crate::sys::futex::{futex_wait, futex_wake, futex_wake_all}; +use crate::sys_common::thread_info::current_thread_unique_ptr; use crate::time::Duration; pub type MovableMutex = Mutex; @@ -162,3 +164,87 @@ impl Condvar { r } } + +/// A reentrant mutex. Used by stdout().lock() and friends. +/// +/// The 'owner' field tracks which thread has locked the mutex. +/// +/// We use current_thread_unique_ptr() as the thread identifier, +/// which is just the address of a thread local variable. +/// +/// If `owner` is set to the identifier of the current thread, +/// we assume the mutex is already locked and instead of locking it again, +/// we increment `lock_count`. +/// +/// When unlocking, we decrement `lock_count`, and only unlock the mutex when +/// it reaches zero. +/// +/// `lock_count` is protected by the mutex and only accessed by the thread that has +/// locked the mutex, so needs no synchronization. +/// +/// `owner` can be checked by other threads that want to see if they already +/// hold the lock, so needs to be atomic. If it compares equal, we're on the +/// same thread that holds the mutex and memory access can use relaxed ordering +/// since we're not dealing with multiple threads. If it compares unequal, +/// synchronization is left to the mutex, making relaxed memory ordering for +/// the `owner` field fine in all cases. +pub struct ReentrantMutex { + mutex: Mutex, + owner: AtomicUsize, + lock_count: UnsafeCell, +} + +unsafe impl Send for ReentrantMutex {} +unsafe impl Sync for ReentrantMutex {} + +impl ReentrantMutex { + #[inline] + pub const unsafe fn uninitialized() -> Self { + Self { mutex: Mutex::new(), owner: AtomicUsize::new(0), lock_count: UnsafeCell::new(0) } + } + + #[inline] + pub unsafe fn init(&self) {} + + #[inline] + pub unsafe fn destroy(&self) {} + + pub unsafe fn try_lock(&self) -> bool { + let this_thread = current_thread_unique_ptr(); + if self.owner.load(Relaxed) == this_thread { + self.increment_lock_count(); + true + } else if self.mutex.try_lock() { + self.owner.store(this_thread, Relaxed); + *self.lock_count.get() = 1; + true + } else { + false + } + } + + pub unsafe fn lock(&self) { + let this_thread = current_thread_unique_ptr(); + if self.owner.load(Relaxed) == this_thread { + self.increment_lock_count(); + } else { + self.mutex.lock(); + self.owner.store(this_thread, Relaxed); + *self.lock_count.get() = 1; + } + } + + unsafe fn increment_lock_count(&self) { + *self.lock_count.get() = (*self.lock_count.get()) + .checked_add(1) + .expect("lock count overflow in reentrant mutex"); + } + + pub unsafe fn unlock(&self) { + *self.lock_count.get() -= 1; + if *self.lock_count.get() == 0 { + self.owner.store(0, Relaxed); + self.mutex.unlock(); + } + } +} diff --git a/library/std/src/sys/unix/locks/mod.rs b/library/std/src/sys/unix/locks/mod.rs index 85afc939d2e89..e0404f40c69bf 100644 --- a/library/std/src/sys/unix/locks/mod.rs +++ b/library/std/src/sys/unix/locks/mod.rs @@ -5,11 +5,7 @@ cfg_if::cfg_if! { ))] { mod futex; mod futex_rwlock; - #[allow(dead_code)] - mod pthread_mutex; // Only used for PthreadMutexAttr, needed by pthread_remutex. - mod pthread_remutex; // FIXME: Implement this using a futex - pub use futex::{Mutex, MovableMutex, Condvar, MovableCondvar}; - pub use pthread_remutex::ReentrantMutex; + pub use futex::{Mutex, MovableMutex, Condvar, MovableCondvar, ReentrantMutex}; pub use futex_rwlock::{RwLock, MovableRwLock}; } else { mod pthread_mutex; From ebebe6f837b3499efb5cf1eb95e7eeffbc5e477a Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 6 Apr 2022 13:50:45 +0200 Subject: [PATCH 3/9] Make current_thread_unique_ptr work during thread destruction. Otherwise we can't use println!() within atexit handlers etc. --- library/std/src/sys_common/thread_info.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/std/src/sys_common/thread_info.rs b/library/std/src/sys_common/thread_info.rs index cd570dca0ff57..b8f2e658214b9 100644 --- a/library/std/src/sys_common/thread_info.rs +++ b/library/std/src/sys_common/thread_info.rs @@ -34,7 +34,9 @@ impl ThreadInfo { /// /// This can be used as a non-null usize-sized ID. pub fn current_thread_unique_ptr() -> usize { - THREAD_INFO.with(|info| <*const _>::addr(info)) + // Use a non-drop type to make sure it's still available during thread destruction. + thread_local! { static X: u8 = 0 } + X.with(|x| <*const _>::addr(x)) } pub fn current_thread() -> Option { From 319a9b0f71d21409858297357bc047fb7a6ba27f Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 6 Apr 2022 22:12:47 +0200 Subject: [PATCH 4/9] Move current_thread_unique_ptr to the only module that uses it. --- library/std/src/sys/unix/locks/futex.rs | 10 +++++++++- library/std/src/sys_common/thread_info.rs | 9 --------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/library/std/src/sys/unix/locks/futex.rs b/library/std/src/sys/unix/locks/futex.rs index f49fbda0d82bd..1df7b53254654 100644 --- a/library/std/src/sys/unix/locks/futex.rs +++ b/library/std/src/sys/unix/locks/futex.rs @@ -4,7 +4,6 @@ use crate::sync::atomic::{ Ordering::{Acquire, Relaxed, Release}, }; use crate::sys::futex::{futex_wait, futex_wake, futex_wake_all}; -use crate::sys_common::thread_info::current_thread_unique_ptr; use crate::time::Duration; pub type MovableMutex = Mutex; @@ -248,3 +247,12 @@ impl ReentrantMutex { } } } + +/// Get an address that is unique per running thread. +/// +/// This can be used as a non-null usize-sized ID. +pub fn current_thread_unique_ptr() -> usize { + // Use a non-drop type to make sure it's still available during thread destruction. + thread_local! { static X: u8 = 0 } + X.with(|x| <*const _>::addr(x)) +} diff --git a/library/std/src/sys_common/thread_info.rs b/library/std/src/sys_common/thread_info.rs index b8f2e658214b9..38c9e50009af5 100644 --- a/library/std/src/sys_common/thread_info.rs +++ b/library/std/src/sys_common/thread_info.rs @@ -30,15 +30,6 @@ impl ThreadInfo { } } -/// Get an address that is unique per running thread. -/// -/// This can be used as a non-null usize-sized ID. -pub fn current_thread_unique_ptr() -> usize { - // Use a non-drop type to make sure it's still available during thread destruction. - thread_local! { static X: u8 = 0 } - X.with(|x| <*const _>::addr(x)) -} - pub fn current_thread() -> Option { ThreadInfo::with(|info| info.thread.clone()) } From 43651aa34fd7762058aea6920d2e401561d97076 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 6 Apr 2022 22:14:43 +0200 Subject: [PATCH 5/9] Initialize thread local with const{}. --- library/std/src/sys/unix/locks/futex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/sys/unix/locks/futex.rs b/library/std/src/sys/unix/locks/futex.rs index 1df7b53254654..0c7fb098c5f63 100644 --- a/library/std/src/sys/unix/locks/futex.rs +++ b/library/std/src/sys/unix/locks/futex.rs @@ -253,6 +253,6 @@ impl ReentrantMutex { /// This can be used as a non-null usize-sized ID. pub fn current_thread_unique_ptr() -> usize { // Use a non-drop type to make sure it's still available during thread destruction. - thread_local! { static X: u8 = 0 } + thread_local! { static X: u8 = const { 0 } } X.with(|x| <*const _>::addr(x)) } From 83e8b9e4ddf41def776ad7d3724f308e689ad063 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 6 Apr 2022 22:15:43 +0200 Subject: [PATCH 6/9] Add debug asserts to futex ReentrantMutex impl. --- library/std/src/sys/unix/locks/futex.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/std/src/sys/unix/locks/futex.rs b/library/std/src/sys/unix/locks/futex.rs index 0c7fb098c5f63..d97777e4da29d 100644 --- a/library/std/src/sys/unix/locks/futex.rs +++ b/library/std/src/sys/unix/locks/futex.rs @@ -215,6 +215,7 @@ impl ReentrantMutex { true } else if self.mutex.try_lock() { self.owner.store(this_thread, Relaxed); + debug_assert_eq!(*self.lock_count.get(), 0); *self.lock_count.get() = 1; true } else { @@ -229,6 +230,7 @@ impl ReentrantMutex { } else { self.mutex.lock(); self.owner.store(this_thread, Relaxed); + debug_assert_eq!(*self.lock_count.get(), 0); *self.lock_count.get() = 1; } } From 5b2591299a2c179301849e76cc137b0e39b68367 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 6 Apr 2022 22:22:43 +0200 Subject: [PATCH 7/9] Add #[deny(unsafe_op_in_unsafe_fn)] to thread_local!(const). This avoids 'unused unsafe' warnings when using this feature inside std. --- library/std/src/thread/local.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/std/src/thread/local.rs b/library/std/src/thread/local.rs index fc307c5666d63..99baca66df092 100644 --- a/library/std/src/thread/local.rs +++ b/library/std/src/thread/local.rs @@ -179,6 +179,7 @@ macro_rules! __thread_local_inner { // used to generate the `LocalKey` value for const-initialized thread locals (@key $t:ty, const $init:expr) => {{ #[cfg_attr(not(windows), inline(always))] // see comments below + #[deny(unsafe_op_in_unsafe_fn)] unsafe fn __getit( _init: $crate::option::Option<&mut $crate::option::Option<$t>>, ) -> $crate::option::Option<&'static $t> { From 8a2c9a96159fcca6d35efb94b900d8be79395ea9 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sat, 9 Apr 2022 16:13:25 +0200 Subject: [PATCH 8/9] Allow cvt_nz to be unused on some platforms. --- library/std/src/sys/unix/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/std/src/sys/unix/mod.rs b/library/std/src/sys/unix/mod.rs index e65c11b6d09fa..3ad03a8868199 100644 --- a/library/std/src/sys/unix/mod.rs +++ b/library/std/src/sys/unix/mod.rs @@ -215,6 +215,7 @@ where } } +#[allow(dead_code)] // Not used on all platforms. pub fn cvt_nz(error: libc::c_int) -> crate::io::Result<()> { if error == 0 { Ok(()) } else { Err(crate::io::Error::from_raw_os_error(error)) } } From d4e44a63910c0e2d06c54534538b4a6e41348556 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 11 Apr 2022 14:06:18 +0200 Subject: [PATCH 9/9] Add missing unsafe marker. This is now necessary because of deny(unsafe_op_in_unsafe_fn). --- library/std/src/thread/local.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/thread/local.rs b/library/std/src/thread/local.rs index 99baca66df092..047d07ad57df1 100644 --- a/library/std/src/thread/local.rs +++ b/library/std/src/thread/local.rs @@ -194,7 +194,7 @@ macro_rules! __thread_local_inner { #[cfg(all(target_family = "wasm", not(target_feature = "atomics")))] { static mut VAL: $t = INIT_EXPR; - $crate::option::Option::Some(&VAL) + unsafe { $crate::option::Option::Some(&VAL) } } // If the platform has support for `#[thread_local]`, use it.