1995-11-05 09:27:32 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-11-05 09:27:32 -08:00
|
|
|
(* *)
|
1995-11-14 09:12:57 -08:00
|
|
|
(* Xavier Leroy and Damien Doligez, INRIA Rocquencourt *)
|
1995-11-05 09:27:32 -08:00
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1995-11-05 09:27:32 -08:00
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-11-15 08:40:01 -08:00
|
|
|
(* Module [Condition]: condition variables to synchronize between threads *)
|
|
|
|
|
|
|
|
(* Condition variables are used when one thread wants to wait until another
|
|
|
|
thread has finished doing something: the former thread ``waits'' on the
|
|
|
|
condition variable, the latter thread ``signals'' the condition when it
|
|
|
|
is done. Condition variables should always be protected by a mutex.
|
|
|
|
The typical use is (if [D] is a shared data structure, [m] its mutex,
|
|
|
|
and [c] is a condition variable):
|
|
|
|
[
|
|
|
|
Mutex.lock m;
|
|
|
|
while (* some predicate P over D is not satisfied *) do
|
|
|
|
Condition.wait c m
|
|
|
|
done;
|
|
|
|
(* Modify D *)
|
|
|
|
if (* the predicate P over D is now satified *) then Condition.signal c;
|
|
|
|
Mutex.unlock m
|
|
|
|
]
|
|
|
|
*)
|
1995-11-05 09:27:32 -08:00
|
|
|
|
|
|
|
type t
|
1995-11-15 08:40:01 -08:00
|
|
|
(* The type of condition variables. *)
|
1996-04-22 04:15:41 -07:00
|
|
|
val create: unit -> t
|
1995-11-15 08:40:01 -08:00
|
|
|
(* Return a new condition variable. *)
|
1996-04-03 02:02:34 -08:00
|
|
|
val wait: t -> Mutex.t -> unit
|
1995-11-15 08:40:01 -08:00
|
|
|
(* [wait c m] atomically unlocks the mutex [m] and suspends the
|
|
|
|
calling process on the condition variable [c]. The process will
|
|
|
|
restart after the condition variable [c] has been signalled.
|
|
|
|
The mutex [m] is locked again before [wait] returns. *)
|
1996-04-03 02:02:34 -08:00
|
|
|
val signal: t -> unit
|
1995-11-15 08:40:01 -08:00
|
|
|
(* [signal c] restarts one of the processes waiting on the
|
|
|
|
condition variable [c]. *)
|
1996-04-03 02:02:34 -08:00
|
|
|
val broadcast: t -> unit
|
1995-11-15 08:40:01 -08:00
|
|
|
(* [broadcast c] restarts all processes waiting on the
|
|
|
|
condition variable [c]. *)
|