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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::fmt;

/// Tracks worker state
#[derive(Clone, Copy, Eq, PartialEq)]
pub(crate) struct State(usize);

/// Set when the worker is pushed onto the scheduler's stack of sleeping
/// threads.
pub(crate) const PUSHED_MASK: usize = 0b001;

/// Manages the worker lifecycle part of the state
const LIFECYCLE_MASK: usize = 0b1110;
const LIFECYCLE_SHIFT: usize = 1;

#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone, Copy)]
#[repr(usize)]
pub(crate) enum Lifecycle {
    /// The worker does not currently have an associated thread.
    Shutdown = 0 << LIFECYCLE_SHIFT,

    /// The worker is doing work
    Running = 1 << LIFECYCLE_SHIFT,

    /// The worker is currently asleep in the condvar
    Sleeping = 2 << LIFECYCLE_SHIFT,

    /// The worker has been notified it should process more work.
    Notified = 3 << LIFECYCLE_SHIFT,

    /// A stronger form of notification. In this case, the worker is expected to
    /// wakeup and try to acquire more work... if it enters this state while
    /// already busy with other work, it is expected to signal another worker.
    Signaled = 4 << LIFECYCLE_SHIFT,
}

impl State {
    /// Returns true if the worker entry is pushed in the sleeper stack
    pub fn is_pushed(&self) -> bool {
        self.0 & PUSHED_MASK == PUSHED_MASK
    }

    pub fn set_pushed(&mut self) {
        self.0 |= PUSHED_MASK
    }

    pub fn is_notified(&self) -> bool {
        use self::Lifecycle::*;

        match self.lifecycle() {
            Notified | Signaled => true,
            _ => false,
        }
    }

    pub fn lifecycle(&self) -> Lifecycle {
        Lifecycle::from(self.0 & LIFECYCLE_MASK)
    }

    pub fn set_lifecycle(&mut self, val: Lifecycle) {
        self.0 = (self.0 & !LIFECYCLE_MASK) | (val as usize)
    }

    pub fn is_signaled(&self) -> bool {
        self.lifecycle() == Lifecycle::Signaled
    }

    pub fn notify(&mut self) {
        use self::Lifecycle::Signaled;

        if self.lifecycle() != Signaled {
            self.set_lifecycle(Signaled)
        }
    }
}

impl Default for State {
    fn default() -> State {
        // All workers will start pushed in the sleeping stack
        State(PUSHED_MASK)
    }
}

impl From<usize> for State {
    fn from(src: usize) -> Self {
        State(src)
    }
}

impl From<State> for usize {
    fn from(src: State) -> Self {
        src.0
    }
}

impl fmt::Debug for State {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.debug_struct("worker::State")
            .field("lifecycle", &self.lifecycle())
            .field("is_pushed", &self.is_pushed())
            .finish()
    }
}

// ===== impl Lifecycle =====

impl From<usize> for Lifecycle {
    fn from(src: usize) -> Lifecycle {
        use self::Lifecycle::*;

        debug_assert!(
            src == Shutdown as usize
                || src == Running as usize
                || src == Sleeping as usize
                || src == Notified as usize
                || src == Signaled as usize
        );

        unsafe { ::std::mem::transmute(src) }
    }
}

impl From<Lifecycle> for usize {
    fn from(src: Lifecycle) -> usize {
        let v = src as usize;
        debug_assert!(v & LIFECYCLE_MASK == v);
        v
    }
}

#[cfg(test)]
mod test {
    use super::Lifecycle::*;
    use super::*;

    #[test]
    fn lifecycle_encode() {
        let lifecycles = &[Shutdown, Running, Sleeping, Notified, Signaled];

        for &lifecycle in lifecycles {
            let mut v: usize = lifecycle.into();
            v &= LIFECYCLE_MASK;

            assert_eq!(lifecycle, Lifecycle::from(v));
        }
    }

    #[test]
    fn lifecycle_ord() {
        assert!(Running >= Shutdown);
        assert!(Signaled >= Notified);
        assert!(Signaled >= Sleeping);
    }
}