aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Utilities.ml
blob: a96d16f3ee52484a19260efa3c73091fb88e4275 (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
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 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