pub struct LocalTimer<T, const N1: usize, const N2: usize, const N3: usize, const N4: usize> {
pub frame_time: u64,
pub start_time: u64,
pub roll_time: u64,
pub index_factory: SlabFactory<usize, ()>,
pub frame_wheel: FrameWheel<T, N1, N2, N3, N4>,
}Expand description
任务轮实现的延时任务管理
T延时任务的数据类型N1一级粒度轮的槽位数 <任务轮分4级粒度>N2二级粒度轮的槽位数 <任务轮分4级粒度>N3三级粒度轮的槽位数 <任务轮分4级粒度>N4四级粒度轮的槽位数 <任务轮分4级粒度>- N1 到 N4 依次减少, 总数不应超过 usize 范围
§Example
let mut timer = LocalTimer::<i32, 100, 60, 60, 24>::new(10, run_millis());
// 插入一个任务 - 数据为 55, 延时为 1000ms
let timeout_handler = timer.insert(Item::new(55 as i32, 1000 as u64));
// 尝试获取一个超时任务
let one_task_option = timer.pop();
// 当获取到的任务为空,可以检查可休眠时间
let sleep_time = timer.check_sleep(now);
// 可移除一个任务
let item_option = timer.try_remove(timeout_handler);Fields§
§frame_time: u64最小粒度世界线的帧间隔
start_time: u64启动时间
roll_time: u64滚动的累积时间
index_factory: SlabFactory<usize, ()>Index 工厂
frame_wheel: FrameWheel<T, N1, N2, N3, N4>帧进度的任务轮
Implementations§
Source§impl<T, const N1: usize, const N2: usize, const N3: usize, const N4: usize> LocalTimer<T, N1, N2, N3, N4>
impl<T, const N1: usize, const N2: usize, const N3: usize, const N4: usize> LocalTimer<T, N1, N2, N3, N4>
Sourcepub fn new(frame_time: u64, now: u64) -> Self
pub fn new(frame_time: u64, now: u64) -> Self
Create a wheel to support four rounds.
frame_time最小时间间隔 - 单位毫秒now当前绝对时间tipframe_time 与 N1..N4 之积 的乘积不应超过 uszie 范围
§Error Example
// 当 usize 最大 2**32, 则下面的创建不保证运行正确,且可能崩溃
let mut timer = WheelTimer::<i32, 100, 60, 60, 24>::new(1000, run_millis()); // 100*60*60*24*1000 = 8640000000 > 2**32Sourcepub fn check_sleep(&self, now: u64) -> u64
pub fn check_sleep(&self, now: u64) -> u64
检查可休眠时间
- now 当前线程时间 <与创建时设置的 now 属于同一时间进度>
Sourcepub fn pop(&mut self, now: u64) -> Option<(TimeoutItem<T>, usize)>
pub fn pop(&mut self, now: u64) -> Option<(TimeoutItem<T>, usize)>
弹出一个超时任务 - 当没有任务弹出,经过检查需要做一次滚动则进行一次滚动 - 当没有任务弹出,外部可以检查睡眠时间
now当前时间
Sourcepub fn try_remove(&mut self, index: usize) -> Option<TimeoutItem<T>>
pub fn try_remove(&mut self, index: usize) -> Option<TimeoutItem<T>>
移除一个任务
index任务插入时返回的 index
Sourcepub fn remove(&mut self, index: usize) -> Option<TimeoutItem<T>>
pub fn remove(&mut self, index: usize) -> Option<TimeoutItem<T>>
Panics if index is out of bounds. 移除一个任务
index任务插入时返回的 index
pub fn get_item_timeout(&self, item: &TimeoutItem<T>) -> u64
Trait Implementations§
Auto Trait Implementations§
impl<T, const N1: usize, const N2: usize, const N3: usize, const N4: usize> Freeze for LocalTimer<T, N1, N2, N3, N4>
impl<T, const N1: usize, const N2: usize, const N3: usize, const N4: usize> RefUnwindSafe for LocalTimer<T, N1, N2, N3, N4>where
T: RefUnwindSafe,
impl<T, const N1: usize, const N2: usize, const N3: usize, const N4: usize> Send for LocalTimer<T, N1, N2, N3, N4>where
T: Send,
impl<T, const N1: usize, const N2: usize, const N3: usize, const N4: usize> Sync for LocalTimer<T, N1, N2, N3, N4>where
T: Sync,
impl<T, const N1: usize, const N2: usize, const N3: usize, const N4: usize> Unpin for LocalTimer<T, N1, N2, N3, N4>where
T: Unpin,
impl<T, const N1: usize, const N2: usize, const N3: usize, const N4: usize> UnwindSafe for LocalTimer<T, N1, N2, N3, N4>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more