This commit is contained in:
aozhiwei 2023-10-12 08:24:05 +08:00
parent 877b9363fd
commit 805594a5fb

View File

@ -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::<Rc::<RefCell::<TimerList>>>;
pub type TimerCb = Box::<dyn FnMut (i32, Option<Vec<Rc::<dyn Any>>>)>;
pub struct TimerAttacher {
timers: Rc::<RefCell::<crate::ListHead<TimerList>>>,
@ -40,16 +40,17 @@ pub struct TimerList {
expire_time: i32,
expires: i64,
cb: Option<Rc::<dyn FnMut (i32, Option<Vec<Rc::<dyn Any>>>)>>,
cb: Option<TimerCb>,
}
pub struct Timer {
free_timer_num: i32,
free_timer_list: Rc::<RefCell::<crate::ListHead<TimerList>>>,
//running_timer: Weak::<RefCell::<TimerList>>,
running_timer: Weak::<RefCell::<TimerList>>,
timer_tick: i64,
get_tick_count: fn () -> i64,
cache_timer_num: i32,
work_list: Rc::<RefCell::<crate::ListHead<TimerList>>>,
tv1: [Rc::<RefCell::<crate::ListHead<TimerList>>>; TVR_SIZE],
tv2: [Rc::<RefCell::<crate::ListHead<TimerList>>>; TVN_SIZE],
tv3: [Rc::<RefCell::<crate::ListHead<TimerList>>>; 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::<TimerList>::new_head();
for i in 0..self.tv1.len() {
self.tv1[i] = crate::ListHead::<TimerList>::new_head();
}
@ -82,18 +84,30 @@ impl Timer {
self.tv5[i] = crate::ListHead::<TimerList>::new_head();
}
self.timer_tick = (self.get_tick_count)();
let cb = Rc::new(
|_event: i32, _args: Option<Vec<Rc::<dyn Any>>>| {
//self.gc_timer(e, args);
let self_wp = self.get_rc_refcell();
let cb = Box::new(
move |_event: i32, _args: Option<Vec<Rc::<dyn Any>>>| {
self_wp.borrow_mut().gc_timer(_event, _args);
}
);
self.set_interval(
gctime,
cb
cb,
);
}
fn get_rc_refcell(&self) -> Rc::<RefCell::<Timer>> {
//let cell_rc: &Rc<RefCell<Timer> = &self ;
//let head: &ListHead<T> = &*cell_rc.borrow();
let cell_start = 0 as *const u8;
//let data_start = head as *const ListHead<T> as *const u8;
//let cell_offset = unsafe { data_start.offset_from(cell_start) };
unsafe {
return Rc::from_raw(cell_start as *const RefCell::<Timer>);
};
}
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::<TimerList>::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::<dyn FnMut (i32, Option<Vec<Rc::<dyn Any>>>)>,
cb: TimerCb,
attacher: Option<Rc::<RefCell::<TimerAttacher>>>) -> 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::<dyn FnMut (i32, Option<Vec<Rc::<dyn Any>>>)>
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::<dyn FnMut (i32, Option<Vec<Rc::<dyn Any>>>)>,
cb: TimerCb,
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: Rc::<dyn FnMut (i32, Option<Vec<Rc::<dyn Any>>>)>
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::<dyn FnMut (i32, Option<Vec<Rc::<dyn Any>>>)>,
cb: TimerCb,
attacher: Rc::<RefCell::<TimerAttacher>>) -> 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<Vec<Rc::<dyn Any>>>) {
}
*/
}