use crate::lib::std::{
borrow::Cow,
sync::{
Arc,
atomic::{AtomicU64, Ordering},
},
};
use wasmer_types::{CompilationProgress, CompilationProgressCallback, CompileError};
#[derive(Clone)]
pub struct ProgressContext {
callback: CompilationProgressCallback,
counter: Arc<AtomicU64>,
total: u64,
phase_name: &'static str,
}
impl ProgressContext {
pub fn new(
callback: CompilationProgressCallback,
total: u64,
phase_name: &'static str,
) -> Self {
Self {
callback,
counter: Arc::new(AtomicU64::new(0)),
total,
phase_name,
}
}
pub fn notify(&self) -> Result<(), CompileError> {
self.notify_steps(1)
}
pub fn notify_steps(&self, steps: u64) -> Result<(), CompileError> {
let step = self.counter.fetch_add(steps, Ordering::SeqCst) + steps;
self.callback
.notify(CompilationProgress::new(
Some(Cow::Borrowed(self.phase_name)),
Some(self.total),
Some(step),
))
.map_err(CompileError::from)
}
}