distortos  v0.6.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 
17 
19 
21 
22 #include <functional>
23 
24 namespace distortos
25 {
26 
27 #if CONFIG_THREAD_DETACH_ENABLE == 1
28 
29 class DynamicThread;
30 
31 #endif // CONFIG_THREAD_DETACH_ENABLE == 1
32 
33 namespace internal
34 {
35 
46 {
47 public:
48 
49 #if CONFIG_THREAD_DETACH_ENABLE == 1
50 
71  template<typename Function, typename... Args>
72  DynamicThreadBase(size_t stackSize, bool canReceiveSignals, size_t queuedSignals, size_t signalActions,
73  uint8_t priority, SchedulingPolicy schedulingPolicy, DynamicThread& owner, Function&& function,
74  Args&&... args);
75 
76 #else // CONFIG_THREAD_DETACH_ENABLE != 1
77 
97  template<typename Function, typename... Args>
98  DynamicThreadBase(size_t stackSize, bool canReceiveSignals, size_t queuedSignals, size_t signalActions,
99  uint8_t priority, SchedulingPolicy schedulingPolicy, Function&& function, Args&&... args);
100 
112  template<typename Function, typename... Args>
113  DynamicThreadBase(const DynamicThreadParameters parameters, Function&& function, Args&&... args) :
114  DynamicThreadBase{parameters.stackSize, parameters.canReceiveSignals, parameters.queuedSignals,
115  parameters.signalActions, parameters.priority, parameters.schedulingPolicy,
116  std::forward<Function>(function), std::forward<Args>(args)...}
117  {
118 
119  }
120 
121 #endif // CONFIG_THREAD_DETACH_ENABLE != 1
122 
123 #if CONFIG_THREAD_DETACH_ENABLE == 1
124 
138  int detach() override;
139 
140 #endif // CONFIG_THREAD_DETACH_ENABLE == 1
141 
151  int start()
152  {
154  }
155 
156  DynamicThreadBase(const DynamicThreadBase&) = delete;
158  const DynamicThreadBase& operator=(const DynamicThreadBase&) = delete;
159  DynamicThreadBase& operator=(DynamicThreadBase&&) = delete;
160 
161 protected:
162 
163 #if CONFIG_THREAD_DETACH_ENABLE == 1
164 
173  void exit0Hook() override;
174 
183  void exit1Hook() override;
184 
185 #endif // CONFIG_THREAD_DETACH_ENABLE == 1
186 
193  void run() override;
194 
195 private:
196 
207  static Stack makeStack(const size_t stackSize)
208  {
209  static_assert(alignof(max_align_t) >= CONFIG_ARCHITECTURE_STACK_ALIGNMENT,
210  "Alignment of dynamically allocated memory is too low!");
211 
212  const auto adjustedStackSize = (stackSize + CONFIG_ARCHITECTURE_STACK_ALIGNMENT - 1) /
213  CONFIG_ARCHITECTURE_STACK_ALIGNMENT * CONFIG_ARCHITECTURE_STACK_ALIGNMENT;
214  return {{new uint8_t[adjustedStackSize + stackGuardSize], storageDeleter<uint8_t>},
215  adjustedStackSize + stackGuardSize};
216  }
217 
218 #if CONFIG_SIGNALS_ENABLE == 1
219 
222 
223 #endif // CONFIG_SIGNALS_ENABLE == 1
224 
226  std::function<void()> boundFunction_;
227 
228 #if CONFIG_THREAD_DETACH_ENABLE == 1
229 
232 
233 #endif // CONFIG_THREAD_DETACH_ENABLE == 1
234 };
235 
236 #if CONFIG_SIGNALS_ENABLE == 1 && CONFIG_THREAD_DETACH_ENABLE == 1
237 
238 template<typename Function, typename... Args>
239 DynamicThreadBase::DynamicThreadBase(const size_t stackSize, const bool canReceiveSignals, const size_t queuedSignals,
240  const size_t signalActions, const uint8_t priority, const SchedulingPolicy schedulingPolicy,
241  DynamicThread& owner, Function&& function, Args&&... args) :
242  ThreadCommon{makeStack(stackSize), 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  owner_{&owner}
248 {
249 
250 }
251 
252 #elif CONFIG_SIGNALS_ENABLE == 1 && CONFIG_THREAD_DETACH_ENABLE != 1
253 
254 template<typename Function, typename... Args>
255 DynamicThreadBase::DynamicThreadBase(const size_t stackSize, const bool canReceiveSignals, const size_t queuedSignals,
256  const size_t signalActions, const uint8_t priority, const SchedulingPolicy schedulingPolicy,
257  Function&& function, Args&&... args) :
258  ThreadCommon{makeStack(stackSize), priority, schedulingPolicy, nullptr,
259  canReceiveSignals == true ? &dynamicSignalsReceiver_ : nullptr},
260  dynamicSignalsReceiver_{canReceiveSignals == true ? queuedSignals : 0,
261  canReceiveSignals == true ? signalActions : 0},
262  boundFunction_{std::bind(std::forward<Function>(function), std::forward<Args>(args)...)}
263 {
264 
265 }
266 
267 #elif CONFIG_SIGNALS_ENABLE != 1 && CONFIG_THREAD_DETACH_ENABLE == 1
268 
269 template<typename Function, typename... Args>
270 DynamicThreadBase::DynamicThreadBase(const size_t stackSize, bool, size_t, size_t, const uint8_t priority,
271  const SchedulingPolicy schedulingPolicy, DynamicThread& owner, Function&& function, Args&&... args) :
272  ThreadCommon{makeStack(stackSize), priority, schedulingPolicy, nullptr, nullptr},
273  boundFunction_{std::bind(std::forward<Function>(function), std::forward<Args>(args)...)},
274  owner_{&owner}
275 {
276 
277 }
278 
279 #else // CONFIG_SIGNALS_ENABLE != 1 && CONFIG_THREAD_DETACH_ENABLE != 1
280 
281 template<typename Function, typename... Args>
282 DynamicThreadBase::DynamicThreadBase(const size_t stackSize, bool, size_t, size_t, const uint8_t priority,
283  const SchedulingPolicy schedulingPolicy, Function&& function, Args&&... args) :
284  ThreadCommon{makeStack(stackSize), priority, schedulingPolicy, nullptr, nullptr},
285  boundFunction_{std::bind(std::forward<Function>(function), std::forward<Args>(args)...)}
286 {
287 
288 }
289 
290 #endif // CONFIG_SIGNALS_ENABLE != 1 && CONFIG_THREAD_DETACH_ENABLE != 1
291 
292 } // namespace internal
293 
294 } // namespace distortos
295 
296 #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:151
DynamicThreadBase class is a type-erased interface for thread that has dynamic storage for bound func...
Definition: DynamicThreadBase.hpp:45
DynamicThreadParameters class header.
void run() override
Thread&#39;s "run" function.
Definition: DynamicThreadBase.cpp:78
SchedulingPolicy schedulingPolicy
scheduling policy of the thread
Definition: DynamicThreadParameters.hpp:94
size_t stackSize
size of stack, bytes
Definition: DynamicThreadParameters.hpp:85
SchedulingPolicy
scheduling policy of the thread
Definition: SchedulingPolicy.hpp:26
static Stack makeStack(const size_t stackSize)
Helper function to make stack with size adjusted to alignment requirements.
Definition: DynamicThreadBase.hpp:207
void exit1Hook() override
Thread&#39;s "exit 1" hook function.
Definition: DynamicThreadBase.cpp:68
uint8_t priority
thread&#39;s priority, 0 - lowest, UINT8_MAX - highest
Definition: DynamicThreadParameters.hpp:91
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:239
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:31
void exit0Hook() override
Thread&#39;s "exit 0" hook function.
Definition: DynamicThreadBase.cpp:60
storageDeleter() definition
DynamicSignalsReceiver dynamicSignalsReceiver_
internal DynamicSignalsReceiver object
Definition: DynamicThreadBase.hpp:221
Top-level namespace of distortos project.
DynamicThreadParameters struct is a helper with parameters for DynamicThread&#39;s constructor.
Definition: DynamicThreadParameters.hpp:32
Stack class is an abstraction of architecture&#39;s stack.
Definition: Stack.hpp:32
ThreadCommon class header.
constexpr size_t stackGuardSize
size of "stack guard", bytes
Definition: stackGuardSize.hpp:26
int startInternal()
Starts the thread.
Definition: ThreadCommon.cpp:164
size_t signalActions
Definition: DynamicThreadParameters.hpp:82
std::function< void()> boundFunction_
bound function object
Definition: DynamicThreadBase.hpp:226
int detach() override
Detaches the thread.
Definition: DynamicThreadBase.cpp:38
DynamicThread * owner_
pointer to owner DynamicThread object, nullptr if thread is detached
Definition: DynamicThreadBase.hpp:231
DynamicThread class is a type-erased interface for thread that has dynamic storage for bound function...
Definition: DynamicThread.hpp:32
ThreadCommon class implements common functionality of threads.
Definition: ThreadCommon.hpp:27