fiber
Loading...
Searching...
No Matches
fiber::Scheduler< n_tasks, logger > Class Template Reference

A real time scheduler that starts tasks once they are ready and schedules them by earliest deadline first. More...

#include <Scheduler.hpp>

Public Member Functions

 Scheduler (TimePoint(*now)(), void(*sleep_until)(TimePoint)=default_sleep_until)
 
TimePoint now () const
 returns the current time
 
void add (TaskBase *task)
 Adds a tasks to the scheduler.
 
void spin ()
 Checks the state of Tasks and executes one if ready.
 
constexpr size_t capacity () const
 returns the capacity of the scheduler
 
constexpr size_t max_size () const
 returns the maximal number of tasks that this scheduler can manage.
 
constexpr size_t n_waiting () const
 returns the number of tasks currently in the waiting queue
 
constexpr size_t n_running () const
 returns the number of tasks currently in the running queue
 
constexpr size_t n_awaiting () const
 returns the number of tasks currently in the awaiting queue
 
constexpr size_t size () const
 returns the current number of tasks that this scheduler manages.
 
constexpr size_t reserve () const
 returns the remaining number of tasks that can still added to the scheduler
 
constexpr bool is_waiting () const
 returns true if there are no tasks in its running queue
 
constexpr bool is_busy () const
 returns true if there are tasks in its running queue and false if the scheduler is waiting.
 
constexpr bool is_empty () const
 returns true if there are no tasks in any queue
 
constexpr bool is_full () const
 returns true if the scheduler is full, the scheduler cannot handle more tasks, no more tasks can be added to the scheduler safely.
 
constexpr bool is_done () const
 returns true if there are no tasks in the sheduler
 
void print (OStream &stream) const
 prints the current state of the scheduler. Lists all queues and contained tasks.
 
void print (OStreamRef stream)
 

Friends

OStreamoperator<< (OStream &stream, const Scheduler &scheduler)
 prints the state of the scheduler. Lists all queues and their contained tasks.
 

Detailed Description

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
class fiber::Scheduler< n_tasks, logger >

A real time scheduler that starts tasks once they are ready and schedules them by earliest deadline first.

Manages three lists of tasks:

  • waiting: a priority list, sorted by the earliest ready times.
  • running: a priority list, sorted by the earliest deadlines.
  • awaiting: a list containing all tasks that are waiting on an awaitable or future.
Template Parameters
n_tasksThe maximum number of thats that will be pre-allocated for this scheduler.
loggerA logger that implements the functions defined by fiber::CSchedulerLogger

Constructor & Destructor Documentation

◆ Scheduler()

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
fiber::Scheduler< n_tasks, logger >::Scheduler ( TimePoint(* now )(),
void(* sleep_until )(TimePoint) = default_sleep_until )
inline

Member Function Documentation

◆ add()

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
void fiber::Scheduler< n_tasks, logger >::add ( TaskBase * task)
inline

Adds a tasks to the scheduler.

Assigns an unique-ID to the task and adds it either to the 'running' or 'waiting' queue.

Exceptions
Throwsan AssertionFailureO1 if FIBER_ASSERTION_LEVEL_O1 or higher is enabled, if the task could not be added and the scheduler is already full.

◆ capacity()

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
size_t fiber::Scheduler< n_tasks, logger >::capacity ( ) const
inlineconstexpr

returns the capacity of the scheduler

The capacity represents the number of total tasks that can be added without reallocation. For static schedulers (like this one) .capacity() is equivalent to .max_size().

To increase the capacity increase the template parameter n_tasks.

◆ is_busy()

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
bool fiber::Scheduler< n_tasks, logger >::is_busy ( ) const
inlineconstexpr

returns true if there are tasks in its running queue and false if the scheduler is waiting.

◆ is_done()

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
bool fiber::Scheduler< n_tasks, logger >::is_done ( ) const
inlineconstexpr

returns true if there are no tasks in the sheduler

◆ is_empty()

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
bool fiber::Scheduler< n_tasks, logger >::is_empty ( ) const
inlineconstexpr

returns true if there are no tasks in any queue

◆ is_full()

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
bool fiber::Scheduler< n_tasks, logger >::is_full ( ) const
inlineconstexpr

returns true if the scheduler is full, the scheduler cannot handle more tasks, no more tasks can be added to the scheduler safely.

◆ is_waiting()

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
bool fiber::Scheduler< n_tasks, logger >::is_waiting ( ) const
inlineconstexpr

returns true if there are no tasks in its running queue

◆ max_size()

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
size_t fiber::Scheduler< n_tasks, logger >::max_size ( ) const
inlineconstexpr

returns the maximal number of tasks that this scheduler can manage.

To increase the max_size() increase the template parameter n_tasks.

◆ n_awaiting()

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
size_t fiber::Scheduler< n_tasks, logger >::n_awaiting ( ) const
inlineconstexpr

returns the number of tasks currently in the awaiting queue

Tasks that are in the awaiting queue are waiting for a future or awaitable to become ready.

◆ n_running()

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
size_t fiber::Scheduler< n_tasks, logger >::n_running ( ) const
inlineconstexpr

returns the number of tasks currently in the running queue

Tasks that are in the ready queue are all tasks that have a ready time that is larger than this->now().

◆ n_waiting()

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
size_t fiber::Scheduler< n_tasks, logger >::n_waiting ( ) const
inlineconstexpr

returns the number of tasks currently in the waiting queue

Tasks that are in the waiting queue are waiting for time to pass until this->now() is larger than the ready time of a task.

◆ now()

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
TimePoint fiber::Scheduler< n_tasks, logger >::now ( ) const
inline

returns the current time

Uses the function passed at construction

Returns
a TimePoint containing the current time

◆ print() [1/2]

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
void fiber::Scheduler< n_tasks, logger >::print ( OStream & stream) const
inline

prints the current state of the scheduler. Lists all queues and contained tasks.

Example output:

@1000us Ready:
┌──────┬────────┬──────────────┬──────────────┬──────────────┬─────────────────────┬─────────────────────┐
│ name │ id │ ready │ deadline │ frame size │ alloc │ max alloc │
╞══════╪════════╪══════════════╪══════════════╪══════════════╪═════════════════════╪═════════════════════╡
└──────┴────────┴──────────────┴──────────────┴──────────────┴─────────────────────┴─────────────────────┘
@1000us Waiting:
┌──────────┬────────┬──────────────┬──────────────┬──────────────┬─────────────────────┬─────────────────────┐
│ name │ id │ ready │ deadline │ frame size │ alloc │ max alloc │
╞══════════╪════════╪══════════════╪══════════════╪══════════════╪═════════════════════╪═════════════════════╡
Task 1 │ 0 │ 1000us │ 5000us │ 256 │ 132 ( 51%) │ 132 ( 51%) │
Task two │ 1 │ 2000us │ 4000us │ 256 │ 132 ( 51%) │ 132 ( 51%) │
└──────────┴────────┴──────────────┴──────────────┴──────────────┴─────────────────────┴─────────────────────┘
@1000us Awaiting:
┌──────┬────────┬──────────────┬──────────────┬──────────────┬─────────────────────┬─────────────────────┐
│ name │ id │ ready │ deadline │ frame size │ alloc │ max alloc │
╞══════╪════════╪══════════════╪══════════════╪══════════════╪═════════════════════╪═════════════════════╡
└──────┴────────┴──────────────┴──────────────┴──────────────┴─────────────────────┴─────────────────────┘
constexpr size_t size() const
returns the current number of tasks that this scheduler manages.
Definition Scheduler.hpp:410
Definition Task.hpp:8
Parameters
streamA reference to an fiber::OStream object

◆ print() [2/2]

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
void fiber::Scheduler< n_tasks, logger >::print ( OStreamRef stream)
inline

◆ reserve()

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
size_t fiber::Scheduler< n_tasks, logger >::reserve ( ) const
inlineconstexpr

returns the remaining number of tasks that can still added to the scheduler

◆ size()

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
size_t fiber::Scheduler< n_tasks, logger >::size ( ) const
inlineconstexpr

returns the current number of tasks that this scheduler manages.

◆ spin()

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
void fiber::Scheduler< n_tasks, logger >::spin ( )
inline

Checks the state of Tasks and executes one if ready.

First promotes tasks from the waiting-queue or the awaiting-queue to the running-queue if they are ready. Then runs the next task with the earliest deadline

Friends And Related Symbol Documentation

◆ operator<<

template<size_t n_tasks, CSchedulerLogger logger = NullLogger>
OStream & operator<< ( OStream & stream,
const Scheduler< n_tasks, logger > & scheduler )
friend

prints the state of the scheduler. Lists all queues and their contained tasks.


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