Skip to content

Commit

Permalink
- fix blocking issue on Windows : issue ocaml-multicore#793.
Browse files Browse the repository at this point in the history
      Adding await_readable before reading fd

- fix broken pipe exception : issue ocaml-multicore#792.
      Use Unix.read_bigarray instead of Unix_cstruct.read

- replace eio_windows_cstruct_stubs.c by Unix functions.
      Unix.read_bigarray and Unix.write_bigarray implemented since Ocaml 5.2
  • Loading branch information
bdodrem committed Jan 25, 2025
1 parent c78db1a commit e509f09
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 154 deletions.
2 changes: 1 addition & 1 deletion lib_eio_windows/dune
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
(foreign_stubs
(language c)
(include_dirs ../lib_eio/unix/include)
(names eio_windows_stubs eio_windows_cstruct_stubs))
(names eio_windows_stubs ))
(c_library_flags :standard -lbcrypt -lntdll)
(libraries eio eio.unix eio.utils fmt))

Expand Down
149 changes: 0 additions & 149 deletions lib_eio_windows/eio_windows_cstruct_stubs.c

This file was deleted.

19 changes: 15 additions & 4 deletions lib_eio_windows/low_level.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,25 @@ let rec do_nonblocking ty fn fd =
do_nonblocking ty fn fd

let read fd buf start len =
await_readable fd;
Fd.use_exn "read" fd @@ fun fd ->
do_nonblocking Read (fun fd -> Unix.read fd buf start len) fd

let read_cstruct fd buf =
let read_cstruct fd (buf:Cstruct.t) =
await_readable fd;
Fd.use_exn "read_cstruct" fd @@ fun fd ->
do_nonblocking Read (fun fd -> Unix_cstruct.read fd buf) fd
do_nonblocking Read (fun fd -> Unix.read_bigarray fd buf.buffer buf.off buf.len) fd

let write fd buf start len =
await_writable fd;
Fd.use_exn "write" fd @@ fun fd ->
do_nonblocking Write (fun fd -> Unix.write fd buf start len) fd

let write_cstruct fd (buf:Cstruct.t) =
await_writable fd;
Fd.use_exn "write_cstruct" fd @@ fun fd ->
do_nonblocking Write (fun fd -> Unix.write_bigarray fd buf.buffer buf.off buf.len) fd

let sleep_until time =
Sched.enter @@ fun t k ->
Sched.await_timeout t k time
Expand Down Expand Up @@ -148,8 +156,11 @@ let readv fd bufs =
do_nonblocking Read (fun fd -> eio_readv fd bufs) fd

let writev fd bufs =
Fd.use_exn "writev" fd @@ fun fd ->
do_nonblocking Write (fun fd -> Unix_cstruct.writev fd bufs) fd
let rec loop buf = if Cstruct.length buf > 0 then begin
let n = write_cstruct fd buf in
loop @@ Cstruct.shift buf n
end in
List.iter loop bufs

let preadv ~file_offset fd bufs =
Fd.use_exn "preadv" fd @@ fun fd ->
Expand Down
1 change: 1 addition & 0 deletions lib_eio_windows/low_level.mli
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ val sleep_until : Mtime.t -> unit
val read : fd -> bytes -> int -> int -> int
val read_cstruct : fd -> Cstruct.t -> int
val write : fd -> bytes -> int -> int -> int
val write_cstruct : fd -> Cstruct.t -> int

val socket : sw:Switch.t -> Unix.socket_domain -> Unix.socket_type -> int -> fd
val connect : fd -> Unix.sockaddr -> unit
Expand Down

0 comments on commit e509f09

Please sign in to comment.