Skip to content

Commit

Permalink
Fix lock trait bound.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee-Janggun committed Feb 20, 2024
1 parent 4a13c50 commit 19faf2c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
22 changes: 13 additions & 9 deletions src/lock/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ use core::mem::ManuallyDrop;
use core::ops::{Deref, DerefMut};

/// Raw lock interface.
pub trait RawLock: Default + Send + Sync {
///
/// # Safety
///
/// Implementations of this trait must ensure that the lock is actually
/// exclusive: a lock can't be acquired while the lock is already locked.
pub unsafe trait RawLock: Default + Send + Sync {
/// Raw lock's token type.
type Token;

Expand All @@ -19,7 +24,13 @@ pub trait RawLock: Default + Send + Sync {
}

/// Raw lock interface for the try_lock API.
pub trait RawTryLock: RawLock {
///
/// # Safety
///
/// Implementations of this trait must ensure that the lock is actually
/// exclusive: a lock can't be acquired while the lock is already locked.
/// Also, `try_lock()`, when successful, should return a token that can be used for `RawLock::unlock`.
pub unsafe trait RawTryLock: RawLock {
/// Tries to acquire the raw lock.
fn try_lock(&self) -> Result<Self::Token, ()>;
}
Expand Down Expand Up @@ -114,13 +125,6 @@ pub struct LockGuard<'s, L: RawLock, T> {
unsafe impl<L: RawLock, T: Sync> Send for LockGuard<'_, L, T> {}
unsafe impl<L: RawLock, T: Sync> Sync for LockGuard<'_, L, T> {}

impl<L: RawLock, T> LockGuard<'_, L, T> {
/// Returns the address of the referenced lock.
pub fn raw(&mut self) -> usize {
self.lock as *const _ as usize
}
}

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
12 changes: 6 additions & 6 deletions src/lock/clhlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@ pub struct ClhLock {
}

impl Node {
const fn new(locked: bool) -> Self {
Self {
fn new(locked: bool) -> *mut CachePadded<Self> {
Box::into_raw(Box::new(CachePadded::new(Self {
locked: AtomicBool::new(locked),
}
})))
}
}

impl Default for ClhLock {
fn default() -> Self {
let node = AtomicPtr::new(Box::into_raw(Box::new(CachePadded::new(Node::new(false)))));
let node = AtomicPtr::new(Node::new(false));

Self { tail: node }
}
}

impl RawLock for ClhLock {
unsafe impl RawLock for ClhLock {
type Token = Token;

fn lock(&self) -> Self::Token {
let node = Box::into_raw(Box::new(CachePadded::new(Node::new(true))));
let node = Node::new(true);
let prev = self.tail.swap(node, AcqRel);
let backoff = Backoff::new();

Expand Down
10 changes: 5 additions & 5 deletions src/lock/mcslock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ pub struct McsLock {
}

impl Node {
const fn new() -> Self {
Self {
fn new() -> *mut CachePadded<Self> {
Box::into_raw(Box::new(CachePadded::new(Self {
locked: AtomicBool::new(true),
next: AtomicPtr::new(ptr::null_mut()),
}
})))
}
}

Expand All @@ -36,11 +36,11 @@ impl Default for McsLock {
}
}

impl RawLock for McsLock {
unsafe impl RawLock for McsLock {
type Token = Token;

fn lock(&self) -> Self::Token {
let node = Box::into_raw(Box::new(CachePadded::new(Node::new())));
let node = Node::new();
let prev = self.tail.swap(node, AcqRel);

if prev.is_null() {
Expand Down
10 changes: 5 additions & 5 deletions src/lock/mcsparkinglock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ pub struct McsParkingLock {
}

impl Node {
fn new() -> Self {
Self {
fn new() -> *mut CachePadded<Self> {
Box::into_raw(Box::new(CachePadded::new(Self {
thread: thread::current(),
locked: AtomicBool::new(true),
next: AtomicPtr::new(ptr::null_mut()),
}
})))
}
}

Expand All @@ -39,11 +39,11 @@ impl Default for McsParkingLock {
}
}

impl RawLock for McsParkingLock {
unsafe impl RawLock for McsParkingLock {
type Token = Token;

fn lock(&self) -> Self::Token {
let node = Box::into_raw(Box::new(CachePadded::new(Node::new())));
let node = Node::new();
let prev = self.tail.swap(node, AcqRel);

if prev.is_null() {
Expand Down
4 changes: 2 additions & 2 deletions src/lock/spinlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Default for SpinLock {
}
}

impl RawLock for SpinLock {
unsafe impl RawLock for SpinLock {
type Token = ();

fn lock(&self) {
Expand All @@ -38,7 +38,7 @@ impl RawLock for SpinLock {
}
}

impl RawTryLock for SpinLock {
unsafe impl RawTryLock for SpinLock {
fn try_lock(&self) -> Result<(), ()> {
self.inner
.compare_exchange(false, true, Acquire, Relaxed)
Expand Down
2 changes: 1 addition & 1 deletion src/lock/ticketlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Default for TicketLock {
}
}

impl RawLock for TicketLock {
unsafe impl RawLock for TicketLock {
type Token = usize;

fn lock(&self) -> usize {
Expand Down

0 comments on commit 19faf2c

Please sign in to comment.