File tree 4 files changed +41
-1
lines changed
4 files changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -41,7 +41,7 @@ let rec notify () =
41
41
end
42
42
else notify ()
43
43
44
- module System = struct
44
+ module System : Picos_lwt . System = struct
45
45
let sleep = Lwt_unix. sleep
46
46
47
47
type trigger = unit Lwt .t * unit Lwt .u
Original file line number Diff line number Diff line change 3
3
4
4
open Picos
5
5
6
+ val system : (module Picos_lwt .System )
7
+ (* * The system module for Unix. *)
8
+
6
9
val run_fiber : Fiber .t -> (Fiber .t -> unit ) -> unit Lwt .t
7
10
(* * [run_fiber fiber main] runs the [main] program as the specified [fiber] as a
8
11
promise with {!Lwt} as the scheduler using a {!Lwt_unix} based
Original file line number Diff line number Diff line change @@ -35,6 +35,37 @@ let await promise =
35
35
| Return value -> value
36
36
| Fail exn -> raise exn
37
37
38
+ let bind_on (module System : System ) thunk =
39
+ let trigger = System. trigger () in
40
+ let promise = Lwt. bind (System. await trigger) thunk in
41
+ System. signal trigger;
42
+ promise
43
+
44
+ let await_on (module System : System ) promise =
45
+ let computation = Computation. create ~mode: `LIFO () in
46
+ let trigger = System. trigger () in
47
+ let promise =
48
+ Lwt. bind (System. await trigger) @@ fun () ->
49
+ Lwt. try_bind
50
+ (fun () -> promise)
51
+ (fun value ->
52
+ Computation. return computation value;
53
+ Lwt. return_unit)
54
+ (fun exn ->
55
+ Computation. cancel computation exn empty_bt;
56
+ Lwt. return_unit)
57
+ in
58
+ System. signal trigger;
59
+ let trigger = Trigger. create () in
60
+ if Computation. try_attach computation trigger then begin
61
+ match Trigger. await trigger with
62
+ | None -> Computation. peek_exn computation
63
+ | Some (exn , bt ) ->
64
+ Lwt. cancel promise;
65
+ Printexc. raise_with_backtrace exn bt
66
+ end
67
+ else Computation. peek_exn computation
68
+
38
69
let [@ alert " -handler" ] rec go : type a r.
39
70
Fiber. t ->
40
71
(module System ) ->
Original file line number Diff line number Diff line change @@ -17,6 +17,12 @@ val await : 'a Lwt.t -> 'a
17
17
18
18
include module type of Intf
19
19
20
+ val bind_on : (module System ) -> (unit -> 'a Lwt .t ) -> 'a Lwt .t
21
+ (* * *)
22
+
23
+ val await_on : (module System ) -> 'a Lwt .t -> 'a
24
+ (* * *)
25
+
20
26
val run_fiber : (module System ) -> Fiber .t -> (Fiber .t -> unit ) -> unit Lwt .t
21
27
(* * [run_fiber (module System) fiber main] runs the [main] program as the
22
28
specified [fiber] as a promise with {!Lwt} as the scheduler using the given
You can’t perform that action at this time.
0 commit comments