Skip to content

Commit

Permalink
Merge pull request #447 from talex5/eio-main
Browse files Browse the repository at this point in the history
eio_main: make EIO_BACKEND handling more uniform
  • Loading branch information
talex5 committed Feb 24, 2023
2 parents e858a8c + 27cf787 commit 5541790
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 51 deletions.
21 changes: 12 additions & 9 deletions lib_eio_linux/eio_linux.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1318,15 +1318,18 @@ let with_uring ~queue_depth ?polling_timeout ?(fallback=no_fallback) fn =
match Uring.create ~queue_depth ?polling_timeout () with
| exception Unix.Unix_error(Unix.ENOSYS, _, _) -> fallback (`Msg "io_uring is not available on this system")
| uring ->
match fn uring with
| x -> Uring.exit uring; x
| exception ex ->
let bt = Printexc.get_raw_backtrace () in
begin
try Uring.exit uring
with ex2 -> Log.warn (fun f -> f "Uring.exit failed (%a) while handling another error" Fmt.exn ex2)
end;
Printexc.raise_with_backtrace ex bt
let probe = Uring.get_probe uring in
if not (Uring.op_supported probe Uring.Op.shutdown) then
fallback (`Msg "Linux >= 5.11 is required for io_uring support")
else match fn uring with
| x -> Uring.exit uring; x
| exception ex ->
let bt = Printexc.get_raw_backtrace () in
begin
try Uring.exit uring
with ex2 -> Log.warn (fun f -> f "Uring.exit failed (%a) while handling another error" Fmt.exn ex2)
end;
Printexc.raise_with_backtrace ex bt

let rec run : type a.
?queue_depth:int -> ?n_blocks:int -> ?block_size:int -> ?polling_timeout:int -> ?fallback:(_ -> a) -> (_ -> a) -> a =
Expand Down
12 changes: 8 additions & 4 deletions lib_main/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
(library
(name eio_main)
(public_name eio_main)
(libraries eio_luv
(select eio_main.ml from
(eio_linux -> eio_main.linux.ml)
( -> eio_main.default.ml))))
(libraries
(select linux_backend.ml from
(eio_linux -> linux_backend.enabled.ml)
( -> linux_backend.disabled.ml))
(select luv_backend.ml from
(eio_luv -> luv_backend.enabled.ml)
( -> luv_backend.disabled.ml))
))
1 change: 0 additions & 1 deletion lib_main/eio_main.default.ml

This file was deleted.

36 changes: 0 additions & 36 deletions lib_main/eio_main.linux.ml

This file was deleted.

12 changes: 12 additions & 0 deletions lib_main/eio_main.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let force run fn =
run ~fallback:(fun (`Msg msg) -> failwith msg) fn

let run fn =
match Sys.getenv_opt "EIO_BACKEND" with
| Some ("io-uring" | "linux") -> force Linux_backend.run fn
| Some "luv" -> force Luv_backend.run fn
| None | Some "" ->
Linux_backend.run fn ~fallback:(fun _ ->
force Luv_backend.run fn
)
| Some x -> Fmt.failwith "Unknown Eio backend %S (from $EIO_BACKEND)" x
2 changes: 1 addition & 1 deletion lib_main/eio_main.mli
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ val run : (Eio.Stdenv.t -> 'a) -> 'a
On recent-enough versions of Linux, it will use {!Eio_linux.run}.
You can override this by setting the $EIO_BACKEND environment variable to
either "io-uring" or "luv". *)
either "linux" or "luv". *)
1 change: 1 addition & 0 deletions lib_main/linux_backend.disabled.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let run ~fallback _ = fallback (`Msg "The io_uring backend was disabled at compile-time")
1 change: 1 addition & 0 deletions lib_main/linux_backend.enabled.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let run ~fallback fn = Eio_linux.run ~fallback (fun env -> fn (env :> Eio.Stdenv.t))
1 change: 1 addition & 0 deletions lib_main/luv_backend.disabled.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let run ~fallback _ = fallback (`Msg "The Luv backend was disabled at compile-time")
1 change: 1 addition & 0 deletions lib_main/luv_backend.enabled.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let run ~fallback:_ fn = Eio_luv.run (fun env -> fn (env :> Eio.Stdenv.t))

0 comments on commit 5541790

Please sign in to comment.