distortos  v0.5.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 
132 #if CONFIG_SIGNALS_ENABLE == 1
133 
146  SignalSet getPendingSignalSet() const override;
147 
148 #endif // CONFIG_SIGNALS_ENABLE == 1
149 
154  uint8_t getPriority() const override;
155 
160  SchedulingPolicy getSchedulingPolicy() const override;
161 
166  size_t getStackHighWaterMark() const override;
167 
172  size_t getStackSize() const override;
173 
178  ThreadState getState() const override;
179 
198  int join() override;
199 
200 #if CONFIG_SIGNALS_ENABLE == 1
201 
220  int queueSignal(uint8_t signalNumber, sigval value) override;
221 
222 #endif // CONFIG_SIGNALS_ENABLE == 1
223 
235  void setPriority(uint8_t priority, bool alwaysBehind = {}) override;
236 
241  void setSchedulingPolicy(SchedulingPolicy schedulingPolicy) override;
242 
253  int start();
254 
255  DynamicThread(const DynamicThread&) = delete;
256  DynamicThread(DynamicThread&&) = default;
257  const DynamicThread& operator=(const DynamicThread&) = delete;
258  DynamicThread& operator=(DynamicThread&&) = delete;
259 
260 private:
261 
263  std::unique_ptr<internal::DynamicThreadBase> detachableThread_;
264 };
265 
266 #else // !def CONFIG_THREAD_DETACH_ENABLE
267 
268 class DynamicThread : public internal::DynamicThreadBase
269 {
270 public:
271 
273 };
274 
275 #endif // !def CONFIG_THREAD_DETACH_ENABLE
276 
298 template<typename Function, typename... Args>
299 DynamicThread makeDynamicThread(const size_t stackSize, const bool canReceiveSignals, const size_t queuedSignals,
300  const size_t signalActions, const uint8_t priority, const SchedulingPolicy schedulingPolicy,
301  Function&& function, Args&&... args)
302 {
303  return {stackSize, canReceiveSignals, queuedSignals, signalActions, priority, schedulingPolicy,
304  std::forward<Function>(function), std::forward<Args>(args)...};
305 }
306 
320 template<typename Function, typename... Args>
321 DynamicThread makeDynamicThread(const DynamicThreadParameters parameters, Function&& function, Args&&... args)
322 {
323  return {parameters, std::forward<Function>(function), std::forward<Args>(args)...};
324 }
325 
347 template<typename Function, typename... Args>
348 DynamicThread makeAndStartDynamicThread(const size_t stackSize, const bool canReceiveSignals,
349  const size_t queuedSignals, const size_t signalActions, const uint8_t priority,
350  const SchedulingPolicy schedulingPolicy, Function&& function, Args&&... args)
351 {
352  auto thread = makeDynamicThread(stackSize, canReceiveSignals, queuedSignals, signalActions, priority,
353  schedulingPolicy, std::forward<Function>(function), std::forward<Args>(args)...);
354  {
355  const auto ret = thread.start();
356  assert(ret == 0 && "Could not start thread!");
357  }
358  return thread;
359 }
360 
374 template<typename Function, typename... Args>
375 DynamicThread makeAndStartDynamicThread(const DynamicThreadParameters parameters, Function&& function, Args&&... args)
376 {
377  auto thread = makeDynamicThread(parameters, std::forward<Function>(function), std::forward<Args>(args)...);
378  {
379  const auto ret = thread.start();
380  assert(ret == 0 && "Could not start thread!");
381  }
382  return thread;
383 }
384 
386 
387 #ifdef CONFIG_THREAD_DETACH_ENABLE
388 
389 template<typename Function, typename... Args>
390 DynamicThread::DynamicThread(const size_t stackSize, const bool canReceiveSignals, const size_t queuedSignals,
391  const size_t signalActions, const uint8_t priority, const SchedulingPolicy schedulingPolicy,
392  Function&& function, Args&&... args) :
393  detachableThread_{new internal::DynamicThreadBase{stackSize, canReceiveSignals, queuedSignals, signalActions,
394  priority, schedulingPolicy, *this, std::forward<Function>(function), std::forward<Args>(args)...}}
395 {
396 
397 }
398 
399 #endif // def CONFIG_THREAD_DETACH_ENABLE
400 
401 } // namespace distortos
402 
403 #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:62
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:348
DynamicThreadBase class is a type-erased interface for thread that has dynamic storage for bound func...
Definition: DynamicThreadBase.hpp:44
int queueSignal(uint8_t signalNumber, sigval value) override
Queues signal for thread.
Definition: DynamicThread.cpp:162
SchedulingPolicy schedulingPolicy
scheduling policy of the thread
Definition: DynamicThreadParameters.hpp:94
size_t getStackHighWaterMark() const override
Definition: DynamicThread.cpp:118
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:108
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:244
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:148
ThreadState getState() const override
Definition: DynamicThread.cpp:138
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:174
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:184
~DynamicThread() override
DynamicThread&#39;s destructor.
Definition: DynamicThread.cpp:33
size_t getStackSize() const override
Definition: DynamicThread.cpp:128
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:263
uint8_t getPriority() const override
Definition: DynamicThread.cpp:98
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:390
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:86
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:299
Thread class is a pure abstract interface for threads.
Definition: Thread.hpp:32
ThreadState
state of the thread
Definition: ThreadState.hpp:28
int start()
Starts the thread.
Definition: DynamicThread.cpp:194
uint8_t getEffectivePriority() const override
Definition: DynamicThread.cpp:74