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
43
44
45
46
47
48
//! Runtime handle for async operations in the concurrency system.
use std::sync::Arc;
use tokio::runtime::Handle;
/// Runtime handle for async operations in the concurrency system.
///
/// This provides access to the global tokio runtime for executing
/// asynchronous operations and managing futures.
pub struct ConcurrencyRuntime {
handle: Handle,
}
impl ConcurrencyRuntime {
/// Gets or creates the global concurrency runtime.
pub fn global() -> Arc<Self> {
use std::sync::OnceLock;
static RUNTIME: OnceLock<Arc<ConcurrencyRuntime>> = OnceLock::new();
RUNTIME.get_or_init(|| {
Arc::new(ConcurrencyRuntime {
handle: tokio::runtime::Handle::current(),
})
}).clone()
}
/// Gets the tokio runtime handle.
pub fn handle(&self) -> &Handle {
&self.handle
}
/// Spawns a task on the runtime.
pub fn spawn<F>(&self, future: F) -> tokio::task::JoinHandle<F::Output>
where
F: std::future::Future + Send + 'static,
F::Output: Send + 'static,
{
self.handle.spawn(future)
}
/// Blocks on a future until completion.
pub fn block_on<F>(&self, future: F) -> F::Output
where
F: std::future::Future,
{
self.handle.block_on(future)
}
}