distortos  v0.7.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 
26 class Scheduler
27 {
28 public:
29 
34  constexpr Scheduler() :
36  runnableList_{},
40  tickCount_{}
41  {
42 
43  }
44 
58  int add(ThreadControlBlock& threadControlBlock);
59 
75  int block(ThreadList& container, ThreadState state, const UnblockFunctor* unblockFunctor = {});
76 
94  int block(ThreadList& container, ThreadList::iterator iterator, ThreadState state,
95  const UnblockFunctor* unblockFunctor = {});
96 
113  int blockUntil(ThreadList& container, ThreadState state, TickClock::time_point timePoint,
114  const UnblockFunctor* unblockFunctor = {});
115 
120  uint64_t getContextSwitchCount() const;
121 
127  {
129  }
130 
136  {
138  }
139 
145  {
147  }
148 
153  uint64_t getTickCount() const;
154 
167  int initialize(ThreadControlBlock& mainThreadControlBlock);
168 
175  void maybeRequestContextSwitch() const;
176 
192  int remove();
193 
205  int resume(ThreadList::iterator iterator);
206 
216  int suspend();
217 
230  int suspend(ThreadList::iterator iterator);
231 
243  void* switchContext(void* stackPointer);
244 
256  bool tickInterruptHandler();
257 
268 
273  void yield();
274 
275 private:
276 
288  int addInternal(ThreadControlBlock& threadControlBlock);
289 
305  int blockInternal(ThreadList& container, ThreadList::iterator iterator, ThreadState state,
306  const UnblockFunctor* unblockFunctor);
307 
319  bool isContextSwitchRequired() const;
320 
333  void unblockInternal(ThreadList::iterator iterator, UnblockReason unblockReason);
334 
337 
340 
343 
346 
349 
351  uint64_t tickCount_;
352 };
353 
354 } // namespace internal
355 
356 } // namespace distortos
357 
358 #endif // INCLUDE_DISTORTOS_INTERNAL_SCHEDULER_SCHEDULER_HPP_
const SoftwareTimerSupervisor & getSoftwareTimerSupervisor() const
Definition: Scheduler.hpp:144
uint64_t contextSwitchCount_
number of context switches
Definition: Scheduler.hpp:348
typename UnsortedIntrusiveList::iterator iterator
iterator of elements on the list
Definition: SortedIntrusiveList.hpp:60
ThreadList class header.
int block(ThreadList &container, ThreadState state, const UnblockFunctor *unblockFunctor={})
Blocks current thread, transferring it to provided container.
Definition: Scheduler.cpp:113
ThreadControlBlock class is a simple description of a Thread.
Definition: ThreadControlBlock.hpp:39
int remove()
Removes current thread from Scheduler's control.
Definition: Scheduler.cpp:204
uint64_t getContextSwitchCount() const
Definition: Scheduler.cpp:175
ThreadControlBlock class header.
void * switchContext(void *stackPointer)
Called by architecture-specific code to do final context switch.
Definition: Scheduler.cpp:241
int blockInternal(ThreadList &container, ThreadList::iterator iterator, ThreadState state, const UnblockFunctor *unblockFunctor)
Blocks thread, transferring it to provided container.
Definition: Scheduler.cpp:324
SoftwareTimerSupervisor & getSoftwareTimerSupervisor()
Definition: Scheduler.hpp:135
ThreadList::iterator currentThreadControlBlock_
iterator to the currently active ThreadControlBlock
Definition: Scheduler.hpp:336
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:229
constexpr Scheduler()
Scheduler's constructor.
Definition: Scheduler.hpp:34
SoftwareTimerSupervisor class header.
bool isContextSwitchRequired() const
Tests whether context switch is required or not.
Definition: Scheduler.cpp:340
int add(ThreadControlBlock &threadControlBlock)
Adds new ThreadControlBlock to scheduler.
Definition: Scheduler.cpp:89
uint64_t tickCount_
tick count
Definition: Scheduler.hpp:351
void unblock(ThreadList::iterator iterator, UnblockReason unblockReason=UnblockReason::unblockRequest)
Unblocks provided thread, transferring it from it's current container to "runnable" container.
Definition: Scheduler.cpp:291
Top-level namespace of distortos project.
Definition: buttons.hpp:33
int blockUntil(ThreadList &container, ThreadState state, TickClock::time_point timePoint, const UnblockFunctor *unblockFunctor={})
Blocks current thread with timeout, transferring it to provided container.
Definition: Scheduler.cpp:146
sorted intrusive list of threads (thread control blocks)
Definition: ThreadList.hpp:55
SoftwareTimerSupervisor softwareTimerSupervisor_
internal SoftwareTimerSupervisor object
Definition: Scheduler.hpp:345
Definition: UnblockFunctor.hpp:29
int resume(ThreadList::iterator iterator)
Resumes suspended thread.
Definition: Scheduler.cpp:218
int addInternal(ThreadControlBlock &threadControlBlock)
Adds new ThreadControlBlock to scheduler.
Definition: Scheduler.cpp:311
SoftwareTimerSupervisor class is a supervisor of software timers.
Definition: SoftwareTimerSupervisor.hpp:24
void unblockInternal(ThreadList::iterator iterator, UnblockReason unblockReason)
Unblocks provided thread, transferring it from it's current container to "runnable" container.
Definition: Scheduler.cpp:351
int initialize(ThreadControlBlock &mainThreadControlBlock)
Scheduler's initialization.
Definition: Scheduler.cpp:187
ThreadList runnableList_
list of ThreadControlBlock elements in "runnable" state, sorted by priority in descending order
Definition: Scheduler.hpp:339
UnblockReason
reason of thread unblocking
Definition: UnblockReason.hpp:26
ThreadList suspendedList_
list of ThreadControlBlock elements in "suspended" state, sorted by priority in descending order
Definition: Scheduler.hpp:342
bool tickInterruptHandler()
Handler of "tick" interrupt.
Definition: Scheduler.cpp:260
ThreadState
state of the thread
Definition: ThreadState.hpp:28
void yield()
Yields time slot of the scheduler to next thread.
Definition: Scheduler.cpp:299
explicit request to unblock the thread - normal unblock
void maybeRequestContextSwitch() const
Requests context switch if it is needed.
Definition: Scheduler.cpp:198
uint64_t getTickCount() const
Definition: Scheduler.cpp:181
Scheduler class is a system's scheduler.
Definition: Scheduler.hpp:26
ThreadControlBlock & getCurrentThreadControlBlock() const
Definition: Scheduler.hpp:126