blob: 52483b3ed18a1c92a98b8e36cb896505940af117 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
module Ordering = struct
type t = Equal | Smaller | Greater
let of_int x = if x < 0 then Smaller else if x > 0 then Greater else Equal
end
module Result = struct
let ( let* ) = Result.bind
let fmap f m = Result.bind m f
let sequence ms = List.fold_right
(fun m acc ->
let* acc = acc in
let* m = m in
Ok (m::acc))
ms
(Ok [])
end
module Option = struct
let ( let* ) = Option.bind
let fmap f m = Option.bind m f
end
module Atomic = struct
let rec swap atom f =
let v = Atomic.get atom in
let v' = f v in
if Atomic.compare_and_set atom v v'
then atom
else swap atom f
let rec mswap atom f =
let open Result in
let v = Atomic.get atom in
let* v' = f v in
if Atomic.compare_and_set atom v v'
then Ok atom
else mswap atom f
end
|