Skip to content

Commit 02f2a65

Browse files
committed
add comments
1 parent f19216e commit 02f2a65

File tree

4 files changed

+5
-3
lines changed

4 files changed

+5
-3
lines changed

src/lock/clhlock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct ClhLock {
2323

2424
impl Node {
2525
fn new(locked: bool) -> *mut CachePadded<Self> {
26-
Box::into_raw(Box::new(CachePadded::new(Self {
26+
Box::into_raw(Box::new(CachePadded::new(Self { // Box allocated in heap
2727
locked: AtomicBool::new(locked),
2828
})))
2929
}

src/lock/mcslock.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@ unsafe impl RawLock for McsLock {
7474
if self
7575
.tail
7676
.compare_exchange(node, ptr::null_mut(), Release, Relaxed)
77-
.is_ok()
77+
.is_ok() // no other thread is waiting, set tail to null
7878
{
7979
// SAFETY: Since `node` was the `tail`, there is no other thread blocked by this
8080
// lock. Hence we have unique access to it.
8181
drop(unsafe { Box::from_raw(node) });
8282
return;
8383
}
8484

85+
// another thread succeed in swap tail but not set prev yet
8586
while {
8687
next = unsafe { (*node).next.load(Acquire) };
8788
next.is_null()

src/lock/mcsparkinglock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ unsafe impl RawLock for McsParkingLock {
9292

9393
// It is important to clone the thread before unlocking, the next waiter,
9494
// because then the next waiter may free `next_ref`.
95+
// If not, use after free may occur if next_ref quickly unlock.
9596
let thread = next_ref.thread.clone();
9697
next_ref.locked.store(false, Release);
9798
thread.unpark();

src/lock/ticketlock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ unsafe impl RawLock for TicketLock {
2525
type Token = usize;
2626

2727
fn lock(&self) -> usize {
28-
let ticket = self.next.fetch_add(1, Relaxed);
28+
let ticket = self.next.fetch_add(1, Relaxed); // fetch_add is fair => TicketLock is fair. fetch_add is not fair => TicketLock is not fair
2929
let backoff = Backoff::new();
3030

3131
while self.curr.load(Acquire) != ticket {

0 commit comments

Comments
 (0)