1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//! Persistent tokio runtime for `#[remote]` async functions.
//!
//! The `#[remote]` proc macro generates C-compatible callbacks that are invoked
//! by Ray's C++ worker threads. These threads have no tokio context, so we need
//! a runtime to `block_on` async futures. Instead of creating a new runtime per
//! call (expensive — allocates threads, I/O driver, etc.), we use a single
//! global multi-threaded runtime created once via `OnceLock`.
//!
//! ## Why this is safe
//!
//! - `block_on_async` is ONLY called from C++ worker callbacks (no tokio context).
//! - Async remote fns use `.await` (get_async) or FFI (get, task_call) internally
//! — neither re-enters `block_on_async`.
//! - The global `Runtime` is `Send + Sync`, so multiple C++ worker threads
//! calling `block_on_async` concurrently is safe.
use OnceLock;
use Runtime;
static RUNTIME: = new;
/// Get the global tokio runtime, creating it on first access.
/// Block on a future using the persistent global runtime.
///
/// Called by the `#[remote]` proc macro for `async fn` callbacks.
/// The future runs on the global multi-threaded runtime, so `spawn_blocking`
/// and other tokio features work correctly inside remote tasks.