distortos  v0.4.0
object-oriented C++ RTOS for microcontrollers
Scheduler.hpp
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_DISTORTOS_INTERNAL_SCHEDULER_SCHEDULER_HPP_
13 #define INCLUDE_DISTORTOS_INTERNAL_SCHEDULER_SCHEDULER_HPP_
14 
18 
19 namespace distortos
20 {
21 
22 namespace internal
23 {
24 
25 class MainThread;
26 
28 class Scheduler
29 {
30 public:
31 
36  constexpr Scheduler() :
38  runnableList_{},
42  tickCount_{}
43  {
44 
45  }
46 
63  int add(void (& run)(Thread&), void (* preTerminationHook)(Thread&), void (& terminationHook)(Thread&),
64  ThreadControlBlock& threadControlBlock);
65 
81  int block(ThreadList& container, ThreadState state, const ThreadControlBlock::UnblockFunctor* unblockFunctor = {});
82 
102  int block(ThreadList& container, ThreadList::iterator iterator, ThreadState state,
103  const ThreadControlBlock::UnblockFunctor* unblockFunctor = {});
104 
121  int blockUntil(ThreadList& container, ThreadState state, TickClock::time_point timePoint,
122  const ThreadControlBlock::UnblockFunctor* unblockFunctor = {});
123 
128  uint64_t getContextSwitchCount() const;
129 
135  {
137  }
138 
144  {
146  }
147 
153  {
155  }
156 
161  uint64_t getTickCount() const;
162 
175  int initialize(MainThread& mainThread);
176 
183  void maybeRequestContextSwitch() const;
184 
200  int remove();
201 
213  int resume(ThreadList::iterator iterator);
214 
224  int suspend();
225 
238  int suspend(ThreadList::iterator iterator);
239 
251  void* switchContext(void* stackPointer);
252 
264  bool tickInterruptHandler();
265 
276  void unblock(ThreadList::iterator iterator,
278 
283  void yield();
284 
285 private:
286 
298  int addInternal(ThreadControlBlock& threadControlBlock);
299 
315  int blockInternal(ThreadList& container, ThreadList::iterator iterator, ThreadState state,
316  const ThreadControlBlock::UnblockFunctor* unblockFunctor);
317 
329  bool isContextSwitchRequired() const;
330 
344 
347 
350 
353 
356 
359 
361  uint64_t tickCount_;
362 };
363 
364 } // namespace internal
365 
366 } // namespace distortos
367 
368 #endif // INCLUDE_DISTORTOS_INTERNAL_SCHEDULER_SCHEDULER_HPP_
int block(ThreadList &container, ThreadState state, const ThreadControlBlock::UnblockFunctor *unblockFunctor={})
Blocks current thread, transferring it to provided container.
Definition: Scheduler.cpp:118
const SoftwareTimerSupervisor & getSoftwareTimerSupervisor() const
Definition: Scheduler.hpp:152
uint64_t contextSwitchCount_
number of context switches
Definition: Scheduler.hpp:358
typename UnsortedIntrusiveList::iterator iterator
iterator of elements on the list
Definition: SortedIntrusiveList.hpp:60
ThreadList class header.
ThreadControlBlock class is a simple description of a Thread.
Definition: ThreadControlBlock.hpp:40
uint64_t getContextSwitchCount() const
Definition: Scheduler.cpp:181
int initialize(MainThread &mainThread)
Scheduler's initialization.
Definition: Scheduler.cpp:193
int add(void(&run)(Thread &), void(*preTerminationHook)(Thread &), void(&terminationHook)(Thread &), ThreadControlBlock &threadControlBlock)
Adds new ThreadControlBlock to scheduler.
Definition: Scheduler.cpp:92
ThreadControlBlock class header.
void * switchContext(void *stackPointer)
Called by architecture-specific code to do final context switch.
Definition: Scheduler.cpp:247
SoftwareTimerSupervisor & getSoftwareTimerSupervisor()
Definition: Scheduler.hpp:143
ThreadList::iterator currentThreadControlBlock_
iterator to the currently active ThreadControlBlock
Definition: Scheduler.hpp:346
std::chrono::time_point< TickClock > time_point
basic time_point type of clock
Definition: TickClock.hpp:42
int suspend()
Suspends current thread.
Definition: Scheduler.cpp:235
constexpr Scheduler()
Scheduler&#39;s constructor.
Definition: Scheduler.hpp:36
explicit request to unblock the thread - normal unblock
Definition: ThreadControlBlock.hpp:57
SoftwareTimerSupervisor class header.
void unblockInternal(ThreadList::iterator iterator, ThreadControlBlock::UnblockReason unblockReason)
Unblocks provided thread, transferring it from it&#39;s current container to "runnable" container...
Definition: Scheduler.cpp:357
bool isContextSwitchRequired() const
Tests whether context switch is required or not.
Definition: Scheduler.cpp:346
UnblockReason
reason of thread unblocking
Definition: ThreadControlBlock.hpp:45
uint64_t tickCount_
tick count
Definition: Scheduler.hpp:361
Top-level namespace of distortos project.
sorted intrusive list of threads (thread control blocks)
Definition: ThreadList.hpp:55
SoftwareTimerSupervisor softwareTimerSupervisor_
internal SoftwareTimerSupervisor object
Definition: Scheduler.hpp:355
int blockInternal(ThreadList &container, ThreadList::iterator iterator, ThreadState state, const ThreadControlBlock::UnblockFunctor *unblockFunctor)
Blocks thread, transferring it to provided container.
Definition: Scheduler.cpp:330
MainThread class is a Thread for main()
Definition: MainThread.hpp:24
int resume(ThreadList::iterator iterator)
Resumes suspended thread.
Definition: Scheduler.cpp:224
int addInternal(ThreadControlBlock &threadControlBlock)
Adds new ThreadControlBlock to scheduler.
Definition: Scheduler.cpp:317
SoftwareTimerSupervisor class is a supervisor of software timers.
Definition: SoftwareTimerSupervisor.hpp:24
int blockUntil(ThreadList &container, ThreadState state, TickClock::time_point timePoint, const ThreadControlBlock::UnblockFunctor *unblockFunctor={})
Blocks current thread with timeout, transferring it to provided container.
Definition: Scheduler.cpp:152
ThreadList runnableList_
list of ThreadControlBlock elements in "runnable" state, sorted by priority in descending order ...
Definition: Scheduler.hpp:349
Thread class is a pure abstract interface for threads.
Definition: Thread.hpp:32
ThreadList suspendedList_
list of ThreadControlBlock elements in "suspended" state, sorted by priority in descending order ...
Definition: Scheduler.hpp:352
bool tickInterruptHandler()
Handler of "tick" interrupt.
Definition: Scheduler.cpp:266
ThreadState
state of the thread
Definition: ThreadState.hpp:26
void yield()
Yields time slot of the scheduler to next thread.
Definition: Scheduler.cpp:305
void maybeRequestContextSwitch() const
Requests context switch if it is needed.
Definition: Scheduler.cpp:204
uint64_t getTickCount() const
Definition: Scheduler.cpp:187
Scheduler class is a system&#39;s scheduler.
Definition: Scheduler.hpp:28
ThreadControlBlock & getCurrentThreadControlBlock() const
Definition: Scheduler.hpp:134
void unblock(ThreadList::iterator iterator, ThreadControlBlock::UnblockReason unblockReason=ThreadControlBlock::UnblockReason::unblockRequest)
Unblocks provided thread, transferring it from it&#39;s current container to "runnable" container...
Definition: Scheduler.cpp:297