Skip to main content

nemo_flow/api/runtime/
global.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Process-global access to the shared runtime context state.
5//!
6//! The public API layer uses this module to resolve the single
7//! [`NemoFlowContextState`] instance that owns middleware registrations and
8//! runtime extensions for the current process.
9
10use std::sync::{Arc, RwLock};
11
12use crate::api::runtime::state::NemoFlowContextState;
13
14static GLOBAL_CONTEXT: std::sync::OnceLock<Arc<RwLock<NemoFlowContextState>>> =
15    std::sync::OnceLock::new();
16
17/// Return the process-global runtime context state handle.
18///
19/// This lazily initializes the shared [`NemoFlowContextState`] on first use and
20/// returns a cloned [`Arc`] handle to the same underlying [`RwLock`] on every
21/// subsequent call.
22///
23/// # Returns
24/// An [`Arc`] pointing at the singleton [`RwLock`] that stores the runtime
25/// context state for the current process.
26///
27/// # Notes
28/// All callers share the same underlying state. Mutations made through one
29/// handle are visible through every other handle returned by this function.
30pub fn global_context() -> Arc<RwLock<NemoFlowContextState>> {
31    GLOBAL_CONTEXT
32        .get_or_init(|| Arc::new(RwLock::new(NemoFlowContextState::new())))
33        .clone()
34}