This commit is contained in:
aozhiwei 2023-10-29 11:24:11 +08:00
parent 2e2456dbca
commit 2fb1fe39d0
2 changed files with 70 additions and 26 deletions

View File

@ -1,5 +1,6 @@
use std::rc::{Rc, Weak};
use std::cell::RefCell;
use std::ptr;
use r9_macro::SharedFromSelf;
use r9_macro_derive::SharedFromSelf;
@ -50,17 +51,45 @@ impl<T> ListHead<T> {
}
pub fn empty(&self) -> bool {
return self.next.ptr_eq(&self.prev);
return ptr::eq(self.next.as_ptr(), Rc::as_ptr(&self.shared_from_self()));
}
pub fn add_tail(head: &Rc::<RefCell::<Self>>, pnew: &Rc::<RefCell::<Self>>) {
let next = head;
let prev = &head.borrow_mut().prev.clone();
let next = head;
/*
let a: &Rc::<i32> = &Rc::new(100);
let b = Rc::downgrade(a);
println!("a:{:p} b:{:p}", Rc::as_ptr(a), b.as_ptr());
println!("add_tail:{} {} {:p} {:p} {:p} {:p} {:p} {:p} {:p}",
head.borrow().empty(),
pnew.borrow().empty(),
pnew.borrow().next.as_ptr(),
pnew.borrow().prev.as_ptr(),
Rc::as_ptr(&pnew.borrow().shared_from_self()),
Rc::as_ptr(pnew),
head.as_ptr(),
prev.as_ptr(),
next.as_ptr()
);
*/
next.borrow_mut().prev = Rc::downgrade(pnew);
pnew.borrow_mut().next = Rc::downgrade(&next);
pnew.borrow_mut().prev = prev.clone();
prev.upgrade().unwrap().borrow_mut().next = Rc::downgrade(pnew);
println!("add_tail:{} {} {:p} {:p} {:p} {:p} {:p} {:p}",
head.borrow().empty(),
pnew.borrow().empty(),
pnew.borrow().next.as_ptr(),
pnew.borrow().prev.as_ptr(),
Rc::as_ptr(pnew),
Rc::as_ptr(head),
prev.as_ptr(),
next.as_ptr()
);
}
pub fn first_entry(&self) -> Weak::<RefCell::<T>> {
@ -71,10 +100,22 @@ impl<T> ListHead<T> {
}
pub fn replace_init(head: &Rc::<RefCell::<Self>>, pnew: &Rc::<RefCell::<Self>>) {
if !head.borrow().empty() {
let timer = &head.borrow().first_entry();
let a = timer.upgrade().unwrap();
} else {
return;
}
//println!("replace_init");
pnew.borrow_mut().next = head.borrow().next.clone();
pnew.borrow_mut().next.upgrade().unwrap().borrow_mut().prev = Rc::downgrade(pnew);
pnew.borrow_mut().prev = head.borrow().prev.clone();
pnew.borrow_mut().prev.upgrade().unwrap().borrow_mut().next = Rc::downgrade(pnew);
head.borrow_mut().init();
if !pnew.borrow().empty() {
let timer = &pnew.borrow().first_entry();
let a = timer.upgrade().unwrap();
}
}
pub fn for_each(&self, cb: fn (&Weak::<RefCell::<T>>) -> bool) {

View File

@ -105,10 +105,10 @@ impl XTimer {
self.free_timer_num = 0;
self.free_timer_list = crate::ListHead::<TimerList>::new_head();
self.work_list = crate::ListHead::<TimerList>::new_head();
for _ in 0..TVR_SIZE-1 {
for _ in 0..TVR_SIZE {
self.tv1.push(crate::ListHead::<TimerList>::new_head());
}
for _ in 0..TVN_SIZE-1 {
for _ in 0..TVN_SIZE {
self.tv2.push(crate::ListHead::<TimerList>::new_head());
self.tv3.push(crate::ListHead::<TimerList>::new_head());
self.tv4.push(crate::ListHead::<TimerList>::new_head());
@ -125,7 +125,7 @@ impl XTimer {
let self_wp = self.shared_from_self();
let cb = Box::new(
move |event: TimerEvent, args: Option<Vec<Rc::<dyn Any>>>| {
//self_wp.borrow_mut().gc_timer(event, args);
self_wp.borrow_mut().gc_timer(event, args);
}
);
self.set_interval(gctime, cb);
@ -196,7 +196,6 @@ impl XTimer {
}
println!("d");
p.borrow_mut().timer_entry = crate::ListHead::<TimerList>::new_node(
//Rc::downgrade(&p.borrow().holder.clone().unwrap())
Rc::downgrade(&p)
);
println!("e");
@ -206,26 +205,27 @@ impl XTimer {
return p;
}
fn cascade(&mut self, idx: usize) -> usize {
fn cascade(&self, idx: usize) -> usize {
let index = self.get_timer_index(idx);
let tv : &mut [TimerListListHeadRp];
let tv : &[TimerListListHeadRp];
match idx {
0 => {
tv = &mut self.tv2;
tv = &self.tv2;
}
1 => {
tv = &mut self.tv3;
tv = &self.tv3;
}
2 => {
tv = &mut self.tv4;
tv = &self.tv4;
}
3 => {
tv = &mut self.tv5;
tv = &self.tv5;
}
_ => {
tv = &mut self.tv5;
tv = &self.tv5;
}
}
println!("zzzzzzz idx:{} index:{}", idx, index);
if !tv[index].borrow().empty() {
let mut cascade_list = crate::ListHead::<TimerList>::new_head();
crate::ListHead::<TimerList>::replace_init
@ -261,15 +261,18 @@ impl XTimer {
self.cascade(1) == 0 &&
self.cascade(2) == 0 {
self.cascade(3);
}
}
self.timer_tick += 1;
println!("wwwwwwwwwwww");
//println!("timer_tick:{} index:{} empty:{}", self.timer_tick, index, self.tv1[index].borrow().empty());
crate::ListHead::<TimerList>::replace_init
(&self.work_list,
&self.tv1[index]);
(&self.tv1[index],
&self.work_list);
while !self.work_list.borrow().empty() {
let timer = &self.work_list.borrow().first_entry();
self.running_timer = timer.clone();
let a = timer.upgrade().unwrap();
match &mut timer.upgrade().unwrap().borrow_mut().cb {
Some(v) => {
(*v)(TimerEvent::Exec, None);
@ -277,6 +280,7 @@ impl XTimer {
None => {
}
}
println!("timer_tick2:{}", self.timer_tick);
match self.running_timer.upgrade() {
Some(v) => {
match v.borrow().timer_type {
@ -330,33 +334,33 @@ impl XTimer {
return Rc::downgrade(&t.borrow().wp.clone().unwrap());
}
fn internal_add2(&mut self, t: &Rc::<RefCell::<TimerList>>) {
fn internal_add2(&self, t: &Rc::<RefCell::<TimerList>>) {
let idx = t.borrow().expires - self.timer_tick;
let index;
let vec: &Rc::<RefCell::<crate::ListHead<TimerList>>>;
if idx < 0 {
index = self.timer_tick & (TVR_MASK as i64);
vec = &mut self.tv1[index as usize];
vec = &self.tv1[index as usize];
} else if idx < TVR_SIZE as i64 {
index = t.borrow().expires & (TVR_MASK as i64);
vec = &mut self.tv1[index as usize];
vec = &self.tv1[index as usize];
} else if idx < (1 << (TVR_BITS + TVN_BITS)) {
index = (t.borrow().expires >> TVR_BITS) & (TVN_MASK as i64);
vec = &mut self.tv2[index as usize];
vec = &self.tv2[index as usize];
} else if idx < (1 << (TVR_BITS + 2 * TVN_BITS)) {
index = (t.borrow().expires >> (TVR_BITS + 1 * TVN_BITS)) & (TVN_MASK as i64);
vec = &mut self.tv3[index as usize];
vec = &self.tv3[index as usize];
} else if idx < (1 << (TVR_BITS + 3 * TVN_BITS)) {
index = (t.borrow().expires >> (TVR_BITS + 2 * TVN_BITS)) & (TVN_MASK as i64);
vec = &mut self.tv4[index as usize];
vec = &self.tv4[index as usize];
} else {
index = (t.borrow().expires >> (TVR_BITS + 3 * TVN_BITS)) & (TVN_MASK as i64);
vec = &mut self.tv5[index as usize];
vec = &self.tv5[index as usize];
}
println!("add2 idx:{} index:{}", idx, index);
crate::ListHead::<TimerList>::add_tail(&vec, &t.borrow_mut().timer_entry);
}
fn internal_delete(&mut self, timer_wp: XTimerWp, is_destory: bool) {
match timer_wp.upgrade() {
Some(v) => {
@ -382,8 +386,6 @@ impl XTimer {
crate::ListHead::<TimerList>::add_tail
(&self.free_timer_list,
&v.borrow_mut().attach_entry);
//ListHead<TimerList>::add_tail(&self.free_timer_list, &v.borrow_mut().timer_entry);
//self.free_timer_list.borrow_mut().add_tail(&v.borrow_mut().timer_entry);
self.free_timer_num += 1;
}
None => {
@ -501,6 +503,7 @@ impl XTimer {
}
fn gc_timer(&mut self, e: TimerEvent, _args: Option<Vec<Rc::<dyn Any>>>) {
println!("aaaaa");
match e {
TimerEvent::Exec => {
while self.free_timer_num > self.cache_timer_num &&