diff --git a/src/timer.rs b/src/timer.rs index 2ed8693..33461bd 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -2,7 +2,6 @@ use std::rc::{Rc, Weak}; use std::cell::RefCell; use std::any::Any; -//const CONFIG_BASE_SMALL: bool = false; const TVN_BITS: usize = 6; const TVR_BITS: usize = 8; const TVN_SIZE: usize = 1 << TVN_BITS; @@ -11,6 +10,7 @@ const TVN_MASK: usize = TVN_SIZE - 1; const TVR_MASK: usize = TVR_SIZE - 1; pub type TimerWp = Weak::>>; +pub type TimerCb = Box::>>)>; pub struct TimerAttacher { timers: Rc::>>, @@ -40,16 +40,17 @@ pub struct TimerList { expire_time: i32, expires: i64, - cb: Option>>)>>, + cb: Option, } pub struct Timer { free_timer_num: i32, free_timer_list: Rc::>>, - //running_timer: Weak::>, + running_timer: Weak::>, timer_tick: i64, get_tick_count: fn () -> i64, cache_timer_num: i32, + work_list: Rc::>>, tv1: [Rc::>>; TVR_SIZE], tv2: [Rc::>>; TVN_SIZE], tv3: [Rc::>>; TVN_SIZE], @@ -59,13 +60,14 @@ pub struct Timer { impl Timer { - pub fn init(&mut self, + pub fn init(mut self, 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; + self.work_list = crate::ListHead::::new_head(); for i in 0..self.tv1.len() { self.tv1[i] = crate::ListHead::::new_head(); } @@ -82,18 +84,30 @@ impl Timer { self.tv5[i] = crate::ListHead::::new_head(); } self.timer_tick = (self.get_tick_count)(); - let cb = Rc::new( - |_event: i32, _args: Option>>| { - //self.gc_timer(e, args); + let self_wp = self.get_rc_refcell(); + let cb = Box::new( + move |_event: i32, _args: Option>>| { + self_wp.borrow_mut().gc_timer(_event, _args); } ); self.set_interval( gctime, - cb + cb, ); } + fn get_rc_refcell(&self) -> Rc::> { + //let cell_rc: &Rc = &self ; + //let head: &ListHead = &*cell_rc.borrow(); + let cell_start = 0 as *const u8; + //let data_start = head as *const ListHead as *const u8; + //let cell_offset = unsafe { data_start.offset_from(cell_start) }; + unsafe { + return Rc::from_raw(cell_start as *const RefCell::); + }; + } + pub fn uninit(&mut self) { self.clear(); } @@ -131,13 +145,37 @@ impl Timer { } pub fn update(&mut self) { + while (self.get_tick_count)() >= self.timer_tick { + let index = (self.timer_tick & (TVR_MASK as i64)) as usize; + if index == 0 { + } + self.timer_tick += 1; + + crate::ListHead::::replace_init + ( + &mut self.work_list, + &mut self.tv1[index] + ); + while !self.work_list.borrow().empty() { + let timer = &self.work_list.borrow().first_entry(); + self.running_timer = timer.clone(); + match &timer.upgrade().unwrap().borrow_mut().cb { + Some(v) => { + //(*v)(0, None); + } + None => { + + } + } + } + } } fn internal_add(&mut self, timer_type: TimerType, expire_time: i32, - cb: Rc::>>)>, + cb: TimerCb, attacher: Option>>) -> TimerWp { let t = self.new_timer_list(); t.borrow_mut().cb = Some(cb); @@ -184,28 +222,28 @@ impl Timer { pub fn set_timeout(&mut self, expire_time: i32, - cb: Rc::>>)> + cb: TimerCb ) -> TimerWp { return self.internal_add(TimerType::Timeout, expire_time, cb, None); } pub fn set_timeout_ex(&mut self, expire_time: i32, - cb: Rc::>>)>, + cb: TimerCb, attacher: Rc::>) -> TimerWp { return self.internal_add(TimerType::Timeout, expire_time, cb, Some(attacher)); } pub fn set_interval(&mut self, expire_time: i32, - cb: Rc::>>)> + cb: TimerCb ) -> TimerWp { return self.internal_add(TimerType::Interval, expire_time, cb, None); } pub fn set_interval_ex(&mut self, expire_time: i32, - cb: Rc::>>)>, + cb: TimerCb, attacher: Rc::>) -> TimerWp { return self.internal_add(TimerType::Interval, expire_time, cb, Some(attacher)); } @@ -239,9 +277,8 @@ impl Timer { return 0; } - /* fn gc_timer(&mut self, _e: i32, _args: Option>>) { } - */ + }