This commit is contained in:
aozhiwei 2023-10-09 22:01:54 +08:00
parent d1a75de683
commit 9adc663db0

View File

@ -40,7 +40,7 @@ struct TimerList {
expire_time: i32,
expires: i64,
cb: Option<fn (i32, Option<Vec<Rc::<dyn Any>>>)>,
cb: Option<Rc::<dyn Fn (i32, Option<Vec<Rc::<dyn Any>>>)>>,
}
pub struct Timer {
@ -63,6 +63,9 @@ impl Timer {
get_tick_count: fn () -> i64,
gctime: i32,
cache_timer_num: i32) {
self.get_tick_count = get_tick_count;
self.cache_timer_num = cache_timer_num;
self.free_timer_num = 0;
for i in 0..self.tv1.len() {
self.tv1[i] = crate::ListHead::<TimerList>::new_head();
}
@ -78,18 +81,32 @@ impl Timer {
for i in 0..self.tv5.len() {
self.tv5[i] = crate::ListHead::<TimerList>::new_head();
}
self.timer_tick = (self.get_tick_count)();
/*
let cb = |e: i32, args: Option<Vec<Rc::<dyn Any>>>| {
};
self.set_interval(
gctime,
cb
);*/
}
pub fn uninit(&mut self) {
}
fn clear(&mut self) {
fn clear(&mut self, cb: Option<Rc::<dyn Fn (i32, Option<Vec<Rc::<dyn Any>>>)>>) {
}
fn new_timer_list(&mut self) -> Rc::<RefCell::<TimerList>> {
let mut p: Rc::<RefCell::<TimerList>>;
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 self.free_timer_list.borrow().empty() {
p = self.free_timer_list.borrow().first_entry().upgrade().unwrap();
if Rc::weak_count(&p.borrow().wp) > 0 {
@ -112,7 +129,7 @@ impl Timer {
Rc::downgrade(&p.borrow().holder)
);
p.borrow_mut().attach_entry = crate::ListHead::<TimerList>::new_head();
}
}*/
return p;
}
@ -121,12 +138,12 @@ impl Timer {
}
fn internal_add(&mut self,
timer_type: TimerType,
expire_time: i32,
cb: &fn (i32, Option<Vec<Rc::<dyn Any>>>),
attacher: Option<Rc::<RefCell::<TimerAttacher>>>) -> TimerWp {
timer_type: TimerType,
expire_time: i32,
cb: Rc::<dyn Fn (i32, Option<Vec<Rc::<dyn Any>>>)>,
attacher: Option<Rc::<RefCell::<TimerAttacher>>>) -> TimerWp {
let t = self.new_timer_list();
t.borrow_mut().cb = Some(*cb);
t.borrow_mut().cb = Some(cb);
t.borrow_mut().timer_type = timer_type;
t.borrow_mut().expire_time = expire_time;
t.borrow_mut().expires = (self.get_tick_count)() + expire_time as i64;
@ -139,31 +156,36 @@ impl Timer {
}
}
{
let idx = t.borrow().expires - self.timer_tick;
}
return Rc::downgrade(&t.borrow().wp);
}
pub fn set_timeout(&mut self,
expire_time: i32,
cb: &fn (i32, Option<Vec<Rc::<dyn Any>>>)) -> TimerWp {
cb: Rc::<dyn Fn (i32, Option<Vec<Rc::<dyn Any>>>)>
) -> TimerWp {
return self.internal_add(TimerType::Timeout, expire_time, cb, None);
}
pub fn set_timeout_ex(&mut self,
expire_time: i32,
cb: &fn (i32, Option<Vec<Rc::<dyn Any>>>),
cb: Rc::<dyn Fn (i32, Option<Vec<Rc::<dyn Any>>>)>,
attacher: Rc::<RefCell::<TimerAttacher>>) -> TimerWp {
return self.internal_add(TimerType::Timeout, expire_time, cb, Some(attacher));
}
pub fn set_interval(&mut self,
expire_time: i32,
cb: &fn (i32, Option<Vec<Rc::<dyn Any>>>)) -> TimerWp {
cb: Rc::<dyn Fn (i32, Option<Vec<Rc::<dyn Any>>>)>
) -> TimerWp {
return self.internal_add(TimerType::Interval, expire_time, cb, None);
}
pub fn set_interval_ex(&mut self,
expire_time: i32,
cb: &fn (i32, Option<Vec<Rc::<dyn Any>>>),
cb: Rc::<dyn Fn (i32, Option<Vec<Rc::<dyn Any>>>)>,
attacher: Rc::<RefCell::<TimerAttacher>>) -> TimerWp {
return self.internal_add(TimerType::Interval, expire_time, cb, Some(attacher));
}