Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
sbl::control::Scheduler< Slots, Capacity > Class Template Reference

Divider slots over one base period, run in a fixed stage order. More...

Detailed Description

template<uint8_t Slots, uint8_t Capacity = 16>
class sbl::control::Scheduler< Slots, Capacity >

Divider slots over one base period, run in a fixed stage order.

The app drives run() once per base period - the 1 kHz timer ISR on hardware, the timer thread on the workbench - and passes the control clock's tick (control/clock.hpp). Each slot runs every Nth base period. Subscribers are told how many ticks elapsed since their slot last ran, read from the clock, so an irregular base period never skews time-constant math: components are told elapsed time and never assume a call rate.

Every run goes through the stages in order, so the update order is enforced by construction:

  • Drain: event queues -> converters (MIDI parsed elsewhere arrives here).
  • Update: sources and components: advance(elapsed_ticks).
  • Merge: per-parameter merge policies resolve.
  • Publish: bridge writes, the only state the audio context reads.

Within a stage, due subscribers run in subscription order. Apps subscribe; components never self-register. Static storage, no allocation.

Timing. Told its base period in clock ticks (set_timing()), the scheduler counts per slot the runs that came late and the periods that passed with no run, and keeps the worst delay. Each slot is measured against a fixed grid on the clock, one due instant every period (its divider times the base period). The slot's first run after set_timing() anchors the grid. A run's delay is its time past the due instant, and the due instant then advances by whole periods, so a run that is on time does not move the grid. A run later than the tolerance is late, and each whole period in its delay is a skipped period. A run that comes early moves the grid earlier, to that run, and is not late: this is what corrects a grid that was anchored on a delayed run.

The grid is fixed because, on a target whose tick is a hardware timer on the same oscillator as the clock, the two do not drift, so the grid gives a run's true delay behind its timer instant. Measured against the previous run instead, a delay that creeps up within the tolerance at each step would be under-read. A tick source that runs steadily slower than the clock is therefore counted: its delay grows until its runs are late, and a period is counted as skipped each time the delay reaches one.

The counting never changes which slots run or what they are told.

Usage:

enum : uint8_t { k1kHz, k200Hz, k60Hz };
sbl::control::Scheduler<3> sched{{1, 5, 17}};
sched.subscribe<MyWidget, &MyWidget::advance>(Stage::Update, k200Hz, widget);
sched.subscribe(Stage::Drain, k1kHz, drain_midi, nullptr);
sched.set_timing(Clock::ticks_from_us(1000), Clock::ticks_from_us(100));
// once per base period (1 kHz timer ISR / timer thread)
sched.run(Clock::now());
// from the main loop
sched.late_runs(k1kHz); sched.skipped_periods(k1kHz); sched.worst_delay_ticks(k1kHz);
static constexpr uint32_t ticks_from_us(uint32_t us)
Definition clock.hpp:42
static uint32_t now()
Definition clock.hpp:31
Divider slots over one base period, run in a fixed stage order.
Definition scheduler.hpp:95
bool subscribe(Stage stage, uint8_t slot, SubscriberFn fn, void *context)
Add a subscriber.
Context
run() from exactly one context. subscribe() and set_timing() at init, before the first run(). The timing accessors read one word each and may be called from another context while run() counts.
Template Parameters
SlotsNumber of divider slots, at least 1
CapacityMaximum subscribers across all slots, at least 1

Definition at line 95 of file scheduler.hpp.

#include <scheduler.hpp>

Public Member Functions

 Scheduler (const uint16_t(&dividers)[Slots])
 Construct with one divider per slot.
 
bool subscribe (Stage stage, uint8_t slot, SubscriberFn fn, void *context)
 Add a subscriber.
 
void set_timing (uint32_t base_period_ticks, uint32_t late_ticks)
 Give the base period, so late and skipped runs are counted.
 
template<typename T , void(T::*)(uint32_t) Method>
bool subscribe (Stage stage, uint8_t slot, T &object)
 Subscribe a member void T::method(uint32_t elapsed_ticks)
 
void run (uint32_t now)
 One run, once per base period.
 
uint32_t late_runs (uint8_t slot) const
 Runs of the slot that came later than the tolerance past their due instant; wraps at 2^32.
 
uint32_t skipped_periods (uint8_t slot) const
 Periods of the slot that passed with no run; wraps at 2^32.
 
uint32_t worst_delay_ticks (uint8_t slot) const
 The longest a run of the slot came past its due instant, since the start or reset_timing()
 
void reset_timing ()
 
uint8_t subscriber_count () const
 Subscribers added.
 
uint16_t divider (uint8_t slot) const
 The slot's divider, or 0 for a slot out of range.
 

Static Public Member Functions

static constexpr uint8_t slots ()
 Slots.
 
static constexpr uint8_t capacity ()
 Maximum subscribers.
 

Constructor & Destructor Documentation

◆ Scheduler()

template<uint8_t Slots, uint8_t Capacity = 16>
sbl::control::Scheduler< Slots, Capacity >::Scheduler ( const uint16_t(&)  dividers[Slots])
inlineexplicit

Construct with one divider per slot.

Parameters
dividersBase periods between a slot's runs, per slot (0 is treated as 1)

Definition at line 104 of file scheduler.hpp.

Member Function Documentation

◆ subscribe() [1/2]

template<uint8_t Slots, uint8_t Capacity = 16>
bool sbl::control::Scheduler< Slots, Capacity >::subscribe ( Stage  stage,
uint8_t  slot,
SubscriberFn  fn,
void *  context 
)
inline

Add a subscriber.

Parameters
stageStage it runs in
slotSlot index, 0 to Slots - 1
fnCallback
contextPassed to fn unchanged
Returns
false if full, the slot is out of range, or fn is null

Definition at line 118 of file scheduler.hpp.

Referenced by sbl::control::Scheduler< Slots, Capacity >::subscribe().

◆ set_timing()

template<uint8_t Slots, uint8_t Capacity = 16>
void sbl::control::Scheduler< Slots, Capacity >::set_timing ( uint32_t  base_period_ticks,
uint32_t  late_ticks 
)
inline

Give the base period, so late and skipped runs are counted.

Parameters
base_period_ticksClock ticks in one base period; 0 turns the counting off
late_ticksA run is late when it comes more than this past its due instant; held below the base period

Without this call nothing is counted. At init; after a later call each slot's next run anchors its grid again.

Definition at line 135 of file scheduler.hpp.

◆ subscribe() [2/2]

template<uint8_t Slots, uint8_t Capacity = 16>
template<typename T , void(T::*)(uint32_t) Method>
bool sbl::control::Scheduler< Slots, Capacity >::subscribe ( Stage  stage,
uint8_t  slot,
T &  object 
)
inline

Subscribe a member void T::method(uint32_t elapsed_ticks)

Template Parameters
TObject type
MethodMember function called with the elapsed ticks
Parameters
stageStage it runs in
slotSlot index, 0 to Slots - 1
objectObject the method is called on; it must outlive the scheduler
Returns
false if full or the slot is out of range

Definition at line 154 of file scheduler.hpp.

References sbl::control::Scheduler< Slots, Capacity >::subscribe().

◆ run()

template<uint8_t Slots, uint8_t Capacity = 16>
void sbl::control::Scheduler< Slots, Capacity >::run ( uint32_t  now)
inline

One run, once per base period.

The first call establishes the baseline: every slot runs, told 0.

Parameters
nowThe control clock's tick (wrapping; unsigned deltas)

Definition at line 166 of file scheduler.hpp.

References sbl::control::STAGE_COUNT.

◆ late_runs()

template<uint8_t Slots, uint8_t Capacity = 16>
uint32_t sbl::control::Scheduler< Slots, Capacity >::late_runs ( uint8_t  slot) const
inline

Runs of the slot that came later than the tolerance past their due instant; wraps at 2^32.

Parameters
slotSlot index
Returns
The count, or 0 for a slot out of range

Definition at line 207 of file scheduler.hpp.

◆ skipped_periods()

template<uint8_t Slots, uint8_t Capacity = 16>
uint32_t sbl::control::Scheduler< Slots, Capacity >::skipped_periods ( uint8_t  slot) const
inline

Periods of the slot that passed with no run; wraps at 2^32.

Parameters
slotSlot index
Returns
The count, or 0 for a slot out of range

Definition at line 214 of file scheduler.hpp.

◆ worst_delay_ticks()

template<uint8_t Slots, uint8_t Capacity = 16>
uint32_t sbl::control::Scheduler< Slots, Capacity >::worst_delay_ticks ( uint8_t  slot) const
inline

The longest a run of the slot came past its due instant, since the start or reset_timing()

Parameters
slotSlot index
Returns
The delay in clock ticks, or 0 for a slot out of range

Definition at line 221 of file scheduler.hpp.

◆ reset_timing()

template<uint8_t Slots, uint8_t Capacity = 16>
void sbl::control::Scheduler< Slots, Capacity >::reset_timing ( )
inline

Zero the counts and the worst delays. From run()'s context, or while it does not run.

Definition at line 224 of file scheduler.hpp.

◆ subscriber_count()

template<uint8_t Slots, uint8_t Capacity = 16>
uint8_t sbl::control::Scheduler< Slots, Capacity >::subscriber_count ( ) const
inline

Subscribers added.

Definition at line 233 of file scheduler.hpp.

◆ divider()

template<uint8_t Slots, uint8_t Capacity = 16>
uint16_t sbl::control::Scheduler< Slots, Capacity >::divider ( uint8_t  slot) const
inline

The slot's divider, or 0 for a slot out of range.

Definition at line 235 of file scheduler.hpp.

◆ slots()

template<uint8_t Slots, uint8_t Capacity = 16>
static constexpr uint8_t sbl::control::Scheduler< Slots, Capacity >::slots ( )
inlinestaticconstexpr

Slots.

Definition at line 237 of file scheduler.hpp.

◆ capacity()

template<uint8_t Slots, uint8_t Capacity = 16>
static constexpr uint8_t sbl::control::Scheduler< Slots, Capacity >::capacity ( )
inlinestaticconstexpr

Maximum subscribers.

Definition at line 239 of file scheduler.hpp.


The documentation for this class was generated from the following file: