Skip to content

Commit b9ced74

Browse files
authored
Merge pull request #4 from sadiqj/dunify
First pass at getting this to build under dune
2 parents 5570dc0 + 934ac30 commit b9ced74

File tree

12 files changed

+66
-81
lines changed

12 files changed

+66
-81
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ tmp
55
\#*#
66
*.install
77
*.native
8-
*.byte
8+
*.byte
9+
**/.merlin

_tags

Lines changed: 0 additions & 6 deletions
This file was deleted.

dune-project

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(lang dune 1.8)

opam renamed to kcas.opam

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
opam-version: "1.2"
1+
opam-version: "2.0"
2+
name: "kcas"
23
maintainer: "KC Sivaramakrishnan <sk826@cl.cam.ac.uk>"
34
authors: ["KC Sivaramakrishnan <sk826@cl.cam.ac.uk>"]
45
homepage: "https://github.com/kayceesrk/kcas"
6+
description: "Multi-word compare-and-swap library"
57
doc: "https://kayceesrk.github.io/kcas/doc"
68
license: "ISC"
7-
dev-repo: "https://github.com/kayceesrk/kcas.git"
9+
dev-repo: "git+https://github.com/kayceesrk/kcas.git"
810
bug-reports: "https://github.com/kayceesrk/kcas/issues"
911
tags: []
10-
available: [ ocaml-version >= "4.01.0"]
1112
depends: [
1213
"ocamlfind" {build}
1314
"ocamlbuild" {build}
14-
"topkg" {build} ]
15+
"dune" {build} ]
1516
depopts: []
1617
build: [
17-
"ocaml" "pkg/pkg.ml" "build"
18-
"--pinned" pinned ]
18+
["dune" "build" "src/kcas.mllib"]
19+
]

pkg/META

Lines changed: 0 additions & 7 deletions
This file was deleted.

pkg/pkg.ml

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/dune

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(library
2+
(name kcas)
3+
(libraries unix)
4+
(modes native))

src/kcas.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ Copyright (c) 2017, Nicolas ASSOUAD <nicolas.assouad@ens.fr>
44
########
55
*)
66

7-
open Printf;;
8-
97
module type Backoff = Kcas_backoff.S;;
108
module Backoff = Kcas_backoff.M;;
119

@@ -62,7 +60,7 @@ let mk_casn st c_l = {
6260
id_casn = Oo.id (object end);
6361
};;
6462

65-
let rec st_eq s s' =
63+
let st_eq s s' =
6664
match s, s' with
6765
|WORD(x), WORD(x') -> x == x'
6866
|RDCSS_DESC(r), RDCSS_DESC(r') -> r.id_rdcss == r'.id_rdcss
@@ -148,7 +146,7 @@ let rec get a =
148146
let kCAS c_l =
149147
match c_l with
150148
|[] -> true
151-
|[CAS(r, o, n) as c] -> ignore @@ get r; commit c
149+
|[CAS(r, _, _) as c] -> ignore @@ get r; commit c
152150
|_ -> casn_proceed (mk_casn (ref UNDECIDED) c_l)
153151
;;
154152

src/kcas.mli

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ Copyright (c) 2017, Nicolas ASSOUAD <nicolas.assouad@ens.fr>
44
########
55
*)
66

7-
type 'a ref;;
87
(** Type of shared memory reference *)
8+
type 'a ref;;
99

10-
type t;;
1110
(** Type of compare and swap value *)
11+
type t;;
1212

1313
(** The type of CAS result. *)
1414
type 'a cas_result =
@@ -17,71 +17,71 @@ type 'a cas_result =
1717
| Success of 'a
1818
;;
1919

20-
val ref : 'a -> 'a ref;;
2120
(** [ref x] returns a reference on a shared memory ceils containing the value [x] *)
21+
val ref : 'a -> 'a ref;;
2222

2323
val equal : 'a ref -> 'b ref -> bool;;
2424

2525
val is_on_ref : t -> 'a ref -> bool;;
2626

27-
val mk_cas : 'a ref -> 'a -> 'a -> t;;
2827
(** [mk_cas a o n] returns a new CAS value, which when performed, updates
2928
the reference [a] to [n] if the current content of [a] is [o] *)
29+
val mk_cas : 'a ref -> 'a -> 'a -> t;;
3030

31-
val set : 'a ref -> 'a -> unit;;
3231
(** [set r n] updates the reference [r] to value [n] directly. Not Safe to use with
3332
shared memory ! *)
33+
val set : 'a ref -> 'a -> unit;;
3434

35-
val cas : 'a ref -> 'a -> 'a -> bool;;
3635
(** [cas r e u] updates the reference [r] to value [u] if the current content
3736
of [r] is [e]. *)
37+
val cas : 'a ref -> 'a -> 'a -> bool;;
3838

39-
val commit : t -> bool;;
4039
(** [commit c] performs the CAS [c] and returns [true] if the CAS is successful. *)
40+
val commit : t -> bool;;
4141

42-
val kCAS : t list -> bool;;
4342
(** [kCAS l] performs a lock-free multi-word CAS and returns [true] if the
4443
multi-word CAS is successful. *)
44+
val kCAS : t list -> bool;;
4545

46-
val get : 'a ref -> 'a;;
4746
(** [get a] reads the value contained in the memory ceil [a]. *)
47+
val get : 'a ref -> 'a;;
4848

49-
val try_map : 'a ref -> ('a -> 'a option) -> 'a cas_result;;
5049
(** [try_map r f] invokes [f c], where [c] is the result of [get r]. If the
5150
result of [f c] is [None], then [Aborted] is returned. If the result of [f c]
5251
is [Some v], then attempt to CAS update [r] from [c] to [v]. If the CAS
5352
succeeds, then [Success c] is returned. If the CAS fails, then [Failed] is
5453
returned. *)
54+
val try_map : 'a ref -> ('a -> 'a option) -> 'a cas_result;;
5555

56-
val map : 'a ref -> ('a -> 'a option) -> 'a cas_result;;
5756
(** Like {!try_map} but retries on CAS failure. Hence, [map r f] never returns
5857
[Failed]. *)
58+
val map : 'a ref -> ('a -> 'a option) -> 'a cas_result;;
5959

60-
val incr : int ref -> unit;;
6160
(** [incr r] atomically increments [r] *)
61+
val incr : int ref -> unit;;
6262

63-
val decr : int ref -> unit;;
6463
(** [decr r] atomically decrements [r] *)
64+
val decr : int ref -> unit;;
6565

6666
(** {2 Backoff}
6767
Suspend domains with exponential backoff. *)
6868
module type Backoff = sig
69-
type t;;
7069
(** The type of backoff value *)
70+
type t;;
7171

72-
val create : ?max:int -> unit -> t;;
7372
(** [create ~max:maxv ()] returns a backoff value, which when waited upon,
7473
suspends the calling domain for [x] milliseconds, where [x] is the
7574
current value of the backoff. The backoff value [x] is doubled after
7675
every wait upto a maximum of [maxv] milliseconds. The default maximum is
7776
32 milliseconds. The initial backoff is 1 millisecond. *)
78-
79-
val once : t -> unit;;
77+
val create : ?max:int -> unit -> t;;
78+
8079
(** [once b] suspends the current domain for [x] milliseconds, where [x] is
8180
the current value of the backoff. *)
82-
83-
val reset : t -> unit;;
81+
val once : t -> unit;;
82+
8483
(** Resets the backoff clock to 1 millisecond. *)
84+
val reset : t -> unit;;
8585
end
8686

8787
module Backoff : Backoff;;
@@ -92,39 +92,36 @@ module Backoff : Backoff;;
9292
internal representation of a single word CAS reference is more efficient
9393
than that of a multi-word CAS reference. *)
9494
module type W1 = sig
95-
96-
type 'a ref;;
9795
(** The type of shared memory reference. *)
96+
type 'a ref;;
9897

99-
val ref : 'a -> 'a ref;;
10098
(** Create a new reference. *)
99+
val ref : 'a -> 'a ref;;
101100

102-
val get : 'a ref -> 'a;;
103101
(** Get the value of the reference. *)
102+
val get : 'a ref -> 'a;;
104103

105104
val set : 'a ref -> 'a -> unit;;
106105

107-
val cas : 'a ref -> 'a -> 'a -> bool;;
108106
(** [cas r e u] updates the reference [r] to value [u] if the current content
109107
of [r] is [e]. *)
110-
111-
val try_map : 'a ref -> ('a -> 'a option) -> 'a cas_result;;
108+
val cas : 'a ref -> 'a -> 'a -> bool;;
109+
112110
(** [try_map r f] invokes [f c], where [c] is the result of [get r]. If the
113111
result of [f c] is [None], then [Aborted] is returned. If the result of [f c]
114112
is [Some v], then attempt to CAS update [r] from [c] to [v]. If the CAS
115113
succeeds, then [Success c] is returned. If the CAS fails, then [Failed] is
116114
returned. *)
115+
val try_map : 'a ref -> ('a -> 'a option) -> 'a cas_result;;
117116

118-
val map : 'a ref -> ('a -> 'a option) -> 'a cas_result;;
119117
(** Like {!try_map} but retries on CAS failure. Hence, [map r f] never returns
120118
[Failed]. *)
119+
val map : 'a ref -> ('a -> 'a option) -> 'a cas_result;;
121120

122-
val incr : int ref -> unit;;
123121
(** [incr r] atomically increments [r] *)
122+
val incr : int ref -> unit;;
124123

125124
val decr : int ref -> unit;;
126-
(** [decr r] atomically decrements [r] *)
127-
128125
end
129126

130127
module W1 : W1;;

test/benchmark.ml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11

22
open Printf;;
33

4-
let print_usage_and_exit () =
5-
print_endline @@ "Usage: " ^ Sys.argv.(0) ^ " <k> <num_iter>";
6-
exit(0)
7-
;;
8-
94
let (k_kCAS, num_iter) =
105
if Array.length Sys.argv < 3 then
11-
print_usage_and_exit ()
6+
(2, 1000)
127
else try
138
let a = int_of_string (Sys.argv.(1)) in
149
let b = int_of_string (Sys.argv.(2)) in
1510
(a,b)
16-
with Failure _ -> print_usage_and_exit ()
11+
with Failure _ -> failwith "Unable to parse arguments"
1712
;;
1813

1914
let make_kCAS k =
@@ -63,7 +58,7 @@ let benchmark () =
6358

6459
let main () =
6560
let n = 10 in
66-
let (m,sd) = Benchmark.benchmark benchmark n in
61+
let (m,_) = Benchmark.benchmark benchmark n in
6762
print_endline (sprintf "%f" m)
6863
;;
6964

test/dune

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(test
2+
(name test)
3+
(libraries kcas)
4+
(modules test)
5+
(modes native))
6+
7+
(test
8+
(name benchmark)
9+
(libraries kcas)
10+
(modules benchmark)
11+
(modes native))

test/test.ml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ let v_y = 1;;
4141
let thread1 (a1, a2) =
4242
let c1 = [mk_cas a1 v_x v_y ; mk_cas a2 v_x v_y] in
4343
let c2 = [mk_cas a1 v_y v_x ; mk_cas a2 v_y v_x] in
44-
for i = 1 to nb_iter do
44+
for _ = 1 to nb_iter do
4545
let out1 = kCAS c1 in
4646
let out2 = kCAS c2 in
4747
if out1 <> true || out2 <> true then begin
@@ -56,7 +56,7 @@ let thread1 (a1, a2) =
5656
let thread2 (a1, a2) =
5757
let c1 = [mk_cas a1 v_y v_x ; mk_cas a2 v_x v_y] in
5858
let c2 = [mk_cas a1 v_x v_y ; mk_cas a2 v_y v_x] in
59-
for i = 1 to nb_iter do
59+
for _ = 1 to nb_iter do
6060
let out1 = kCAS c1 in
6161
let out2 = kCAS c2 in
6262
if out1 <> false || out2 <> false then begin
@@ -69,7 +69,7 @@ let thread2 (a1, a2) =
6969
let thread3 (a1, a2) =
7070
let c1 = [mk_cas a1 v_x v_y ; mk_cas a2 v_y v_x] in
7171
let c2 = [mk_cas a1 v_y v_x ; mk_cas a2 v_x v_y] in
72-
for i = 1 to nb_iter do
72+
for _ = 1 to nb_iter do
7373
let out1 = kCAS c1 in
7474
let out2 = kCAS c2 in
7575
if out1 <> false || out2 <> false then begin
@@ -89,7 +89,7 @@ let thread4 (a1, a2) =
8989
;;
9090

9191
let thread5 (a1, a2) =
92-
for i = 0 to nb_iter do
92+
for _ = 0 to nb_iter do
9393
let a = get a1 in
9494
let b = get a2 in
9595
if a > b then
@@ -101,9 +101,9 @@ let test_casn () =
101101
let a1 = ref v_x in
102102
let a2 = ref v_x in
103103

104-
Domain.spawn (fun () -> thread1 (a1, a2));
105-
Domain.spawn (fun () -> thread2 (a1, a2));
106-
Domain.spawn (fun () -> thread3 (a1, a2));
104+
Domain.spawn (fun () -> thread1 (a1, a2)) |> ignore;
105+
Domain.spawn (fun () -> thread2 (a1, a2)) |> ignore;
106+
Domain.spawn (fun () -> thread3 (a1, a2)) |> ignore;
107107

108108
Unix.sleep wait_time;
109109
print_endline (sprintf "a1 = %d et a2 = %d" (get a1) (get a2));
@@ -114,8 +114,8 @@ let test_read_casn () =
114114
let a1 = ref 0 in
115115
let a2 = ref 0 in
116116

117-
Domain.spawn (fun () -> thread4 (a1, a2));
118-
Domain.spawn (fun () -> thread5 (a1, a2));
117+
Domain.spawn (fun () -> thread4 (a1, a2)) |> ignore;
118+
Domain.spawn (fun () -> thread5 (a1, a2)) |> ignore;
119119

120120
Unix.sleep wait_time;
121121
!th4_success && !th5_success

0 commit comments

Comments
 (0)