add CCLock.update_map

This commit is contained in:
Simon Cruanes 2016-01-25 15:13:12 +01:00
parent 483f90cb52
commit 49991717c1
3 changed files with 17 additions and 0 deletions

View file

@ -146,3 +146,4 @@ val (>|=) : 'a t -> ('a -> 'b) -> 'b t
val stop_pool : unit -> unit
(** Stop the thread pool *)

View file

@ -104,6 +104,17 @@ let update l f =
let l = create 5 in update l (fun x->x+1); get l = 6
*)
let update_map l f =
with_lock l
(fun x ->
let x', y = f x in
l.content <- x';
y)
(*$T
let l = create 5 in update_map l (fun x->x+1, string_of_int x) = "5" && get l = 6
*)
let get l =
Mutex.lock l.mutex;
let x = l.content in

View file

@ -60,6 +60,11 @@ val with_lock_as_ref : 'a t -> f:('a LockRef.t -> 'b) -> 'b
val update : 'a t -> ('a -> 'a) -> unit
(** [update l f] replaces the content [x] of [l] with [f x], atomically *)
val update_map : 'a t -> ('a -> 'a * 'b) -> 'b
(** [update_map l f] computes [x', y = f (get l)], then puts [x'] in [l]
and returns [y]
@since NEXT_RELEASE *)
val mutex : _ t -> Mutex.t
(** Underlying mutex *)