Source file sync_or_async.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
open! Core
open! Import0
open! Async_kernel
type ('a, 'b) t =
| Sync : ('a, 'a) t
| Async : ('a, 'a Deferred.t) t
[@@deriving sexp_of]
let async_protect ~f ~finally =
Monitor.protect f ~finally:(fun () ->
finally ();
return ())
;;
let return (type a b) (t : (a, b) t) (a : a) : b =
match t with
| Sync -> a
| Async -> return a
;;
let protect
(type a b)
?(allow_in_background = false)
here
(t : (a, b) t)
~(f : unit -> b)
~finally
: b
=
match t with
| Sync -> Exn.protect ~f ~finally
| Async ->
async_protect
~f:(fun () ->
if not allow_in_background then Background.assert_foreground here;
f ())
~finally
;;