beekeeper/hive/inner/queue/
status.rs

1use crate::atomic::{Atomic, AtomicU8};
2
3const OPEN: u8 = 0;
4const CLOSED_PUSH: u8 = 1;
5const CLOSED_POP: u8 = 2;
6
7/// Represents the status of a task queue.
8///
9/// This is a simple state machine
10/// OPEN -> CLOSED_PUSH -> CLOSED_POP
11///   |________________________^
12pub struct Status(AtomicU8);
13
14impl Status {
15    /// Returns `true` if the queue status is `CLOSED_PUSH` or `CLOSED_POP`.
16    pub fn is_closed(&self) -> bool {
17        self.0.get() > OPEN
18    }
19
20    /// Returns `true` if the queue can accept new tasks.
21    pub fn can_push(&self) -> bool {
22        self.0.get() < CLOSED_PUSH
23    }
24
25    /// Returns `true` if the queue can remove tasks.
26    pub fn can_pop(&self) -> bool {
27        self.0.get() < CLOSED_POP
28    }
29
30    /// Sets the queue status to `CLOSED_PUSH` if `urgent` is `false`, or `CLOSED_POP` if `urgent`
31    /// is `true`.
32    pub fn set(&self, urgent: bool) {
33        // TODO: this update should be done with `fetch_max`
34        let new_status = if urgent { CLOSED_POP } else { CLOSED_PUSH };
35        if new_status > self.0.get() {
36            self.0.set(new_status);
37        }
38    }
39}
40
41impl Default for Status {
42    fn default() -> Self {
43        Self(AtomicU8::new(OPEN))
44    }
45}