From 9adc663db05fd6f91d368900e9860bf31038be7d Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Mon, 9 Oct 2023 22:01:54 +0800 Subject: [PATCH] 1 --- src/timer.rs | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/src/timer.rs b/src/timer.rs index ea3b881..aa81036 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -40,7 +40,7 @@ struct TimerList { expire_time: i32, expires: i64, - cb: Option>>)>, + cb: Option>>)>>, } 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::::new_head(); } @@ -78,18 +81,32 @@ impl Timer { for i in 0..self.tv5.len() { self.tv5[i] = crate::ListHead::::new_head(); } + self.timer_tick = (self.get_tick_count)(); + /* + let cb = |e: i32, args: Option>>| { + }; + + self.set_interval( + gctime, + cb + );*/ } pub fn uninit(&mut self) { } - fn clear(&mut self) { + fn clear(&mut self, cb: Option>>)>>) { } fn new_timer_list(&mut self) -> Rc::> { let mut 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 { @@ -112,7 +129,7 @@ impl Timer { Rc::downgrade(&p.borrow().holder) ); p.borrow_mut().attach_entry = crate::ListHead::::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>>), - attacher: Option>>) -> TimerWp { + timer_type: TimerType, + expire_time: i32, + cb: Rc::>>)>, + attacher: Option>>) -> 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>>)) -> TimerWp { + cb: Rc::>>)> + ) -> 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: Rc::>>)>, attacher: Rc::>) -> 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>>)) -> TimerWp { + cb: Rc::>>)> + ) -> 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>>), + cb: Rc::>>)>, attacher: Rc::>) -> TimerWp { return self.internal_add(TimerType::Interval, expire_time, cb, Some(attacher)); }