From fda47a7195d0a74f215cfa8fd0d41f1ffd0c9bea Mon Sep 17 00:00:00 2001 From: Pedro Fedricci Date: Sat, 23 Mar 2024 03:24:13 -0300 Subject: [PATCH] fix(api): unbound R from Send and Sync --- src/raw/mutex.rs | 9 ++++++--- src/thread_local/mutex.rs | 6 +++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/raw/mutex.rs b/src/raw/mutex.rs index 04bb6da..998bc2e 100644 --- a/src/raw/mutex.rs +++ b/src/raw/mutex.rs @@ -148,8 +148,8 @@ pub struct Mutex { } // Same unsafe impls as `std::sync::Mutex`. -unsafe impl Sync for Mutex {} unsafe impl Send for Mutex {} +unsafe impl Sync for Mutex {} impl Mutex { /// Creates a new mutex in an unlocked state ready for use. @@ -585,7 +585,10 @@ pub struct MutexGuard<'a, T: ?Sized, R: Relax> { node: &'a MutexNodeInit, } -// Same unsafe impl as `std::sync::MutexGuard`. +// `std::sync::MutexGuard` is not Send for pthread compatibility, but this +// implementation is safe to be Send. +unsafe impl Send for MutexGuard<'_, T, R> {} +// Same unsafe Sync impl as `std::sync::MutexGuard`. unsafe impl Sync for MutexGuard<'_, T, R> {} impl<'a, T: ?Sized, R: Relax> MutexGuard<'a, T, R> { @@ -652,7 +655,7 @@ impl<'a, T: ?Sized, R: Relax> core::ops::DerefMut for MutexGuard<'a, T, R> { unsafe impl crate::loom::Guard for MutexGuard<'_, T, R> { type Target = T; - fn get(&self) -> &UnsafeCell { + fn get(&self) -> &loom::cell::UnsafeCell { &self.lock.data } } diff --git a/src/thread_local/mutex.rs b/src/thread_local/mutex.rs index 4861045..1fad68c 100644 --- a/src/thread_local/mutex.rs +++ b/src/thread_local/mutex.rs @@ -105,6 +105,10 @@ fn panic_already_held(caller: &Location<'static>) -> ! { /// [`try_lock_with`]: Mutex::try_lock_with pub struct Mutex(RawMutex); +// Same unsafe impls as `crate::raw::Mutex`. +unsafe impl Send for Mutex {} +unsafe impl Sync for Mutex {} + impl Mutex { /// Creates a new mutex in an unlocked state ready for use. /// @@ -697,7 +701,7 @@ pub struct MutexGuard<'a, T: ?Sized, R: Relax> { } // SAFETY: Guard only access thread local storage during drop call, can be Sync. -unsafe impl<'a, T: ?Sized + Sync, R: Relax> Sync for MutexGuard<'a, T, R> {} +unsafe impl Sync for MutexGuard<'_, T, R> {} impl<'a, T: ?Sized, R: Relax> MutexGuard<'a, T, R> { /// Creates a new guard instance from a raw guard.