jon.recoil.org

Source file extended.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
open! Import

module Access = struct
  type 'k t = 'k Prim.Access.t
  type packed = Prim.Access.packed = P : 'k t -> packed [@@unboxed]
  type 'k boxed = 'k Prim.Access.boxed

  let current = Prim.current
  let unbox = Prim.Access.unbox
  let box = Prim.Access.box
end

module Key = struct
  type 'k t = 'k Prim.Key.t

  let%template access = (Prim.Key.access [@mode l]) [@@mode l = (local, global)]
  let globalize_unique = Prim.Key.globalize_unique
  let destroy = Prim.Key.destroy
end

module Data = struct
  type ('a, 'k) t = ('a, 'k) Prim.Data.t

  let create = Prim.Data.create
  let wrap = Prim.Data.wrap
  let unwrap = Prim.Data.unwrap
  let%template[@mode global shared] unwrap = Prim.Data.unwrap_shared
  let return = Prim.Data.inject
  let get_id = Prim.Data.project
  let project_shared = Prim.Data.project_shared
  let both = Prim.Data.both
  let fst = Prim.Data.fst
  let snd = Prim.Data.snd
  let idx = Prim.Data.idx

  [%%template
  [@@@mode.default unique]

  let create = Prim.Data.create_unique
  let unwrap = Prim.Data.unwrap_unique]

  [%%template
  [@@@mode.default local]

  let create = Prim.Data.Local.create
  let wrap = Prim.Data.Local.wrap
  let unwrap = Prim.Data.Local.unwrap
  let[@mode local shared] unwrap = Prim.Data.Local.unwrap_shared
  let return = Prim.Data.Local.inject
  let get_id = Prim.Data.Local.project
  let project_shared = Prim.Data.Local.project_shared
  let both = Prim.Data.Local.both
  let fst = Prim.Data.Local.fst
  let snd = Prim.Data.Local.snd

  [@@@mode.default unique]

  let create = Prim.Data.Local.create_unique
  let unwrap = Prim.Data.Local.unwrap_unique]

  [%%template let idx = Prim.Data.Local.idx [@@mode local]]
end

module Initial = struct
  type k = Prim.initial

  let access = Prim.initial

  module Data = struct
    type 'a t = ('a, k) Data.t

    [%%template
    [@@@mode.default l = (global, local)]

    let wrap a = (Data.wrap [@mode l]) ~access a [@exclave_if_local l]
    let unwrap a = (Data.unwrap [@mode l]) ~access a [@exclave_if_local l]]

    let sexp_of_t sexp_of_a t = sexp_of_a (unwrap t)
    let t_of_sexp a_of_sexp a = wrap (a_of_sexp a)
  end
end

module%template Isolated = struct
  module Repr = struct
    type ('a, 'k) inner =
      #{ data : ('a, 'k) Data.t
       ; key : 'k Key.t
       }

    type 'a t = P : ('a, 'k) inner -> 'a t [@@unboxed]
  end

  (* We use this type declaration just as justification for the kind annotation on ['a t]. *)
  type ('a
       , 'k)
       _inner :
       (value & void)
       mod contended forkable many portable unyielding
       with 'a @@ contended portable =
    ('a, 'k) Repr.inner

  type 'a
       t :
       value mod contended forkable many portable unyielding with 'a @@ contended portable

  [@@@mode.default.explicit u = (unique, aliased)]

  external unsafe_to_data
    :  ('a t[@local_opt]) @ u
    -> (('a, 'k) Data.t[@local_opt]) @ u
    @@ portable
    = "%identity"

  external unsafe_of_data
    :  (('a, 'k) Data.t[@local_opt]) @ u
    -> ('a t[@local_opt]) @ u
    @@ portable
    = "%identity"

  [@@@mode.default.explicit l = (global, local)]

  let to_repr (t @ l) =
    (let (P key) = Prim.create () in
     let data = (unsafe_to_data [@mode.explicit u]) t in
     Repr.P #{ data; key })
    [@exclave_if_local l ~reasons:[ May_return_regional ]]
  ;;

  let of_repr (Repr.P #{ data; key = _ } @ l) =
    (unsafe_of_data [@mode.explicit u]) data [@exclave_if_local l]
  ;;
end

module%template Frozen = struct
  type 'a t = 'a portable Isolated.t

  [@@@mode.default l = (global, local)]

  let to_repr = (Isolated.to_repr [@mode.explicit aliased l])
  let of_repr = (Isolated.of_repr [@mode.explicit aliased l])

  let create f =
    (let (P key) = Prim.create () in
     let data =
       (Data.create [@mode l]) (fun () ->
         { portable = f () } [@exclave_if_local l ~reasons:[ May_return_local ]])
     in
     (of_repr [@mode l]) (P #{ key; data }))
    [@exclave_if_local l ~reasons:[ May_return_local ]]
  ;;

  let unwrap t =
    (let (P #{ key; data }) = (to_repr [@mode l]) t in
     ((Data.project_shared [@mode l]) ~key data).portable)
    [@exclave_if_local l ~reasons:[ May_return_regional ]]
  ;;
end

module%template Owned = struct
  type 'a t = 'a Isolated.t

  [@@@mode.default l = (global, local)]

  let to_repr = (Isolated.to_repr [@mode.explicit l unique])
  let of_repr = (Isolated.of_repr [@mode.explicit l unique])

  let create f =
    (let (P key) = Prim.create () in
     let data = (Data.create [@mode l unique]) f in
     (of_repr [@mode l]) (P #{ key; data }))
    [@exclave_if_local l ~reasons:[ May_return_local ]]
  ;;

  let freeze t =
    (let open struct
       (*_ Safe because ['a = 'a portable] for ('a : value mod portable) *)
       external wrap_portable
         : ('a : value mod portable) 'k.
         ('a, 'k) Prim.Data.t @ l -> ('a portable, 'k) Prim.Data.t @ l
         @@ portable
         = "%identity"
     end in
     let (P #{ data; key }) = (Isolated.to_repr [@mode.explicit l aliased]) t in
     let data = wrap_portable data in
     (Isolated.of_repr [@mode.explicit l aliased]) (P #{ data; key }))
    [@exclave_if_local l ~reasons:[ May_return_local ]]
  ;;

  let unwrap t =
    (let (P #{ key; data }) = (to_repr [@mode l]) t in
     let access = Key.destroy key in
     (Data.unwrap [@mode l unique]) ~access data)
    [@exclave_if_local l ~reasons:[ May_return_regional ]]
  ;;

  let dup (type a : value mod aliased) (t : a @ l unique) : #(a * a) @ l unique =
    #(t, t) [@exclave_if_local l ~reasons:[ May_return_regional ]]
  ;;

  let get_contended (type a : value mod aliased portable) (t : a t) : #(a t * a) @ unique =
    (let (P #{ data; key }) = (to_repr [@mode l]) t in
     let #(data, data') = (dup [@mode l]) data in
     let t = (of_repr [@mode l]) (P #{ data; key }) in
     #(t, (Data.get_id [@mode l]) data'))
    [@exclave_if_local l ~reasons:[ May_return_regional ]]
  ;;

  let with_ (type a : value mod aliased) (t : a t) ~f =
    (let (P #{ key; data }) = (to_repr [@mode l]) t in
     let key = Key.globalize_unique key in
     let #(data, data') = (dup [@mode l]) data in
     let #(result, key) =
       (Key.access [@mode l]) key ~f:(fun access ->
         { many = f ((Data.unwrap [@mode l]) ~access data') }
         [@exclave_if_local l ~reasons:[ May_return_local ]])
     in
     let t = (of_repr [@mode l]) (P #{ key; data }) in
     #(t, result.many))
    [@exclave_if_local l ~reasons:[ May_return_local ]]
  ;;
end

module Scoped = struct
  type ('a, 'k) inner =
    #{ data : ('a, 'k) Data.t @@ global
     ; password : 'k Prim.Password.t
     }

  type 'a t = P : ('a, 'k) inner -> 'a t [@@unboxed]

  let%template[@inline] with_ a ~f =
    let (P access) = Access.current () in
    let data = Data.wrap ~access a in
    ((Prim.Password.with_current [@mode local]) access (fun password -> exclave_
       { aliased_many = { global = f (P #{ data; password }) } }))
      .aliased_many
      .global
  ;;

  let[@inline] get (P #{ data; password }) ~f =
    (Prim.Data.extract data ~password ~f:(fun a -> { many = { aliased = f a } })).many
      .aliased
  ;;

  let iter = get

  let[@inline] map (P #{ data; password }) ~f = exclave_
    P #{ data = Prim.Data.map data ~password ~f; password }
  ;;

  module Shared = struct
    type ('a, 'k) inner =
      #{ data : ('a shared, 'k) Data.t @@ global
       ; password : 'k Prim.Password.Shared.t
       }

    type 'a t = P : ('a, 'k) inner -> 'a t [@@unboxed]

    module Uncontended = struct
      type ('a, 'k) t = ('a, 'k) inner =
        #{ data : ('a shared, 'k) Data.t @@ global
         ; password : 'k Prim.Password.Shared.t
         }

      type ('a, 'b) f =
        { f : 'k. ('a, 'k) inner @ forkable local -> ('b, 'k) Prim.Data.Shared.t }

      let[@inline] with_ data { f } =
        let (P access) = Access.current () in
        let data = Data.wrap ~access { shared = data } in
        let { many = { global = { aliased = data } } } =
          Prim.Password.with_current access (fun [@inline] password ->
            let password = Prim.Password.shared password in
            { many =
                Prim.Password.Shared.borrow password (fun [@inline] password ->
                  { global = { aliased = f #{ data; password } } })
            })
        in
        Prim.Data.Shared.unwrap ~access data
      ;;

      let[@inline] get #{ data; password } ~f =
        Prim.Data.Shared.map_into data ~password ~f:(fun [@inline] { shared } -> f shared)
        [@nontail]
      ;;

      let[@inline] map #{ data; password } ~f = exclave_
        #{ data =
             Prim.Data.map_shared data ~password ~f:(fun [@inline] { shared } ->
               { shared = f shared })
         ; password
         }
      ;;
    end

    let[@inline] with_ data ~f =
      let (P access) = Access.current () in
      let data = Data.wrap ~access { shared = data } in
      (Prim.Password.with_current access (fun [@inline] password ->
         let password = Prim.Password.shared password in
         { many =
             Prim.Password.Shared.borrow password (fun [@inline] password ->
               { global = { aliased = f (P #{ data; password }) } })
         }))
        .many
        .global
        .aliased
    ;;

    let[@inline] get (P t) ~f =
      (Prim.Data.Shared.project
         (Uncontended.get t ~f:(fun [@inline] a -> { portended = f a })))
        .portended
    ;;

    let iter = get
    let[@inline] map (P t) ~f = exclave_ P (Uncontended.map t ~f)
  end
end