1
This commit is contained in:
parent
f246e59d3e
commit
26edaf0cf6
@ -63,9 +63,10 @@ pub struct XTimer {
|
||||
free_timer_list: TimerListListHeadRp,
|
||||
running_timer: TimerListWp,
|
||||
timer_tick: i64,
|
||||
get_tick_count: Box<dyn Fn () -> i64>,
|
||||
get_tick_count: Option<Box<dyn Fn () -> i64>>,
|
||||
cache_timer_num: i32,
|
||||
work_list: TimerListListHeadRp,
|
||||
tv0: [Option<TimerListListHeadRp>; TVR_SIZE],
|
||||
tv1: [TimerListListHeadRp; TVR_SIZE],
|
||||
tv2: [TimerListListHeadRp; TVN_SIZE],
|
||||
tv3: [TimerListListHeadRp; TVN_SIZE],
|
||||
@ -76,18 +77,48 @@ pub struct XTimer {
|
||||
|
||||
impl XTimer {
|
||||
|
||||
pub fn init(mut self,
|
||||
/*
|
||||
pub fn new() -> Rc::<RefCell::<Self>> {
|
||||
let t: TimerListListHeadRp = Default::default();
|
||||
let this = Rc::new(RefCell::new(XTimer{
|
||||
free_timer_num: 0,
|
||||
free_timer_list: Default::default(),
|
||||
running_timer: Default::default(),
|
||||
timer_tick: Default::default(),
|
||||
get_tick_count: Default::default(),
|
||||
cache_timer_num: 0,
|
||||
work_list: Default::default(),
|
||||
tv0: [None; TVR_SIZE],
|
||||
tv1: [t.clone(); TVR_SIZE],
|
||||
tv2: [Default::default(); TVN_SIZE],
|
||||
tv3: [Default::default(); TVN_SIZE],
|
||||
tv4: [Default::default(); TVN_SIZE],
|
||||
tv5: [Default::default(); TVN_SIZE],
|
||||
_self_wp: Default::default(),
|
||||
}));
|
||||
this.borrow_mut()._self_wp = Rc::downgrade(&this);
|
||||
return this;
|
||||
}*/
|
||||
|
||||
pub fn init(&mut self,
|
||||
get_tick_count: Box<dyn Fn () -> i64>,
|
||||
gctime: i32,
|
||||
cache_timer_num: i32) {
|
||||
self.get_tick_count = get_tick_count;
|
||||
self.get_tick_count = Some(get_tick_count);
|
||||
self.cache_timer_num = cache_timer_num;
|
||||
self.free_timer_num = 0;
|
||||
self.mut_traverse_timer_list(
|
||||
&mut |timer_list: &mut TimerListListHeadRp| {
|
||||
*timer_list = crate::ListHead::<TimerList>::new_head();
|
||||
});
|
||||
self.timer_tick = (self.get_tick_count)();
|
||||
match &self.get_tick_count {
|
||||
Some(v) => {
|
||||
self.timer_tick = (*v)();
|
||||
}
|
||||
None => {
|
||||
|
||||
}
|
||||
}
|
||||
{
|
||||
let self_wp = self.shared_from_self();
|
||||
let cb = Box::new(
|
||||
@ -212,7 +243,15 @@ impl XTimer {
|
||||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
let tick = (self.get_tick_count)();
|
||||
let mut tick = 0;
|
||||
match &self.get_tick_count {
|
||||
Some(v) => {
|
||||
tick = (*v)();
|
||||
}
|
||||
None => {
|
||||
|
||||
}
|
||||
}
|
||||
while tick >= self.timer_tick {
|
||||
let index = (self.timer_tick & (TVR_MASK as i64)) as usize;
|
||||
if index == 0 &&
|
||||
@ -263,7 +302,14 @@ impl XTimer {
|
||||
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;
|
||||
match &self.get_tick_count {
|
||||
Some(v) => {
|
||||
t.borrow_mut().expires = (*v)() + expire_time as i64;
|
||||
}
|
||||
None => {
|
||||
|
||||
}
|
||||
}
|
||||
match attacher {
|
||||
Some(v) => {
|
||||
v.borrow_mut().timers.borrow_mut().add_tail(&t.borrow_mut().attach_entry);
|
||||
@ -413,8 +459,15 @@ impl XTimer {
|
||||
pub fn get_remain_time(&mut self, timer_wp: XTimerWp) -> i64 {
|
||||
match timer_wp.upgrade() {
|
||||
Some(t) => {
|
||||
let remain_time = t.borrow().expires - (self.get_tick_count)();
|
||||
return cmp::max(remain_time, 0);
|
||||
match &self.get_tick_count {
|
||||
Some(v) => {
|
||||
let remain_time = t.borrow().expires - (*v)();
|
||||
return cmp::max(remain_time, 0);
|
||||
}
|
||||
None => {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
return 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user