2
* RMutex - Reentrant mutexes
3
* Copyright (C) 1996 Xavier Leroy
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version,
11
* with the special exception on linking described in file LICENSE.
13
* This library is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this library; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
(** Locks for mutual exclusion.
26
Mutexes (mutual-exclusion locks) are used to implement critical sections
27
and protect shared mutable data structures against concurrent accesses.
28
The typical use is (if [m] is the mutex associated with the data structure
31
RMutex.synchronize ~lock:m (fun () ->
32
(* Critical section that operates over D *);
36
This module implements reentrant mutexes, i.e. a version of mutexes which
37
may be locked again by their owner thread without blocking this thread.
38
Reentrant mutexes are typically slower than regular mutexes but also safer.
42
@author Xavier Leroy (Base module)
43
@author Damien Doligez (Base module)
48
(** The type of mutexes. *)
50
val create : unit -> t
51
(** Return a new mutex. *)
54
(** Lock the given mutex. Only one thread can have the mutex locked
55
at any time. A thread that attempts to lock a mutex already locked
56
will suspend until the other mutex is unlocked.
58
{b Note} attempting to lock a mutex you already have locked from
59
the same thread will not suspend your thread.
62
val try_lock : t -> bool
63
(** Same as {!RMutex.lock}, but does not suspend the calling thread if
64
the mutex is already locked: just return [false] immediately
65
in that case. If the mutex is unlocked, lock it and
68
val unlock : t -> unit
69
(** Unlock the given mutex. Other threads suspended trying to lock
70
the mutex will restart. If the mutex wasn't locked, nothing happens.*)
72
val synchronize : ?lock:t -> ('a -> 'b) -> 'a -> 'b
73
(** Protect a function.
75
[synchronize f] returns a new function [f'] with the same behavior
76
as [f] but such that concurrenty calls to [f'] are queued if
77
necessary to avoid races.
79
[synchronize ~lock:l f] behaves as [synchronize f] but uses a
80
user-specified lock [l], which may be useful to share a lock
81
between several function.
83
In either case, the lock is acquired when entering the function
84
and released when the function call ends, whether this is due
85
to normal termination or to some exception being raised.
89
val make : unit -> Extlib.Concurrent.lock
91
Create a new abstract lock based on Reentrant Mutexes.