distortos  v0.4.0
object-oriented C++ RTOS for microcontrollers
StaticThread.hpp
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_DISTORTOS_STATICTHREAD_HPP_
13 #define INCLUDE_DISTORTOS_STATICTHREAD_HPP_
14 
15 #include "distortos/assert.h"
18 
19 namespace distortos
20 {
21 
22 namespace internal
23 {
24 
32 template<typename Function, typename... Args>
34 {
35 public:
36 
39 
54  StaticThreadBase(StackStorageUniquePointer&& stackStorageUniquePointer, const size_t size, const uint8_t priority,
55  const SchedulingPolicy schedulingPolicy, SignalsReceiver* const signalsReceiver, Function&& function,
56  Args&&... args) :
57  Base{{std::move(stackStorageUniquePointer), size}, priority, schedulingPolicy, nullptr,
58  signalsReceiver},
59  boundFunction_{std::bind(std::forward<Function>(function), std::forward<Args>(args)...)}
60  {
61 
62  }
63 
64  StaticThreadBase(const StaticThreadBase&) = delete;
66  const StaticThreadBase& operator=(const StaticThreadBase&) = delete;
67  StaticThreadBase& operator=(StaticThreadBase&&) = delete;
68 
78  int start()
79  {
81  }
82 
83 protected:
84 
86  constexpr static size_t stackGuardSize_ {(CONFIG_STACK_GUARD_SIZE + CONFIG_ARCHITECTURE_STACK_ALIGNMENT - 1) /
87  CONFIG_ARCHITECTURE_STACK_ALIGNMENT * CONFIG_ARCHITECTURE_STACK_ALIGNMENT};
88 
89 private:
90 
99  static void run(Thread& thread)
100  {
101  static_cast<StaticThreadBase&>(thread).boundFunction_();
102  }
103 
105  decltype(std::bind(std::declval<Function>(), std::declval<Args>()...)) boundFunction_;
106 };
107 
108 } // namespace internal
109 
112 
126 template<size_t StackSize, bool CanReceiveSignals, size_t QueuedSignals, size_t SignalActions, typename Function,
127  typename... Args>
128 class StaticThread : public internal::StaticThreadBase<Function, Args...>
129 {
130 public:
131 
133  using Base = internal::StaticThreadBase<Function, Args...>;
134 
144  StaticThread(uint8_t priority, SchedulingPolicy schedulingPolicy, Function&& function, Args&&... args);
145 
154  StaticThread(const uint8_t priority, Function&& function, Args&&... args) :
155  StaticThread{priority, SchedulingPolicy::roundRobin, std::forward<Function>(function),
156  std::forward<Args>(args)...}
157  {
158 
159  }
160 
161  StaticThread(const StaticThread&) = delete;
162  StaticThread(StaticThread&&) = default;
163  const StaticThread& operator=(const StaticThread&) = delete;
164  StaticThread& operator=(StaticThread&&) = delete;
165 
166 private:
167 
169  typename std::aligned_storage<StackSize + Base::stackGuardSize_>::type stack_;
170 };
171 
187 template<size_t StackSize, size_t QueuedSignals, size_t SignalActions, typename Function, typename... Args>
188 class StaticThread<StackSize, true, QueuedSignals, SignalActions, Function, Args...> :
189  public internal::StaticThreadBase<Function, Args...>
190 {
191 public:
192 
194  using Base = internal::StaticThreadBase<Function, Args...>;
195 
205  StaticThread(uint8_t priority, SchedulingPolicy schedulingPolicy, Function&& function, Args&&... args);
206 
215  StaticThread(const uint8_t priority, Function&& function, Args&&... args) :
216  StaticThread{priority, SchedulingPolicy::roundRobin, std::forward<Function>(function),
217  std::forward<Args>(args)...}
218  {
219 
220  }
221 
222  StaticThread(const StaticThread&) = delete;
223  StaticThread(StaticThread&&) = default;
224  const StaticThread& operator=(const StaticThread&) = delete;
225  StaticThread& operator=(StaticThread&&) = delete;
226 
227 private:
228 
230  typename std::aligned_storage<StackSize + Base::stackGuardSize_>::type stack_;
231 
234 };
235 
256 template<size_t StackSize, bool CanReceiveSignals = {}, size_t QueuedSignals = {}, size_t SignalActions = {},
257  typename Function, typename... Args>
258 StaticThread<StackSize, CanReceiveSignals, QueuedSignals, SignalActions, Function, Args...>
259 makeStaticThread(const uint8_t priority, const SchedulingPolicy schedulingPolicy, Function&& function, Args&&... args)
260 {
261  return {priority, schedulingPolicy, std::forward<Function>(function), std::forward<Args>(args)...};
262 }
263 
283 template<size_t StackSize, bool CanReceiveSignals = {}, size_t QueuedSignals = {}, size_t SignalActions = {},
284  typename Function, typename... Args>
285 StaticThread<StackSize, CanReceiveSignals, QueuedSignals, SignalActions, Function, Args...>
286 makeStaticThread(const uint8_t priority, Function&& function, Args&&... args)
287 {
288  return {priority, std::forward<Function>(function), std::forward<Args>(args)...};
289 }
290 
311 template<size_t StackSize, bool CanReceiveSignals = {}, size_t QueuedSignals = {}, size_t SignalActions = {},
312  typename Function, typename... Args>
313 StaticThread<StackSize, CanReceiveSignals, QueuedSignals, SignalActions, Function, Args...>
314 makeAndStartStaticThread(const uint8_t priority, const SchedulingPolicy schedulingPolicy, Function&& function,
315  Args&&... args)
316 {
317  auto thread = makeStaticThread<StackSize, CanReceiveSignals, QueuedSignals, SignalActions>(priority,
318  schedulingPolicy, std::forward<Function>(function), std::forward<Args>(args)...);
319  {
320  const auto ret = thread.start();
321  assert(ret == 0 && "Could not start thread!");
322  }
323  return thread;
324 }
325 
345 template<size_t StackSize, bool CanReceiveSignals = {}, size_t QueuedSignals = {}, size_t SignalActions = {},
346  typename Function, typename... Args>
347 StaticThread<StackSize, CanReceiveSignals, QueuedSignals, SignalActions, Function, Args...>
348 makeAndStartStaticThread(const uint8_t priority, Function&& function, Args&&... args)
349 {
350  auto thread = makeStaticThread<StackSize, CanReceiveSignals, QueuedSignals, SignalActions>(priority,
351  std::forward<Function>(function), std::forward<Args>(args)...);
352  {
353  const auto ret = thread.start();
354  assert(ret == 0 && "Could not start thread!");
355  }
356  return thread;
357 }
358 
360 
361 template<size_t StackSize, bool CanReceiveSignals, size_t QueuedSignals, size_t SignalActions, typename Function,
362  typename... Args>
364 StaticThread(const uint8_t priority, const SchedulingPolicy schedulingPolicy, Function&& function, Args&&... args) :
365  Base{{&stack_, internal::dummyDeleter<decltype(stack_)>}, sizeof(stack_), priority, schedulingPolicy, nullptr,
366  std::forward<Function>(function), std::forward<Args>(args)...}
367 {
368 
369 }
370 
371 template<size_t StackSize, size_t QueuedSignals, size_t SignalActions, typename Function, typename... Args>
373  const SchedulingPolicy schedulingPolicy, Function&& function, Args&&... args) :
374  Base{{&stack_, internal::dummyDeleter<decltype(stack_)>}, sizeof(stack_), priority, schedulingPolicy,
375  &static_cast<SignalsReceiver&>(staticSignalsReceiver_), std::forward<Function>(function),
376  std::forward<Args>(args)...},
377  staticSignalsReceiver_{}
378 {
379 
380 }
381 
382 } // namespace distortos
383 
384 #endif // INCLUDE_DISTORTOS_STATICTHREAD_HPP_
StaticThread(const uint8_t priority, Function &&function, Args &&... args)
StaticThread&#39;s constructor.
Definition: StaticThread.hpp:215
UndetachableThread class is a ThreadCommon that cannot be detached.
Definition: UndetachableThread.hpp:28
StaticSignalsReceiver< QueuedSignals, SignalActions > staticSignalsReceiver_
internal StaticSignalsReceiver object
Definition: StaticThread.hpp:233
UndetachableThread class header.
StaticThread(const uint8_t priority, Function &&function, Args &&... args)
StaticThread&#39;s constructor.
Definition: StaticThread.hpp:154
StaticThreadBase class is a templated common base for StaticThread.
Definition: StaticThread.hpp:33
StaticThread< StackSize, CanReceiveSignals, QueuedSignals, SignalActions, Function, Args... > makeAndStartStaticThread(const uint8_t priority, const SchedulingPolicy schedulingPolicy, Function &&function, Args &&... args)
Helper factory function to make and start StaticThread object with partially deduced template argumen...
Definition: StaticThread.hpp:314
StaticThreadBase(StackStorageUniquePointer &&stackStorageUniquePointer, const size_t size, const uint8_t priority, const SchedulingPolicy schedulingPolicy, SignalsReceiver *const signalsReceiver, Function &&function, Args &&... args)
StaticThreadBase&#39;s constructor.
Definition: StaticThread.hpp:54
static void run(Thread &thread)
Thread&#39;s "run" function.
Definition: StaticThread.hpp:99
decltype(std::bind(std::declval< Function >(), std::declval< Args >()...)) boundFunction_
bound function object
Definition: StaticThread.hpp:105
SignalsReceiver class is a container for internal::SignalsReceiverControlBlock.
Definition: SignalsReceiver.hpp:28
std::aligned_storage< StackSize+Base::stackGuardSize_ >::type stack_
stack buffer
Definition: StaticThread.hpp:169
SchedulingPolicy
scheduling policy of the thread
Definition: SchedulingPolicy.hpp:26
std::aligned_storage< StackSize+Base::stackGuardSize_ >::type stack_
stack buffer
Definition: StaticThread.hpp:230
static void terminationHook(Thread &thread)
Termination hook function of thread.
Definition: ThreadCommon.cpp:143
StaticThread< StackSize, CanReceiveSignals, QueuedSignals, SignalActions, Function, Args... > makeStaticThread(const uint8_t priority, const SchedulingPolicy schedulingPolicy, Function &&function, Args &&... args)
Helper factory function to make StaticThread object with partially deduced template arguments...
Definition: StaticThread.hpp:259
assert() macro
StaticSignalsReceiver class header.
Top-level namespace of distortos project.
void thread(uint32_t value, bool &sharedResult)
Test thread.
Definition: ARMv7-M-FpuThreadTestCase.cpp:68
StaticThread(uint8_t priority, SchedulingPolicy schedulingPolicy, Function &&function, Args &&... args)
StaticThread&#39;s constructor.
Definition: StaticThread.hpp:364
internal::Stack::StorageUniquePointer StackStorageUniquePointer
unique_ptr (with deleter) to storage for stack
Definition: ThreadCommon.hpp:34
static constexpr size_t stackGuardSize_
size of "stack guard", bytes
Definition: StaticThread.hpp:86
int start()
Starts the thread.
Definition: StaticThread.hpp:78
int startInternal(void(&runFunction)(Thread &), void(*preTerminationHookFunction)(Thread &), void(&terminationHookFunction)(Thread &))
Starts the thread.
Definition: ThreadCommon.cpp:132
StaticThread class is a templated interface for thread that has automatic storage for stack...
Definition: StaticThread.hpp:128
StaticSignalsReceiver class is a templated interface for SignalsReceiver that has automatic storage f...
Definition: StaticSignalsReceiver.hpp:34
round-robin scheduling policy
Thread class is a pure abstract interface for threads.
Definition: Thread.hpp:32