distortos  v0.4.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 
120  int generateSignal(uint8_t signalNumber) override;
121 
126  uint8_t getEffectivePriority() const override;
127 
140  SignalSet getPendingSignalSet() const override;
141 
146  uint8_t getPriority() const override;
147 
152  SchedulingPolicy getSchedulingPolicy() const override;
153 
158  size_t getStackHighWaterMark() const override;
159 
164  size_t getStackSize() const override;
165 
170  ThreadState getState() const override;
171 
190  int join() override;
191 
210  int queueSignal(uint8_t signalNumber, sigval value) override;
211 
223  void setPriority(uint8_t priority, bool alwaysBehind = {}) override;
224 
229  void setSchedulingPolicy(SchedulingPolicy schedulingPolicy) override;
230 
241  int start();
242 
243  DynamicThread(const DynamicThread&) = delete;
244  DynamicThread(DynamicThread&&) = default;
245  const DynamicThread& operator=(const DynamicThread&) = delete;
246  DynamicThread& operator=(DynamicThread&&) = delete;
247 
248 private:
249 
251  std::unique_ptr<internal::DynamicThreadBase> detachableThread_;
252 };
253 
254 #else // !def CONFIG_THREAD_DETACH_ENABLE
255 
256 class DynamicThread : public internal::DynamicThreadBase
257 {
258 public:
259 
261 };
262 
263 #endif // !def CONFIG_THREAD_DETACH_ENABLE
264 
286 template<typename Function, typename... Args>
287 DynamicThread makeDynamicThread(const size_t stackSize, const bool canReceiveSignals, const size_t queuedSignals,
288  const size_t signalActions, const uint8_t priority, const SchedulingPolicy schedulingPolicy,
289  Function&& function, Args&&... args)
290 {
291  return {stackSize, canReceiveSignals, queuedSignals, signalActions, priority, schedulingPolicy,
292  std::forward<Function>(function), std::forward<Args>(args)...};
293 }
294 
308 template<typename Function, typename... Args>
309 DynamicThread makeDynamicThread(const DynamicThreadParameters parameters, Function&& function, Args&&... args)
310 {
311  return {parameters, std::forward<Function>(function), std::forward<Args>(args)...};
312 }
313 
335 template<typename Function, typename... Args>
336 DynamicThread makeAndStartDynamicThread(const size_t stackSize, const bool canReceiveSignals,
337  const size_t queuedSignals, const size_t signalActions, const uint8_t priority,
338  const SchedulingPolicy schedulingPolicy, Function&& function, Args&&... args)
339 {
340  auto thread = makeDynamicThread(stackSize, canReceiveSignals, queuedSignals, signalActions, priority,
341  schedulingPolicy, std::forward<Function>(function), std::forward<Args>(args)...);
342  {
343  const auto ret = thread.start();
344  assert(ret == 0 && "Could not start thread!");
345  }
346  return thread;
347 }
348 
362 template<typename Function, typename... Args>
363 DynamicThread makeAndStartDynamicThread(const DynamicThreadParameters parameters, Function&& function, Args&&... args)
364 {
365  auto thread = makeDynamicThread(parameters, std::forward<Function>(function), std::forward<Args>(args)...);
366  {
367  const auto ret = thread.start();
368  assert(ret == 0 && "Could not start thread!");
369  }
370  return thread;
371 }
372 
374 
375 #ifdef CONFIG_THREAD_DETACH_ENABLE
376 
377 template<typename Function, typename... Args>
378 DynamicThread::DynamicThread(const size_t stackSize, const bool canReceiveSignals, const size_t queuedSignals,
379  const size_t signalActions, const uint8_t priority, const SchedulingPolicy schedulingPolicy,
380  Function&& function, Args&&... args) :
381  detachableThread_{new internal::DynamicThreadBase{stackSize, canReceiveSignals, queuedSignals, signalActions,
382  priority, schedulingPolicy, *this, std::forward<Function>(function), std::forward<Args>(args)...}}
383 {
384 
385 }
386 
387 #endif // def CONFIG_THREAD_DETACH_ENABLE
388 
389 } // namespace distortos
390 
391 #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:60
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:336
DynamicThreadBase class is a type-erased interface for thread that has dynamic storage for bound func...
Definition: DynamicThreadBase.hpp:42
int queueSignal(uint8_t signalNumber, sigval value) override
Queues signal for thread.
Definition: DynamicThread.cpp:152
SchedulingPolicy schedulingPolicy
scheduling policy of the thread
Definition: DynamicThreadParameters.hpp:94
size_t getStackHighWaterMark() const override
Definition: DynamicThread.cpp:110
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:100
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:221
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:140
ThreadState getState() const override
Definition: DynamicThread.cpp:130
SignalSet class is used as a set of signals.
Definition: SignalSet.hpp:28
void setPriority(uint8_t priority, bool alwaysBehind={}) override
Changes priority of thread.
Definition: DynamicThread.cpp:162
Top-level namespace of distortos project.
void thread(uint32_t value, bool &sharedResult)
Test thread.
Definition: ARMv7-M-FpuThreadTestCase.cpp:68
void setSchedulingPolicy(SchedulingPolicy schedulingPolicy) override
Definition: DynamicThread.cpp:172
~DynamicThread() override
DynamicThread&#39;s destructor.
Definition: DynamicThread.cpp:33
size_t getStackSize() const override
Definition: DynamicThread.cpp:120
DynamicThreadParameters struct is a helper with parameters for DynamicThread&#39;s constructor.
Definition: DynamicThreadParameters.hpp:32
std::unique_ptr< internal::DynamicThreadBase > detachableThread_
internal thread object
Definition: DynamicThread.hpp:251
uint8_t getPriority() const override
Definition: DynamicThread.cpp:90
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:378
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:38
SignalSet getPendingSignalSet() const override
Gets set of currently pending signals.
Definition: DynamicThread.cpp:80
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:287
Thread class is a pure abstract interface for threads.
Definition: Thread.hpp:32
ThreadState
state of the thread
Definition: ThreadState.hpp:26
int start()
Starts the thread.
Definition: DynamicThread.cpp:182
uint8_t getEffectivePriority() const override
Definition: DynamicThread.cpp:70