From 28274dd0283e6f3477d49752005ef3ea5894cb9a Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Mon, 9 Oct 2023 10:35:19 +0800 Subject: [PATCH] 1 --- src/timer.rs | 65 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 13 deletions(-) diff --git a/src/timer.rs b/src/timer.rs index 8cb781c..e864cbd 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -12,18 +12,31 @@ const TVR_MASK: usize = TVR_SIZE - 1; pub type TimerWp = Weak::>>; -#[derive(Default)] pub struct TimerAttacher { timers: Rc::>>, } +enum TimerType { + Timeout, + Interval +} + +impl Default for TimerType { + + fn default() -> Self + { + return TimerType::Timeout; + } + +} + #[derive(Default)] -struct TimerList { +pub struct TimerList { holder: Rc::>, wp: Rc::>>, timer_entry: Rc::>>, attach_entry: Rc::>>, - timer_type: i8, + timer_type: TimerType, expire_time: i32, expires: i64, @@ -76,19 +89,19 @@ impl Timer { } fn new_timer_list(&mut self) -> Rc::> { - let mut p: Rc::>; + let p: Rc::>; + 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 { - p.borrow_mut().wp = Rc::new(p.borrow().holder.clone()); - } } else { p = Rc::new(RefCell::new(TimerList{ holder: Default::default(), wp: Default::default(), timer_entry: Default::default(), attach_entry: Default::default(), - timer_type: 0, + timer_type: TimerType::Timeout, expire_time: 0, expires: 0, cb: None, @@ -107,17 +120,43 @@ impl Timer { } + fn internal_add(&mut self, + timer_type: TimerType, + expire_time: i32, + cb: &fn (i32, Option>>), + attacher: Option>>) -> TimerWp { + let t = self.new_timer_list(); + t.borrow_mut().cb = Some(*cb); + t.borrow_mut().timer_type = timer_type; + t.borrow_mut().expire_time = expire_time; + t.borrow_mut().expires = expire_time as i64; + return Rc::downgrade(&t.borrow().wp); + } + pub fn set_timeout(&mut self, expire_time: i32, - cb: fn (i32, Option>>)) -> TimerWp { - return TimerWp::new(); + cb: &fn (i32, Option>>)) -> 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>>), + cb: &fn (i32, Option>>), attacher: Rc::>) -> TimerWp { - return TimerWp::new(); + return self.internal_add(TimerType::Timeout, expire_time, cb, Some(attacher)); + } + + pub fn set_interval(&mut self, + expire_time: i32, + cb: &fn (i32, Option>>)) -> 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>>), + attacher: Rc::>) -> TimerWp { + return self.internal_add(TimerType::Interval, expire_time, cb, Some(attacher)); } pub fn fire_event(&mut self,