Skip to content

Commit 6b64cae

Browse files
committed
Upgrade dscheck to allow testing on OCaml 4 with ocaml-ci
1 parent 8937436 commit 6b64cae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+444
-322
lines changed

.github/workflows/main.yml

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,32 +46,3 @@ jobs:
4646

4747
- name: Test
4848
run: opam exec -- dune runtest
49-
50-
build-4_x:
51-
strategy:
52-
fail-fast: false
53-
matrix:
54-
os:
55-
- ubuntu-latest
56-
ocaml-compiler:
57-
- 4.13.x
58-
- 4.14.x
59-
60-
runs-on: ${{ matrix.os }}
61-
62-
steps:
63-
- name: Check out code
64-
uses: actions/checkout@v3
65-
66-
- name: Set up OCaml
67-
uses: ocaml/setup-ocaml@v2
68-
with:
69-
opam-pin: false
70-
opam-depext: false
71-
ocaml-compiler: ${{ matrix.ocaml-compiler }}
72-
73-
- name: Install dependencies
74-
run: opam install . --deps-only
75-
76-
- name: Build (release)
77-
run: opam exec -- dune build --release

bench/bench_queue.ml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@ let run_one ~budgetf ?(n_adders = 2) ?(n_takers = 2)
4646
let n = Util.alloc n_msgs_to_take in
4747
if n <> 0 then
4848
let rec loop n =
49-
if 0 < n then
50-
loop (n - Bool.to_int (Option.is_some (Queue.pop_opt t)))
49+
if 0 < n then begin
50+
match Queue.pop_opt t with
51+
| None ->
52+
Domain.cpu_relax ();
53+
loop n
54+
| Some _ -> loop (n - 1)
55+
end
5156
else work ()
5257
in
5358
loop n

bench/bench_relaxed_queue.ml

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,23 @@ let run_one ~budgetf ~n_adders ~n_takers ?(n_msgs = 50 * Util.iter_factor)
3131
work ()
3232
| `Not_lockfree ->
3333
let rec loop n =
34-
if 0 < n then loop (n - Bool.to_int (Not_lockfree.push t i))
34+
if 0 < n then
35+
if Not_lockfree.push t i then loop (n - 1)
36+
else begin
37+
Domain.cpu_relax ();
38+
loop n
39+
end
3540
else work ()
3641
in
3742
loop n
3843
| `CAS_interface ->
3944
let rec loop n =
40-
if 0 < n then loop (n - Bool.to_int (CAS_interface.push t i))
45+
if 0 < n then
46+
if CAS_interface.push t i then loop (n - 1)
47+
else begin
48+
Domain.cpu_relax ();
49+
loop n
50+
end
4151
else work ()
4252
in
4353
loop n
@@ -56,15 +66,25 @@ let run_one ~budgetf ~n_adders ~n_takers ?(n_msgs = 50 * Util.iter_factor)
5666
work ()
5767
| `Not_lockfree ->
5868
let rec loop n =
59-
if 0 < n then
60-
loop (n - Bool.to_int (Option.is_some (Not_lockfree.pop t)))
69+
if 0 < n then begin
70+
match Not_lockfree.pop t with
71+
| None ->
72+
Domain.cpu_relax ();
73+
loop n
74+
| Some _ -> loop (n - 1)
75+
end
6176
else work ()
6277
in
6378
loop n
6479
| `CAS_interface ->
6580
let rec loop n =
66-
if 0 < n then
67-
loop (n - Bool.to_int (Option.is_some (CAS_interface.pop t)))
81+
if 0 < n then begin
82+
match CAS_interface.pop t with
83+
| None ->
84+
Domain.cpu_relax ();
85+
loop n
86+
| Some _ -> loop (n - 1)
87+
end
6888
else work ()
6989
in
7090
loop n

bench/bench_spsc_queue.ml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,22 @@ let run_one ~budgetf ?(size_exponent = 3) ?(n_msgs = 80 * Util.iter_factor) () =
1717
let work i () =
1818
if i = 0 then
1919
let rec loop n =
20-
if 0 < n then loop (n - Bool.to_int (Queue.try_push t n))
20+
if 0 < n then
21+
if Queue.try_push t n then loop (n - 1)
22+
else begin
23+
Domain.cpu_relax ();
24+
loop n
25+
end
2126
in
2227
loop n_msgs
2328
else
2429
let rec loop n =
2530
if 0 < n then
26-
match Queue.pop_opt t with Some _ -> loop (n - 1) | None -> loop n
31+
match Queue.pop_opt t with
32+
| Some _ -> loop (n - 1)
33+
| None ->
34+
Domain.cpu_relax ();
35+
loop n
2736
in
2837
loop n_msgs
2938
in

bench/bench_ws_deque.ml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ let run_one ~budgetf ?(n_domains = 1) () =
2929
| work ->
3030
work own;
3131
run own
32-
| exception Exit -> if not !exit then run own
32+
| exception Exit ->
33+
if not !exit then begin
34+
Domain.cpu_relax ();
35+
run own
36+
end
3337
in
3438

3539
let spawn own work =
@@ -42,7 +46,9 @@ let run_one ~budgetf ?(n_domains = 1) () =
4246
let x = !promise in
4347
if x == Obj.magic exit then begin
4448
begin
45-
match try_own own with exception Exit -> () | work -> work own
49+
match try_own own with
50+
| exception Exit -> Domain.cpu_relax ()
51+
| work -> work own
4652
end;
4753
await own promise
4854
end

bench/dune

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1+
(* -*- tuareg -*- *)
2+
3+
let maybe_domain_shims =
4+
if Jbuild_plugin.V1.ocaml_version < "5" then "domain_shims" else ""
5+
6+
let () =
7+
Jbuild_plugin.V1.send
8+
@@ {|
9+
110
(test
211
(package saturn)
312
(name main)
4-
(libraries saturn multicore-bench multicore-magic))
13+
(libraries saturn multicore-bench multicore-magic |}
14+
^ maybe_domain_shims ^ {| ))
15+
|}

dune-project

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(lang dune 3.2)
1+
(lang dune 3.9)
22
(name saturn)
33
(generate_opam_files true)
44
(implicit_transitive_deps false)
@@ -16,12 +16,13 @@
1616
(saturn_lockfree (= :version))
1717
(multicore-magic (and (>= 2.1.0) :with-test))
1818
(multicore-bench (and (>= 0.1.0) :with-test))
19+
(backoff (and (>= 0.1.0) :with-test))
1920
(alcotest (and (>= 1.7.0) :with-test))
2021
(qcheck (and (>= 0.21.3) :with-test))
2122
(qcheck-stm (and (>= 0.3) :with-test))
2223
(qcheck-alcotest (and (>= 0.21.3) :with-test))
2324
(yojson (and (>= 2.0.2) :with-test))
24-
(dscheck (and (>= 0.2.0) :with-test))))
25+
(dscheck (and (>= 0.4.0) :with-test))))
2526
(package
2627
(name saturn_lockfree)
2728
(synopsis "Collection of lock-free data structures for Multicore OCaml")
@@ -37,4 +38,4 @@
3738
(qcheck-multicoretests-util (and (>= 0.3) :with-test))
3839
(qcheck-alcotest (and (>= 0.21.3) :with-test))
3940
(yojson (and (>= 2.0.2) :with-test))
40-
(dscheck (and (>= 0.2.0) :with-test))))
41+
(dscheck (and (>= 0.4.0) :with-test))))

saturn.opam

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ homepage: "https://github.com/ocaml-multicore/saturn"
99
doc: "https://ocaml-multicore.github.io/saturn/"
1010
bug-reports: "https://github.com/ocaml-multicore/saturn/issues"
1111
depends: [
12-
"dune" {>= "3.2"}
12+
"dune" {>= "3.9"}
1313
"ocaml" {>= "4.13"}
1414
"domain_shims" {>= "0.1.0" & with-test}
1515
"saturn_lockfree" {= version}
1616
"multicore-magic" {>= "2.1.0" & with-test}
1717
"multicore-bench" {>= "0.1.0" & with-test}
18+
"backoff" {>= "0.1.0" & with-test}
1819
"alcotest" {>= "1.7.0" & with-test}
1920
"qcheck" {>= "0.21.3" & with-test}
2021
"qcheck-stm" {>= "0.3" & with-test}
2122
"qcheck-alcotest" {>= "0.21.3" & with-test}
2223
"yojson" {>= "2.0.2" & with-test}
23-
"dscheck" {>= "0.2.0" & with-test}
24+
"dscheck" {>= "0.4.0" & with-test}
2425
"odoc" {with-doc}
2526
]
2627
build: [

saturn_lockfree.opam

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ homepage: "https://github.com/ocaml-multicore/saturn"
88
doc: "https://ocaml-multicore.github.io/saturn/"
99
bug-reports: "https://github.com/ocaml-multicore/saturn/issues"
1010
depends: [
11-
"dune" {>= "3.2"}
11+
"dune" {>= "3.9"}
1212
"ocaml" {>= "4.13"}
1313
"domain_shims" {>= "0.1.0" & with-test}
1414
"backoff" {>= "0.1.0"}
@@ -20,7 +20,7 @@ depends: [
2020
"qcheck-multicoretests-util" {>= "0.3" & with-test}
2121
"qcheck-alcotest" {>= "0.21.3" & with-test}
2222
"yojson" {>= "2.0.2" & with-test}
23-
"dscheck" {>= "0.2.0" & with-test}
23+
"dscheck" {>= "0.4.0" & with-test}
2424
"odoc" {with-doc}
2525
]
2626
build: [

src/domain.ocaml4.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let cpu_relax = Thread.yield

src/dune

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1+
(* -*- tuareg -*- *)
2+
3+
let maybe_threads =
4+
if Jbuild_plugin.V1.ocaml_version < "5" then "threads.posix" else ""
5+
6+
let () =
7+
Jbuild_plugin.V1.send
8+
@@ {|
9+
110
(library
211
(name saturn)
312
(public_name saturn)
413
(libraries
5-
(re_export saturn_lockfree)))
14+
(re_export saturn_lockfree) |}
15+
^ maybe_threads
16+
^ {| ))
17+
18+
(rule
19+
(enabled_if
20+
(< %{ocaml_version} 5.0.0))
21+
(action
22+
(copy domain.ocaml4.ml domain.ml)))
23+
|}

src/mpmc_relaxed_queue.ml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ module Not_lockfree = struct
4444
time_to_try_push_forward nth_attempt && ccas head tail_val (tail_val + 1)
4545
then (* pushed forward head *)
4646
false
47-
else (* retry *)
47+
else begin
48+
Domain.cpu_relax ();
49+
(* retry *)
4850
take_or_rollback (nth_attempt + 1)
51+
end
4952
in
5053

5154
(* if succeeded return true otherwise clean up *)
@@ -87,7 +90,10 @@ module Not_lockfree = struct
8790
&& ccas tail old_head (old_head + 1)
8891
then (* pushed tail forward *)
8992
None
90-
else take_or_rollback (nth_attempt + 1)
93+
else begin
94+
Domain.cpu_relax ();
95+
take_or_rollback (nth_attempt + 1)
96+
end
9197
in
9298

9399
(* return if got item, clean up otherwise *)
@@ -99,7 +105,7 @@ module Not_lockfree = struct
99105
let head_val = Atomic.get head in
100106
let size = mask + 1 in
101107
if tail_val - head_val >= size then false
102-
else if ccas tail tail_val (tail_val + 1) then (
108+
else if ccas tail tail_val (tail_val + 1) then begin
103109
let index = tail_val land mask in
104110
let cell = Array.get array index in
105111
(*
@@ -119,25 +125,28 @@ module Not_lockfree = struct
119125
standard interface.
120126
*)
121127
while not (Atomic.compare_and_set cell None (Some item)) do
122-
()
128+
Domain.cpu_relax ()
123129
done;
124-
true)
130+
true
131+
end
125132
else push t item
126133

127134
let rec pop ({ array; tail; head; mask; _ } as t) =
128135
let tail_val = Atomic.get tail in
129136
let head_val = Atomic.get head in
130137
if head_val - tail_val >= 0 then None
131-
else if ccas head head_val (head_val + 1) then (
138+
else if ccas head head_val (head_val + 1) then begin
132139
let index = head_val land mask in
133140
let cell = Array.get array index in
134141
let item = ref (Atomic.get cell) in
135142
while
136143
not (Option.is_some !item && Atomic.compare_and_set cell !item None)
137144
do
145+
Domain.cpu_relax ();
138146
item := Atomic.get cell
139147
done;
140-
!item)
148+
!item
149+
end
141150
else pop t
142151
end
143152
end

test/atomic/dune

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
(library
22
(name atomic)
3-
(libraries dscheck))
3+
(libraries dscheck)
4+
(enabled_if
5+
(>= %{ocaml_version} 5)))

test/barrier/dune

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
(library
2-
(name barrier))
2+
(name barrier)
3+
(libraries domain_shims))

test/michael_scott_queue/dune

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
(package saturn_lockfree)
88
(name michael_scott_queue_dscheck)
99
(libraries atomic dscheck alcotest backoff)
10-
(enabled_if
11-
(not
12-
(and
13-
(= %{arch_sixtyfour} false)
14-
(= %{architecture} arm))))
10+
(build_if
11+
(and
12+
(>= %{ocaml_version} 5)
13+
(not
14+
(and
15+
(= %{arch_sixtyfour} false)
16+
(= %{architecture} arm)))))
1517
(modules michael_scott_queue michael_scott_queue_dscheck))
1618

1719
(test
@@ -23,21 +25,14 @@
2325
qcheck
2426
qcheck-core
2527
qcheck-alcotest
28+
domain_shims
2629
alcotest)
2730
(modules qcheck_michael_scott_queue))
2831

2932
(test
3033
(package saturn_lockfree)
3134
(name stm_michael_scott_queue)
3235
(modules stm_michael_scott_queue)
33-
(libraries
34-
saturn_lockfree
35-
qcheck-core
36-
qcheck-core.runner
37-
qcheck-stm.stm
38-
qcheck-stm.sequential
39-
qcheck-stm.domain)
36+
(libraries saturn_lockfree qcheck-core qcheck-stm.stm stm_run)
4037
(enabled_if
41-
(= %{arch_sixtyfour} true))
42-
(action
43-
(run %{test} --verbose)))
38+
(= %{arch_sixtyfour} true)))

0 commit comments

Comments
 (0)