|
| 1 | +module Domain = struct |
| 2 | + |
| 3 | + (**************************************************************************) |
| 4 | + (* *) |
| 5 | + (* OCaml *) |
| 6 | + (* *) |
| 7 | + (* KC Sivaramakrishnan, Indian Institute of Technology, Madras *) |
| 8 | + (* Stephen Dolan, University of Cambridge *) |
| 9 | + (* Tom Kelly, OCaml Labs Consultancy *) |
| 10 | + (* *) |
| 11 | + (* Copyright 2019 Indian Institute of Technology, Madras *) |
| 12 | + (* Copyright 2014 University of Cambridge *) |
| 13 | + (* Copyright 2021 OCaml Labs Consultancy Ltd *) |
| 14 | + (* *) |
| 15 | + (* All rights reserved. The type DLST is distributed under the terms of *) |
| 16 | + (* the GNU Lesser General Public License version 2.1, with the *) |
| 17 | + (* special exception on linking described in the LICENSE file *) |
| 18 | + (* of the OCaml distribution: *) |
| 19 | + (* https://github.com/ocaml/ocaml/blob/4.14.0/LICENSE *) |
| 20 | + (* *) |
| 21 | + (**************************************************************************) |
| 22 | + |
| 23 | + module type DLSS = sig |
| 24 | + (** Domain-local Storage *) |
| 25 | + |
| 26 | + type 'a key |
| 27 | + (** Type of a DLS key *) |
| 28 | + |
| 29 | + val new_key : ?split_from_parent:('a -> 'a) -> (unit -> 'a) -> 'a key |
| 30 | + (** [new_key f] returns a new key bound to initialiser [f] for accessing |
| 31 | + domain-local variables. |
| 32 | +
|
| 33 | + If [split_from_parent] is provided, spawning a domain will derive the |
| 34 | + child value (for this key) from the parent value. |
| 35 | +
|
| 36 | + Note that the [split_from_parent] call is computed in the parent |
| 37 | + domain, and is always computed regardless of whether the child domain |
| 38 | + will use it. If the splitting function is expensive or requires |
| 39 | + client-side computation, consider using ['a Lazy.t key]. |
| 40 | + *) |
| 41 | + |
| 42 | + val get : 'a key -> 'a |
| 43 | + (** [get k] returns [v] if a value [v] is associated to the key [k] on |
| 44 | + the calling domain's domain-local state. Sets [k]'s value with its |
| 45 | + initialiser and returns it otherwise. *) |
| 46 | + |
| 47 | + val set : 'a key -> 'a -> unit |
| 48 | + (** [set k v] updates the calling domain's domain-local state to associate |
| 49 | + the key [k] with value [v]. It overwrites any previous values associated |
| 50 | + to [k], which cannot be restored later. *) |
| 51 | + end |
| 52 | + |
| 53 | + module DLS : DLSS = struct |
| 54 | + type 'a key = { |
| 55 | + value: 'a option ref; |
| 56 | + initialiser: unit -> 'a; |
| 57 | + } |
| 58 | + |
| 59 | + let new_key ?split_from_parent initialiser = |
| 60 | + { value = None; initialiser } |
| 61 | + |
| 62 | + let get k = |
| 63 | + match k.value with |
| 64 | + | Some v -> v |
| 65 | + | None -> |
| 66 | + let v = k.initialiser () in |
| 67 | + k.value <- Some v; |
| 68 | + v |
| 69 | + |
| 70 | + let set k v = |
| 71 | + k.value <- Some v |
| 72 | + end |
| 73 | +end |
0 commit comments