distortos  v0.4.0
object-oriented C++ RTOS for microcontrollers
DynamicThreadBase.hpp
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_DISTORTOS_INTERNAL_SCHEDULER_DYNAMICTHREADBASE_HPP_
13 #define INCLUDE_DISTORTOS_INTERNAL_SCHEDULER_DYNAMICTHREADBASE_HPP_
14 
18 
20 
21 namespace distortos
22 {
23 
24 #ifdef CONFIG_THREAD_DETACH_ENABLE
25 
26 class DynamicThread;
27 
28 #endif // def CONFIG_THREAD_DETACH_ENABLE
29 
30 namespace internal
31 {
32 
43 {
44 public:
45 
46 #ifdef CONFIG_THREAD_DETACH_ENABLE
47 
68  template<typename Function, typename... Args>
69  DynamicThreadBase(size_t stackSize, bool canReceiveSignals, size_t queuedSignals, size_t signalActions,
70  uint8_t priority, SchedulingPolicy schedulingPolicy, DynamicThread& owner, Function&& function,
71  Args&&... args);
72 
73 #else // !def CONFIG_THREAD_DETACH_ENABLE
74 
94  template<typename Function, typename... Args>
95  DynamicThreadBase(size_t stackSize, bool canReceiveSignals, size_t queuedSignals, size_t signalActions,
96  uint8_t priority, SchedulingPolicy schedulingPolicy, Function&& function, Args&&... args);
97 
109  template<typename Function, typename... Args>
110  DynamicThreadBase(const DynamicThreadParameters parameters, Function&& function, Args&&... args) :
111  DynamicThreadBase{parameters.stackSize, parameters.canReceiveSignals, parameters.queuedSignals,
112  parameters.signalActions, parameters.priority, parameters.schedulingPolicy,
113  std::forward<Function>(function), std::forward<Args>(args)...}
114  {
115 
116  }
117 
118 #endif // !def CONFIG_THREAD_DETACH_ENABLE
119 
120 #ifdef CONFIG_THREAD_DETACH_ENABLE
121 
135  int detach() override;
136 
137 #endif // def CONFIG_THREAD_DETACH_ENABLE
138 
148  int start()
149  {
150 #ifdef CONFIG_THREAD_DETACH_ENABLE
152 #else // !def CONFIG_THREAD_DETACH_ENABLE
154 #endif // !def CONFIG_THREAD_DETACH_ENABLE
155  }
156 
157  DynamicThreadBase(const DynamicThreadBase&) = delete;
158  DynamicThreadBase(DynamicThreadBase&&) = default;
159  const DynamicThreadBase& operator=(const DynamicThreadBase&) = delete;
160  DynamicThreadBase& operator=(DynamicThreadBase&&) = delete;
161 
162 protected:
163 
164 #ifdef CONFIG_THREAD_DETACH_ENABLE
165 
174  static void preTerminationHook(Thread& thread);
175 
184  static void terminationHook(Thread& thread);
185 
186 #endif // def CONFIG_THREAD_DETACH_ENABLE
187 
188 private:
189 
198  static void run(Thread& thread);
199 
201  constexpr static size_t stackGuardSize_ {(CONFIG_STACK_GUARD_SIZE + CONFIG_ARCHITECTURE_STACK_ALIGNMENT - 1) /
202  CONFIG_ARCHITECTURE_STACK_ALIGNMENT * CONFIG_ARCHITECTURE_STACK_ALIGNMENT};
203 
206 
208  std::function<void()> boundFunction_;
209 
210 #ifdef CONFIG_THREAD_DETACH_ENABLE
211 
214 
215 #endif // def CONFIG_THREAD_DETACH_ENABLE
216 };
217 
218 #ifdef CONFIG_THREAD_DETACH_ENABLE
219 
220 template<typename Function, typename... Args>
221 DynamicThreadBase::DynamicThreadBase(const size_t stackSize, const bool canReceiveSignals, const size_t queuedSignals,
222  const size_t signalActions, const uint8_t priority, const SchedulingPolicy schedulingPolicy,
223  DynamicThread& owner, Function&& function, Args&&... args) :
224  ThreadCommon{{{new uint8_t[stackSize + stackGuardSize_], storageDeleter<uint8_t>},
225  stackSize + stackGuardSize_}, priority, schedulingPolicy, nullptr,
226  canReceiveSignals == true ? &dynamicSignalsReceiver_ : nullptr},
227  dynamicSignalsReceiver_{canReceiveSignals == true ? queuedSignals : 0,
228  canReceiveSignals == true ? signalActions : 0},
229  boundFunction_{std::bind(std::forward<Function>(function), std::forward<Args>(args)...)},
230  owner_{&owner}
231 {
232 
233 }
234 
235 #else // !def CONFIG_THREAD_DETACH_ENABLE
236 
237 template<typename Function, typename... Args>
238 DynamicThreadBase::DynamicThreadBase(const size_t stackSize, const bool canReceiveSignals, const size_t queuedSignals,
239  const size_t signalActions, const uint8_t priority, const SchedulingPolicy schedulingPolicy,
240  Function&& function, Args&&... args) :
241  ThreadCommon{{{new uint8_t[stackSize + stackGuardSize_], storageDeleter<uint8_t>},
242  stackSize + stackGuardSize_,}, priority, schedulingPolicy, nullptr,
243  canReceiveSignals == true ? &dynamicSignalsReceiver_ : nullptr},
244  dynamicSignalsReceiver_{canReceiveSignals == true ? queuedSignals : 0,
245  canReceiveSignals == true ? signalActions : 0},
246  boundFunction_{std::bind(std::forward<Function>(function), std::forward<Args>(args)...)}
247 {
248 
249 }
250 
251 #endif // !def CONFIG_THREAD_DETACH_ENABLE
252 
253 } // namespace internal
254 
255 } // namespace distortos
256 
257 #endif // INCLUDE_DISTORTOS_INTERNAL_SCHEDULER_DYNAMICTHREADBASE_HPP_
DynamicSignalsReceiver class header.
size_t queuedSignals
Definition: DynamicThreadParameters.hpp:78
int start()
Starts the thread.
Definition: DynamicThreadBase.hpp:148
DynamicThreadBase class is a type-erased interface for thread that has dynamic storage for bound func...
Definition: DynamicThreadBase.hpp:42
DynamicThreadParameters class header.
static constexpr size_t stackGuardSize_
size of "stack guard", bytes
Definition: DynamicThreadBase.hpp:201
SchedulingPolicy schedulingPolicy
scheduling policy of the thread
Definition: DynamicThreadParameters.hpp:94
size_t stackSize
size of stack, bytes
Definition: DynamicThreadParameters.hpp:85
static void terminationHook(Thread &thread)
Termination hook function of thread.
Definition: DynamicThreadBase.cpp:68
SchedulingPolicy
scheduling policy of the thread
Definition: SchedulingPolicy.hpp:26
uint8_t priority
thread&#39;s priority, 0 - lowest, UINT8_MAX - highest
Definition: DynamicThreadParameters.hpp:91
static void run(Thread &thread)
Thread&#39;s "run" function.
Definition: DynamicThreadBase.cpp:84
DynamicThreadBase(size_t stackSize, bool canReceiveSignals, size_t queuedSignals, size_t signalActions, uint8_t priority, SchedulingPolicy schedulingPolicy, DynamicThread &owner, Function &&function, Args &&... args)
DynamicThreadBase&#39;s constructor.
Definition: DynamicThreadBase.hpp:221
bool canReceiveSignals
selects whether reception of signals is enabled (true) or disabled (false) for this thread ...
Definition: DynamicThreadParameters.hpp:88
DynamicSignalsReceiver class is a templated interface for SignalsReceiver that has dynamic storage fo...
Definition: DynamicSignalsReceiver.hpp:27
storageDeleter() definition
DynamicSignalsReceiver dynamicSignalsReceiver_
internal DynamicSignalsReceiver object
Definition: DynamicThreadBase.hpp:205
Top-level namespace of distortos project.
ThreadCommon class implements common functionality of threads.
Definition: ThreadCommon.hpp:29
void thread(uint32_t value, bool &sharedResult)
Test thread.
Definition: ARMv7-M-FpuThreadTestCase.cpp:68
DynamicThreadParameters struct is a helper with parameters for DynamicThread&#39;s constructor.
Definition: DynamicThreadParameters.hpp:32
ThreadCommon class header.
static void preTerminationHook(Thread &thread)
Pre-termination hook function of thread.
Definition: DynamicThreadBase.cpp:60
size_t signalActions
Definition: DynamicThreadParameters.hpp:82
std::function< void()> boundFunction_
bound function object
Definition: DynamicThreadBase.hpp:208
int detach() override
Detaches the thread.
Definition: DynamicThreadBase.cpp:38
int startInternal(void(&runFunction)(Thread &), void(*preTerminationHookFunction)(Thread &), void(&terminationHookFunction)(Thread &))
Starts the thread.
Definition: ThreadCommon.cpp:132
DynamicThread * owner_
pointer to owner DynamicThread object, nullptr if thread is detached
Definition: DynamicThreadBase.hpp:213
DynamicThread class is a type-erased interface for thread that has dynamic storage for bound function...
Definition: DynamicThread.hpp:32
Thread class is a pure abstract interface for threads.
Definition: Thread.hpp:32