Skip to content

Commit

Permalink
Construct domain executor only on main domain
Browse files Browse the repository at this point in the history
  • Loading branch information
Lupus committed Dec 12, 2024
1 parent adb6421 commit f6532ca
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/Domain_compat.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val is_main_domain : unit -> bool
1 change: 1 addition & 0 deletions lib/Domain_compat_v4.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let is_main_domain () = true
1 change: 1 addition & 0 deletions lib/Domain_compat_v5.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let is_main_domain = Domain.is_main_domain
8 changes: 7 additions & 1 deletion lib/Rust_async.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ module Runtime = struct
;;

let current () =
if not (Domain_compat.is_main_domain ())
then
failwith
"Initializing Rust_async executor from non-main domain is not going to work well";
match !current with
| Some executor -> executor
| None ->
Expand Down Expand Up @@ -48,5 +52,7 @@ let () =
fut
(fun value -> Stubs.Future.resolve wrapper value)
(fun exn -> Stubs.Future.reject wrapper (Printexc.to_string exn));
wrapper)
wrapper);
(* Ensure we initialize the executor during module init *)
Runtime.current () |> ignore
;;
16 changes: 16 additions & 0 deletions lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
(name rust_async)
(public_name rust-async)
(libraries lwt.unix ocaml-rs-smartptr rust-staticlib-virtual)
(modules
:standard
\
("Domain_compat_v4" "Domain_compat_v5"))
(flags (-linkall))
(preprocess
(pps lwt_ppx)))
Expand All @@ -10,6 +14,18 @@
(action
(copy %{workspace_root}/stubs-gen/Ocaml_lwt_interop.ml Stubs.ml.new)))

(rule
(action
(copy Domain_compat_v4.ml Domain_compat.ml))
(enabled_if
(< %{ocaml_version} 5.00)))

(rule
(action
(copy Domain_compat_v5.ml Domain_compat.ml))
(enabled_if
(>= %{ocaml_version} 5.00)))

(rule
(alias runtest)
(action
Expand Down

0 comments on commit f6532ca

Please sign in to comment.