add shims for Atomic on OCaml < 4.12

This commit is contained in:
Simon Cruanes 2023-06-05 21:57:22 -04:00
parent 911db76864
commit 3f3d3e3464
3 changed files with 52 additions and 2 deletions

View file

@ -18,3 +18,12 @@
(with-stdout-to (with-stdout-to
%{targets} %{targets}
(run %{bin})))) (run %{bin}))))
(rule
(targets Tiny_httpd_atomic_.ml)
(deps
(:bin ./gen/mkshims.exe))
(action
(with-stdout-to
%{targets}
(run %{bin}))))

View file

@ -1,2 +1,2 @@
(executable (executables
(name gentags)) (names gentags mkshims))

41
src/gen/mkshims.ml Normal file
View file

@ -0,0 +1,41 @@
let atomic_before_412 =
{|
type 'a t = {mutable x: 'a}
let[@inline] make x = {x}
let[@inline] get {x} = x
let[@inline] set r x = r.x <- x
let[@inline] exchange r x =
let y = r.x in
r.x <- x;
y
let[@inline] compare_and_set r seen v =
if r.x == seen then (
r.x <- v;
true
) else false
let[@inline] fetch_and_add r x =
let v = r.x in
r.x <- x + r.x;
v
let[@inline] incr r = r.x <- 1 + r.x
let[@inline] decr r = r.x <- r.x - 1
|}
let atomic_after_412 = {|include Atomic|}
let write_file file s =
let oc = open_out file in
output_string oc s;
close_out oc
let () =
let version = Scanf.sscanf Sys.ocaml_version "%d.%d.%s" (fun x y _ -> x, y) in
print_endline
(if version >= (4, 12) then
atomic_after_412
else
atomic_before_412);
()