diff --git a/lib/Domain_compat.mli b/lib/Domain_compat.mli new file mode 100644 index 0000000..46da0c0 --- /dev/null +++ b/lib/Domain_compat.mli @@ -0,0 +1 @@ +val is_main_domain : unit -> bool diff --git a/lib/Domain_compat_v4.ml b/lib/Domain_compat_v4.ml new file mode 100644 index 0000000..d84ea61 --- /dev/null +++ b/lib/Domain_compat_v4.ml @@ -0,0 +1 @@ +let is_main_domain () = true diff --git a/lib/Domain_compat_v5.ml b/lib/Domain_compat_v5.ml new file mode 100644 index 0000000..e9ca4d5 --- /dev/null +++ b/lib/Domain_compat_v5.ml @@ -0,0 +1 @@ +let is_main_domain = Domain.is_main_domain diff --git a/lib/Rust_async.ml b/lib/Rust_async.ml index d3eff11..ac67208 100644 --- a/lib/Rust_async.ml +++ b/lib/Rust_async.ml @@ -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 -> @@ -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 ;; diff --git a/lib/dune b/lib/dune index 3c52ba6..0475f57 100644 --- a/lib/dune +++ b/lib/dune @@ -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))) @@ -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