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 Thread(TInstance& instance, TFunction fn,
void* pStartParam =
nullptr);
121 template<
class TInstance,
class TFunction>
122 Thread(
const TInstance& instance, TFunction fn,
void* pStartParam =
nullptr);
129 template<
class TInstance,
class TFunction>
130 Thread(TInstance* pInstance, TFunction fn,
void* pStartParam =
nullptr);
137 template<
class TInstance,
class TFunction>
138 Thread(
const TInstance* pInstance, TFunction fn,
void* pStartParam =
nullptr);
144 explicit Thread(ThreadStartDelegate&& threadStart,
void* pStartParam =
nullptr);
154 Thread(ThreadStartFunction threadStart,
void* pStartParam =
nullptr);
162 template<
class TInstance,
class TFunction>
163 Thread(
const ThreadSettings& settings, TInstance& instance, TFunction fn,
void* pStartParam =
nullptr);
171 template<
class TInstance,
class TFunction>
172 Thread(
const ThreadSettings& settings,
const TInstance& instance, TFunction fn,
void* pStartParam =
nullptr);
180 template<
class TInstance,
class TFunction>
181 Thread(
const ThreadSettings& settings, TInstance* pInstance, TFunction fn,
void* pStartParam =
nullptr);
189 template<
class TInstance,
class TFunction>
190 Thread(
const ThreadSettings& settings,
const TInstance* pInstance, TFunction fn,
void* pStartParam =
nullptr);
197 Thread(
ThreadSettings& settings, ThreadStartDelegate&& threadStart,
void* pStartParam =
nullptr);
211 Thread(
const ThreadSettings& settings, ThreadStartDelegate&& threadStart,
void* pStartParam =
nullptr);
218 Thread(
const ThreadSettings& settings, ThreadStartFunction threadStart,
void* pStartParam =
nullptr);
229 static void Sleep(
size_t milliseconds);
233 static size_t GetCurrentThreadId(
void);
237 static Thread* GetCurrentThread(
void);
247 static void SetAsynchronousCancelability(
bool enable);
251 bool IsRunning(
void)
const;
270 void SetCpuAffinity(
size_t mask);
275 size_t GetCpuAffinity(
void)
const;
280 void SetPriority(
size_t value);
284 size_t GetPriority(
void)
const;
298 ARP_DEPRECATED(
"Set thread stack size at the constructor")
299 void SetStackSize(
size_t value);
303 size_t GetStackSize(
void)const;
312 void SetName(const
String& value);
316 const
String& GetName(
void)const;
326 bool IsJoinable(
void);
343 void Interrupt(
void);
346 void Terminate(
void);
349 using IThreadService =
Arp::System::Ve::IThreadService;
350 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 351 #else // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 352 using ThreadEventPtr = std::unique_ptr<ThreadEvent, ThreadEventDeleter>;
353 #endif // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 356 static void* RunInternal(
void* pParam);
357 static void CheckInterruptOfCurrentThread(
void);
358 static void SetStateOfCurrentThread(
ThreadState state);
359 static String CreateDefaultThreadName(
void);
362 void CreateThreadInternal(
void);
364 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 365 void RunThread(ThreadBinaryCompatibilityExtensions* pBinCompat);
366 #else // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 367 void RunThread(
void);
368 #endif // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 375 ThreadStartDelegate threadStart;
377 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 378 ThreadBinaryCompatibilityExtensions* pBinaryCompatibilityExtensions;
379 #else // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 380 IThreadService* pThreadService;
381 #endif // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 383 std::atomic<ThreadState> state;
384 std::atomic<bool> interrupting;
385 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 386 #else // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 387 ThreadEventPtr eventWakeupPtr;
391 std::atomic<bool> isActive;
392 #endif // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 395 static thread_local
Thread* pCurrentThread;
400 template<
class TInstance,
class TFunction>
402 :
Thread(&instance, fn, pStartParam)
406 template<
class TInstance,
class TFunction>
407 inline Thread::Thread(
const TInstance& instance, TFunction fn,
void* pStartParam)
408 :
Thread(&instance, fn, pStartParam)
412 template<
class TInstance,
class TFunction>
415 , pStartParam(pStartParam)
416 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
417 , pBinaryCompatibilityExtensions(nullptr)
419 , pThreadService(nullptr)
422 , interrupting(false)
423 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
425 , eventWakeupPtr(nullptr)
432 this->CreateThreadInternal();
435 template<
class TInstance,
class TFunction>
436 inline Thread::Thread(
const TInstance* pInstance, TFunction fn,
void* pStartParam)
438 , pStartParam(pStartParam)
439 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
440 , pBinaryCompatibilityExtensions(nullptr)
442 , pThreadService(nullptr)
445 , interrupting(false)
446 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
448 , eventWakeupPtr(nullptr)
455 this->CreateThreadInternal();
459 : threadStart(
std::move(threadStart))
460 , pStartParam(pStartParam)
461 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
462 , pBinaryCompatibilityExtensions(nullptr)
464 , pThreadService(nullptr)
467 , interrupting(false)
468 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
470 , eventWakeupPtr(nullptr)
477 this->CreateThreadInternal();
482 , pStartParam(pStartParam)
483 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
484 , pBinaryCompatibilityExtensions(nullptr)
486 , pThreadService(nullptr)
489 , interrupting(false)
490 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
492 , eventWakeupPtr(nullptr)
499 this->CreateThreadInternal();
502 template<
class TInstance,
class TFunction>
504 :
Thread(settings, &instance, fn, pStartParam)
508 template<
class TInstance,
class TFunction>
510 :
Thread(settings, &instance, fn, pStartParam)
514 template<
class TInstance,
class TFunction>
517 , pStartParam(pStartParam)
518 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
519 , pThreadService(nullptr)
521 , pBinaryCompatibilityExtensions(nullptr)
524 , interrupting(false)
525 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
526 , eventWakeupPtr(nullptr)
527 , semIsCreatedPtr(nullptr)
528 , semStartPtr(nullptr)
529 , semIsStartedPtr(nullptr)
532 this->CreateThreadInternal(settings);
535 template<
class TInstance,
class TFunction>
538 , pStartParam(pStartParam)
539 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
540 , pThreadService(nullptr)
542 , pBinaryCompatibilityExtensions(nullptr)
545 , interrupting(false)
546 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
547 , eventWakeupPtr(nullptr)
548 , semIsCreatedPtr(nullptr)
549 , semStartPtr(nullptr)
550 , semIsStartedPtr(nullptr)
553 this->CreateThreadInternal(settings);
567 : threadStart(
std::move(threadStart))
568 , pStartParam(pStartParam)
569 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
570 , pThreadService(nullptr)
572 , pBinaryCompatibilityExtensions(nullptr)
575 , interrupting(false)
576 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
577 , eventWakeupPtr(nullptr)
578 , semIsCreatedPtr(nullptr)
579 , semStartPtr(nullptr)
580 , semIsStartedPtr(nullptr)
583 this->CreateThreadInternal(settings);
588 , pStartParam(pStartParam)
589 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
590 , pThreadService(nullptr)
592 , pBinaryCompatibilityExtensions(nullptr)
595 , interrupting(false)
596 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
597 , eventWakeupPtr(nullptr)
598 , semIsCreatedPtr(nullptr)
599 , semStartPtr(nullptr)
600 , semIsStartedPtr(nullptr)
603 this->CreateThreadInternal(settings);
613 return Thread::pCurrentThread;
616 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 619 this->interrupting =
true;
621 #endif // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 625 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:611
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:606
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:617
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:401
summary>Thread is up and running
ThreadState GetState() const
Returns the current state of the thread.
Definition: Thread.hpp:623