#timer #events #arceos

no-std timer_list

A list of timed events that will be triggered sequentially when the timer expires

1 unstable release

0.1.0 Jul 17, 2024

#596 in Data structures

Download history 578/week @ 2024-07-29 216/week @ 2024-08-05 214/week @ 2024-08-12 102/week @ 2024-08-19 189/week @ 2024-08-26 439/week @ 2024-09-02 424/week @ 2024-09-09 265/week @ 2024-09-16 369/week @ 2024-09-23 114/week @ 2024-09-30 622/week @ 2024-10-07 1134/week @ 2024-10-14 713/week @ 2024-10-21 339/week @ 2024-10-28 62/week @ 2024-11-04 284/week @ 2024-11-11

1,494 downloads per month

GPL-3.0-or-later OR Apache-2…

9KB
152 lines

timer_list

Crates.io Docs.rs CI

A list of timed events that will be triggered sequentially when the timer expires.

Examples

use timer_list::{TimerEvent, TimerEventFn, TimerList};
use std::time::{Duration, Instant};

let mut timer_list = TimerList::new();

// set a timer that will be triggered after 1 second
let start_time = Instant::now();
timer_list.set(Duration::from_secs(1), TimerEventFn::new(|now| {
    println!("timer event after {:?}", now);
}));

while !timer_list.is_empty() {
    // check if there is any event that is expired
    let now = Instant::now().duration_since(start_time);
    if let Some((deadline, event)) = timer_list.expire_one(now) {
        // trigger the event, will print "timer event after 1.00s"
        event.callback(now);
        break;
    }
    std::thread::sleep(Duration::from_millis(10)); // relax the CPU
}

No runtime deps