|
| 1 | +(** Value protected by a mutex *) |
| 2 | + |
| 3 | +type 'a t |
| 4 | +(** A value surrounded with a lock *) |
| 5 | + |
| 6 | +val create : 'a -> 'a t |
| 7 | +(** Create a new protected value. *) |
| 8 | + |
| 9 | +val with_lock : 'a t -> ('a -> 'b) -> 'b |
| 10 | +(** [with_lock l f] runs [f x] where [x] is the value protected with the lock |
| 11 | + [l], in a critical section. If [f x] fails, [with_lock l f] fails too but |
| 12 | + the lock is released. *) |
| 13 | + |
| 14 | +val update : 'a t -> ('a -> 'a) -> unit |
| 15 | +(** [update l f] replaces the content [x] of [l] with [f x], atomically. *) |
| 16 | + |
| 17 | +val update_map : 'a t -> ('a -> 'a * 'b) -> 'b |
| 18 | +(** [update_map l f] computes [x', y = f (get l)], then puts [x'] in [l] and |
| 19 | + returns [y]. *) |
| 20 | + |
| 21 | +val mutex : _ t -> Mutex.t |
| 22 | +(** Underlying mutex. *) |
| 23 | + |
| 24 | +val get : 'a t -> 'a |
| 25 | +(** Atomically get the value in the lock. The value that is returned isn't |
| 26 | + protected! *) |
| 27 | + |
| 28 | +val set : 'a t -> 'a -> unit |
| 29 | +(** Atomically set the value. *) |
| 30 | + |
| 31 | +val exchange : 'a t -> 'a -> 'a |
| 32 | +(** [exchange lock x] atomically sets [lock := x] and returns the previous value |
| 33 | +*) |
| 34 | + |
| 35 | +(** Type allowing to manipulate the lock as a reference when one is holding it. |
| 36 | +*) |
| 37 | +module LockRef : sig |
| 38 | + type 'a t |
| 39 | + |
| 40 | + val as_ref : 'a t -> 'a ref |
| 41 | + val get : 'a t -> 'a |
| 42 | + val set : 'a t -> 'a -> unit |
| 43 | +end |
| 44 | + |
| 45 | +val with_lock_as_ref : 'a t -> ('a LockRef.t -> 'b) -> 'b |
| 46 | +(** [with_lock_as_ref l f] calls [f] with a reference-like object that allows to |
| 47 | + manipulate the value of [l] safely. The object passed to [f] must not escape |
| 48 | + the function call. *) |
| 49 | + |
| 50 | +val pp : 'a Fmt.printer -> 'a t Fmt.printer |
0 commit comments