Module scheduler
Expand description
See scheduler for the system contract and examples.
Bounded tick deadlines with deterministic FIFO ties and explicit work limits.
IDs belong to their scheduler; tick values are monotonic absolute host ticks.
TickScheduler::drain_ready_iter processes tasks without allocating an output
collection. TickScheduler::drain_ready_into reuses a caller’s buffer without
growing it. Both leave excess ready work queued. The tree may release internal
storage while draining; scheduling and rescheduling may allocate tree nodes.
Owner scopes are opt-in via TickScheduler::with_scope_capacity. Cancelling
a scope invalidates its handle and removes only its queued jobs. A handle is
tied to its issuing scheduler even after that scheduler is moved or dropped.
Dropping a handle does not cancel anything; the host owns lifecycle decisions.
Deadlines use ordinary numeric u64 order, not wrapping arithmetic. Cancel
old timeline scopes before resetting/wrapping the clock and discard retained
tick batches; explicitly handle any unscoped jobs on that timeline as well.
§Delayed respawn on actual absolute ticks
This headless recipe executes task 201 contexts, including a zero-tick frame, catch-up, manual stepping and a round restart. A local multiplayer menu must suppress commands rather than pause this authoritative session.
use ferravex_engine::{scheduler::TickScheduler, session::GameSession};
let mut session = GameSession::new(64.0, 4, true);
let mut jobs = TickScheduler::with_scope_capacity(8, 1);
let round = jobs.create_scope().unwrap();
let mut alive = true;
let mut respawns = 0;
let mut executed = 0;
assert_eq!(session.advance_batch(0.5 / 64.0).ticks().count(), 0);
for context in session.advance_batch(3.0 / 64.0).ticks() {
executed += 1;
if context.tick == 1 {
alive = false; // Accepted death event, not a render-frame request.
let deadline = context.tick.checked_add(2).expect("restart before wrap");
jobs.schedule_scoped(&round, deadline, "respawn").unwrap();
}
for event in jobs.drain_ready_iter(context.tick, 8) {
assert_eq!((context.tick, event), (3, "respawn"));
alive = true;
respawns += 1; // Feedback can follow this accepted spawn.
}
}
assert!(alive);
assert_eq!((executed, respawns), (3, 1));
session.set_paused(true);
jobs.schedule_scoped(&round, 4, "respawn").unwrap();
for context in session.step_paused_batch().unwrap().ticks() {
assert_eq!(jobs.drain_ready(context.tick, 8), ["respawn"]);
}
jobs.schedule_scoped(&round, 100, "old round").unwrap();
assert_eq!(jobs.cancel_scope(&round), Some(1));
session.restart(); // Discard any retained batches and old tick deadlines.
let next_round = jobs.create_scope().unwrap();
assert_ne!(round, next_round);
assert_eq!(jobs.schedule_scoped(&round, 1, "stale"), Err("stale"));
assert_eq!(jobs.cancel_scope(&round), None);
jobs.schedule_scoped(&next_round, 1, "new round").unwrap();
for context in session.advance_batch(1.0 / 64.0).ticks() {
assert_eq!(jobs.drain_ready(context.tick, 8), ["new round"]);
}Structs§
- ScopeId
- A generation-checked lifecycle handle issued by one
TickScheduler. - TaskId
- A task’s identity and original insertion order within its scheduler.
- Tick
Scheduler - A bounded deadline queue ordered by tick, then original insertion order.