1995-11-05 09:27:32 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
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 *)
|
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. *)
|
1995-11-05 09:27:32 -08:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
2001-10-29 12:07:20 -08:00
|
|
|
(** Locks for mutual exclusion.
|
1995-11-05 09:27:32 -08:00
|
|
|
|
2001-10-29 12:07:20 -08:00
|
|
|
Mutexes (mutual-exclusion locks) are used to implement critical sections
|
1995-11-15 08:40:01 -08:00
|
|
|
and protect shared mutable data structures against concurrent accesses.
|
|
|
|
The typical use is (if [m] is the mutex associated with the data structure
|
|
|
|
[D]):
|
2001-10-29 12:07:20 -08:00
|
|
|
{[
|
1995-11-15 08:40:01 -08:00
|
|
|
Mutex.lock m;
|
|
|
|
(* Critical section that operates over D *);
|
|
|
|
Mutex.unlock m
|
2001-10-29 12:07:20 -08:00
|
|
|
]}
|
1995-11-15 08:40:01 -08:00
|
|
|
*)
|
|
|
|
|
1995-11-05 09:27:32 -08:00
|
|
|
type t
|
2001-12-04 08:16:05 -08:00
|
|
|
(** The type of mutexes. *)
|
2001-10-29 12:07:20 -08:00
|
|
|
|
2001-12-04 08:16:05 -08:00
|
|
|
val create : unit -> t
|
2001-10-29 12:07:20 -08:00
|
|
|
(** Return a new mutex. *)
|
|
|
|
|
2001-12-04 08:16:05 -08:00
|
|
|
val lock : t -> unit
|
2001-10-29 12:07:20 -08:00
|
|
|
(** Lock the given mutex. Only one thread can have the mutex locked
|
|
|
|
at any time. A thread that attempts to lock a mutex already locked
|
|
|
|
by another thread will suspend until the other thread unlocks
|
|
|
|
the mutex. *)
|
|
|
|
|
2001-12-04 08:16:05 -08:00
|
|
|
val try_lock : t -> bool
|
2001-10-29 12:07:20 -08:00
|
|
|
(** Same as {!Mutex.lock}, but does not suspend the calling thread if
|
|
|
|
the mutex is already locked: just return [false] immediately
|
|
|
|
in that case. If the mutex is unlocked, lock it and
|
|
|
|
return [true]. *)
|
|
|
|
|
2001-12-04 08:16:05 -08:00
|
|
|
val unlock : t -> unit
|
2001-10-29 12:07:20 -08:00
|
|
|
(** Unlock the given mutex. Other threads suspended trying to lock
|
|
|
|
the mutex will restart. *)
|