Syncopate
A hierarchical, power-aware task scheduler for Rust applications requiring precise timing control.
Overview
Syncopate provides a flexible scheduler for managing periodic tasks with configurable execution windows. It's designed for applications that need:
- Deterministic timing: Schedule tasks to run at specific intervals
- Execution windows: Define acceptable time ranges for task execution (early/on-time/late detection)
- Power efficiency: Idle durations calculated to minimize CPU wakeups
- Virtual time: Simulated clock for deterministic testing
- Flexible scheduling: Fixed-rate, fixed-delay, one-shot, and wall-clock-anchored tasks
Quick Start
Add syncopate to your Cargo.toml:
[]
= "0.0.1"
Examples
Basic Usage
use Duration;
use ;
Deterministic Testing with SimClock
use Rc;
use Duration;
use ;
let clock = new;
let mut scheduler = new_with_clock;
let task = every
.name
.build
.unwrap;
scheduler.add_task.unwrap;
// Immediate fire at t=0
let result = scheduler.tick;
assert_eq!;
assert_eq!;
// Advance to next deadline
clock.advance;
let result = scheduler.tick;
assert_eq!;
Wall-Clock-Anchored Tasks
use Duration;
use ;
let mut scheduler = new;
// Fires on absolute wall-clock boundaries (e.g. every second at :00, :01, :02...)
let task = every_absolute
.window
.name
.build
.unwrap;
scheduler.add_task.unwrap;
// With offset: fires at 500ms past each second boundary
let task = every_absolute
.offset
.window
.name
.build
.unwrap;
scheduler.add_task.unwrap;
One-Shot Tasks
use Duration;
use ;
let mut scheduler = new;
// Fire once after 5 seconds
let task = once_after
.window
.name
.build
.unwrap;
scheduler.add_task.unwrap;
Core Concepts
Task Types
- Relative (
TaskBuilder::every): Period measured from last fire. SupportsFixedRate(grid-aligned) andFixedDelay(drift-accumulating) schedules. - Absolute (
TaskBuilder::every_absolute): Fires on wall-clock boundaries. Handles clock jumps (suspend/resume) gracefully. - One-shot (
TaskBuilder::once_after,TaskBuilder::once_at): Single-fire tasks that are evicted after execution.
Execution Windows
Tasks define a Window with early and late tolerances:
- Early: Task fires before the ideal deadline (within tolerance)
- On-Time: Task fires exactly at the deadline
- Late: Task fires after the deadline (within tolerance)
- Missed: Task could not fire within the window
Missed Tick Behavior
When a task misses its window (FixedRate only):
Skip(default): Report missed deadlines, advance to current positionRunLatest: Fire for the most recent deadline, report earlier ones as missedBurst { max }: Fire multiple missed deadlines in rapid succession
Repeat Control
Repeat::Forever(default): Task runs indefinitelyRepeat::Times(n): Task fires exactlyntimes, then is evicted
Timer Delay Compensation
set_timer_delay() compensates for consistent OS timer overhead by waking slightly early.
License
MIT OR Apache-2.0