esp_idf_svc/private/
waitable.rs

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
use core::time::Duration;

use super::mutex::{Condvar, Mutex};

use crate::sys::EspError;
#[cfg(not(feature = "std"))]
use crate::sys::*;

pub struct Waitable<T> {
    pub cvar: Condvar,
    pub state: Mutex<T>,
}

impl<T> Waitable<T>
where
    T: Send,
{
    pub fn new(state: T) -> Self {
        Self {
            cvar: Condvar::new(),
            state: Mutex::new(state),
        }
    }

    pub fn get<Q>(&self, getter: impl FnOnce(&T) -> Q) -> Q {
        let state = self.state.lock();

        getter(&state)
    }

    pub fn get_mut<Q>(&self, getter: impl FnOnce(&mut T) -> Q) -> Q {
        let mut state = self.state.lock();

        getter(&mut state)
    }

    pub fn wait_while<F: FnMut(&T) -> Result<bool, EspError>>(
        &self,
        condition: F,
    ) -> Result<(), EspError> {
        self.wait_while_and_get(condition, |_| ())
    }

    #[allow(dead_code)]
    pub fn wait_timeout_while<F: FnMut(&T) -> Result<bool, EspError>>(
        &self,
        dur: Duration,
        condition: F,
    ) -> Result<bool, EspError> {
        self.wait_timeout_while_and_get(dur, condition, |_| ())
            .map(|(timeout, _)| timeout)
    }

    pub fn wait_while_and_get<F: FnMut(&T) -> Result<bool, EspError>, G: FnMut(&T) -> Q, Q>(
        &self,
        mut condition: F,
        mut getter: G,
    ) -> Result<Q, EspError> {
        let mut state = self.state.lock();

        loop {
            if !condition(&state)? {
                return Ok(getter(&state));
            }

            state = self.cvar.wait(state);
        }
    }

    pub fn wait_timeout_while_and_get<
        F: FnMut(&T) -> Result<bool, EspError>,
        G: FnMut(&T) -> Q,
        Q,
    >(
        &self,
        dur: Duration,
        mut condition: F,
        mut getter: G,
    ) -> Result<(bool, Q), EspError> {
        let mut state = self.state.lock();

        loop {
            if !condition(&state)? {
                return Ok((false, getter(&state)));
            }

            let (new_state, timeout) = self.cvar.wait_timeout(state, dur);

            state = new_state;

            if timeout {
                return Ok((true, getter(&state)));
            }
        }
    }
}