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;
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!")
145 ARP_DEPRECATED("Use constructor with
ThreadSettings instead. Supply at least a thread name!")
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);
221 static
void Sleep(
size_t milliseconds);
290 ARP_DEPRECATED("Set thread stack size at the constructor")
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);
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();
Definition: Loggable.hxx:19
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)
Sets a new stack size for the thread.
ThreadState GetState() const
Returns the current state of the thread.
Definition: Thread.hpp:505
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:500
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)
Constructs a Thread instance for a class method.
Definition: Thread.hpp:373
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:490
delegate< void(void *)> ThreadStartDelegate
Definition of signature of class method to be executed in a separate thread.
Definition: Thread.hpp:87
const String & GetName(void) const
Returns the current name of the thread.
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:495
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
Root namespace for the PLCnext API
Namespace of the C++ standard library