This commit is contained in:
azw 2023-11-09 00:19:56 +00:00
parent d371216ec4
commit 9761c32056
2 changed files with 38 additions and 0 deletions

View File

@ -1,5 +1,7 @@
pub mod xtimer;
pub mod listhead;
pub mod queue;
pub use xtimer::XTimer;
pub use listhead::ListHead;
pub use queue::Queue;

36
r9/src/queue.rs Normal file
View File

@ -0,0 +1,36 @@
use std::rc::{Rc, Weak};
use std::cell::RefCell;
use r9_macro::SharedFromSelf;
use r9_macro_derive::SharedFromSelf;
#[derive(SharedFromSelf)]
pub struct Queue<T> {
msg_list: Rc::<RefCell::<crate::ListHead<T>>>,
pub work_list: Rc::<RefCell::<crate::ListHead<T>>>,
_self_wp: Weak::<RefCell::<Self>>,
}
impl<T> Queue<T> {
pub fn new() -> Rc::<RefCell::<Self>> {
let this = Rc::new(RefCell::new(Self{
msg_list: crate::ListHead::<T>::new_head(),
work_list: crate::ListHead::<T>::new_head(),
_self_wp: Default::default(),
}));
this.borrow_mut()._self_wp = Rc::downgrade(&this);
return this;
}
pub fn push(&mut self, node: &Rc::<RefCell::<crate::ListHead<T>>>) {
crate::ListHead::<T>::add_tail(&self.msg_list, node);
}
pub fn fetch(&mut self) {
if !self.msg_list.borrow().empty() &&
self.work_list.borrow().empty() {
crate::ListHead::replace_init(&self.work_list, &self.msg_list);
}
}
}