File tree Expand file tree Collapse file tree 4 files changed +21
-18
lines changed Expand file tree Collapse file tree 4 files changed +21
-18
lines changed Original file line number Diff line number Diff line change @@ -273,8 +273,16 @@ let[@alert "-handler"] rec insert_fd s fds (Return_on r as op) =
273
273
wakeup s `Alive
274
274
else insert_fd s fds op
275
275
276
- let return_on computation file_descr event value =
276
+ let return_on computation file_descr op value =
277
277
let s = get () in
278
278
insert_fd s
279
- (match event with `R -> s.new_rd | `W -> s.new_wr | `E -> s.new_ex)
279
+ (match op with `R -> s.new_rd | `W -> s.new_wr | `E -> s.new_ex)
280
280
(Return_on { computation; file_descr; value })
281
+
282
+ let await_on file_descr op =
283
+ let computation = Computation. create () in
284
+ return_on computation file_descr op file_descr;
285
+ try Computation. await computation
286
+ with exn ->
287
+ Computation. cancel computation exit_exn_bt;
288
+ raise exn
Original file line number Diff line number Diff line change 7
7
8
8
open Picos
9
9
10
+ (* * {1 Timeouts} *)
11
+
10
12
val cancel_after : _ Computation .t -> seconds :float -> Exn_bt .t -> unit
11
13
(* * [cancel_after computation ~seconds exn_bt] arranges for [computation] to be
12
14
{{!Picos.Computation.cancel} canceled} with given [exn_bt] after given time
@@ -16,6 +18,8 @@ val cancel_after : _ Computation.t -> seconds:float -> Exn_bt.t -> unit
16
18
ℹ️ You can use [cancel_after] to implement the handler for the
17
19
{{!Picos.Computation.Cancel_after} [Cancel_after]} effect. *)
18
20
21
+ (* * {1 IO} *)
22
+
19
23
val return_on :
20
24
'a Computation .t -> Unix .file_descr -> [ `R | `W | `E ] -> 'a -> unit
21
25
(* * [return_on computation file_descr op value] arranges for [computation] to be
@@ -25,3 +29,7 @@ val return_on :
25
29
26
30
ℹ️ Using [Unix.set_nonblock] and [return_on] you can implement direct-style
27
31
transparently asynchronous IO on top of the [Unix] module. *)
32
+
33
+ val await_on : Unix .file_descr -> [ `R | `W | `E ] -> Unix .file_descr
34
+ (* * [await_on file_descr op] awaits until [file_descr] becomes available for
35
+ [op]. *)
Original file line number Diff line number Diff line change 1
- open Picos
2
1
include Unix
3
2
4
- let await file_descr op =
5
- let c = Computation. create () in
6
- Picos_select. return_on c file_descr op file_descr;
7
- try Computation. await c
8
- with exn ->
9
- Computation. cancel c (Exn_bt. get_callstack 0 exn );
10
- raise exn
11
-
12
3
let read file_descr bytes pos len =
13
4
try Unix. read file_descr bytes pos len
14
5
with Unix. Unix_error ((EAGAIN | EWOULDBLOCK ), _ , _ ) ->
15
- Unix. read (await file_descr `R ) bytes pos len
6
+ Unix. read (Picos_select. await_on file_descr `R ) bytes pos len
16
7
17
8
let write file_descr bytes pos len =
18
9
try Unix. write file_descr bytes pos len
19
10
with Unix. Unix_error ((EAGAIN | EWOULDBLOCK ), _ , _ ) ->
20
- Unix. write (await file_descr `W ) bytes pos len
11
+ Unix. write (Picos_select. await_on file_descr `W ) bytes pos len
21
12
22
13
let accept ?cloexec file_descr =
23
14
try Unix. accept ?cloexec file_descr
24
15
with Unix. Unix_error ((EAGAIN | EWOULDBLOCK ), _ , _ ) ->
25
- Unix. accept ?cloexec (await file_descr `R )
16
+ Unix. accept ?cloexec (Picos_select. await_on file_descr `R )
Original file line number Diff line number Diff line change 1
1
(* * A transparently asynchronous [Unix] module for {!Picos}. *)
2
2
3
3
include module type of Unix
4
-
5
- val await : file_descr -> [ `R | `W | `E ] -> file_descr
6
- (* * [await file_descr op] awaits for [file_descr] to be ready for [op] and
7
- returns [file_descr]. *)
You can’t perform that action at this time.
0 commit comments