Struct TimerQueue

Source
pub struct TimerQueue(/* private fields */);
Expand description

TimerQueue manages scheduling of timers. Timer can be created from TimerQueue only.

Implementation of TimerQueue in Windows OS uses Win32 API Timer Queues.

In Unix platforms, TimerQueue use POSIX timer_create API to schedule signal in a dedicated thread and dispatch task executions depending on the task’s hint. That is, for any quick function handler, it will be called from another common, dedicated thread. For the slow function handler, each execution will be on its own thread.

TimerQueue has a default queue which can be used right away. But if you need to have another set of working threads, you can use TimerQueue::new too.

§Example

use native_timer::TimerQueue;

let mut count = 0;
let default_queue = TimerQueue::default();  // you can use TimerQueue::new() too.
let t = default_queue.schedule_timer(Duration::from_millis(100), Duration::default(), 
                                     None, || count += 1);
sleep(Duration::from_millis(200));
drop(t);
assert_eq!(count, 1);

Implementations§

Source§

impl TimerQueue

Source

pub fn new() -> Self

Create a new TimerQueue

Source

pub fn default() -> &'static TimerQueue

Default OS common timer queue

Source

pub fn schedule_timer<'h, F>( &self, due: Duration, period: Duration, hint: Option<CallbackHint>, handler: F, ) -> Result<Timer<'h>>
where F: FnMut() + Send + 'h,

Schedule a timer, either a one-shot timer, or a periodical timer.

§Arguments
  • due: Due time to execute the task
  • period: Time interval to repeat the task
  • hint: Behavior hint of handler, which impacts how the task will be scheduled
  • handler: The task to be called back

returns: Result<Timer, TimerError>

§Examples
§Single-shot timer
use native_timer::TimerQueue;

let my_queue = TimerQueue::new();
let mut called = 0;
let timer = my_queue.schedule_timer(Duration::from_millis(400), Duration::ZERO,
                                    None, || called += 1).unwrap();
thread::sleep(Duration::from_secs(1));
drop(timer);
assert_eq!(called, 1);
§Periodical timer
use native_timer::TimerQueue;

let my_queue = TimerQueue::new();
let mut called = 0;
let duration = Duration::from_millis(300);
let t = my_queue.schedule_timer(duration, duration, None, || called += 1).unwrap();
thread::sleep(Duration::from_secs(1));
drop(t);
assert_eq!(called, 3);
Source

pub fn schedule_oneshot<'h, F>( &self, due: Duration, hint: Option<CallbackHint>, handler: F, ) -> Result<Timer<'h>>
where F: FnOnce() + Send + 'h,

Schedule an one-shot timer. This is different from schedule_timer that this function’s handler is FnOnce. Parameter period is also not needed here.

§Arguments
  • due: Due time to execute the task
  • hint: Behavior hint of handler, which impacts how the task will be scheduled
  • handler: The task to be called back

returns: Result<Timer, TimerError>

§Examples
use native_timer::TimerQueue;

let my_queue = TimerQueue::new();
let mut called = 0;
let timer = my_queue.schedule_oneshot(Duration::from_millis(400), None,
                                   || called += 1).unwrap();
thread::sleep(Duration::from_secs(1));
drop(timer);
assert_eq!(called, 1);
Source

pub fn fire_oneshot<F>( &self, due: Duration, hint: Option<CallbackHint>, handler: F, ) -> Result<()>
where F: FnOnce() + Send + 'static,

Schedule an one-shot of the task. Note that this fire_oneshot requires a 'static lifetime closure.

§Arguments
  • due: Due time to execute the task
  • hint: Behavior hint of handler, which impacts how the task will be scheduled
  • handler: The task to be called back

returns: Result<(), TimerError>

§Examples

This example creates a new TimerQueue to show that the lifetime of the queue is irrelevant. But in actual usage, you can simply use TimerQueue::default.

use native_timer::TimerQueue;

let flag = Arc::new(AtomicBool::new(false));
let flag_writer = flag.clone();
TimerQueue::new().fire_oneshot(Duration::from_millis(100), 
                                   None, move || flag_writer.store(true, Ordering::SeqCst)).unwrap();
thread::sleep(Duration::from_millis(200));
assert!(flag.load(Ordering::SeqCst));

Auto Trait Implementations§

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.