Skip to main content

Module task

Module task 

Source
Expand description

Periodic task scheduler built on tokio::time::interval.

The C engine ships a custom red-black-tree-backed task manager (schedule_task_1, time_to_next_task, execute_expired_tasks) that fires one-shot timeouts from the main loop. The Rust port delegates timer wheel maintenance to the tokio runtime: callers register a periodic callback through task_register and receive a TaskHandle that cancels the underlying tokio task when dropped or explicitly cancelled.

A one-shot variant, task_schedule_once, is provided for parity with schedule_task_1. The runtime drives both APIs, so the per-iteration time_to_next_task and execute_expired_tasks helpers from the C reference have no Rust counterpart; tokio’s reactor performs the equivalent work transparently.

§Examples

use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;
use dynomite::core::task::task_register;

let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
    let counter = Arc::new(AtomicUsize::new(0));
    let c = counter.clone();
    let handle = task_register(Duration::from_millis(5), Arc::new(move || {
        c.fetch_add(1, Ordering::Relaxed);
    }));
    tokio::time::sleep(Duration::from_millis(40)).await;
    handle.cancel();
    assert!(counter.load(Ordering::Relaxed) >= 1);
});

Structs§

TaskHandle
A handle that cancels a registered task.

Functions§

task_register
Register a periodic task that fires its callback every period.
task_schedule_once
Register a one-shot task that fires once after delay elapses.