Skip to content

Commit

Permalink
fix(api): unbound R from Send and Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromfedricci committed Mar 23, 2024
1 parent 34858aa commit fda47a7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/raw/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ pub struct Mutex<T: ?Sized, R> {
}

// Same unsafe impls as `std::sync::Mutex`.
unsafe impl<T: ?Sized + Send, R> Sync for Mutex<T, R> {}
unsafe impl<T: ?Sized + Send, R> Send for Mutex<T, R> {}
unsafe impl<T: ?Sized + Send, R> Sync for Mutex<T, R> {}

impl<T, R> Mutex<T, R> {
/// Creates a new mutex in an unlocked state ready for use.
Expand Down Expand Up @@ -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<T: ?Sized + Send, R: Relax> Send for MutexGuard<'_, T, R> {}
// Same unsafe Sync impl as `std::sync::MutexGuard`.
unsafe impl<T: ?Sized + Sync, R: Relax> Sync for MutexGuard<'_, T, R> {}

impl<'a, T: ?Sized, R: Relax> MutexGuard<'a, T, R> {
Expand Down Expand Up @@ -652,7 +655,7 @@ impl<'a, T: ?Sized, R: Relax> core::ops::DerefMut for MutexGuard<'a, T, R> {
unsafe impl<T: ?Sized, R: Relax> crate::loom::Guard for MutexGuard<'_, T, R> {
type Target = T;

fn get(&self) -> &UnsafeCell<Self::Target> {
fn get(&self) -> &loom::cell::UnsafeCell<Self::Target> {
&self.lock.data
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/thread_local/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ fn panic_already_held(caller: &Location<'static>) -> ! {
/// [`try_lock_with`]: Mutex::try_lock_with
pub struct Mutex<T: ?Sized, R>(RawMutex<T, R>);

// Same unsafe impls as `crate::raw::Mutex`.
unsafe impl<T: ?Sized + Send, R> Send for Mutex<T, R> {}
unsafe impl<T: ?Sized + Send, R> Sync for Mutex<T, R> {}

impl<T, R> Mutex<T, R> {
/// Creates a new mutex in an unlocked state ready for use.
///
Expand Down Expand Up @@ -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<T: ?Sized + Sync, R: Relax> Sync for MutexGuard<'_, T, R> {}

impl<'a, T: ?Sized, R: Relax> MutexGuard<'a, T, R> {
/// Creates a new guard instance from a raw guard.
Expand Down

0 comments on commit fda47a7

Please sign in to comment.