mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-07 19:55:31 -05:00
add CCLock.update_map
This commit is contained in:
parent
483f90cb52
commit
49991717c1
3 changed files with 17 additions and 0 deletions
|
|
@ -146,3 +146,4 @@ val (>|=) : 'a t -> ('a -> 'b) -> 'b t
|
||||||
|
|
||||||
val stop_pool : unit -> unit
|
val stop_pool : unit -> unit
|
||||||
(** Stop the thread pool *)
|
(** Stop the thread pool *)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -104,6 +104,17 @@ let update l f =
|
||||||
let l = create 5 in update l (fun x->x+1); get l = 6
|
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 =
|
let get l =
|
||||||
Mutex.lock l.mutex;
|
Mutex.lock l.mutex;
|
||||||
let x = l.content in
|
let x = l.content in
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,11 @@ val with_lock_as_ref : 'a t -> f:('a LockRef.t -> 'b) -> 'b
|
||||||
val update : 'a t -> ('a -> 'a) -> unit
|
val update : 'a t -> ('a -> 'a) -> unit
|
||||||
(** [update l f] replaces the content [x] of [l] with [f x], atomically *)
|
(** [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
|
val mutex : _ t -> Mutex.t
|
||||||
(** Underlying mutex *)
|
(** Underlying mutex *)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue