1995-11-05 09:27:32 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Caml Special Light *)
|
|
|
|
(* *)
|
1995-11-14 09:12:57 -08:00
|
|
|
(* Xavier Leroy and Damien Doligez, INRIA Rocquencourt *)
|
1995-11-05 09:27:32 -08:00
|
|
|
(* *)
|
|
|
|
(* Copyright 1995 Institut National de Recherche en Informatique et *)
|
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-11-14 09:12:57 -08:00
|
|
|
(* Module [Mutex]: locks for mutual exclusion *)
|
1995-11-05 09:27:32 -08:00
|
|
|
|
1995-11-15 08:40:01 -08:00
|
|
|
(* Mutexes (mutual-exclusion locks) are used to implement critical sections
|
|
|
|
and protect shared mutable data structures against concurrent accesses.
|
|
|
|
The typical use is (if [m] is the mutex associated with the data structure
|
|
|
|
[D]):
|
|
|
|
[
|
|
|
|
Mutex.lock m;
|
|
|
|
(* Critical section that operates over D *);
|
|
|
|
Mutex.unlock m
|
|
|
|
]
|
|
|
|
*)
|
|
|
|
|
1995-11-05 09:27:32 -08:00
|
|
|
type t
|
1995-11-15 08:40:01 -08:00
|
|
|
(* The type of mutexes. *)
|
1996-04-01 07:26:00 -08:00
|
|
|
external new: unit -> t = "csl_mutex_new"
|
1995-11-15 08:40:01 -08:00
|
|
|
(* Return a new mutex. *)
|
1996-04-01 07:26:00 -08:00
|
|
|
external lock: t -> unit = "csl_mutex_lock"
|
1995-11-15 08:40:01 -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. *)
|
1996-04-01 07:26:00 -08:00
|
|
|
external try_lock: t -> bool = "csl_mutex_try_lock"
|
1995-11-15 08:40:01 -08:00
|
|
|
(* Same as [try_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]. *)
|
1996-04-01 07:26:00 -08:00
|
|
|
external unlock: t -> unit = "csl_mutex_unlock"
|
1995-11-15 08:40:01 -08:00
|
|
|
(* Unlock the given mutex. Other threads suspended trying to lock
|
|
|
|
the mutex will restart. *)
|