]> matita.cs.unibo.it Git - helm.git/blob - helm/hbugs/common/threadSafe.ml
- fixed helm web page url and copyright notice
[helm.git] / helm / hbugs / common / threadSafe.ml
1 (*
2  * Copyright (C) 2003:
3  *    Stefano Zacchiroli <zack@cs.unibo.it>
4  *    for the HELM Team http://helm.cs.unibo.it/
5  *
6  *  This file is part of HELM, an Hypertextual, Electronic
7  *  Library of Mathematics, developed at the Computer Science
8  *  Department, University of Bologna, Italy.
9  *
10  *  HELM is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU General Public License
12  *  as published by the Free Software Foundation; either version 2
13  *  of the License, or (at your option) any later version.
14  *
15  *  HELM is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with HELM; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23  *  MA  02111-1307, USA.
24  *
25  *  For details, see the HELM World-Wide-Web page,
26  *  http://helm.cs.unibo.it/
27  *)
28
29 let debug_print = let debug = false in fun s -> if debug then prerr_endline s;;
30
31 class threadSafe =
32   object (self)
33
34     val mutex = Mutex.create ()
35
36       (** condition variable: 'no readers is currently reading' *)
37     val noReaders = Condition.create ()
38
39       (** readers count *)
40     val mutable readersCount = 0
41
42     method private incrReadersCount = (* internal, not exported *)
43       self#doCritical (lazy (
44         readersCount <- readersCount + 1
45       ))
46
47     method private decrReadersCount = (* internal, not exported *)
48       self#doCritical (lazy (
49         if readersCount > 0 then readersCount <- readersCount - 1;
50       ))
51
52     method private signalNoReaders =  (* internal, not exported *)
53       self#doCritical (lazy (
54         if readersCount = 0 then Condition.signal noReaders
55       ))
56
57     method private doCritical: 'a. 'a lazy_t -> 'a =
58       fun action ->
59         debug_print "<doCritical>";
60         (try
61           Mutex.lock mutex;
62           let res = Lazy.force action in
63           Mutex.unlock mutex;
64           debug_print "</doCritical>";
65           res
66         with e ->
67           Mutex.unlock mutex;
68           raise e);
69
70     method private doReader: 'a. 'a lazy_t -> 'a =
71       fun action ->
72         debug_print "<doReader>";
73         let cleanup () =
74           self#decrReadersCount;
75           self#signalNoReaders
76         in
77         self#incrReadersCount;
78         let res = (try Lazy.force action with e -> (cleanup (); raise e)) in
79         cleanup ();
80         debug_print "</doReader>";
81         res
82
83       (* TODO may starve!!!! is what we want or not? *)
84     method private doWriter: 'a. 'a lazy_t -> 'a =
85       fun action ->
86         debug_print "<doWriter>";
87         self#doCritical (lazy (
88           while readersCount > 0 do
89             Condition.wait noReaders mutex
90           done;
91           let res = Lazy.force action in
92           debug_print "</doWriter>";
93           res
94         ))
95
96   end