use super::{
MemoryRuntime, RuntimeBootstrapError, RuntimeConstructionError, RuntimeDiagnosticError,
RuntimeOpenError, RuntimeStateError, policy::NoopPolicy,
};
use crate::{
CommittedAllocations, DiagnosticExport, MemoryRuntimeDoctorReport, RuntimeBootstrapPolicy,
physical::CommitStoreDiagnostic, registry::sealed_declaration_snapshot,
};
use ic_stable_structures::{DefaultMemoryImpl, memory_manager::VirtualMemory};
use std::{cell::RefCell, convert::Infallible};
thread_local! {
static DEFAULT_RUNTIME:
RefCell<Result<MemoryRuntime<DefaultMemoryImpl>, RuntimeConstructionError>> =
RefCell::new(MemoryRuntime::new(DefaultMemoryImpl::default()));
}
fn with_default_runtime<T, E>(
operation: impl FnOnce(&MemoryRuntime<DefaultMemoryImpl>) -> Result<T, E>,
) -> Result<T, E>
where
E: From<RuntimeStateError>,
{
match DEFAULT_RUNTIME.try_with(|runtime| {
let runtime = runtime
.try_borrow()
.map_err(|_| E::from(RuntimeStateError::ReentrantAccess))?;
let runtime = runtime
.as_ref()
.map_err(|error| E::from(RuntimeStateError::Construction(*error)))?;
operation(runtime)
}) {
Ok(result) => result,
Err(_) => Err(E::from(RuntimeStateError::Unavailable)),
}
}
fn with_default_runtime_mut<T, E>(
operation: impl FnOnce(&mut MemoryRuntime<DefaultMemoryImpl>) -> Result<T, E>,
) -> Result<T, E>
where
E: From<RuntimeStateError>,
{
match DEFAULT_RUNTIME.try_with(|runtime| {
let mut runtime = runtime
.try_borrow_mut()
.map_err(|_| E::from(RuntimeStateError::ReentrantAccess))?;
let runtime = runtime
.as_mut()
.map_err(|error| E::from(RuntimeStateError::Construction(*error)))?;
operation(runtime)
}) {
Ok(result) => result,
Err(_) => Err(E::from(RuntimeStateError::Unavailable)),
}
}
pub fn is_default_memory_manager_bootstrapped() -> Result<bool, RuntimeStateError> {
with_default_runtime(|runtime| Ok(runtime.is_bootstrapped()))
}
pub fn committed_allocations() -> Result<CommittedAllocations, RuntimeOpenError> {
with_default_runtime(|runtime| runtime.committed_allocations().cloned())
}
pub fn bootstrap_default_memory_manager()
-> Result<CommittedAllocations, RuntimeBootstrapError<Infallible>> {
bootstrap_default_memory_manager_with_policy(&NoopPolicy)
}
pub fn bootstrap_default_memory_manager_with_policy<P: RuntimeBootstrapPolicy>(
policy: &P,
) -> Result<CommittedAllocations, RuntimeBootstrapError<P::Error>> {
let declarations = sealed_declaration_snapshot()?;
with_default_runtime_mut(|runtime| runtime.bootstrap(&declarations, policy).cloned())
}
pub fn open_default_memory_manager_memory(
stable_key: &str,
id: u8,
) -> Result<VirtualMemory<DefaultMemoryImpl>, RuntimeOpenError> {
with_default_runtime(|runtime| runtime.open_memory(stable_key, id))
}
pub fn default_memory_manager_diagnostic_export() -> Result<DiagnosticExport, RuntimeDiagnosticError>
{
with_default_runtime(MemoryRuntime::diagnostic_export)
}
pub fn default_memory_manager_commit_recovery_diagnostic()
-> Result<CommitStoreDiagnostic, RuntimeDiagnosticError> {
with_default_runtime(MemoryRuntime::commit_recovery_diagnostic)
}
pub fn default_memory_manager_doctor_report()
-> Result<MemoryRuntimeDoctorReport, RuntimeDiagnosticError> {
let declarations = sealed_declaration_snapshot()?;
with_default_runtime(|runtime| Ok(runtime.doctor_report(&declarations)))
}
#[cfg(test)]
pub(super) fn with_default_runtime_borrowed(
operation: impl FnOnce() -> Result<(), RuntimeStateError>,
) -> Result<(), RuntimeStateError> {
DEFAULT_RUNTIME.with(|runtime| {
let _borrow = runtime.borrow_mut();
operation()
})
}