This commit is contained in:
aozhiwei 2023-10-28 22:11:59 +08:00
parent 45963c4a6f
commit 2e2456dbca
2 changed files with 56 additions and 37 deletions

View File

@ -53,9 +53,9 @@ impl<T> ListHead<T> {
return self.next.ptr_eq(&self.prev);
}
pub fn add_tail(&mut self, pnew: &Rc::<RefCell::<Self>>) {
let next = self.shared_from_self();
let prev = &mut self.prev;
pub fn add_tail(head: &Rc::<RefCell::<Self>>, pnew: &Rc::<RefCell::<Self>>) {
let next = head;
let prev = &head.borrow_mut().prev.clone();
next.borrow_mut().prev = Rc::downgrade(pnew);
pnew.borrow_mut().next = Rc::downgrade(&next);
@ -70,11 +70,11 @@ impl<T> ListHead<T> {
return self.next.upgrade().unwrap().borrow().data();
}
pub fn replace_init(&mut self, pnew: &mut Rc::<RefCell::<Self>>) {
pnew.borrow_mut().next = self.next.clone();
pub fn replace_init(head: &Rc::<RefCell::<Self>>, pnew: &Rc::<RefCell::<Self>>) {
pnew.borrow_mut().next = head.borrow().next.clone();
pnew.borrow_mut().next.upgrade().unwrap().borrow_mut().prev = Rc::downgrade(pnew);
pnew.borrow_mut().prev = self.prev.clone();
self.init();
pnew.borrow_mut().prev = head.borrow().prev.clone();
head.borrow_mut().init();
}
pub fn for_each(&self, cb: fn (&Weak::<RefCell::<T>>) -> bool) {

View File

@ -46,8 +46,8 @@ impl Default for TimerType {
#[derive(Default)]
pub struct TimerList {
holder: TimerListRp,
wp: Rc::<TimerListRp>,
holder: Option<TimerListRp>,
wp: Option<Rc::<TimerListRp>>,
timer_entry: TimerListListHeadRp,
attach_entry: TimerListListHeadRp,
timer_type: TimerType,
@ -77,7 +77,6 @@ pub struct XTimer {
impl XTimer {
pub fn new() -> Rc::<RefCell::<Self>> {
let t: TimerListListHeadRp = Default::default();
let this = Rc::new(RefCell::new(XTimer{
free_timer_num: 0,
free_timer_list: Default::default(),
@ -104,20 +103,15 @@ impl XTimer {
self.get_tick_count = Some(get_tick_count);
self.cache_timer_num = cache_timer_num;
self.free_timer_num = 0;
self.free_timer_list = crate::ListHead::<TimerList>::new_head();
self.work_list = crate::ListHead::<TimerList>::new_head();
for i in 0..TVR_SIZE-1 {
for _ in 0..TVR_SIZE-1 {
self.tv1.push(crate::ListHead::<TimerList>::new_head());
}
for i in 0..TVN_SIZE-1 {
for _ in 0..TVN_SIZE-1 {
self.tv2.push(crate::ListHead::<TimerList>::new_head());
}
for i in 0..TVN_SIZE-1 {
self.tv3.push(crate::ListHead::<TimerList>::new_head());
}
for i in 0..TVN_SIZE-1 {
self.tv4.push(crate::ListHead::<TimerList>::new_head());
}
for i in 0..TVN_SIZE-1 {
self.tv5.push(crate::ListHead::<TimerList>::new_head());
}
match &self.get_tick_count {
@ -131,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);
@ -174,12 +168,13 @@ impl XTimer {
fn new_timer_list(&mut self) -> Rc::<RefCell::<TimerList>> {
let p: Rc::<RefCell::<TimerList>>;
if self.free_timer_list.borrow().empty() {
if !self.free_timer_list.borrow().empty() {
p = self.free_timer_list.borrow().first_entry().upgrade().unwrap();
if Rc::weak_count(&p.borrow().wp) > 0 {
p.borrow_mut().wp = Rc::new(p.borrow().holder.clone());
if Rc::weak_count(&p.borrow().wp.clone().unwrap()) > 0 {
p.borrow_mut().wp = Some(Rc::new(p.borrow().holder.clone().unwrap()));
}
} else {
println!("a");
p = Rc::new(RefCell::new(TimerList{
holder: Default::default(),
wp: Default::default(),
@ -190,12 +185,23 @@ impl XTimer {
expires: 0,
cb: None,
}));
p.borrow_mut().holder = p.clone();
p.borrow_mut().wp = Rc::new(p.borrow().holder.clone());
println!("b");
{
p.borrow_mut().holder = Some(p.clone());
}
println!("c");
{
let z = Some(Rc::new(p.borrow().holder.clone().unwrap()));
p.borrow_mut().wp = z;
}
println!("d");
p.borrow_mut().timer_entry = crate::ListHead::<TimerList>::new_node(
Rc::downgrade(&p.borrow().holder)
//Rc::downgrade(&p.borrow().holder.clone().unwrap())
Rc::downgrade(&p)
);
println!("e");
p.borrow_mut().attach_entry = crate::ListHead::<TimerList>::new_head();
println!("f");
}
return p;
}
@ -222,7 +228,9 @@ impl XTimer {
}
if !tv[index].borrow().empty() {
let mut cascade_list = crate::ListHead::<TimerList>::new_head();
tv[index].borrow_mut().replace_init(&mut cascade_list);
crate::ListHead::<TimerList>::replace_init
(&tv[index],
&cascade_list);
while !cascade_list.borrow().empty() {
let timer = &cascade_list.borrow().first_entry().upgrade().unwrap();
timer.borrow_mut().timer_entry.borrow_mut().del_init();
@ -256,7 +264,9 @@ impl XTimer {
}
self.timer_tick += 1;
self.work_list.borrow_mut().replace_init(&mut self.tv1[index]);
crate::ListHead::<TimerList>::replace_init
(&self.work_list,
&self.tv1[index]);
while !self.work_list.borrow().empty() {
let timer = &self.work_list.borrow().first_entry();
self.running_timer = timer.clone();
@ -271,10 +281,10 @@ impl XTimer {
Some(v) => {
match v.borrow().timer_type {
TimerType::Timeout => {
self.internal_delete(Rc::downgrade(&v.borrow().wp), false);
self.internal_delete(Rc::downgrade(&v.borrow().wp.clone().unwrap()), false);
}
TimerType::Interval => {
self.modify(Rc::downgrade(&v.borrow().wp),
self.modify(Rc::downgrade(&v.borrow().wp.clone().unwrap()),
v.borrow().expire_time);
}
}
@ -292,7 +302,9 @@ impl XTimer {
expire_time: i32,
cb: TimerCb,
attacher: Option<XTimerAttacherRp>) -> XTimerWp {
println!("2222");
let t = self.new_timer_list();
println!("111111");
t.borrow_mut().cb = Some(cb);
t.borrow_mut().timer_type = timer_type;
t.borrow_mut().expire_time = expire_time;
@ -306,20 +318,22 @@ impl XTimer {
}
match attacher {
Some(v) => {
v.borrow_mut().timers.borrow_mut().add_tail(&t.borrow_mut().attach_entry);
crate::ListHead::<TimerList>::add_tail
(&v.borrow().timers,
&t.borrow_mut().attach_entry);
}
None => {
}
}
self.internal_add2(&t);
return Rc::downgrade(&t.borrow().wp);
return Rc::downgrade(&t.borrow().wp.clone().unwrap());
}
fn internal_add2(&mut self, t: &Rc::<RefCell::<TimerList>>) {
let idx = t.borrow().expires - self.timer_tick;
let index;
let vec: &mut Rc::<RefCell::<crate::ListHead<TimerList>>>;
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];
@ -339,9 +353,10 @@ impl XTimer {
index = (t.borrow().expires >> (TVR_BITS + 3 * TVN_BITS)) & (TVN_MASK as i64);
vec = &mut self.tv5[index as usize];
}
vec.borrow_mut().add_tail(&t.borrow_mut().timer_entry);
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) => {
@ -361,10 +376,14 @@ impl XTimer {
None => {
}
}
if Rc::weak_count(&v.borrow().wp) > 0 {
v.borrow_mut().wp = Rc::new(v.borrow().holder.clone());
if Rc::weak_count(&v.borrow().wp.clone().unwrap()) > 0 {
v.borrow_mut().wp = Some(Rc::new(v.borrow().holder.clone().unwrap()));
}
self.free_timer_list.borrow_mut().add_tail(&v.borrow_mut().timer_entry);
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 => {
@ -422,7 +441,7 @@ impl XTimer {
pub fn delete_current_timer(&mut self) {
match self.running_timer.upgrade() {
Some(v) => {
self.internal_delete(Rc::downgrade(&v.borrow().wp), false);
self.internal_delete(Rc::downgrade(&v.borrow().wp.clone().unwrap()), false);
}
None => {
}