1996-09-04 07:17:43 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Objective Caml *)
|
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy and Damien Doligez, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* under the terms of the GNU Library General Public License, with *)
|
|
|
|
(* the special exception on linking described in file ../../LICENSE. *)
|
1996-09-04 07:17:43 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
2001-12-05 08:26:03 -08:00
|
|
|
(** Condition variables to synchronize between threads.
|
1996-09-04 07:17:43 -07:00
|
|
|
|
2001-12-05 08:26:03 -08:00
|
|
|
Condition variables are used when one thread wants to wait until another
|
1996-09-04 07:17:43 -07:00
|
|
|
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):
|
2001-12-05 08:26:03 -08:00
|
|
|
{[
|
1996-09-04 07:17:43 -07:00
|
|
|
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
|
2001-12-05 08:26:03 -08:00
|
|
|
]}
|
1996-09-04 07:17:43 -07:00
|
|
|
*)
|
|
|
|
|
|
|
|
type t
|
2001-12-05 08:26:03 -08:00
|
|
|
(** The type of condition variables. *)
|
|
|
|
|
|
|
|
val create : unit -> t
|
|
|
|
(** Return a new condition variable. *)
|
|
|
|
|
|
|
|
val wait : t -> Mutex.t -> unit
|
|
|
|
(** [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. *)
|
|
|
|
|
|
|
|
val signal : t -> unit
|
|
|
|
(** [signal c] restarts one of the processes waiting on the
|
|
|
|
condition variable [c]. *)
|
|
|
|
|
|
|
|
val broadcast : t -> unit
|
|
|
|
(** [broadcast c] restarts all processes waiting on the
|
|
|
|
condition variable [c]. *)
|