Skip to content

Commit 2713111

Browse files
authored
Merge pull request #66 from ocaml-multicore/fix-ms-queue-tail-update
Fix the lock-free update of Michael-Scott style queue tail
2 parents 9abc664 + bd9b0b5 commit 2713111

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/michael_scott_queue.ml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ let pop { head; _ } =
4545
in
4646
loop ()
4747

48-
let rec fix_tail tail old_tail new_tail =
49-
if Atomic.compare_and_set tail old_tail new_tail then
50-
match Atomic.get new_tail with
51-
| Nil -> ()
52-
| Next (_, new_new_tail) -> fix_tail tail new_tail new_new_tail
48+
let rec fix_tail tail new_tail =
49+
let old_tail = Atomic.get tail in
50+
if
51+
Atomic.get new_tail == Nil
52+
&& not (Atomic.compare_and_set tail old_tail new_tail)
53+
then fix_tail tail new_tail
5354

5455
let push { tail; _ } value =
5556
let rec find_tail_and_enq curr_end node =
@@ -62,10 +63,8 @@ let push { tail; _ } value =
6263
let newnode = Next (value, new_tail) in
6364
let old_tail = Atomic.get tail in
6465
find_tail_and_enq old_tail newnode;
65-
if Atomic.compare_and_set tail old_tail new_tail then
66-
match Atomic.get new_tail with
67-
| Nil -> ()
68-
| Next (_, new_new_tail) -> fix_tail tail new_tail new_new_tail
66+
if not (Atomic.compare_and_set tail old_tail new_tail) then
67+
fix_tail tail new_tail
6968

7069
let clean_until { head; _ } f =
7170
let b = Backoff.create () in

0 commit comments

Comments
 (0)