package rowex

  1. Overview
  2. Docs
A Read-Optimistic Write-EXclusive data-structure

Install

dune-project
 Dependency

Authors

Maintainers

Sources

bancos-0.0.1.tbz
sha256=f9603b60308f70937f49cc2a32657549bdcb2db197ed5409ce9bd688187d994c
sha512=6cbfffa0c08b9bee5d8729656fb9ad6ea70eaf056ec01923b99eb47e113ddb7b51fa5c2b66b149116fb511f2506cb369ee73c3ca52cec2c372b8f0a7158c894f

doc/src/rowex.atomic/atomic.ml.html

Source file atomic.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
(**************************************************************************)
(*                                                                        *)
(*                                 OCaml                                  *)
(*                                                                        *)
(*             Gabriel Scherer, projet Partout, INRIA Paris-Saclay        *)
(*                                                                        *)
(*   Copyright 2020 Institut National de Recherche en Informatique et     *)
(*     en Automatique.                                                    *)
(*                                                                        *)
(*   All rights reserved.  This file is distributed under the terms of    *)
(*   the GNU Lesser General Public License version 2.1, with the          *)
(*   special exception on linking described in the file LICENSE.          *)
(*                                                                        *)
(**************************************************************************)

external ( == ) : 'a -> 'a -> bool = "%eq"
external ( + ) : int -> int -> int = "%addint"
external ignore : 'a -> unit = "%ignore"

type 'a t = { mutable v : 'a }

let make v = { v }
let get r = r.v
let set r v = r.v <- v

let[@inline never] exchange r v =
  (* BEGIN ATOMIC *)
  let cur = r.v in
  r.v <- v;
  (* END ATOMIC *)
  cur

let[@inline never] compare_and_set r seen v =
  (* BEGIN ATOMIC *)
  let cur = r.v in
  if cur == seen then (
    r.v <- v;
    (* END ATOMIC *)
    true)
  else false

let[@inline never] fetch_and_add r n =
  (* BEGIN ATOMIC *)
  let cur = r.v in
  r.v <- cur + n;
  (* END ATOMIC *)
  cur

let incr r = ignore (fetch_and_add r 1)
let decr r = ignore (fetch_and_add r (-1))