PLCnext API Documentation  20.0.0.24462
WorkerThread.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/System/Commons/Threading/Thread.hpp"
9 
10 namespace Arp { namespace System { namespace Commons { namespace Threading
11 {
12 
15 {
16 public: // typedefs
18  typedef delegate<void(void)> WorkDelegate;
19 
20 public: // construction/destruction
21 
28  template<class TInstance, class TFunction>
29  WorkerThread(TInstance& instance, TFunction fn, int idleTime, const char* threadName);
30 
37  template<class TInstance, class TFunction>
38  WorkerThread(const TInstance& instance, TFunction fn, int idleTime, const char* threadName);
39 
46  template<class TInstance, class TFunction>
47  WorkerThread(TInstance* pInstance, TFunction fn, int idleTime, const char* threadName);
48 
55  template<class TInstance, class TFunction>
56  WorkerThread(const TInstance* pInstance, TFunction fn, int idleTime, const char* threadName);
57 
64  template<class TInstance, class TFunction>
65  WorkerThread(ThreadSettings& settings, TInstance& instance, TFunction fn, int idleTime);
66 
73  template<class TInstance, class TFunction>
74  WorkerThread(ThreadSettings& settings, const TInstance& instance, TFunction fn, int idleTime);
75 
82  template<class TInstance, class TFunction>
83  WorkerThread(ThreadSettings& settings, TInstance* pInstance, TFunction fn, int idleTime);
84 
91  template<class TInstance, class TFunction>
92  WorkerThread(ThreadSettings& settings, const TInstance* pInstance, TFunction fn, int idleTime);
93 
99  explicit WorkerThread(WorkDelegate&& loopDelegate, int idleTime, const char* threadName);
100 
106  WorkerThread(ThreadSettings& settings, WorkDelegate&& loopDelegate, int idleTime);
107 
109  ~WorkerThread(void) = default;
110 
111 public: // setter/getter operations
112 
115  bool IsRunning(void)const;
116 
117 public: // operations
119  void Start(void);
120 
122  void Stop(void);
123 
124 private: // static methods
125  static void RunInternal(void* pParam);
126 
127 private: // fields
128  Thread thread;
129  WorkDelegate work;
130  std::atomic<bool> stopWorking{false};
131  std::atomic<bool> isStarted{false};
132  int idleTime = 1;
133 };
134 
136 // inline methods of class WorkerThread
137 
138 template<class TInstance, class TFunction>
139 inline WorkerThread::WorkerThread(TInstance& instance, TFunction fn, int idleTime, const char* threadName)
140  : WorkerThread(&instance, fn, idleTime, threadName)
141 {
142 }
143 
144 template<class TInstance, class TFunction>
145 inline WorkerThread::WorkerThread(const TInstance& instance, TFunction fn, int idleTime, const char* threadName)
146  : WorkerThread(&instance, fn, idleTime, threadName)
147 {
148 }
149 
150 template<class TInstance, class TFunction>
151 inline WorkerThread::WorkerThread(TInstance* pInstance, TFunction fn, int idleTime, const char* threadName)
152  : thread(&WorkerThread::RunInternal, (void*)this)
153  , idleTime(idleTime)
154  , work(make_delegate(pInstance, fn))
155 {
156  this->thread.SetName(threadName);
157 }
158 
159 template<class TInstance, class TFunction>
160 inline WorkerThread::WorkerThread(const TInstance* pInstance, TFunction fn, int idleTime, const char* threadName)
161  : thread(&WorkerThread::RunInternal, (void*)this)
162  , idleTime(idleTime)
163  , work(make_delegate(pInstance, fn))
164 {
165  this->thread.SetName(threadName);
166 }
167 
168 inline WorkerThread::WorkerThread(WorkDelegate&& workDelegate, int idleTime, const char* threadName)
169  : thread(&WorkerThread::RunInternal, (void*)this)
170  , idleTime(idleTime)
171  , work(workDelegate)
172 {
173  this->thread.SetName(threadName);
174 }
175 
176 template<class TInstance, class TFunction>
177 inline WorkerThread::WorkerThread(ThreadSettings& settings, TInstance& instance, TFunction fn, int idleTimeArg)
178  : WorkerThread(settings, &instance, fn, idleTime)
179 {
180 }
181 
182 template<class TInstance, class TFunction>
183 inline WorkerThread::WorkerThread(ThreadSettings& settings, const TInstance& instance, TFunction fn, int idleTimeArg)
184  : WorkerThread(settings, &instance, fn, idleTime)
185 {
186 }
187 
188 template<class TInstance, class TFunction>
189 inline WorkerThread::WorkerThread(ThreadSettings& settings, TInstance* pInstance, TFunction fn, int idleTimeArg)
190  : thread(&WorkerThread::RunInternal, (void*)this)
191  , idleTime(idleTimeArg)
192  , work(make_delegate(pInstance, fn))
193 {
194  this->thread.SetName(settings.Name);
195  this->thread.SetCpuAffinity(settings.CpuAffinity);
196  this->thread.SetPriority(settings.Priority);
197  this->thread.SetStackSize(settings.StackSize);
198 }
199 
200 template<class TInstance, class TFunction>
201 inline WorkerThread::WorkerThread(ThreadSettings& settings, const TInstance* pInstance, TFunction fn, int idleTimeArg)
202  : thread(&WorkerThread::RunInternal, (void*)this)
203  , idleTime(idleTimeArg)
204  , work(make_delegate(pInstance, fn))
205 {
206  this->thread.SetName(settings.Name);
207  this->thread.SetCpuAffinity(settings.CpuAffinity);
208  this->thread.SetPriority(settings.Priority);
209  this->thread.SetStackSize(settings.StackSize);
210 }
211 
212 inline WorkerThread::WorkerThread(ThreadSettings& settings, WorkDelegate&& workDelegate, int idleTimeArg)
213  : thread(&WorkerThread::RunInternal, (void*)this)
214  , idleTime(idleTimeArg)
215  , work(workDelegate)
216 {
217  this->thread.SetName(settings.Name);
218  this->thread.SetCpuAffinity(settings.CpuAffinity);
219  this->thread.SetPriority(settings.Priority);
220  this->thread.SetStackSize(settings.StackSize);
221 }
222 
224 inline bool WorkerThread::IsRunning(void)const
225 {
226  return this->thread.IsRunning();
227 }
228 
229 }}}} // end of namespace Arp::System::Commons::Threading
String Name
Name for the thread.
Definition: ThreadSettings.hpp:28
delegate< R(A...)> make_delegate(R(*const function_ptr)(A...)) noexcept
Creates a delegate from a static function.
Definition: delegate.hxx:215
size_t CpuAffinity
CPU affinity mask determine which CPU is allowed to run the thread.
Definition: ThreadSettings.hpp:36
void Start(void)
Starts the execution of the thread.
Worker threads repeat the execution of the threaded code until Stop is called.
Definition: WorkerThread.hpp:14
~WorkerThread(void)=default
Deallocates the memory used for this instance but does not quit the thread.
void SetStackSize(size_t value)
Sets a new stack size for the thread.
The Thread-class provides methods to execute functions and methods in a separate thread.
Definition: Thread.hpp:91
void Stop(void)
Stops the execution of the thread syncronously.
void SetPriority(size_t value)
Assigns a new priority to the thread.
bool IsRunning(void) const
Determines if this thread is in running state.
Definition: Thread.hpp:605
bool IsRunning(void) const
Checks if the thread is in running state.
Definition: WorkerThread.hpp:224
void SetName(const String &value)
Sets a new name to the thread.
size_t Priority
Priority of thread.
Definition: ThreadSettings.hpp:31
delegate< void(void)> WorkDelegate
The delegate of threads loop body.
Definition: WorkerThread.hpp:18
Container class for adaptable thread settings.
Definition: ThreadSettings.hpp:12
Root namespace for the PLCnext API
WorkerThread(TInstance &instance, TFunction fn, int idleTime, const char *threadName)
Constructs an WorkerThread instance for a class method.
Definition: WorkerThread.hpp:139
size_t StackSize
Bytes-size of requested stack for new thread.
Definition: ThreadSettings.hpp:39
System components used by the System, Device, Plc or Io domains.
void SetCpuAffinity(size_t mask)
Pins the thread to a specific CPUs inside a multiprocessor system.