Skip to content

Commit aa81fc1

Browse files
committed
Cleanup comments a bit more.
1 parent d025c9a commit aa81fc1

File tree

4 files changed

+13
-28
lines changed

4 files changed

+13
-28
lines changed

src/lock/api.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,25 @@ pub unsafe trait RawLock: Default + Send + Sync {
2323
///
2424
/// # Safety
2525
///
26-
/// `unlock()` should be called with the token given by the corresponding `lock()`.
26+
/// - `self` must be a an acquired lock.
27+
/// - `token` must be from a [`RawLock::lock`] or [`RawTryLock::unlock`] call to `self`.
2728
unsafe fn unlock(&self, token: Self::Token);
2829
}
2930

3031
/// Raw lock interface for the try_lock API.
3132
///
3233
/// # Safety
3334
///
34-
/// Implementations of this trait must ensure that the lock is actually exclusive: a lock can't be
35-
/// acquired while the lock is already locked.
35+
/// See [`RawLock`] for safety requirements.
3636
///
37-
/// Also, `try_lock()`, when successful, should return a token that can be used for
38-
/// `RawLock::unlock`.
37+
/// Also, [`RawTryLock::try_lock`] should return a token that can be used for [`RawLock::unlock`].
3938
pub unsafe trait RawTryLock: RawLock {
4039
/// Tries to acquire the raw lock.
4140
fn try_lock(&self) -> Result<Self::Token, ()>;
4241
}
4342

4443
/// A type-safe lock.
45-
#[repr(C)]
46-
#[derive(Debug)]
44+
#[derive(Debug, Default)]
4745
pub struct Lock<L: RawLock, T> {
4846
inner: L,
4947
data: UnsafeCell<T>,
@@ -54,19 +52,6 @@ pub struct Lock<L: RawLock, T> {
5452
// SATEFY: threads can only access `&mut T` via the lock, and `L` is `Sync`.
5553
unsafe impl<L: RawLock, T: Send> Sync for Lock<L, T> {}
5654

57-
impl<L: RawLock, T: Default> Default for Lock<L, T>
58-
where
59-
L: Default,
60-
{
61-
// Manual impl for minimum trait bound.
62-
fn default() -> Self {
63-
Self {
64-
inner: L::default(),
65-
data: UnsafeCell::default(),
66-
}
67-
}
68-
}
69-
7055
impl<L: RawLock, T> Lock<L, T> {
7156
/// Creates a new lock.
7257
pub fn new(data: T) -> Self {

src/lock/seqlock.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl RawSeqLock {
5656
/// # Safety
5757
///
5858
/// - `self` must be a an acquired writer's lock.
59-
/// - `seq` must be the be the value returned from the corresponding of the `write_lock()`.
59+
/// - `seq` must be from the most recent [`SeqLock::write_lock`] call on `self`.
6060
pub unsafe fn write_unlock(&self, seq: usize) {
6161
self.seq.store(seq.wrapping_add(2), Release);
6262
}
@@ -88,6 +88,7 @@ impl RawSeqLock {
8888
/// # Safety
8989
///
9090
/// - `seq` must be even.
91+
// No need to require `self` to be a read lock, as the sequence number is enough to validate.
9192
pub unsafe fn upgrade(&self, seq: usize) -> bool {
9293
if self
9394
.seq
@@ -223,7 +224,7 @@ impl<T> Drop for ReadGuard<'_, T> {
223224
fn drop(&mut self) {
224225
// HACK(@jeehoonkang): we really need linear type here:
225226
// https://github.com/rust-lang/rfcs/issues/814
226-
panic!("`seqlock::ReadGuard` should never drop. Use `ReadGuard::finish()` instead.");
227+
panic!("`seqlock::ReadGuard` should never drop. Use `ReadGuard::finish` instead.");
227228
}
228229
}
229230

src/lockfree/list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct Node<K, V> {
2121
/// Sorted singly linked list.
2222
///
2323
/// Use-after-free will be caused when an unprotected guard is used, as the lifetime of returned
24-
/// elements are linked to that of the guard in the same way a `Shared<'g,T>` is.
24+
/// elements are linked to that of the guard in the same way a [`Shared`] is.
2525
#[derive(Debug)]
2626
pub struct List<K, V> {
2727
head: Atomic<Node<K, V>>,

src/lockfree/queue.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ impl<T> Queue<T> {
6666
});
6767

6868
loop {
69-
guard.repin();
70-
7169
// We push onto the tail, so we'll start optimistically by looking there first.
7270
let tail = self.tail.load(Acquire, guard);
7371

@@ -97,6 +95,7 @@ impl<T> Queue<T> {
9795
}
9896
Err(e) => new = e.new,
9997
}
98+
guard.repin();
10099
}
101100
}
102101

@@ -105,8 +104,6 @@ impl<T> Queue<T> {
105104
/// Returns `None` if the queue is observed to be empty.
106105
pub fn try_pop(&self, guard: &mut Guard) -> Option<T> {
107106
loop {
108-
guard.repin();
109-
110107
let head = self.head.load(Acquire, guard);
111108
let next = unsafe { head.deref() }.next.load(Acquire, guard);
112109

@@ -125,7 +122,8 @@ impl<T> Queue<T> {
125122
// than that of current head. We relase that view to the head with the below CAS,
126123
// ensuring that the index of the new head is less than or equal to that of the tail.
127124
//
128-
// Note: similar reasoning is done in SC memory regarding index of head and tail.
125+
// Note: this reasoning is also done in SC memory regarding index of head and tail,
126+
// albeit simpler.
129127
if self
130128
.head
131129
.compare_exchange(head, next, Release, Relaxed, guard)
@@ -150,6 +148,7 @@ impl<T> Queue<T> {
150148

151149
return Some(result);
152150
}
151+
guard.repin();
153152
}
154153
}
155154
}

0 commit comments

Comments
 (0)