-
Notifications
You must be signed in to change notification settings - Fork 22
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
should the runtime be a thread-local variable? #42
Comments
@c-cube ocaml-rs uses Users of ocaml-interop should avoid using |
One more user of #[ocaml::func]
#[ocaml::sig("runtime -> (int -> promise) -> unit")]
pub fn runtime_test(rt: ocaml::Pointer<Runtime>, f: ocaml::Value) {
let f = ensure_rooted_value(f);
rt.as_ref().spawn(async move {
let mut page_nb = 0;
let gc = unsafe { ocaml::interop::OCamlRuntime::recover_handle() };
let f_callable = ocaml::function!(f, (n: ocaml::Int) -> CamlPtr<Promise>);
loop {
f_callable(&gc, &page_nb).unwrap().await.unwrap();
page_nb = page_nb + 1;
}
});
} I have single-threaded Rust executor, that notifies Lwt via Lwt_unix notification when there are pending tasks, and Lwt side then calls the executor to move its ready tasks forward. This even somewhat works. The contract that I have is that OCaml is keeping the executor reference alive and will properly stop it before terminating the OCaml runtime (in my case - exit the application as it's an OCaml application). This should be a safe thing to do, everything runs on the same thread as OCaml code, properly rooted OCaml values are safe to be referenced in Rust async taks running in that thread-local executor, and as I externally guarantee that OCaml part will outlive the executor, I should have the OCaml runtime legally available to my async Rust code. The only way to do that is probably using |
@Lupus these patterns cannot yet be safely expressed with A safe version of https://v2.ocaml.org/manual/intfc.html#s:C-multithreading
Yes, this is true in your case because all your Rust functions are "entered" through OCaml calls, and the Rust code never runs in parallel to the OCaml code, only concurrently, right? |
No worries, and thank your for your work on this project! It's more that sufficient for me to just know that the recover runtime handle part of the interface won't just disappear because of some new way of writing tests that allows to avoid unsafe static variable :)
Sounds good, I could probably obtain it and save it along with my thread-local executor, as long as running the functions that you mentioned one more time on OCaml's main thread is okay.
Yes, this is correct for my use case. I'm extending OCaml with Rust and try to go further than just synchronous primitives. |
using
static mut
is very dangerous [^1] and almost impossible to get right. Should it be instead a form of either once_cell/lazy_static, or, better, a thread-local variable withArc<Mutex<OCamlRuntime>>
?I'm asking because ocaml-rs apparently uses
runtime::recover_handle
to access the runtime from within function stubs, despite it being advertised as test-only. Which, accessorily: should it be#[cfg(test)]
, too?[^1] rust-lang/rust#53639
The text was updated successfully, but these errors were encountered: