Skip to content

Commit

Permalink
Fix treiber minor issues (#163)
Browse files Browse the repository at this point in the history
* Fix stack overflow in test, due to List.@ not being not tail rec.

* Run mdx on the .mli.
  • Loading branch information
lyrm authored Nov 21, 2024
1 parent 8ed5de4 commit d509fc6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
9 changes: 9 additions & 0 deletions src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@ let () =
(< %{ocaml_version} 5.2.0))
(action
(copy atomic.without_contended.ml atomic.ml)))

(mdx
(package saturn)
(enabled_if
(and
(<> %{os_type} Win32)
(>= %{ocaml_version} 5.0.0)))
(libraries saturn)
(files treiber_stack.mli))
|}
11 changes: 4 additions & 7 deletions src/treiber_stack.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,10 @@ let push t value = push t value Backoff.default
(**)

let rec push_all_ t backoff values =
match Atomic.get t with
| [] ->
if Atomic.compare_and_set t [] values then ()
else push_all_ t (Backoff.once backoff) values
| _ as old_head ->
if Atomic.compare_and_set t old_head (values @ old_head) then ()
else push_all_ t (Backoff.once backoff) values
let before = Atomic.get t in
if Atomic.compare_and_set t before (List.rev_append (List.rev values) before)
then ()
else push_all_ t (Backoff.once backoff) values

let push_all t values =
match values with
Expand Down
8 changes: 4 additions & 4 deletions src/treiber_stack.mli
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ val pop_all : 'a t -> 'a list
order.
{[
# open Saturn_lockfree.Stack
# open Saturn.Stack
# let t : int t = create ()
val t : int t = <abstr>
# push t 1
Expand Down Expand Up @@ -105,7 +105,7 @@ val add_seq : 'a t -> 'a Seq.t -> unit
(** {1 Examples}
An example top-level session:
{[
# open Saturn_lockfree.Stack
# open Saturn.Stack
# let t : int t = create ()
val t : int t = <abstr>
# push t 42
Expand All @@ -119,11 +119,11 @@ val add_seq : 'a t -> 'a Seq.t -> unit
# pop_all t
- : int list = [2; 1; 42]
# pop_exn t
Exception: Saturn_lockfree__Treiber_stack.Empty.]}
Exception: Saturn__Treiber_stack.Empty.]}
A multicore example:
{@ocaml non-deterministic[
# open Saturn_lockfree.Stack
# open Saturn.Stack
# let t : int t = create ()
val t : int t = <abstr>
# let barrier = Atomic.make 2
Expand Down
2 changes: 1 addition & 1 deletion test/treiber_stack/stm_treiber_stack.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module Spec = struct
(Gen.oneof
[
Gen.map (fun i -> Push i) int_gen;
Gen.map (fun l -> Push_all l) (Gen.list int_gen);
Gen.map (fun l -> Push_all l) (Gen.small_list int_gen);
Gen.return Pop_opt;
Gen.return Pop_all;
Gen.return Peek_opt;
Expand Down

0 comments on commit d509fc6

Please sign in to comment.