Crate naive_timer[][src]

Expand description

A minimal naive timer for embedded (no_std) platforms.

Example: single thread

use alloc::sync::Arc;
use core::time::Duration;
use core::sync::atomic::{AtomicBool, Ordering};
use naive_timer::Timer;
extern crate alloc;

let mut timer = Timer::default();
let event = Arc::new(AtomicBool::new(false));

timer.add(Duration::from_secs(1), {
    let event = event.clone();
    move |now| event.store(true, Ordering::SeqCst)
});

timer.expire(Duration::from_millis(999));
assert_eq!(event.load(Ordering::SeqCst), false);
assert_eq!(timer.next(), Some(Duration::from_secs(1)));

timer.expire(Duration::from_millis(1000));
assert_eq!(event.load(Ordering::SeqCst), true);
assert_eq!(timer.next(), None);

Example: ticks and wakeup

use alloc::sync::Arc;
use core::time::Duration;
use core::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
use naive_timer::Timer;
extern crate alloc;

/// Get current time in `Duration`.
fn now() -> Duration {
    SystemTime::now().duration_since(UNIX_EPOCH).unwrap()
}

let mut timer = Timer::default();

// add timer to wake me up
let thread = std::thread::current();
timer.add(now() + Duration::from_millis(25), move |_| thread.unpark());

// generate ticks (5 times per 10ms)
// spawn a thread to emulate timer interrupt
let handle = std::thread::spawn(move || {
    for _ in 0..5 {
        std::thread::sleep(Duration::from_millis(10));
        timer.expire(now());
    }
});

// wait for wakeup
let t0 = now();
std::thread::park();
let sleep_time = now() - t0;
assert!(sleep_time > Duration::from_millis(30));
assert!(sleep_time < Duration::from_millis(40));

// join thread
handle.join().unwrap();

Limitations

For simplicity, timer cancellation is not supported.

The callback function should check the current time now and its own information, to decide whether it is still a valid event.

Structs

A naive timer.