LocalTimer

Struct LocalTimer 

Source
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>

Source

pub fn new(frame_time: u64, now: u64) -> Self

Create a wheel to support four rounds.

  • frame_time 最小时间间隔 - 单位毫秒
  • now 当前绝对时间
  • tip frame_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**32
Source

pub fn check_sleep(&self, now: u64) -> u64

检查可休眠时间

  • now 当前线程时间 <与创建时设置的 now 属于同一时间进度>
Source

pub fn len(&self) -> usize

总任务数量

Source

pub fn get_time(&mut self) -> u64

当前运行时长

Source

pub fn insert(&mut self, data: T, timeout: u64) -> usize

插入元素

  • data 目标数据
  • timeout 延时时间 - 单位毫秒
Source

pub fn clear(&mut self)

clear all elem

Source

pub fn pop(&mut self, now: u64) -> Option<(TimeoutItem<T>, usize)>

弹出一个超时任务 - 当没有任务弹出,经过检查需要做一次滚动则进行一次滚动 - 当没有任务弹出,外部可以检查睡眠时间

  • now 当前时间
Source

pub fn try_remove(&mut self, index: usize) -> Option<TimeoutItem<T>>

移除一个任务

  • index 任务插入时返回的 index
Source

pub fn remove(&mut self, index: usize) -> Option<TimeoutItem<T>>

Panics if index is out of bounds. 移除一个任务

  • index 任务插入时返回的 index
Source

pub fn check(&self, now: u64) -> bool

检查是否应该做一次滚动 - 内部使用

  • now 当前线程时间 <与创建时设置的 now 属于同一时间进度>
Source

pub fn roll_once(&mut self)

滚动一次 - 使用者不调用

Source

pub fn get_item_timeout(&self, item: &TimeoutItem<T>) -> u64

Trait Implementations§

Source§

impl<T, const N1: usize, const N2: usize, const N3: usize, const N4: usize> Debug for LocalTimer<T, N1, N2, N3, N4>
where T: Debug,

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> FResult

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.