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

/// All possible states of a [`BlockingArbiter`].
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Blocking {
    // Not requested nor allowed
    Void,
    // Requested Once
    Pending,
    // One blocking operation granted
    Once,
    // Any/all blocking operations perpetually granted
    Always,
}

/// Trait for arbitration of where blocking operations are acceptable to the
/// runtime.
pub trait BlockingArbiter {
    /// Return true if blocking is allowed.
    ///
    /// This may record the need to block and/or consume any one-time blocking
    /// allowance.
    fn can_block(&mut self) -> bool;

    /// Return the current blocking state, without making any change.
    fn state(&self) -> Blocking;
}

/// A lenient arbiter that always allows blocking.
#[derive(Debug, Default)]
pub struct LenientArbiter;

impl BlockingArbiter for LenientArbiter {

    /// Return true if blocking is allowed.
    ///
    /// This implementation always returns true.
    fn can_block(&mut self) -> bool {
        true
    }

    fn state(&self) -> Blocking {
        Blocking::Always
    }
}

/// A stateful arbiter that records the need to block and grants one-time
/// allowances.
#[derive(Debug)]
pub struct StatefulArbiter {
    state: Blocking
}

impl Default for StatefulArbiter {
    fn default() -> Self {
        StatefulArbiter { state: Blocking::Void }
    }
}

impl StatefulArbiter {
    pub(crate) fn set(&mut self, state: Blocking) {
        self.state = state;
    }
}

impl BlockingArbiter for StatefulArbiter {
    /// Return true if blocking is allowed, consuming any one-time allowance.
    /// Otherwise records that blocking has been requested.
    fn can_block(&mut self) -> bool {
        match self.state {
            Blocking::Void => {
                self.state = Blocking::Pending;
                false
            }
            Blocking::Pending => {
                false
            }
            Blocking::Once => {
                self.state = Blocking::Void;
                true
            }
            Blocking::Always => {
                true
            }
        }
    }

    fn state(&self) -> Blocking {
        self.state
    }
}