wasmer_compiler/
progress.rs

1//! Shared helpers for reporting compilation progress across the different backends.
2
3use crate::lib::std::{
4    borrow::Cow,
5    sync::{
6        Arc,
7        atomic::{AtomicU64, Ordering},
8    },
9};
10use wasmer_types::{CompilationProgress, CompilationProgressCallback, CompileError};
11
12/// Tracks progress within a compilation phase and forwards updates to a callback.
13///
14/// Convenience wrapper around a [`CompilationProgressCallback`] for the compilers.
15#[derive(Clone)]
16pub struct ProgressContext {
17    callback: CompilationProgressCallback,
18    counter: Arc<AtomicU64>,
19    total: u64,
20    phase_name: &'static str,
21}
22
23impl ProgressContext {
24    /// Creates a new [`ProgressContext`] for the given phase.
25    pub fn new(
26        callback: CompilationProgressCallback,
27        total: u64,
28        phase_name: &'static str,
29    ) -> Self {
30        Self {
31            callback,
32            counter: Arc::new(AtomicU64::new(0)),
33            total,
34            phase_name,
35        }
36    }
37
38    /// Notifies the callback that the next step in the phase has completed.
39    pub fn notify(&self) -> Result<(), CompileError> {
40        let step = self.counter.fetch_add(1, Ordering::SeqCst) + 1;
41        self.callback
42            .notify(CompilationProgress::new(
43                Some(Cow::Borrowed(self.phase_name)),
44                Some(self.total),
45                Some(step),
46            ))
47            .map_err(CompileError::from)
48    }
49}