maycoon_core/app/
update.rs1use bitflags::bitflags;
2use std::sync::Arc;
3use std::sync::atomic::{AtomicU8, Ordering};
4
5bitflags! {
6 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14 pub struct Update: u8 {
15 const EVAL = 0b00000001;
17 const DRAW = 0b00000010;
19 const LAYOUT = 0b00000100;
21 const FORCE = 0b00001000;
23 const EXIT = 0b00010000;
25 }
26}
27
28#[derive(Clone, Debug)]
32pub struct UpdateManager {
33 update: Arc<AtomicU8>,
34}
35
36impl UpdateManager {
37 #[inline(always)]
39 pub fn new() -> Self {
40 Self {
41 update: Arc::new(AtomicU8::new(Update::FORCE.bits())),
42 }
43 }
44
45 #[inline(always)]
47 pub fn insert(&self, update: Update) {
48 tracing::debug!("inserting update {update:?}");
49 self.update.fetch_or(update.bits(), Ordering::AcqRel);
50 }
51
52 #[inline(always)]
54 pub fn remove(&self, update: Update) {
55 tracing::debug!("removing update {update:?}");
56 self.update.fetch_and(!update.bits(), Ordering::AcqRel);
57 }
58
59 #[inline(always)]
61 pub fn get(&self) -> Update {
62 Update::from_bits(self.update.load(Ordering::Acquire))
63 .expect("failed to decode update bits")
64 }
65
66 #[inline(always)]
68 pub fn set(&self, update: Update) {
69 tracing::debug!("setting update {update:?}");
70 self.update.store(update.bits(), Ordering::Release);
71 }
72
73 #[inline(always)]
75 pub fn clear(&self) {
76 self.update.store(0, Ordering::Release);
77 }
78}
79
80impl Default for UpdateManager {
81 #[inline(always)]
82 fn default() -> Self {
83 Self::new()
84 }
85}