mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-07 19:55:31 -05:00
add CCLock.{decr_then_get, get_then_{decr,set,clear}}
atomic updates for integers and bools
This commit is contained in:
parent
ec70f865e4
commit
067e89b1fe
2 changed files with 52 additions and 50 deletions
|
|
@ -1,28 +1,5 @@
|
||||||
(*
|
|
||||||
copyright (c) 2013-2014, simon cruanes
|
|
||||||
all rights reserved.
|
|
||||||
|
|
||||||
redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions are met:
|
|
||||||
|
|
||||||
redistributions of source code must retain the above copyright notice, this
|
|
||||||
list of conditions and the following disclaimer. redistributions in binary
|
|
||||||
form must reproduce the above copyright notice, this list of conditions and the
|
|
||||||
following disclaimer in the documentation and/or other materials provided with
|
|
||||||
the distribution.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
||||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
||||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
||||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
||||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
||||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
||||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
||||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*)
|
|
||||||
|
|
||||||
|
(* This file is free software, part of containers. See file "license" for more details. *)
|
||||||
|
|
||||||
(** {1 Utils around Mutex} *)
|
(** {1 Utils around Mutex} *)
|
||||||
|
|
||||||
|
|
@ -162,7 +139,38 @@ let get_then_incr l =
|
||||||
Mutex.unlock l.mutex;
|
Mutex.unlock l.mutex;
|
||||||
x
|
x
|
||||||
|
|
||||||
|
let decr_then_get l =
|
||||||
|
Mutex.lock l.mutex;
|
||||||
|
l.content <- l.content - 1;
|
||||||
|
let x = l.content in
|
||||||
|
Mutex.unlock l.mutex;
|
||||||
|
x
|
||||||
|
|
||||||
|
let get_then_decr l =
|
||||||
|
Mutex.lock l.mutex;
|
||||||
|
let x = l.content in
|
||||||
|
l.content <- l.content - 1;
|
||||||
|
Mutex.unlock l.mutex;
|
||||||
|
x
|
||||||
|
|
||||||
(*$T
|
(*$T
|
||||||
let l = create 0 in 1 = incr_then_get l && 1 = get l
|
let l = create 0 in 1 = incr_then_get l && 1 = get l
|
||||||
let l = create 0 in 0 = get_then_incr l && 1 = get l
|
let l = create 0 in 0 = get_then_incr l && 1 = get l
|
||||||
|
let l = create 10 in 9 = decr_then_get l && 9 = get l
|
||||||
|
let l = create 10 in 10 = get_then_decr l && 9 = get l
|
||||||
*)
|
*)
|
||||||
|
|
||||||
|
let get_then_set l =
|
||||||
|
Mutex.lock l.mutex;
|
||||||
|
let x = l.content in
|
||||||
|
l.content <- true;
|
||||||
|
Mutex.unlock l.mutex;
|
||||||
|
x
|
||||||
|
|
||||||
|
let get_then_clear l =
|
||||||
|
Mutex.lock l.mutex;
|
||||||
|
let x = l.content in
|
||||||
|
l.content <- false;
|
||||||
|
Mutex.unlock l.mutex;
|
||||||
|
x
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,32 +1,11 @@
|
||||||
(*
|
|
||||||
copyright (c) 2013-2014, simon cruanes
|
|
||||||
all rights reserved.
|
|
||||||
|
|
||||||
redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions are met:
|
|
||||||
|
|
||||||
redistributions of source code must retain the above copyright notice, this
|
|
||||||
list of conditions and the following disclaimer. redistributions in binary
|
|
||||||
form must reproduce the above copyright notice, this list of conditions and the
|
|
||||||
following disclaimer in the documentation and/or other materials provided with
|
|
||||||
the distribution.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
||||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
||||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
||||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
||||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
||||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
||||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
||||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
*)
|
|
||||||
|
|
||||||
|
(* This file is free software, part of containers. See file "license" for more details. *)
|
||||||
|
|
||||||
(** {1 Utils around Mutex}
|
(** {1 Utils around Mutex}
|
||||||
|
|
||||||
@since 0.8 *)
|
A value wrapped into a Mutex, for more safety.
|
||||||
|
|
||||||
|
@since 0.8 *)
|
||||||
|
|
||||||
type 'a t
|
type 'a t
|
||||||
(** A value surrounded with a lock *)
|
(** A value surrounded with a lock *)
|
||||||
|
|
@ -88,6 +67,21 @@ val incr_then_get : int t -> int
|
||||||
@since NEXT_RELEASE *)
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
val get_then_incr : int t -> int
|
val get_then_incr : int t -> int
|
||||||
(** [incr_then_get x] increments [x], and return its previous value
|
(** [get_then_incr x] increments [x], and return its previous value
|
||||||
@since NEXT_RELEASE *)
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
|
val decr_then_get : int t -> int
|
||||||
|
(** [decr_then_get x] decrements [x], and return its new value
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
|
val get_then_decr : int t -> int
|
||||||
|
(** [get_then_decr x] decrements [x], and return its previous value
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
|
val get_then_set : bool t -> bool
|
||||||
|
(** [get_then_set b] sets [b] to [true], and return the old value
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
|
val get_then_clear : bool t -> bool
|
||||||
|
(** [get_then_clear b] sets [b] to [false], and return the old value
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue