aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/remoteCounter.ml
blob: e0e2d5cbc7cc5cd5c5a22a3176c765785faa8b74 (plain)
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
(************************************************************************)
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
(* <O___,, *   INRIA - CNRS - LIX - LRI - PPS - Copyright 1999-2012     *)
(*   \VV/  **************************************************************)
(*    //   *      This file is distributed under the terms of the       *)
(*         *       GNU Lesser General Public License Version 2.1        *)
(************************************************************************)

type 'a getter = unit -> 'a
type 'a installer = ('a getter) -> unit

let new_counter a ~incr ~build =
  let data = ref a in
  let m = Mutex.create () in
  let mk_thsafe_getter f () =
    (* - slaves must use a remote counter getter, not this one! *)
    (* - in the main process there is a race condition between slave
         managers (that are threads) and the main thread, hence the mutex *)
    (match !Flags.async_proofs_mode with
    | Flags.APonParallel n when n > 0 ->
        Errors.anomaly(Pp.str"Slave processes must install remote counters");
    | _ -> ());
    Mutex.lock m; let x = f () in Mutex.unlock m;
    build x in
  let getter = ref (mk_thsafe_getter (fun () -> data := incr !data; !data)) in
  let installer f =
    (match !Flags.async_proofs_mode with
    | Flags.APoff | Flags.APonLazy | Flags.APonParallel 0 -> 
      Errors.anomaly(Pp.str"Only slave processes can install a remote counter")
    | _ -> ());
    getter := f in
  (fun () -> !getter ()), installer