8 #include "Arp/System/Core/delegate.hxx" 9 #include "Arp/System/Commons/Threading/ThreadSettings.hpp" 10 #include "Arp/System/Commons/Logging.h" 11 #include "Arp/System/Commons/Threading/AutoResetEvent.hpp" 12 #include "Arp/System/Commons/Threading/ThreadState.hpp" 13 #include "Arp/System/Commons/Threading/Semaphore.hpp" 18 namespace Arp {
namespace System {
namespace Ve
23 namespace Arp {
namespace System {
namespace Commons {
namespace Threading
27 class ThreadBinaryCompatibilityExtensions;
30 class AutoResetEvent2;
80 friend class AutoResetEvent2;
84 typedef void (*ThreadStartFunction)(
void*);
99 template<
class TInstance,
class TFunction>
100 ARP_DEPRECATED(
"Use constructor with ThreadSettings instead. Supply at least a thread name!")
101 Thread(TInstance& instance, TFunction fn,
void* pStartParam =
nullptr);
108 template<class TInstance, class TFunction>
109 ARP_DEPRECATED("Use constructor with
ThreadSettings instead. Supply at least a thread name!")
110 Thread(const TInstance& instance, TFunction fn,
void* pStartParam =
nullptr);
117 template<class TInstance, class TFunction>
118 ARP_DEPRECATED("Use constructor with
ThreadSettings instead. Supply at least a thread name!")
119 Thread(TInstance* pInstance, TFunction fn,
void* pStartParam =
nullptr);
126 template<class TInstance, class TFunction>
127 ARP_DEPRECATED("Use constructor with
ThreadSettings instead. Supply at least a thread name!")
128 Thread(const TInstance* pInstance, TFunction fn,
void* pStartParam =
nullptr);
134 ARP_DEPRECATED("Use constructor with
ThreadSettings instead. Supply at least a thread name!")
135 explicit
Thread(ThreadStartDelegate&& threadStart,
void* pStartParam =
nullptr);
145 ARP_DEPRECATED("Use constructor with
ThreadSettings instead. Supply at least a thread name!")
146 Thread(ThreadStartFunction threadStart,
void* pStartParam =
nullptr);
154 template<class TInstance, class TFunction>
155 Thread(const
ThreadSettings& settings, TInstance& instance, TFunction fn,
void* pStartParam =
nullptr);
163 template<class TInstance, class TFunction>
164 Thread(const
ThreadSettings& settings, const TInstance& instance, TFunction fn,
void* pStartParam =
nullptr);
172 template<class TInstance, class TFunction>
173 Thread(const
ThreadSettings& settings, TInstance* pInstance, TFunction fn,
void* pStartParam =
nullptr);
181 template<class TInstance, class TFunction>
182 Thread(const
ThreadSettings& settings, const TInstance* pInstance, TFunction fn,
void* pStartParam =
nullptr);
189 Thread(
ThreadSettings& settings, ThreadStartDelegate&& threadStart,
void* pStartParam =
nullptr);
203 Thread(const
ThreadSettings& settings, ThreadStartDelegate&& threadStart,
void* pStartParam =
nullptr);
210 Thread(const
ThreadSettings& settings, ThreadStartFunction threadStart,
void* pStartParam =
nullptr);
221 static
void Sleep(
size_t milliseconds);
225 static
size_t GetCurrentThreadId(
void);
229 static
Thread* GetCurrentThread(
void);
239 static
void SetAsynchronousCancelability(
bool enable);
243 bool IsRunning(
void)const;
262 void SetCpuAffinity(
size_t mask);
267 size_t GetCpuAffinity(
void)const;
272 void SetPriority(
size_t value);
276 size_t GetPriority(
void)const;
290 ARP_DEPRECATED("Set thread stack size at the constructor")
291 void SetStackSize(
size_t value);
295 size_t GetStackSize(
void)const;
304 void SetName(const
String& value);
308 const
String& GetName(
void)const;
318 bool IsJoinable(
void);
335 void Interrupt(
void);
338 void Terminate(
void);
341 using IThreadService =
Arp::System::Ve::IThreadService;
344 static
void* RunInternal(
void* pParam);
345 static
void CheckInterruptOfCurrentThread(
void);
346 static
void SetStateOfCurrentThread(
ThreadState state);
347 static
String CreateDefaultThreadName(
void);
350 void CreateThreadInternal(
void);
352 void RunThread(ThreadBinaryCompatibilityExtensions* pBinCompat);
359 ThreadStartDelegate threadStart;
361 ThreadBinaryCompatibilityExtensions* pBinaryCompatibilityExtensions;
364 std::atomic<
bool> interrupting;
367 static thread_local
Thread* pCurrentThread;
372 template<class TInstance, class TFunction>
373 inline
Thread::
Thread(TInstance& instance, TFunction fn,
void* pStartParam)
374 :
Thread(&instance, fn, pStartParam)
378 template<
class TInstance,
class TFunction>
379 inline Thread::Thread(
const TInstance& instance, TFunction fn,
void* pStartParam)
380 :
Thread(&instance, fn, pStartParam)
384 template<
class TInstance,
class TFunction>
387 , pStartParam(pStartParam)
388 , pBinaryCompatibilityExtensions(nullptr)
390 , interrupting(false)
392 this->CreateThreadInternal();
395 template<
class TInstance,
class TFunction>
396 inline Thread::Thread(
const TInstance* pInstance, TFunction fn,
void* pStartParam)
398 , pStartParam(pStartParam)
399 , pBinaryCompatibilityExtensions(nullptr)
401 , interrupting(false)
403 this->CreateThreadInternal();
407 : threadStart(
std::move(threadStart))
408 , pStartParam(pStartParam)
409 , pBinaryCompatibilityExtensions(nullptr)
411 , interrupting(false)
413 this->CreateThreadInternal();
418 , pStartParam(pStartParam)
419 , pBinaryCompatibilityExtensions(nullptr)
421 , interrupting(false)
423 this->CreateThreadInternal();
426 template<
class TInstance,
class TFunction>
428 :
Thread(settings, &instance, fn, pStartParam)
432 template<
class TInstance,
class TFunction>
434 :
Thread(settings, &instance, fn, pStartParam)
438 template<
class TInstance,
class TFunction>
441 , pStartParam(pStartParam)
442 , pBinaryCompatibilityExtensions(nullptr)
444 , interrupting(false)
446 this->CreateThreadInternal(settings);
449 template<
class TInstance,
class TFunction>
452 , pStartParam(pStartParam)
453 , pBinaryCompatibilityExtensions(nullptr)
455 , interrupting(false)
457 this->CreateThreadInternal(settings);
471 : threadStart(
std::move(threadStart))
472 , pStartParam(pStartParam)
473 , pBinaryCompatibilityExtensions(nullptr)
475 , interrupting(false)
477 this->CreateThreadInternal(settings);
482 , pStartParam(pStartParam)
483 , pBinaryCompatibilityExtensions(nullptr)
485 , interrupting(false)
487 this->CreateThreadInternal(settings);
497 return Thread::pCurrentThread;
502 this->interrupting =
true;
507 return this->state.load();
Event object to signal a single thread that an event has occurred. Can be used to synchronize threads...
Definition: AutoResetEvent.hpp:22
delegate< R(A...)> make_delegate(R(*const function_ptr)(A...)) noexcept
Creates a delegate from a static function.
Definition: delegate.hxx:223
static Thread * GetCurrentThread(void)
Returns the current thread of the calling context.
Definition: Thread.hpp:495
Namespace of the C++ standard library
static const std::size_t CpuAffinityAll
Use this constant to express an affinity of the thread to all available CPUs aka. cores...
Definition: Thread.hpp:90
The Thread-class provides methods to execute functions and methods in a separate thread.
Definition: Thread.hpp:76
Definition: Loggable.hxx:18
delegate< void(void *)> ThreadStartDelegate
Definition of signature of class method to be executed in a separate thread.
Definition: Thread.hpp:87
summary>Thread is either waiting for an event, currently sleeping or waits for another thread to fini...
bool IsRunning(void) const
Determines if this thread is in running state.
Definition: Thread.hpp:490
Container class for adaptable thread settings.
Definition: ThreadSettings.hpp:12
Root namespace for the PLCnext API
ThreadState
Possible thread states.
Definition: ThreadState.hpp:14
void Interrupt(void)
Interrupts the currents execution.
Definition: Thread.hpp:500
void(* ThreadStartFunction)(void *)
Definition of signature of function to be executed in a separate thread.
Definition: Thread.hpp:84
System components used by the System, Device, Plc or Io domains.
Thread(TInstance &instance, TFunction fn, void *pStartParam=nullptr)
Constructs a Thread instance for a class method.
Definition: Thread.hpp:373
summary>Thread is up and running
ThreadState GetState() const
Returns the current state of the thread.
Definition: Thread.hpp:505