1
This commit is contained in:
parent
0a99049c4b
commit
28274dd028
65
src/timer.rs
65
src/timer.rs
@ -12,18 +12,31 @@ const TVR_MASK: usize = TVR_SIZE - 1;
|
||||
|
||||
pub type TimerWp = Weak::<Rc::<RefCell::<TimerList>>>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct TimerAttacher {
|
||||
timers: Rc::<RefCell::<crate::ListHead<TimerAttacher>>>,
|
||||
}
|
||||
|
||||
enum TimerType {
|
||||
Timeout,
|
||||
Interval
|
||||
}
|
||||
|
||||
impl Default for TimerType {
|
||||
|
||||
fn default() -> Self
|
||||
{
|
||||
return TimerType::Timeout;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct TimerList {
|
||||
pub struct TimerList {
|
||||
holder: Rc::<RefCell::<TimerList>>,
|
||||
wp: Rc::<Rc::<RefCell::<TimerList>>>,
|
||||
timer_entry: Rc::<RefCell::<crate::ListHead<TimerList>>>,
|
||||
attach_entry: Rc::<RefCell::<crate::ListHead<TimerList>>>,
|
||||
timer_type: i8,
|
||||
timer_type: TimerType,
|
||||
expire_time: i32,
|
||||
expires: i64,
|
||||
|
||||
@ -76,19 +89,19 @@ impl Timer {
|
||||
}
|
||||
|
||||
fn new_timer_list(&mut self) -> Rc::<RefCell::<TimerList>> {
|
||||
let mut p: Rc::<RefCell::<TimerList>>;
|
||||
let 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 {
|
||||
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<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().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<Vec<Rc::<dyn Any>>>)) -> TimerWp {
|
||||
return TimerWp::new();
|
||||
cb: &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: &fn (i32, Option<Vec<Rc::<dyn Any>>>),
|
||||
attacher: Rc::<RefCell::<TimerAttacher>>) -> 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<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>>>),
|
||||
attacher: Rc::<RefCell::<TimerAttacher>>) -> TimerWp {
|
||||
return self.internal_add(TimerType::Interval, expire_time, cb, Some(attacher));
|
||||
}
|
||||
|
||||
pub fn fire_event(&mut self,
|
||||
|
Loading…
x
Reference in New Issue
Block a user