distortos  v0.6.0
object-oriented C++ RTOS for microcontrollers
DynamicThread.hpp
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_DISTORTOS_DYNAMICTHREAD_HPP_
13 #define INCLUDE_DISTORTOS_DYNAMICTHREAD_HPP_
14 
16 
17 #include "distortos/assert.h"
18 
19 namespace distortos
20 {
21 
24 
30 #ifdef CONFIG_THREAD_DETACH_ENABLE
31 
32 class DynamicThread : public Thread
33 {
34 public:
35 
55  template<typename Function, typename... Args>
56  DynamicThread(size_t stackSize, bool canReceiveSignals, size_t queuedSignals, size_t signalActions,
57  uint8_t priority, SchedulingPolicy schedulingPolicy, Function&& function, Args&&... args);
58 
70  template<typename Function, typename... Args>
71  DynamicThread(const DynamicThreadParameters parameters, Function&& function, Args&&... args) :
72  DynamicThread{parameters.stackSize, parameters.canReceiveSignals, parameters.queuedSignals,
73  parameters.signalActions, parameters.priority, parameters.schedulingPolicy,
74  std::forward<Function>(function), std::forward<Args>(args)...}
75  {
76 
77  }
78 
83  ~DynamicThread() override;
84 
101  int detach() override;
102 
103 #if CONFIG_SIGNALS_ENABLE == 1
104 
122  int generateSignal(uint8_t signalNumber) override;
123 
124 #endif // CONFIG_SIGNALS_ENABLE == 1
125 
130  uint8_t getEffectivePriority() const override;
131 
137  ThreadIdentifier getIdentifier() const override;
138 
139 #if CONFIG_SIGNALS_ENABLE == 1
140 
153  SignalSet getPendingSignalSet() const override;
154 
155 #endif // CONFIG_SIGNALS_ENABLE == 1
156 
161  uint8_t getPriority() const override;
162 
167  SchedulingPolicy getSchedulingPolicy() const override;
168 
173  size_t getStackHighWaterMark() const override;
174 
179  size_t getStackSize() const override;
180 
185  ThreadState getState() const override;
186 
205  int join() override;
206 
207 #if CONFIG_SIGNALS_ENABLE == 1
208 
227  int queueSignal(uint8_t signalNumber, sigval value) override;
228 
229 #endif // CONFIG_SIGNALS_ENABLE == 1
230 
242  void setPriority(uint8_t priority, bool alwaysBehind = {}) override;
243 
248  void setSchedulingPolicy(SchedulingPolicy schedulingPolicy) override;
249 
260  int start();
261 
262  DynamicThread(const DynamicThread&) = delete;
263  DynamicThread(DynamicThread&&) = default;
264  const DynamicThread& operator=(const DynamicThread&) = delete;
265  DynamicThread& operator=(DynamicThread&&) = delete;
266 
267 private:
268 
270  std::unique_ptr<internal::DynamicThreadBase> detachableThread_;
271 };
272 
273 #else // !def CONFIG_THREAD_DETACH_ENABLE
274 
276 {
277 public:
278 
280 };
281 
282 #endif // !def CONFIG_THREAD_DETACH_ENABLE
283 
305 template<typename Function, typename... Args>
306 DynamicThread makeDynamicThread(const size_t stackSize, const bool canReceiveSignals, const size_t queuedSignals,
307  const size_t signalActions, const uint8_t priority, const SchedulingPolicy schedulingPolicy,
308  Function&& function, Args&&... args)
309 {
310  return {stackSize, canReceiveSignals, queuedSignals, signalActions, priority, schedulingPolicy,
311  std::forward<Function>(function), std::forward<Args>(args)...};
312 }
313 
327 template<typename Function, typename... Args>
328 DynamicThread makeDynamicThread(const DynamicThreadParameters parameters, Function&& function, Args&&... args)
329 {
330  return {parameters, std::forward<Function>(function), std::forward<Args>(args)...};
331 }
332 
354 template<typename Function, typename... Args>
355 DynamicThread makeAndStartDynamicThread(const size_t stackSize, const bool canReceiveSignals,
356  const size_t queuedSignals, const size_t signalActions, const uint8_t priority,
357  const SchedulingPolicy schedulingPolicy, Function&& function, Args&&... args)
358 {
359  auto thread = makeDynamicThread(stackSize, canReceiveSignals, queuedSignals, signalActions, priority,
360  schedulingPolicy, std::forward<Function>(function), std::forward<Args>(args)...);
361  {
362  const auto ret = thread.start();
363  assert(ret == 0 && "Could not start thread!");
364  }
365  return thread;
366 }
367 
381 template<typename Function, typename... Args>
382 DynamicThread makeAndStartDynamicThread(const DynamicThreadParameters parameters, Function&& function, Args&&... args)
383 {
384  auto thread = makeDynamicThread(parameters, std::forward<Function>(function), std::forward<Args>(args)...);
385  {
386  const auto ret = thread.start();
387  assert(ret == 0 && "Could not start thread!");
388  }
389  return thread;
390 }
391 
393 
394 #ifdef CONFIG_THREAD_DETACH_ENABLE
395 
396 template<typename Function, typename... Args>
397 DynamicThread::DynamicThread(const size_t stackSize, const bool canReceiveSignals, const size_t queuedSignals,
398  const size_t signalActions, const uint8_t priority, const SchedulingPolicy schedulingPolicy,
399  Function&& function, Args&&... args) :
400  detachableThread_{new internal::DynamicThreadBase{stackSize, canReceiveSignals, queuedSignals, signalActions,
401  priority, schedulingPolicy, *this, std::forward<Function>(function), std::forward<Args>(args)...}}
402 {
403 
404 }
405 
406 #endif // def CONFIG_THREAD_DETACH_ENABLE
407 
408 } // namespace distortos
409 
410 #endif // INCLUDE_DISTORTOS_DYNAMICTHREAD_HPP_
DynamicThread(const DynamicThreadParameters parameters, Function &&function, Args &&... args)
DynamicThread&#39;s constructor.
Definition: DynamicThread.hpp:71
int generateSignal(uint8_t signalNumber) override
Generates signal for thread.
Definition: DynamicThread.cpp:63
Definition: signal.h:28
DynamicThreadBase class header.
size_t queuedSignals
Definition: DynamicThreadParameters.hpp:78
DynamicThread makeAndStartDynamicThread(const size_t stackSize, const bool canReceiveSignals, const size_t queuedSignals, const size_t signalActions, const uint8_t priority, const SchedulingPolicy schedulingPolicy, Function &&function, Args &&... args)
Helper factory function to make and start DynamicThread object.
Definition: DynamicThread.hpp:355
DynamicThreadBase class is a type-erased interface for thread that has dynamic storage for bound func...
Definition: DynamicThreadBase.hpp:45
int queueSignal(uint8_t signalNumber, sigval value) override
Queues signal for thread.
Definition: DynamicThread.cpp:173
SchedulingPolicy schedulingPolicy
scheduling policy of the thread
Definition: DynamicThreadParameters.hpp:94
size_t getStackHighWaterMark() const override
Definition: DynamicThread.cpp:129
size_t stackSize
size of stack, bytes
Definition: DynamicThreadParameters.hpp:85
SchedulingPolicy
scheduling policy of the thread
Definition: SchedulingPolicy.hpp:26
SchedulingPolicy getSchedulingPolicy() const override
Definition: DynamicThread.cpp:119
uint8_t priority
thread&#39;s priority, 0 - lowest, UINT8_MAX - highest
Definition: DynamicThreadParameters.hpp:91
assert() macro
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
int join() override
Waits for thread termination.
Definition: DynamicThread.cpp:159
ThreadState getState() const override
Definition: DynamicThread.cpp:149
void setPriority(uint8_t priority, bool alwaysBehind={}) override
Changes priority of thread.
Definition: DynamicThread.cpp:185
Top-level namespace of distortos project.
void setSchedulingPolicy(SchedulingPolicy schedulingPolicy) override
Definition: DynamicThread.cpp:195
~DynamicThread() override
DynamicThread&#39;s destructor.
Definition: DynamicThread.cpp:34
size_t getStackSize() const override
Definition: DynamicThread.cpp:139
DynamicThreadParameters struct is a helper with parameters for DynamicThread&#39;s constructor.
Definition: DynamicThreadParameters.hpp:32
ThreadIdentifier getIdentifier() const override
Definition: DynamicThread.cpp:85
std::unique_ptr< internal::DynamicThreadBase > detachableThread_
internal thread object
Definition: DynamicThread.hpp:270
uint8_t getPriority() const override
Definition: DynamicThread.cpp:109
DynamicThread(size_t stackSize, bool canReceiveSignals, size_t queuedSignals, size_t signalActions, uint8_t priority, SchedulingPolicy schedulingPolicy, Function &&function, Args &&... args)
DynamicThread&#39;s constructor.
Definition: DynamicThread.hpp:397
size_t signalActions
Definition: DynamicThreadParameters.hpp:82
DynamicThread class is a type-erased interface for thread that has dynamic storage for bound function...
Definition: DynamicThread.hpp:32
int detach() override
Detaches the thread.
Definition: DynamicThread.cpp:39
SignalSet getPendingSignalSet() const override
Gets set of currently pending signals.
Definition: DynamicThread.cpp:97
DynamicThread makeDynamicThread(const size_t stackSize, const bool canReceiveSignals, const size_t queuedSignals, const size_t signalActions, const uint8_t priority, const SchedulingPolicy schedulingPolicy, Function &&function, Args &&... args)
Helper factory function to make DynamicThread object.
Definition: DynamicThread.hpp:306
Thread class is a pure abstract interface for threads.
Definition: Thread.hpp:34
ThreadState
state of the thread
Definition: ThreadState.hpp:28
int start()
Starts the thread.
Definition: DynamicThread.cpp:205
uint8_t getEffectivePriority() const override
Definition: DynamicThread.cpp:75