Repository navigation
Expand file tree
/
Copy pathstop.rs
More file actions
79 lines (69 loc) · 2.4 KB
/
Copy pathstop.rs
File metadata and controls
79 lines (69 loc) · 2.4 KB
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
//! A run's stop request, and the clock the program reads.
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
/// Milliseconds since the Unix epoch, as the program's clock reads; 0 for a clock set before it.
pub(super) fn now_millis() -> i64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|since| i64::try_from(since.as_millis()).unwrap_or(i64::MAX))
.unwrap_or(0)
}
/// How a host program is asked to end a run: once requested, it stays requested. Cloning it shares it, so the one
/// who started a run and the run itself hold the same request.
#[derive(Clone, Default)]
pub struct StopRequest {
shared: Arc<(Mutex<bool>, Condvar)>,
}
impl std::fmt::Debug for StopRequest {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(formatter, "StopRequest(requested: {})", self.is_requested())
}
}
impl StopRequest {
/// A request not yet made.
pub fn new() -> StopRequest {
StopRequest::default()
}
fn requested(&self) -> MutexGuard<'_, bool> {
self.shared
.0
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
/// Requests the stop; a later request changes nothing.
pub fn request(&self) {
*self.requested() = true;
self.shared.1.notify_all();
}
/// Whether the stop is requested.
pub fn is_requested(&self) -> bool {
*self.requested()
}
/// Waits until the stop is requested.
pub(super) fn wait(&self) {
let mut requested = self.requested();
while !*requested {
requested = self
.shared
.1
.wait(requested)
.unwrap_or_else(|poisoned| poisoned.into_inner());
}
}
/// Waits until the clock reads `at` or later, reading it again after every wait, or until the stop is requested.
pub(super) fn wait_until(&self, at: i64) {
let mut requested = self.requested();
loop {
let left = at.saturating_sub(now_millis());
if *requested || left <= 0 {
return;
}
requested = self
.shared
.1
.wait_timeout(requested, Duration::from_millis(left as u64))
.unwrap_or_else(|poisoned| poisoned.into_inner())
.0;
}
}
}