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 #define ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 25 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 26 #else // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 27 namespace Arp {
namespace System {
namespace Commons {
namespace Threading {
namespace Internal
30 class ThreadEventDeleter
33 void operator()(ThreadEvent*)
const;
36 #endif // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 38 namespace Arp {
namespace System {
namespace Commons {
namespace Threading
40 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 42 class ThreadBinaryCompatibilityExtensions;
43 #else // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 44 using namespace Arp::System::Commons::Threading::Internal;
45 #endif // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 98 typedef void (*ThreadStartFunction)(
void*);
113 template<
class TInstance,
class TFunction>
114 ARP_DEPRECATED(
"Use constructor with ThreadSettings instead. Supply at least a thread name!")
115 Thread(TInstance& instance, TFunction fn,
void* pStartParam =
nullptr);
122 template<class TInstance, class TFunction>
123 ARP_DEPRECATED("Use constructor with
ThreadSettings instead. Supply at least a thread name!")
124 Thread(const TInstance& instance, TFunction fn,
void* pStartParam =
nullptr);
131 template<class TInstance, class TFunction>
132 ARP_DEPRECATED("Use constructor with
ThreadSettings instead. Supply at least a thread name!")
133 Thread(TInstance* pInstance, TFunction fn,
void* pStartParam =
nullptr);
140 template<class TInstance, class TFunction>
141 ARP_DEPRECATED("Use constructor with
ThreadSettings instead. Supply at least a thread name!")
142 Thread(const TInstance* pInstance, TFunction fn,
void* pStartParam =
nullptr);
148 ARP_DEPRECATED("Use constructor with
ThreadSettings instead. Supply at least a thread name!")
149 explicit
Thread(ThreadStartDelegate&& threadStart,
void* pStartParam =
nullptr);
159 ARP_DEPRECATED("Use constructor with
ThreadSettings instead. Supply at least a thread name!")
160 Thread(ThreadStartFunction threadStart,
void* pStartParam =
nullptr);
168 template<class TInstance, class TFunction>
169 Thread(const
ThreadSettings& settings, TInstance& instance, TFunction fn,
void* pStartParam =
nullptr);
177 template<class TInstance, class TFunction>
178 Thread(const
ThreadSettings& settings, const TInstance& instance, TFunction fn,
void* pStartParam =
nullptr);
186 template<class TInstance, class TFunction>
187 Thread(const
ThreadSettings& settings, TInstance* pInstance, TFunction fn,
void* pStartParam =
nullptr);
195 template<class TInstance, class TFunction>
196 Thread(const
ThreadSettings& settings, const TInstance* pInstance, TFunction fn,
void* pStartParam =
nullptr);
203 Thread(
ThreadSettings& settings, ThreadStartDelegate&& threadStart,
void* pStartParam =
nullptr);
217 Thread(const
ThreadSettings& settings, ThreadStartDelegate&& threadStart,
void* pStartParam =
nullptr);
224 Thread(const
ThreadSettings& settings, ThreadStartFunction threadStart,
void* pStartParam =
nullptr);
235 static
void Sleep(
size_t milliseconds);
239 static
size_t GetCurrentThreadId(
void);
243 static
Thread* GetCurrentThread(
void);
253 static
void SetAsynchronousCancelability(
bool enable);
257 bool IsRunning(
void)const;
276 void SetCpuAffinity(
size_t mask);
281 size_t GetCpuAffinity(
void)const;
286 void SetPriority(
size_t value);
290 size_t GetPriority(
void)const;
304 ARP_DEPRECATED("Set thread stack size at the constructor")
305 void SetStackSize(
size_t value);
309 size_t GetStackSize(
void)const;
318 void SetName(const
String& value);
322 const
String& GetName(
void)const;
332 bool IsJoinable(
void);
349 void Interrupt(
void);
352 void Terminate(
void);
355 using IThreadService =
Arp::System::Ve::IThreadService;
356 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 357 #else // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 358 using ThreadEventPtr = std::unique_ptr<ThreadEvent, ThreadEventDeleter>;
359 #endif // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 362 static void* RunInternal(
void* pParam);
363 static void CheckInterruptOfCurrentThread(
void);
364 static void SetStateOfCurrentThread(
ThreadState state);
365 static String CreateDefaultThreadName(
void);
368 void CreateThreadInternal(
void);
370 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 371 void RunThread(ThreadBinaryCompatibilityExtensions* pBinCompat);
372 #else // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 373 void RunThread(
void);
374 #endif // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 381 ThreadStartDelegate threadStart;
383 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 384 ThreadBinaryCompatibilityExtensions* pBinaryCompatibilityExtensions;
385 #else // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 386 IThreadService* pThreadService;
387 #endif // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 389 std::atomic<ThreadState> state;
390 std::atomic<bool> interrupting;
391 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 392 #else // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 393 ThreadEventPtr eventWakeupPtr;
397 std::atomic<bool> isActive;
398 #endif // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 401 static thread_local
Thread* pCurrentThread;
406 template<
class TInstance,
class TFunction>
408 :
Thread(&instance, fn, pStartParam)
412 template<
class TInstance,
class TFunction>
413 inline Thread::Thread(
const TInstance& instance, TFunction fn,
void* pStartParam)
414 :
Thread(&instance, fn, pStartParam)
418 template<
class TInstance,
class TFunction>
421 , pStartParam(pStartParam)
422 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
423 , pBinaryCompatibilityExtensions(nullptr)
425 , pThreadService(nullptr)
428 , interrupting(false)
429 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
431 , eventWakeupPtr(nullptr)
438 this->CreateThreadInternal();
441 template<
class TInstance,
class TFunction>
442 inline Thread::Thread(
const TInstance* pInstance, TFunction fn,
void* pStartParam)
444 , pStartParam(pStartParam)
445 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
446 , pBinaryCompatibilityExtensions(nullptr)
448 , pThreadService(nullptr)
451 , interrupting(false)
452 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
454 , eventWakeupPtr(nullptr)
461 this->CreateThreadInternal();
465 : threadStart(
std::move(threadStart))
466 , pStartParam(pStartParam)
467 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
468 , pBinaryCompatibilityExtensions(nullptr)
470 , pThreadService(nullptr)
473 , interrupting(false)
474 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
476 , eventWakeupPtr(nullptr)
483 this->CreateThreadInternal();
488 , pStartParam(pStartParam)
489 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
490 , pBinaryCompatibilityExtensions(nullptr)
492 , pThreadService(nullptr)
495 , interrupting(false)
496 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
498 , eventWakeupPtr(nullptr)
505 this->CreateThreadInternal();
508 template<
class TInstance,
class TFunction>
510 :
Thread(settings, &instance, fn, pStartParam)
514 template<
class TInstance,
class TFunction>
516 :
Thread(settings, &instance, fn, pStartParam)
520 template<
class TInstance,
class TFunction>
523 , pStartParam(pStartParam)
524 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
525 , pThreadService(nullptr)
527 , pBinaryCompatibilityExtensions(nullptr)
530 , interrupting(false)
531 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
532 , eventWakeupPtr(nullptr)
533 , semIsCreatedPtr(nullptr)
534 , semStartPtr(nullptr)
535 , semIsStartedPtr(nullptr)
538 this->CreateThreadInternal(settings);
541 template<
class TInstance,
class TFunction>
544 , pStartParam(pStartParam)
545 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
546 , pThreadService(nullptr)
548 , pBinaryCompatibilityExtensions(nullptr)
551 , interrupting(false)
552 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
553 , eventWakeupPtr(nullptr)
554 , semIsCreatedPtr(nullptr)
555 , semStartPtr(nullptr)
556 , semIsStartedPtr(nullptr)
559 this->CreateThreadInternal(settings);
573 : threadStart(
std::move(threadStart))
574 , pStartParam(pStartParam)
575 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
576 , pThreadService(nullptr)
578 , pBinaryCompatibilityExtensions(nullptr)
581 , interrupting(false)
582 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
583 , eventWakeupPtr(nullptr)
584 , semIsCreatedPtr(nullptr)
585 , semStartPtr(nullptr)
586 , semIsStartedPtr(nullptr)
589 this->CreateThreadInternal(settings);
594 , pStartParam(pStartParam)
595 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
596 , pThreadService(nullptr)
598 , pBinaryCompatibilityExtensions(nullptr)
601 , interrupting(false)
602 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
603 , eventWakeupPtr(nullptr)
604 , semIsCreatedPtr(nullptr)
605 , semStartPtr(nullptr)
606 , semIsStartedPtr(nullptr)
609 this->CreateThreadInternal(settings);
619 return Thread::pCurrentThread;
622 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 625 this->interrupting =
true;
627 #endif // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 631 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:215
static Thread * GetCurrentThread(void)
Returns the current thread of the calling context.
Definition: Thread.hpp:617
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:104
The Thread-class provides methods to execute functions and methods in a separate thread.
Definition: Thread.hpp:91
Definition: Loggable.hxx:18
delegate< void(void *)> ThreadStartDelegate
Definition of signature of class method to be executed in a separate thread.
Definition: Thread.hpp:101
Implementation of named or unnamed semaphore used to synchronize processes and threads.
Definition: Semaphore.hpp:22
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:612
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:623
void(* ThreadStartFunction)(void *)
Definition of signature of function to be executed in a separate thread.
Definition: Thread.hpp:98
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:407
summary>Thread is up and running
ThreadState GetState() const
Returns the current state of the thread.
Definition: Thread.hpp:629