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