Skip to content

Commit

Permalink
Polish comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee-Janggun committed Nov 1, 2024
1 parent 47d9787 commit 5f1f3fd
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/lock/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use core::ops::{Deref, DerefMut};
///
/// # 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.
/// Implementations of this trait must ensure that the lock is actually exclusive: a lock can't be
/// acquired while the lock is already locked.
// 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.
///
/// Send + Sync is needed to make LockGuard Send + Sync.
type Token: Send + Sync;

/// Acquires the raw lock.
Expand All @@ -30,8 +30,9 @@ pub unsafe 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.
/// 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 {
Expand All @@ -41,7 +42,7 @@ pub unsafe trait RawTryLock: RawLock {

/// A type-safe lock.
#[repr(C)]
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct Lock<L: RawLock, T> {
inner: L,
data: UnsafeCell<T>,
Expand All @@ -50,6 +51,19 @@ pub struct Lock<L: RawLock, T> {
// Send is automatically implemented for Lock.
unsafe impl<L: RawLock, T: Send> Sync for Lock<L, T> {}

impl<L: RawLock, T: Default> Default for Lock<L, T>
where
L: Default,
{
// Manual impl for minimum trait bound.
fn default() -> Self {
Self {
inner: L::default(),
data: UnsafeCell::default(),
}
}
}

impl<L: RawLock, T> Lock<L, T> {
/// Creates a new lock.
pub fn new(data: T) -> Self {
Expand Down Expand Up @@ -85,8 +99,7 @@ impl<L: RawTryLock, T> Lock<L, T> {
}

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

0 comments on commit 5f1f3fd

Please sign in to comment.