WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
wpi::internal::PeriodicPriorityQueue Class Reference

A priority queue for scheduling periodic callbacks based on their next execution time. More...

#include <wpi/internal/PeriodicPriorityQueue.hpp>

Classes

class  Callback
 A periodic callback with scheduling metadata. More...

Public Member Functions

void Add (std::function< void()> func, std::chrono::microseconds startTime, std::chrono::microseconds period)
 Adds a periodic callback to the queue.
void Add (std::function< void()> func, std::chrono::microseconds startTime, std::chrono::microseconds period, std::chrono::microseconds offset)
 Adds a periodic callback to the queue.
void Add (std::function< void()> func, std::chrono::microseconds startTime, wpi::units::second_t period)
 Adds a periodic callback to the queue.
void Add (std::function< void()> func, std::chrono::microseconds startTime, wpi::units::second_t period, wpi::units::second_t offset)
 Adds a periodic callback to the queue.
void Add (Callback callback)
 Adds a pre-constructed callback to the queue.
bool Remove (const Callback &callback)
 Removes a specific callback from the queue.
void Clear ()
 Removes all callbacks from the queue.
bool RunCallbacks (HAL_NotifierHandle notifier)
 Executes all callbacks that are due, then waits for the next callback's scheduled time.
wpi::util::priority_queue< Callback, std::vector< Callback >, std::greater<> > & GetQueue ()
 Returns the underlying priority queue.
wpi::units::microsecond_t GetLoopStartTime () const
 Return the system clock time in microseconds for the start of the current periodic loop.

Detailed Description

A priority queue for scheduling periodic callbacks based on their next execution time.

This class manages a collection of periodic callbacks that execute at specified intervals. Callbacks are scheduled using FPGA timestamps and automatically rescheduled after execution to maintain their periodic behavior. The queue uses a priority heap to efficiently determine the next callback to execute.

This is an internal scheduling primitive used by robot frameworks like TimedRobot.

Member Function Documentation

◆ Add() [1/5]

void wpi::internal::PeriodicPriorityQueue::Add ( Callback callback)

Adds a pre-constructed callback to the queue.

Parameters
callbackThe callback to add.

◆ Add() [2/5]

void wpi::internal::PeriodicPriorityQueue::Add ( std::function< void()> func,
std::chrono::microseconds startTime,
std::chrono::microseconds period )

Adds a periodic callback to the queue.

Parameters
funcThe callback to run.
startTimeThe common starting point for all callback scheduling.
periodThe period at which to run the callback.

◆ Add() [3/5]

void wpi::internal::PeriodicPriorityQueue::Add ( std::function< void()> func,
std::chrono::microseconds startTime,
std::chrono::microseconds period,
std::chrono::microseconds offset )

Adds a periodic callback to the queue.

Parameters
funcThe callback to run.
startTimeThe common starting point for all callback scheduling.
periodThe period at which to run the callback.
offsetThe offset from the common starting time.

◆ Add() [4/5]

void wpi::internal::PeriodicPriorityQueue::Add ( std::function< void()> func,
std::chrono::microseconds startTime,
wpi::units::second_t period )

Adds a periodic callback to the queue.

Parameters
funcThe callback to run.
startTimeThe common starting point for all callback scheduling in FPGA timestamp microseconds.
periodThe period at which to run the callback.

◆ Add() [5/5]

void wpi::internal::PeriodicPriorityQueue::Add ( std::function< void()> func,
std::chrono::microseconds startTime,
wpi::units::second_t period,
wpi::units::second_t offset )

Adds a periodic callback to the queue.

Parameters
funcThe callback to run.
startTimeThe common starting point for all callback scheduling in FPGA timestamp microseconds.
periodThe period at which to run the callback.
offsetThe offset from the common starting time.

◆ Clear()

void wpi::internal::PeriodicPriorityQueue::Clear ( )

Removes all callbacks from the queue.

◆ GetLoopStartTime()

wpi::units::microsecond_t wpi::internal::PeriodicPriorityQueue::GetLoopStartTime ( ) const
inline

Return the system clock time in microseconds for the start of the current periodic loop.

This is in the same time base as Timer.getMonotonicTimeStamp(), but is stable through a loop. It is updated at the beginning of every periodic callback (including the normal periodic loop).

Returns
Robot running time in microseconds, as of the start of the current periodic function.

◆ GetQueue()

wpi::util::priority_queue< Callback, std::vector< Callback >, std::greater<> > & wpi::internal::PeriodicPriorityQueue::GetQueue ( )
inline

Returns the underlying priority queue.

◆ Remove()

bool wpi::internal::PeriodicPriorityQueue::Remove ( const Callback & callback)

Removes a specific callback from the queue.

Parameters
callbackThe callback to remove.
Returns
true if the callback was found and removed, false otherwise.

◆ RunCallbacks()

bool wpi::internal::PeriodicPriorityQueue::RunCallbacks ( HAL_NotifierHandle notifier)

Executes all callbacks that are due, then waits for the next callback's scheduled time.

This method performs the following steps:

  1. Retrieves the callback with the earliest expiration time from the queue
  2. Sets a hardware notifier alarm to wait until that callback's expiration time
  3. Blocks until the notifier signals or is interrupted
  4. Executes the callback and reschedules it for its next period
  5. Processes any additional callbacks that have become due during execution

When rescheduling callbacks, this method automatically compensates for execution delays by advancing the expiration time by the number of full periods that have elapsed, ensuring callbacks maintain their scheduled phase over time.

Parameters
notifierThe HAL notifier handle to use for timing.
Returns
false if the notifier was destroyed (loop should exit), true otherwise.

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