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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
#[derive(Clone)]
pub struct Progress(Arc<AtomicU32>);
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
enum ProgressState {
NoUpdate,
Updated,
ProtectedZone(u32),
}
#[allow(clippy::from_over_into)]
impl Into<u32> for ProgressState {
fn into(self) -> u32 {
match self {
ProgressState::NoUpdate => 0,
ProgressState::Updated => 1,
ProgressState::ProtectedZone(level) => 2 + level,
}
}
}
impl From<u32> for ProgressState {
fn from(level: u32) -> Self {
match level {
0 => ProgressState::NoUpdate,
1 => ProgressState::Updated,
level => ProgressState::ProtectedZone(level - 2),
}
}
}
impl Default for Progress {
fn default() -> Progress {
Progress(Arc::new(AtomicU32::new(ProgressState::Updated.into())))
}
}
impl Progress {
pub fn record_progress(&self) {
self.0
.fetch_max(ProgressState::Updated.into(), Ordering::Relaxed);
}
pub fn protect_zone(&self) -> ProtectedZoneGuard {
loop {
let previous_state: ProgressState = self.0.load(Ordering::SeqCst).into();
let new_state = match previous_state {
ProgressState::NoUpdate | ProgressState::Updated => ProgressState::ProtectedZone(0),
ProgressState::ProtectedZone(level) => ProgressState::ProtectedZone(level + 1),
};
if self
.0
.compare_exchange(
previous_state.into(),
new_state.into(),
Ordering::SeqCst,
Ordering::SeqCst,
)
.is_ok()
{
return ProtectedZoneGuard(self.0.clone());
}
}
}
pub fn registered_activity_since_last_call(&self) -> bool {
let previous_state: ProgressState = self
.0
.compare_exchange(
ProgressState::Updated.into(),
ProgressState::NoUpdate.into(),
Ordering::Relaxed,
Ordering::Relaxed,
)
.unwrap_or_else(|previous_value| previous_value)
.into();
previous_state != ProgressState::NoUpdate
}
}
pub struct ProtectedZoneGuard(Arc<AtomicU32>);
impl Drop for ProtectedZoneGuard {
fn drop(&mut self) {
let previous_state: ProgressState = self.0.fetch_sub(1, Ordering::SeqCst).into();
assert!(matches!(previous_state, ProgressState::ProtectedZone(_)));
}
}
#[cfg(test)]
mod tests {
use super::Progress;
#[test]
fn test_progress() {
let progress = Progress::default();
assert!(progress.registered_activity_since_last_call());
progress.record_progress();
assert!(progress.registered_activity_since_last_call());
assert!(!progress.registered_activity_since_last_call());
}
#[test]
fn test_progress_protect_zone() {
let progress = Progress::default();
assert!(progress.registered_activity_since_last_call());
progress.record_progress();
assert!(progress.registered_activity_since_last_call());
{
let _protect_guard = progress.protect_zone();
assert!(progress.registered_activity_since_last_call());
assert!(progress.registered_activity_since_last_call());
}
assert!(progress.registered_activity_since_last_call());
assert!(!progress.registered_activity_since_last_call());
}
#[test]
fn test_progress_several_protect_zone() {
let progress = Progress::default();
assert!(progress.registered_activity_since_last_call());
progress.record_progress();
assert!(progress.registered_activity_since_last_call());
let first_protect_guard = progress.protect_zone();
let second_protect_guard = progress.protect_zone();
assert!(progress.registered_activity_since_last_call());
assert!(progress.registered_activity_since_last_call());
std::mem::drop(first_protect_guard);
assert!(progress.registered_activity_since_last_call());
assert!(progress.registered_activity_since_last_call());
std::mem::drop(second_protect_guard);
assert!(progress.registered_activity_since_last_call());
assert!(!progress.registered_activity_since_last_call());
}
}