diff --git a/src/threads/CCFuture.mli b/src/threads/CCFuture.mli index c42a5785..0d2a1fb2 100644 --- a/src/threads/CCFuture.mli +++ b/src/threads/CCFuture.mli @@ -146,3 +146,4 @@ val (>|=) : 'a t -> ('a -> 'b) -> 'b t val stop_pool : unit -> unit (** Stop the thread pool *) + diff --git a/src/threads/CCLock.ml b/src/threads/CCLock.ml index 107e7c7d..1088c605 100644 --- a/src/threads/CCLock.ml +++ b/src/threads/CCLock.ml @@ -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 diff --git a/src/threads/CCLock.mli b/src/threads/CCLock.mli index ad46c6c2..e1982e8f 100644 --- a/src/threads/CCLock.mli +++ b/src/threads/CCLock.mli @@ -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 *)