-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial Eio_posix backend #448
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4926768
Initial Eio_posix backend
talex5 e19bc4a
eio_posix: do not include sys/random.h on non-Linux systems
avsm 3b0ae13
Ensure errno isn't changed by caml_stat_free
talex5 e2eeba0
Avoid memory leak if caml_stat_strdup raises
talex5 a696a23
Only depend on eio_luv on Windows
talex5 4b58f8b
Fix comment about read_dir
talex5 3f3aab4
Improve error handling creating sockets and pipes
talex5 eab5587
Clean up C stubs
haesbaert 4cc9e13
Clarify purpose of epoll FD test
talex5 6939cd1
It's ok to include stdlib unconditionally
haesbaert 2c90835
Fix missing ; in fs.md
haesbaert 7576297
Lower poll_maxi when we can
haesbaert 8164a13
Ensure standard streams are unique
polytypic 8118c1f
Remove EINPROGRESS from do_nonblocking
haesbaert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# This file is generated by dune, edit dune-project instead | ||
opam-version: "2.0" | ||
synopsis: "Eio implementation for POSIX systems" | ||
description: "An Eio implementation for most Unix-like platforms" | ||
maintainer: ["anil@recoil.org"] | ||
authors: ["Anil Madhavapeddy" "Thomas Leonard"] | ||
license: "ISC" | ||
homepage: "https://github.com/ocaml-multicore/eio" | ||
doc: "https://ocaml-multicore.github.io/eio/" | ||
bug-reports: "https://github.com/ocaml-multicore/eio/issues" | ||
depends: [ | ||
"dune" {>= "3.0"} | ||
"eio" {= version} | ||
"iomux" {>= "0.2"} | ||
"mdx" {>= "1.10.0" & with-test} | ||
"fmt" {>= "0.8.9"} | ||
"odoc" {with-doc} | ||
] | ||
build: [ | ||
["dune" "subst"] {dev} | ||
[ | ||
"dune" | ||
"build" | ||
"-p" | ||
name | ||
"-j" | ||
jobs | ||
"@install" | ||
"@runtest" {with-test} | ||
"@doc" {with-doc} | ||
] | ||
] | ||
dev-repo: "git+https://github.com/ocaml-multicore/eio.git" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
(* | ||
* Copyright (C) 2023 Thomas Leonard | ||
* | ||
* Permission to use, copy, modify, and distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*) | ||
|
||
open Eio.Std | ||
|
||
[@@@alert "-unstable"] | ||
|
||
(* Run an event loop in the current domain, using [fn x] as the root fiber. *) | ||
let run_event_loop fn x = | ||
Sched.with_sched @@ fun sched -> | ||
let open Effect.Deep in | ||
let extra_effects : _ effect_handler = { | ||
effc = fun (type a) (e : a Effect.t) : ((a, Sched.exit) continuation -> Sched.exit) option -> | ||
match e with | ||
| Eio_unix.Private.Get_monotonic_clock -> Some (fun k -> continue k (Time.mono_clock : Eio.Time.Mono.t)) | ||
| Eio_unix.Private.Socket_of_fd (sw, close_unix, unix_fd) -> Some (fun k -> | ||
let fd = Fd.of_unix ~sw ~blocking:false ~close_unix unix_fd in | ||
Unix.set_nonblock unix_fd; | ||
continue k (Flow.of_fd fd :> Eio_unix.socket) | ||
) | ||
| Eio_unix.Private.Socketpair (sw, domain, ty, protocol) -> Some (fun k -> | ||
match | ||
let unix_a, unix_b = Unix.socketpair ~cloexec:true domain ty protocol in | ||
let a = Fd.of_unix ~sw ~blocking:false ~close_unix:true unix_a in | ||
let b = Fd.of_unix ~sw ~blocking:false ~close_unix:true unix_b in | ||
Unix.set_nonblock unix_a; | ||
Unix.set_nonblock unix_b; | ||
(Flow.of_fd a :> Eio_unix.socket), (Flow.of_fd b :> Eio_unix.socket) | ||
with | ||
| r -> continue k r | ||
| exception Unix.Unix_error (code, name, arg) -> | ||
discontinue k (Err.wrap code name arg) | ||
) | ||
| Eio_unix.Private.Pipe sw -> Some (fun k -> | ||
match | ||
let unix_r, unix_w = Unix.pipe ~cloexec:true () in | ||
let r = Fd.of_unix ~sw ~blocking:false ~close_unix:true unix_r in | ||
let w = Fd.of_unix ~sw ~blocking:false ~close_unix:true unix_w in | ||
Unix.set_nonblock unix_r; | ||
Unix.set_nonblock unix_w; | ||
let source = (Flow.of_fd r :> <Eio.Flow.source; Eio.Flow.close; Eio_unix.unix_fd>) in | ||
let sink = (Flow.of_fd w :> <Eio.Flow.sink; Eio.Flow.close; Eio_unix.unix_fd>) in | ||
(source, sink) | ||
with | ||
| r -> continue k r | ||
| exception Unix.Unix_error (code, name, arg) -> | ||
discontinue k (Err.wrap code name arg) | ||
) | ||
| _ -> None | ||
} | ||
in | ||
Sched.run ~extra_effects sched fn x | ||
|
||
let v = object | ||
inherit Eio.Domain_manager.t | ||
|
||
method run_raw fn = | ||
let domain = ref None in | ||
Eio.Private.Suspend.enter (fun _ctx enqueue -> | ||
domain := Some (Domain.spawn (fun () -> Fun.protect fn ~finally:(fun () -> enqueue (Ok ())))) | ||
); | ||
Domain.join (Option.get !domain) | ||
|
||
method run fn = | ||
let domain = ref None in | ||
Eio.Private.Suspend.enter (fun ctx enqueue -> | ||
let cancelled, set_cancelled = Promise.create () in | ||
Eio.Private.Fiber_context.set_cancel_fn ctx (Promise.resolve set_cancelled); | ||
domain := Some (Domain.spawn (fun () -> | ||
Fun.protect (run_event_loop (fun () -> fn ~cancelled)) | ||
~finally:(fun () -> enqueue (Ok ())))) | ||
); | ||
Domain.join (Option.get !domain) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
(library | ||
(name eio_posix) | ||
(public_name eio_posix) | ||
(enabled_if (= %{os_type} "Unix")) | ||
(foreign_stubs | ||
(language c) | ||
(flags :standard -D_LARGEFILE64_SOURCE) | ||
(names eio_posix_stubs)) | ||
(libraries eio eio.utils eio.unix fmt iomux)) | ||
|
||
(rule | ||
(targets config.ml) | ||
(action (run ./include/discover.exe))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
(* | ||
* Copyright (C) 2023 Thomas Leonard | ||
* | ||
* Permission to use, copy, modify, and distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*) | ||
|
||
module Low_level = Low_level | ||
|
||
type stdenv = < | ||
stdin : <Eio.Flow.source; Eio_unix.unix_fd>; | ||
stdout : <Eio.Flow.sink; Eio_unix.unix_fd>; | ||
stderr : <Eio.Flow.sink; Eio_unix.unix_fd>; | ||
net : Eio.Net.t; | ||
domain_mgr : Eio.Domain_manager.t; | ||
clock : Eio.Time.clock; | ||
mono_clock : Eio.Time.Mono.t; | ||
fs : Eio.Fs.dir Eio.Path.t; | ||
cwd : Eio.Fs.dir Eio.Path.t; | ||
secure_random : Eio.Flow.source; | ||
debug : Eio.Debug.t; | ||
> | ||
|
||
let run main = | ||
(* SIGPIPE makes no sense in a modern application. *) | ||
Sys.(set_signal sigpipe Signal_ignore); | ||
let stdin = (Flow.of_fd Low_level.Fd.stdin :> <Eio.Flow.source; Eio_unix.unix_fd>) in | ||
let stdout = (Flow.of_fd Low_level.Fd.stdout :> <Eio.Flow.sink; Eio_unix.unix_fd>) in | ||
let stderr = (Flow.of_fd Low_level.Fd.stderr :> <Eio.Flow.sink; Eio_unix.unix_fd>) in | ||
Domain_mgr.run_event_loop main @@ object (_ : stdenv) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice that the main domain goes through the same path as the manager ! |
||
method stdin = stdin | ||
method stdout = stdout | ||
method stderr = stderr | ||
method debug = Eio.Private.Debug.v | ||
method clock = Time.clock | ||
method mono_clock = Time.mono_clock | ||
method net = Net.v | ||
method domain_mgr = Domain_mgr.v | ||
method cwd = ((Fs.cwd, "") :> Eio.Fs.dir Eio.Path.t) | ||
method fs = ((Fs.fs, "") :> Eio.Fs.dir Eio.Path.t) | ||
method secure_random = Flow.secure_random | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
(** Fallback Eio backend for POSIX systems. *) | ||
|
||
type stdenv = < | ||
stdin : <Eio.Flow.source; Eio_unix.unix_fd>; | ||
stdout : <Eio.Flow.sink; Eio_unix.unix_fd>; | ||
stderr : <Eio.Flow.sink; Eio_unix.unix_fd>; | ||
net : Eio.Net.t; | ||
domain_mgr : Eio.Domain_manager.t; | ||
clock : Eio.Time.clock; | ||
mono_clock : Eio.Time.Mono.t; | ||
fs : Eio.Fs.dir Eio.Path.t; | ||
cwd : Eio.Fs.dir Eio.Path.t; | ||
secure_random : Eio.Flow.source; | ||
debug : Eio.Debug.t; | ||
> | ||
(** An extended version of {!Eio.Stdenv.t} with some extra features available on POSIX systems. *) | ||
|
||
val run : (stdenv -> 'a) -> 'a | ||
(** [run main] runs an event loop and calls [main stdenv] inside it. | ||
|
||
For portable code, you should use {!Eio_main.run} instead, which will call this for you if appropriate. *) | ||
|
||
module Low_level = Low_level | ||
(** Low-level API for making POSIX calls directly. *) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there no
os_type
here like dune ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They're the ones from
opam var
, so it doesn't look like it.