Skip to content

Commit

Permalink
Optimize some send/sync impls.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee-Janggun committed Oct 23, 2024
1 parent 95578bf commit a9ac6e6
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/lock/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use core::ops::{Deref, DerefMut};
// TODO: For weak memory, there needs to be a bit more stricter condition. unlock -hb→ lock.
pub unsafe trait RawLock: Default + Send + Sync {
/// Raw lock's token type.
//
// Send + Sync is needed to make LockGuard Send + Sync.
type Token: Send + Sync;

/// Acquires the raw lock.
Expand Down Expand Up @@ -45,7 +47,7 @@ pub struct Lock<L: RawLock, T> {
data: UnsafeCell<T>,
}

unsafe impl<L: RawLock, T: Send> Send for Lock<L, T> {}
// Send is automatically implemented for Lock.
unsafe impl<L: RawLock, T: Send> Sync for Lock<L, T> {}

impl<L: RawLock, T> Lock<L, T> {
Expand Down Expand Up @@ -83,15 +85,14 @@ impl<L: RawTryLock, T> Lock<L, T> {
}

/// A guard that holds the lock and dereferences the inner value.
///
/// Send/Sync are is automatically implemented.
#[derive(Debug)]
pub struct LockGuard<'s, L: RawLock, T> {
lock: &'s Lock<L, T>,
token: ManuallyDrop<L::Token>,
}

unsafe impl<L: RawLock, T: Send> Send for LockGuard<'_, L, T> {}
unsafe impl<L: RawLock, T: Sync> Sync for LockGuard<'_, L, T> {}

impl<L: RawLock, T> Drop for LockGuard<'_, L, T> {
fn drop(&mut self) {
// SAFETY: `self.token` is not used anymore in this function, and as we are `drop`ing
Expand Down

0 comments on commit a9ac6e6

Please sign in to comment.