This commit is contained in:
azw 2024-05-11 08:27:29 +08:00
parent cbca2a666f
commit c950f6d288
2 changed files with 21 additions and 13 deletions

View File

@ -58,7 +58,7 @@ pub struct App {
zone_id: i32, zone_id: i32,
node_id: i32, node_id: i32,
instance_id: i32, instance_id: i32,
tokio_rt: Runtime, //tokio_rt: Runtime,
user_app: Option<Rc::<RefCell::<dyn UserApp>>>, user_app: Option<Rc::<RefCell::<dyn UserApp>>>,
im_msgs: Rc::<RefCell::<r9::ListHead::<IMMsgNode>>>, im_msgs: Rc::<RefCell::<r9::ListHead::<IMMsgNode>>>,
im_work_msgs: Rc::<RefCell::<r9::ListHead::<IMMsgNode>>>, im_work_msgs: Rc::<RefCell::<r9::ListHead::<IMMsgNode>>>,
@ -134,7 +134,7 @@ impl App {
zone_id: 0, zone_id: 0,
node_id: 0, node_id: 0,
instance_id: 0, instance_id: 0,
tokio_rt: Runtime::new().unwrap(), //tokio_rt: Runtime::new().unwrap(),
user_app: None, user_app: None,
im_msgs: r9::ListHead::<IMMsgNode>::new_head(), im_msgs: r9::ListHead::<IMMsgNode>::new_head(),
im_work_msgs: r9::ListHead::<IMMsgNode>::new_head(), im_work_msgs: r9::ListHead::<IMMsgNode>::new_head(),
@ -158,7 +158,7 @@ impl App {
pub fn init(user_app: Rc::<RefCell::<dyn UserApp>>) { pub fn init(user_app: Rc::<RefCell::<dyn UserApp>>) {
App::instance().borrow_mut().user_app = Some(user_app.clone()); App::instance().borrow_mut().user_app = Some(user_app.clone());
App::instance().borrow_mut().tokio_rt.enter(); //App::instance().borrow_mut().tokio_rt.enter();
//App::instance().borrow_mut().init_http_server(); //App::instance().borrow_mut().init_http_server();
crate::Timer::instance().borrow_mut().init(); crate::Timer::instance().borrow_mut().init();
user_app.borrow_mut().init(); user_app.borrow_mut().init();

View File

@ -1,44 +1,52 @@
use std::rc::{Rc, Weak}; use std::rc::{Rc, Weak};
use std::cell::RefCell; use std::cell::RefCell;
use std::sync::Mutex; use std::sync::Mutex;
use std::sync::Arc;
use std::thread; use std::thread;
use std::thread::Thread; use std::thread::Thread;
use r9_macro::SharedFromSelf; use r9_macro::SharedFromSelf;
use r9_macro_derive::SharedFromSelf; use r9_macro_derive::SharedFromSelf;
use std::default::Default;
#[derive(SharedFromSelf)]
pub struct Queue<T> { pub struct Queue<T> {
msg_list: Mutex<Rc::<RefCell::<crate::ListHead<T>>>>, msg_list: Mutex<Rc::<RefCell::<crate::ListHead<T>>>>,
pub work_list: Rc::<RefCell::<crate::ListHead<T>>>, pub work_list: Mutex<Rc::<RefCell::<crate::ListHead<T>>>>,
_self_wp: Weak::<RefCell::<Self>>,
} }
unsafe impl<T> Send for Queue<T> {}
unsafe impl<T> Sync for Queue<T> {}
impl<T> Queue<T> { impl<T> Queue<T> {
pub fn new() -> Rc::<RefCell::<Self>> { pub fn new() -> Rc::<RefCell::<Self>> {
let this = Rc::new(RefCell::new(Self{ let this = Rc::new(RefCell::new(Self{
msg_list: Mutex::new(crate::ListHead::<T>::new_head()), msg_list: Mutex::new(crate::ListHead::<T>::new_head()),
work_list: crate::ListHead::<T>::new_head(), work_list: Mutex::new(crate::ListHead::<T>::new_head()),
_self_wp: Default::default(), }));
return this;
}
pub fn new_ex() -> Arc::<Mutex::<Self>> {
let this = Arc::new(Mutex::new(Self{
msg_list: Mutex::new(crate::ListHead::<T>::new_head()),
work_list: Mutex::new(crate::ListHead::<T>::new_head()),
})); }));
this.borrow_mut()._self_wp = Rc::downgrade(&this);
return this; return this;
} }
pub fn push(&self, node: &Rc::<RefCell::<crate::ListHead<T>>>) { pub fn push(&self, node: &Rc::<RefCell::<crate::ListHead<T>>>) {
println!("Child thread ID is: {:?}", thread::current().id());
crate::ListHead::<T>::add_tail(&self.msg_list.lock().unwrap(), node); crate::ListHead::<T>::add_tail(&self.msg_list.lock().unwrap(), node);
} }
pub fn fetch(&self) { pub fn fetch(&self) {
if !self.msg_list.lock().unwrap().borrow().empty() && if !self.msg_list.lock().unwrap().borrow().empty() &&
self.work_list.borrow().empty() { self.work_list.lock().unwrap().borrow().empty() {
crate::ListHead::replace_init(&self.work_list, &self.msg_list.lock().unwrap()); crate::ListHead::replace_init(&self.msg_list.lock().unwrap(), &self.work_list.lock().unwrap());
} }
} }
pub fn empty(&self) -> bool { pub fn empty(&self) -> bool {
if !self.work_list.borrow().empty() { if !self.work_list.lock().unwrap().borrow().empty() {
return false return false
} }
return self.msg_list.lock().unwrap().borrow().empty() return self.msg_list.lock().unwrap().borrow().empty()