-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Replace ReentrantMutex by a futex-based one on Linux. #95727
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
Changes from all commits
c62c8cb
bd61bec
ebebe6f
319a9b0
43651aa
83e8b9e
5b25912
8a2c9a9
d4e44a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
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}; | ||
|
@@ -162,3 +163,98 @@ 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That might not work if a thread doesn't unlock and then exits. Another new thread may then suddenly acquire the lock if the allocator happens to return the same pointer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a ThreadId type you can use instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That should work out fine, as the old thread doesn't exist anymore, it's fine for the new thread to take over ownership of the lock.
I am aware, but the ThreadId type is 64-bit, which is problematic on some 32-bit platforms. |
||
/// | ||
/// 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<u32>, | ||
} | ||
|
||
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); | ||
m-ou-se marked this conversation as resolved.
Show resolved
Hide resolved
|
||
debug_assert_eq!(*self.lock_count.get(), 0); | ||
*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(); | ||
m-ou-se marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.owner.store(this_thread, Relaxed); | ||
debug_assert_eq!(*self.lock_count.get(), 0); | ||
*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(); | ||
} | ||
} | ||
} | ||
|
||
/// 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 = const { 0 } } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we avoid creating new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That'd be nice. I used the address of the THREAD_INFO thread local in an earlier version, to avoid creating a new one, but that one needs Drop, which makes it inaccessible during thread shutdown. We don't have a good way of getting the address of such a thread local's storage after its destructor ran, since its getter function just returns None after destruction. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like |
||
X.with(|x| <*const _>::addr(x)) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.