use ort::environment::{Environment, EnvironmentBuilder};
use ort::logging::LogLevel;
use std::sync::Mutex;
struct EnvironmentState {
pending: bool,
owned: bool,
log_level: LogLevel,
}
static ENVIRONMENT_STATE: Mutex<EnvironmentState> = Mutex::new(EnvironmentState {
pending: false,
owned: false,
log_level: LogLevel::Error,
});
pub fn initialize_ort_environment() -> ort::Result<bool> {
commit_environment(ort::init())
}
pub(super) fn commit_environment(builder: EnvironmentBuilder) -> ort::Result<bool> {
let mut state = ENVIRONMENT_STATE
.lock()
.map_err(|_| ort::Error::new("ONNX Runtime environment initialization lock poisoned"))?;
let committed = builder.commit();
state.pending |= committed;
state.owned |= committed;
if state.pending {
Environment::current()?.set_log_level(state.log_level);
state.pending = false;
}
Ok(committed)
}
pub(super) fn lower_owned_environment_log_level(level: LogLevel) -> ort::Result<()> {
let mut state = ENVIRONMENT_STATE
.lock()
.map_err(|_| ort::Error::new("ONNX Runtime environment initialization lock poisoned"))?;
if state.owned && level < state.log_level {
Environment::current()?.set_log_level(level);
state.log_level = level;
}
Ok(())
}