Skyward boardcore
Loading...
Searching...
No Matches
Boardcore::SignaledDeadlineTask Class Referenceabstract

A task that executes a user-defined function at specific time points, or when signaled. More...

#include <SignaledDeadlineTask.h>

Inheritance diagram for Boardcore::SignaledDeadlineTask:
Collaboration diagram for Boardcore::SignaledDeadlineTask:

Public Types

using Clock = std::chrono::system_clock
 
using TimePoint = std::chrono::time_point<Clock>
 

Public Member Functions

 SignaledDeadlineTask (unsigned int stackSize=miosix::STACK_DEFAULT_FOR_PTHREAD, miosix::Priority priority=miosix::MAIN_PRIORITY)
 Constructor.
 
void signalTask ()
 Signals the task to run and go to sleep until the next deadline.
 
virtual TimePoint nextTaskDeadline ()=0
 Calculates the next deadline for the task to run.
 
virtual void task ()=0
 The user-defined task to run.
 
- Public Member Functions inherited from Boardcore::ActiveObject
 ActiveObject (unsigned int stacksize=miosix::STACK_DEFAULT_FOR_PTHREAD, miosix::Priority priority=miosix::MAIN_PRIORITY)
 
virtual ~ActiveObject ()
 
virtual bool start ()
 Start the thread associated with this active object.
 
virtual void stop ()
 Signals the runner thread to terminate and joins the thread.
 
bool isRunning ()
 

Protected Member Functions

void run () override
 
- Protected Member Functions inherited from Boardcore::ActiveObject
bool shouldStop ()
 Tells whether or not the ActiveObject should stop its execution.
 

Additional Inherited Members

- Protected Attributes inherited from Boardcore::ActiveObject
miosix::Thread * thread = nullptr
 Gives access to the thread object.
 
std::atomic< bool > stopFlag {false}
 
std::atomic< bool > running {false}
 

Detailed Description

A task that executes a user-defined function at specific time points, or when signaled.

This class is designed to be used in situations where a task needs to be executed within specific deadlines, irregularly. The task will wake up at the specified time points and execute the user-defined function. The task can also be signaled to wake up and run immediately, regardless of the time points.

This class essentially emulates an EDF (Earliest Deadline First) scheduler on a single thread. If a high-enough priority value is used, the task will be scheduled as soon as the deadline is reached, this can be useful for hard real-time tasks.

Users must implement the following functions:

  • calculateNextDeadline(): This function should return the next time point when the task should wake up and run. Use TimePoint::max() to indicate that there is no deadline, and the task should not run until signaled.
  • task(): This function is invoked when the task wakes up after a deadline expiration. Users should implement the logic that needs to be executed at that time.

Definition at line 58 of file SignaledDeadlineTask.h.

Member Typedef Documentation

◆ Clock

using Boardcore::SignaledDeadlineTask::Clock = std::chrono::system_clock

The current version of the STL (GCC 9.2.0) has a bug where condition_variable::wait_until is broken with clocks != system clock.

When absolute times big enough are used, the resulting time point will overflow, causing the condvar to return immediately.

TODO: Use steady_clock once we update to a newer Miosix version using a newer GCC version and therefore a newer STL.

Definition at line 71 of file SignaledDeadlineTask.h.

◆ TimePoint

using Boardcore::SignaledDeadlineTask::TimePoint = std::chrono::time_point<Clock>

Definition at line 72 of file SignaledDeadlineTask.h.

Constructor & Destructor Documentation

◆ SignaledDeadlineTask()

Boardcore::SignaledDeadlineTask::SignaledDeadlineTask ( unsigned int stackSize = miosix::STACK_DEFAULT_FOR_PTHREAD,
miosix::Priority priority = miosix::MAIN_PRIORITY )
inline

Constructor.

Parameters
stackSizeThe size of the stack for the thread.
priorityThe priority of the thread.

Definition at line 80 of file SignaledDeadlineTask.h.

Member Function Documentation

◆ nextTaskDeadline()

virtual TimePoint Boardcore::SignaledDeadlineTask::nextTaskDeadline ( )
pure virtual

Calculates the next deadline for the task to run.

◆ run()

void Boardcore::SignaledDeadlineTask::run ( )
inlineoverrideprotectedvirtual

The thread that will be spawned just calls this function. Override it to implement your logic. Remember to frequently check for shouldStop() to see if you should terminate the execution of the thread.

Implements Boardcore::ActiveObject.

Definition at line 108 of file SignaledDeadlineTask.h.

◆ signalTask()

void Boardcore::SignaledDeadlineTask::signalTask ( )
inline

Signals the task to run and go to sleep until the next deadline.

Definition at line 90 of file SignaledDeadlineTask.h.

◆ task()

virtual void Boardcore::SignaledDeadlineTask::task ( )
pure virtual

The user-defined task to run.


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