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"
18namespace Arp {
namespace System {
namespace Ve
23namespace Arp {
namespace System {
namespace Commons {
namespace Threading
27class ThreadBinaryCompatibilityExtensions;
80 friend class AutoResetEvent2;
100 template<
class TInstance,
class TFunction>
101 ARP_DEPRECATED(
"Use constructor with ThreadSettings instead. Supply at least a thread name!")
102 Thread(TInstance& instance, TFunction fn,
void* pStartParam =
nullptr);
110 template<class TInstance, class TFunction>
112 Thread(const TInstance& instance, TFunction fn,
void* pStartParam =
nullptr);
120 template<class TInstance, class TFunction>
122 Thread(TInstance* pInstance, TFunction fn,
void* pStartParam =
nullptr);
130 template<class TInstance, class TFunction>
132 Thread(const TInstance* pInstance, TFunction fn,
void* pStartParam =
nullptr);
160 template<class TInstance, class TFunction>
161 Thread(const
ThreadSettings& settings, TInstance& instance, TFunction fn,
void* pStartParam =
nullptr);
169 template<class TInstance, class TFunction>
170 Thread(const
ThreadSettings& settings, const TInstance& instance, TFunction fn,
void* pStartParam =
nullptr);
178 template<class TInstance, class TFunction>
179 Thread(const
ThreadSettings& settings, TInstance* pInstance, TFunction fn,
void* pStartParam =
nullptr);
187 template<class TInstance, class TFunction>
188 Thread(const
ThreadSettings& settings, const TInstance* pInstance, TFunction fn,
void* pStartParam =
nullptr);
227 static
void Sleep(
size_t milliseconds);
347 using IThreadService =
Arp::System::Ve::IThreadService;
350 static
void* RunInternal(
void* pParam);
351 static
void CheckInterruptOfCurrentThread(
void);
352 static
void SetStateOfCurrentThread(
ThreadState state);
353 static
String CreateDefaultThreadName(
void);
356 void CreateThreadInternal(
void);
358 void RunThread(ThreadBinaryCompatibilityExtensions* pBinCompat);
367 ThreadBinaryCompatibilityExtensions* pBinaryCompatibilityExtensions;
370 std::atomic<
bool> interrupting;
373 static thread_local
Thread* pCurrentThread;
378template<class TInstance, class TFunction>
379inline
Thread::
Thread(TInstance& instance, TFunction fn,
void* pStartParam)
380 :
Thread(&instance, fn, pStartParam)
384template<
class TInstance,
class TFunction>
386 :
Thread(&instance, fn, pStartParam)
390template<
class TInstance,
class TFunction>
393 , pStartParam(pStartParam)
394 , pBinaryCompatibilityExtensions(nullptr)
396 , interrupting(false)
398 this->CreateThreadInternal();
401template<
class TInstance,
class TFunction>
402inline Thread::Thread(
const TInstance* pInstance, TFunction fn,
void* pStartParam)
404 , pStartParam(pStartParam)
405 , pBinaryCompatibilityExtensions(nullptr)
407 , interrupting(false)
409 this->CreateThreadInternal();
413 : threadStart(
std::move(threadStart))
414 , pStartParam(pStartParam)
415 , pBinaryCompatibilityExtensions(nullptr)
417 , interrupting(false)
419 this->CreateThreadInternal();
424 , pStartParam(pStartParam)
425 , pBinaryCompatibilityExtensions(nullptr)
427 , interrupting(false)
429 this->CreateThreadInternal();
432template<
class TInstance,
class TFunction>
434 :
Thread(settings, &instance, fn, pStartParam)
438template<
class TInstance,
class TFunction>
440 :
Thread(settings, &instance, fn, pStartParam)
444template<
class TInstance,
class TFunction>
447 , pStartParam(pStartParam)
448 , pBinaryCompatibilityExtensions(nullptr)
450 , interrupting(false)
452 this->CreateThreadInternal(settings);
455template<
class TInstance,
class TFunction>
458 , pStartParam(pStartParam)
459 , pBinaryCompatibilityExtensions(nullptr)
461 , interrupting(false)
463 this->CreateThreadInternal(settings);
477 : threadStart(
std::move(threadStart))
478 , pStartParam(pStartParam)
479 , pBinaryCompatibilityExtensions(nullptr)
481 , interrupting(false)
483 this->CreateThreadInternal(settings);
488 , pStartParam(pStartParam)
489 , pBinaryCompatibilityExtensions(nullptr)
491 , interrupting(false)
493 this->CreateThreadInternal(settings);
503 return Thread::pCurrentThread;
508 this->interrupting =
true;
513 return this->state.load();
Definition: Loggable.hxx:20
Event object to signal a single thread that an event has occurred. Can be used to synchronize threads...
Definition: AutoResetEvent.hpp:23
Container class for adaptable thread settings.
Definition: ThreadSettings.hpp:13
The Thread-class provides methods to execute functions and methods in a separate thread.
Definition: Thread.hpp:77
size_t GetPriority(void) const
Returns the current priority of the thread.
void SetStackSize(size_t value)
Deprecated! Sets a new stack size for the thread.
const String & GetName(void) const
Returns the current name of the thread.
ThreadState GetState() const
Returns the current state of the thread.
Definition: Thread.hpp:511
void SetName(const String &value)
Sets a new name to the thread.
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
void Interrupt(void)
Interrupts the currents execution.
Definition: Thread.hpp:506
static size_t GetCurrentThreadId(void)
Returns the id of the calling thread.
void(* ThreadStartFunction)(void *)
Definition of signature of function to be executed in a separate thread.
Definition: Thread.hpp:84
void Join(void)
Waits for the thread to terminate.
static void Sleep(size_t milliseconds)
Suspends the execution of the calling thread.
void SetPriority(size_t value)
Assigns a new priority to the thread.
Thread(TInstance &instance, TFunction fn, void *pStartParam=nullptr)
Deprecated! Constructs a Thread instance for a class method.
Definition: Thread.hpp:379
size_t GetStackSize(void) const
Returns the current stack size of the thread.Current size of stack in bytes.
static void SetAsynchronousCancelability(bool enable)
Activates/Deactivates the asynchronous cancelability of the calling thread.
void Start(void)
Starts the execution of the thread.
bool IsRunning(void) const
Determines if this thread is in running state.
Definition: Thread.hpp:496
delegate< void(void *)> ThreadStartDelegate
Definition of signature of class method to be executed in a separate thread.
Definition: Thread.hpp:87
size_t GetCpuAffinity(void) const
Returns the CPU affinity mask.
static Thread * GetCurrentThread(void)
Returns the current thread of the calling context.
Definition: Thread.hpp:501
bool IsJoinable(void)
Checks if this thread is joinable.
void SetCpuAffinity(size_t mask)
Pins the thread to a specific CPUs inside a multiprocessor system.
void Terminate(void)
Aborts the execution of this thread.
delegate< R(A...)> make_delegate(R(*const function_ptr)(A...)) noexcept
Creates a delegate from a static function.
Definition: delegate.hxx:225
@ System
System components used by the System, Device, Plc or Io domains.
ThreadState
Possible thread states.
Definition: ThreadState.hpp:15
@ Running
summary>Thread is up and running
@ Stopped
summary>Thread is either waiting for an event, currently sleeping or waits for another thread to fini...
Root namespace for the PLCnext API
class ARP_DEPRECATED("Use Arp::Enum<T> instead.") EnumStrings
Deprecated! The class implements an adapter for enums to define the string literals of the enum entri...
Definition: EnumStrings.hxx:38
Namespace of the C++ standard library