54 lines | 2417 chars
1 | (**************************************************************************) |
2 | (* *) |
3 | (* OCaml *) |
4 | (* *) |
5 | (* Xavier Leroy and Damien Doligez, INRIA Rocquencourt *) |
6 | (* *) |
7 | (* Copyright 1996 Institut National de Recherche en Informatique et *) |
8 | (* en Automatique. *) |
9 | (* *) |
10 | (* All rights reserved. This file is distributed under the terms of *) |
11 | (* the GNU Lesser General Public License version 2.1, with the *) |
12 | (* special exception on linking described in the file LICENSE. *) |
13 | (* *) |
14 | (**************************************************************************) |
15 | |
16 | (** Condition variables to synchronize between threads. |
17 | |
18 | Condition variables are used when one thread wants to wait until another |
19 | thread has finished doing something: the former thread 'waits' on the |
20 | condition variable, the latter thread 'signals' the condition when it |
21 | is done. Condition variables should always be protected by a mutex. |
22 | The typical use is (if [D] is a shared data structure, [m] its mutex, |
23 | and [c] is a condition variable): |
24 | {[ |
25 | Mutex.lock m; |
26 | while (* some predicate P over D is not satisfied *) do |
27 | Condition.wait c m |
28 | done; |
29 | (* Modify D *) |
30 | if (* the predicate P over D is now satisfied *) then Condition.signal c; |
31 | Mutex.unlock m |
32 | ]} |
33 | *) |
34 | |
35 | type t |
36 | (** The type of condition variables. *) |
37 | |
38 | val create : unit -> t |
39 | (** Return a new condition variable. *) |
40 | |
41 | val wait : t -> Mutex.t -> unit |
42 | (** [wait c m] atomically unlocks the mutex [m] and suspends the |
43 | calling process on the condition variable [c]. The process will |
44 | restart after the condition variable [c] has been signalled. |
45 | The mutex [m] is locked again before [wait] returns. *) |
46 | |
47 | val signal : t -> unit |
48 | (** [signal c] restarts one of the processes waiting on the |
49 | condition variable [c]. *) |
50 | |
51 | val broadcast : t -> unit |
52 | (** [broadcast c] restarts all processes waiting on the |
53 | condition variable [c]. *) |
54 |