Resurgence (PY2022)
Codebase for the Husky Robotics 2021-2022 rover Resurgence
Loading...
Searching...
No Matches
util::PeriodicScheduler< Clock > Class Template Reference

Uses a single thread to periodically invoke callbacks at a given frequency. More...

#include <scheduler.h>

Inheritance diagram for util::PeriodicScheduler< Clock >:
Collaboration diagram for util::PeriodicScheduler< Clock >:

Public Types

using eventid_t = uint64_t
 The type of event ids.
 

Public Member Functions

 PeriodicScheduler (const std::optional< std::string > &name=std::nullopt)
 Create a new PeriodicScheduler.
 
 PeriodicScheduler (const PeriodicScheduler &)=delete
 
 ~PeriodicScheduler ()
 Join the thread and destruct.
 
PeriodicScheduleroperator= (const PeriodicScheduler &)=delete
 
void clear ()
 Clears all currently scheduled recurring events.
 
void removeEvent (eventid_t id)
 Remove an event from the schedule.
 
eventid_t scheduleEvent (std::chrono::milliseconds period, const std::function< void()> &fn)
 Schedule a new event to be executed periodically.
 

Friends

void util::impl::notifyScheduler (PeriodicScheduler &)
 

Detailed Description

template<typename Clock = std::chrono::steady_clock>
class util::PeriodicScheduler< Clock >

Uses a single thread to periodically invoke callbacks at a given frequency.

The underlying thread is initialized upon construction, so do not create PeriodicSchedulers that will not be used.

Template Parameters
ClockThe clock to use for scheduling.

Constructor & Destructor Documentation

◆ PeriodicScheduler()

template<typename Clock = std::chrono::steady_clock>
util::PeriodicScheduler< Clock >::PeriodicScheduler ( const std::optional< std::string > & name = std::nullopt)
inlineexplicit

Create a new PeriodicScheduler.

Parameters
nameThe name of this scheduler, for logging purposes.

Member Function Documentation

◆ clear()

template<typename Clock = std::chrono::steady_clock>
void util::PeriodicScheduler< Clock >::clear ( )
inline

Clears all currently scheduled recurring events.

This invalidates all previously returned event ids.

◆ removeEvent()

template<typename Clock = std::chrono::steady_clock>
void util::PeriodicScheduler< Clock >::removeEvent ( eventid_t id)
inline

Remove an event from the schedule.

It is undefined behavior to remove events that are not scheduled, e.g. have already been removed.

Parameters
idThe id of the event.

◆ scheduleEvent()

template<typename Clock = std::chrono::steady_clock>
eventid_t util::PeriodicScheduler< Clock >::scheduleEvent ( std::chrono::milliseconds period,
const std::function< void()> & fn )
inline

Schedule a new event to be executed periodically.

The first execution will be scheduled to happen one period from now. It is undefined behavior to schedule more than 2^64 events.

Parameters
periodThe period in between executions of the event.
fnThe function to call when the event is executed. This should not block.
Returns
The id of the scheduled event, which can be used with removeEvent().

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