Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix treiber minor issues ( mdx, use of List.(@)) #163

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading