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
use std::sync::{
    atomic::{AtomicUsize, Ordering},
    Arc,
};

use lazy_static::lazy_static;

/// 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
#[derive(Debug, Clone)]
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,
        }
    }
    pub fn unlimited() -> TaskLifetime {
        // Use a global static Arc<AtomicUsize> so that we don't have to
        // allocate more than once
        lazy_static! {
            static ref ZERO: Arc<AtomicUsize> = Arc::new(AtomicUsize::new(0));
        }

        TaskLifetime {
            initial_value: 0,
            external_value: ZERO.clone(),
        }
    }
    pub fn is_expired(&self) -> bool {
        self.initial_value != self.external_value.load(Ordering::Relaxed)
    }
}