]> matita.cs.unibo.it Git - helm.git/blob - components/thread/threadSafe.ml
tagged 0.5.0-rc1
[helm.git] / components / thread / threadSafe.ml
1 (*
2  * Copyright (C) 2003-2004:
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 (* $Id$ *)
30
31 let debug = false
32 let debug_print s = if debug then prerr_endline (Lazy.force s)
33
34 class threadSafe =
35   object (self)
36
37     val mutex = Mutex.create ()
38
39       (** condition variable: 'no readers is currently reading' *)
40     val noReaders = Condition.create ()
41
42       (** readers count *)
43     val mutable readersCount = 0
44
45     method private incrReadersCount = (* internal, not exported *)
46       self#doCritical (lazy (
47         readersCount <- readersCount + 1
48       ))
49
50     method private decrReadersCount = (* internal, not exported *)
51       self#doCritical (lazy (
52         if readersCount > 0 then readersCount <- readersCount - 1;
53       ))
54
55     method private signalNoReaders =  (* internal, not exported *)
56       self#doCritical (lazy (
57         if readersCount = 0 then Condition.signal noReaders
58       ))
59
60     method private doCritical: 'a. 'a lazy_t -> 'a =
61       fun action ->
62         debug_print (lazy "<doCritical>");
63         (try
64           Mutex.lock mutex;
65           let res = Lazy.force action in
66           Mutex.unlock mutex;
67           debug_print (lazy "</doCritical>");
68           res
69         with e ->
70           Mutex.unlock mutex;
71           raise e);
72
73     method private doReader: 'a. 'a lazy_t -> 'a =
74       fun action ->
75         debug_print (lazy "<doReader>");
76         let cleanup () =
77           self#decrReadersCount;
78           self#signalNoReaders
79         in
80         self#incrReadersCount;
81         let res = (try Lazy.force action with e -> (cleanup (); raise e)) in
82         cleanup ();
83         debug_print (lazy "</doReader>");
84         res
85
86       (* TODO may starve!!!! is what we want or not? *)
87     method private doWriter: 'a. 'a lazy_t -> 'a =
88       fun action ->
89         debug_print (lazy "<doWriter>");
90         self#doCritical (lazy (
91           while readersCount > 0 do
92             Condition.wait noReaders mutex
93           done;
94           let res = Lazy.force action in
95           debug_print (lazy "</doWriter>");
96           res
97         ))
98
99   end
100