Skip to content

Commit

Permalink
Simplify Treiber stack.
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrm committed Dec 8, 2022
1 parent 0dcc62e commit 08cbcf6
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/treiber_stack.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(** Treiber's Lock Free stack *)

type 'a node = Nil | Next of 'a * 'a node Atomic.t
type 'a node = Nil | Next of 'a * 'a node
type 'a t = { head : 'a node Atomic.t }

let create () =
Expand All @@ -11,8 +11,7 @@ let is_empty q = match Atomic.get q.head with Nil -> true | Next _ -> false

let push q v =
let head = Atomic.get q.head in
let next_node = Atomic.make head in
let new_node = Next (v, next_node) in
let new_node = Next (v, head) in
if Atomic.compare_and_set q.head head new_node then ()
else
let b = Backoff.create () in
Expand All @@ -21,7 +20,7 @@ let push q v =
(* retry *)
let rec loop b =
let head = Atomic.get q.head in
Atomic.set next_node head;
let new_node = Next (v, head) in
if Atomic.compare_and_set q.head head new_node then ()
else (
Backoff.once b;
Expand All @@ -35,7 +34,7 @@ let pop q =
match s with
| Nil -> None
| Next (v, next) ->
if Atomic.compare_and_set q.head s (Atomic.get next) then Some v
if Atomic.compare_and_set q.head s next then Some v
else (
Backoff.once b;
loop b)
Expand All @@ -45,7 +44,7 @@ let pop q =
match s with
| Nil -> None
| Next (v, next) ->
if Atomic.compare_and_set q.head s (Atomic.get next) then Some v
if Atomic.compare_and_set q.head s next then Some v
else
let b = Backoff.create () in
Backoff.once b;
Expand Down

0 comments on commit 08cbcf6

Please sign in to comment.