Skip to content

Commit a6c0a01

Browse files
committed
add some comments
1 parent 51ffe72 commit a6c0a01

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/lockfree/queue.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<T> Default for Queue<T> {
4646
impl<T> Queue<T> {
4747
/// Create a new, empty queue.
4848
pub fn new() -> Self {
49-
let sentinel = Box::into_raw(Box::new(Node {
49+
let sentinel = Box::into_raw(Box::new(Node { // dummy node
5050
data: MaybeUninit::uninit(),
5151
next: Atomic::null(),
5252
}))
@@ -59,15 +59,15 @@ impl<T> Queue<T> {
5959
}
6060

6161
/// Adds `t` to the back of the queue.
62-
pub fn push(&self, t: T, guard: &mut Guard) {
62+
pub fn push(&self, t: T, guard: &mut Guard) { // `guard` is used as a proof to access shared data
6363
let mut new = Owned::new(Node {
6464
data: MaybeUninit::new(t),
6565
next: Atomic::null(),
6666
});
6767

6868
loop {
6969
// We push onto the tail, so we'll start optimistically by looking there first.
70-
let tail = self.tail.load(Acquire, guard);
70+
let tail = self.tail.load(Acquire, guard); // latest value of tail
7171

7272
// Attempt to push onto the `tail` snapshot; fails if `tail.next` has changed.
7373
let tail_ref = unsafe { tail.deref() };
@@ -77,20 +77,20 @@ impl<T> Queue<T> {
7777
if !next.is_null() {
7878
let _ = self
7979
.tail
80-
.compare_exchange(tail, next, Release, Relaxed, guard);
80+
.compare_exchange(tail, next, Release, Relaxed, guard); // release match with let next
8181
continue;
8282
}
8383

8484
// looks like the actual tail; attempt to link at `tail.next`.
8585
match tail_ref
8686
.next
87-
.compare_exchange(Shared::null(), new, Release, Relaxed, guard)
87+
.compare_exchange(Shared::null(), new, Release, Relaxed, guard) // release match with let mut new
8888
{
8989
Ok(new) => {
9090
// try to move the tail pointer forward.
9191
let _ = self
9292
.tail
93-
.compare_exchange(tail, new, Release, Relaxed, guard);
93+
.compare_exchange(tail, new, Release, Relaxed, guard); // release match with let mut new
9494
break;
9595
}
9696
Err(e) => new = e.new,
@@ -104,13 +104,13 @@ impl<T> Queue<T> {
104104
/// Returns `None` if the queue is observed to be empty.
105105
pub fn try_pop(&self, guard: &mut Guard) -> Option<T> {
106106
loop {
107-
let head = self.head.load(Acquire, guard);
108-
let next = unsafe { head.deref() }.next.load(Acquire, guard);
107+
let head = self.head.load(Acquire, guard); // latest value of head
108+
let next = unsafe { head.deref() }.next.load(Acquire, guard); // latest value of next
109109

110110
let next_ref = unsafe { next.as_ref() }?;
111111

112112
// Moves `tail` if it's stale. Relaxed load is enough because if tail == head, then the
113-
// messages for that node are already acquired.
113+
// messages for that node are already acquired. head is acquired, head can not be more updated than tail
114114
let tail = self.tail.load(Relaxed, guard);
115115
if tail == head {
116116
let _ = self

src/lockfree/stack.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<T> Stack<T> {
4040

4141
/// Pushes a value on top of the stack.
4242
pub fn push(&self, t: T) {
43-
let mut node = Owned::new(Node {
43+
let mut node = Owned::new(Node { // heap allocation
4444
data: MaybeUninit::new(t),
4545
next: ptr::null(),
4646
});
@@ -73,7 +73,7 @@ impl<T> Stack<T> {
7373

7474
loop {
7575
let head = self.head.load(Acquire, &guard);
76-
let h = unsafe { head.as_ref() }?;
76+
let h = unsafe { head.as_ref() }?; // return None here if head is null
7777
let next = Shared::from(h.next);
7878

7979
if self

0 commit comments

Comments
 (0)