blob: 1380da2bf6e4127b338cdf06b34e414c106d286c (
plain)
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
|
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
/// a TL initialized from an Arc<AtomicUsize> stays
/// alive as long as the passed arc doesn't change.
/// When it changes, is_expired returns true
pub struct TaskLifetime {
initial_value: usize,
external_value: Arc<AtomicUsize>,
}
impl TaskLifetime {
pub fn new(external_value: &Arc<AtomicUsize>) -> TaskLifetime {
TaskLifetime {
initial_value: external_value.load(Ordering::Relaxed),
external_value: Arc::clone(external_value),
}
}
pub fn unlimited() -> TaskLifetime {
TaskLifetime {
initial_value: 0,
external_value: Arc::new(AtomicUsize::new(0)),
}
}
pub fn is_expired(&self) -> bool {
self.initial_value != self.external_value.load(Ordering::Relaxed)
}
}
|