-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #497 from patricoferris/posix-windows
Posix-based windows implementation
- Loading branch information
Showing
28 changed files
with
1,542 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
(mdx | ||
(package eio_main) | ||
(deps (package eio_main) (env_var "EIO_BACKEND")) | ||
(enabled_if (<> %{os_type} "Win32")) | ||
(files multicore.md)) |
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,7 @@ | ||
pin-depends: [ | ||
# Removes base bytes for crowbar | ||
[ "ocplib-endian.dev" "git+https://github.com/Leonidas-from-XIV/ocplib-endian#fda4d5525063c8444020be369c63de23d39c246e" ] | ||
# Needed for the cstruct read and writes without copying | ||
[ "cstruct.dev" "git+https://github.com/djs55/ocaml-cstruct#471ca03b49b3a372945fcf13c89e0447a8bd3932" ] | ||
[ "cstruct-unix.dev" "git+https://github.com/djs55/ocaml-cstruct#471ca03b49b3a372945fcf13c89e0447a8bd3932" ] | ||
] |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
(mdx | ||
(package eio) | ||
(enabled_if (<> %{os_type} "Win32")) | ||
(deps | ||
(package eio) | ||
(file ./dscheck/fake_sched.ml) | ||
|
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,86 @@ | ||
(* | ||
* 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"] | ||
|
||
module Fd = Eio_unix.Fd | ||
|
||
(* 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 | ||
(* TODO: On Windows, if the FD from Unix.pipe () is passed this will fail *) | ||
(try Unix.set_nonblock unix_fd with Unix.Unix_error (Unix.ENOTSOCK, _, _) -> ()); | ||
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 r, w = Low_level.pipe ~sw in | ||
let source = (Flow.of_fd r :> Eio_unix.source) in | ||
let sink = (Flow.of_fd w :> Eio_unix.sink) 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 |
---|---|---|
@@ -1,5 +1,14 @@ | ||
(library | ||
(name eio_windows) | ||
(public_name eio_windows) | ||
(library_flags :standard -ccopt -lbcrypt) | ||
(enabled_if (= %{os_type} "Win32")) | ||
(libraries eio eio.utils fmt)) | ||
(foreign_stubs | ||
(language c) | ||
(include_dirs ../lib_eio/unix/include) | ||
(names eio_windows_stubs)) | ||
(libraries eio eio.unix eio.utils fmt cstruct-unix)) | ||
|
||
(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 |
---|---|---|
@@ -1,3 +1,49 @@ | ||
(* Can base this on the eio_posix directory structure. | ||
See HACKING.md for instructions on creating a new backend. *) | ||
let run _main = failwith "TODO: Windows support." | ||
(* | ||
* 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.Resource.t>; | ||
stdout : <Eio.Flow.sink; Eio_unix.Resource.t>; | ||
stderr : <Eio.Flow.sink; Eio_unix.Resource.t>; | ||
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 = | ||
let stdin = (Flow.of_fd Eio_unix.Fd.stdin :> <Eio.Flow.source; Eio_unix.Resource.t>) in | ||
let stdout = (Flow.of_fd Eio_unix.Fd.stdout :> <Eio.Flow.sink; Eio_unix.Resource.t>) in | ||
let stderr = (Flow.of_fd Eio_unix.Fd.stderr :> <Eio.Flow.sink; Eio_unix.Resource.t>) in | ||
Domain_mgr.run_event_loop main @@ object (_ : stdenv) | ||
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 = failwith "file-system operations not supported on Windows yet" | ||
method fs = failwith "file-system operations not supported on Windows yet" | ||
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 Windows using OCaml's [Unix.select]. *) | ||
|
||
type stdenv = < | ||
stdin : <Eio.Flow.source; Eio_unix.Resource.t>; | ||
stdout : <Eio.Flow.sink; Eio_unix.Resource.t>; | ||
stderr : <Eio.Flow.sink; Eio_unix.Resource.t>; | ||
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 Windows. *) | ||
|
||
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. *) |
Oops, something went wrong.