diff options
Diffstat (limited to 'lib/Utilities.ml')
| -rw-r--r-- | lib/Utilities.ml | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/Utilities.ml b/lib/Utilities.ml index a6b36c7..e4ee9c3 100644 --- a/lib/Utilities.ml +++ b/lib/Utilities.ml @@ -1,3 +1,9 @@ +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 @@ -69,3 +75,25 @@ module FingerTree = struct BatFingerTree.print ~first:"" ~last:"" ~sep:sep BatIO.nwrite |> Fun.flip BatIO.to_string ss 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 |
