package apero-core

  1. Overview
  2. Docs

Source file guard.ml

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
type 'a t = {mutable self: 'a;  mutex: Lwt_mutex.t}

let create v = 
    { self = v
    ; mutex = Lwt_mutex.create () }

let get g = g.self

let acquire g = 
    let open Lwt.Infix in 
    Lwt_mutex.lock g.mutex 
    >>= fun () -> Lwt.return g.self

let release g v = 
    g.self <- v;
    Lwt_mutex.unlock g.mutex
    
let set v g = 
    let open Lwt.Infix in 
    Lwt_mutex.lock g.mutex 
    >>= fun () -> Lwt.return (g.self <- v) 
    >>= fun () -> Lwt.return @@ Lwt_mutex.unlock g.mutex

let guarded g f = 
    let open Lwt.Infix in 
    Lwt_mutex.lock g.mutex >>=
    fun () -> 
        Lwt.try_bind 
            (fun () -> f g.self)
            (fun (s, r) -> 
                g.self <- s;
                Lwt_mutex.unlock g.mutex
                ; r)
            (fun e -> Lwt_mutex.unlock g.mutex ; Lwt.fail e)        
    
let return v s = Lwt.return (s, Lwt.return v)

let return_lwt v s = Lwt.return (s, v)
OCaml

Innovation. Community. Security.