pub struct Runtime { /* private fields */ }Expand description
A configured Asupersync runtime.
Created via RuntimeBuilder. The runtime owns worker threads and a
three-lane priority scheduler. Clone is cheap (shared Arc).
§Example
let runtime = RuntimeBuilder::new().worker_threads(2).build()?;
// Run a future to completion on the current thread.
let result = runtime.block_on(async { 1 + 1 });
assert_eq!(result, 2);
// Spawn from outside async context via a handle.
let handle = runtime.handle().spawn(async { 42u32 });
let value = runtime.block_on(handle);
assert_eq!(value, 42);Implementations§
Source§impl Runtime
impl Runtime
Sourcepub fn with_config(config: RuntimeConfig) -> Result<Self, Error>
pub fn with_config(config: RuntimeConfig) -> Result<Self, Error>
Construct a runtime from the given configuration.
Sourcepub fn with_config_and_reactor(
config: RuntimeConfig,
reactor: Option<Arc<dyn Reactor>>,
) -> Result<Self, Error>
pub fn with_config_and_reactor( config: RuntimeConfig, reactor: Option<Arc<dyn Reactor>>, ) -> Result<Self, Error>
Construct a runtime from the given configuration and reactor.
Sourcepub fn handle(&self) -> RuntimeHandle
pub fn handle(&self) -> RuntimeHandle
Returns a handle that can spawn tasks from outside the runtime.
Sourcepub fn block_on<F: Future>(&self, future: F) -> F::Output
pub fn block_on<F: Future>(&self, future: F) -> F::Output
Run a future to completion on the current thread.
While the future is being polled, a thread-local RuntimeHandle is
available via Runtime::current_handle. This allows futures inside
block_on to spawn tasks onto the real scheduler without having to
thread the handle through every layer.
Sourcepub fn current_handle() -> Option<RuntimeHandle>
pub fn current_handle() -> Option<RuntimeHandle>
Returns a handle to the current runtime, if called from within
Runtime::block_on or a worker thread.
Returns None when called outside of a runtime context.
§Example
runtime.block_on(async {
let handle = Runtime::current_handle()
.expect("inside block_on");
handle.spawn(async { do_work().await });
});Returns None when no runtime is installed on the current thread and
during thread-local teardown, where the ambient handle is no longer
accessible.
Sourcepub fn config(&self) -> &RuntimeConfig
pub fn config(&self) -> &RuntimeConfig
Returns a reference to the runtime configuration.
Sourcepub fn is_quiescent(&self) -> bool
pub fn is_quiescent(&self) -> bool
Returns true if the runtime is quiescent (no live tasks or I/O).
Sourcepub fn spawn_blocking<F>(&self, f: F) -> Option<BlockingTaskHandle>
pub fn spawn_blocking<F>(&self, f: F) -> Option<BlockingTaskHandle>
Sourcepub fn blocking_handle(&self) -> Option<BlockingPoolHandle>
pub fn blocking_handle(&self) -> Option<BlockingPoolHandle>
Returns a handle to the blocking pool, if configured.