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 void SetStackSize(
size_t value);
302 size_t GetStackSize(
void)
const;
311 void SetName(
const String& value);
315 const String& GetName(
void)
const;
325 bool IsJoinable(
void);
342 void Interrupt(
void);
345 void Terminate(
void);
348 using IThreadService = Arp::System::Ve::IThreadService;
349 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 350 #else // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 351 using ThreadEventPtr = std::unique_ptr<ThreadEvent, ThreadEventDeleter>;
352 #endif // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 355 static void* RunInternal(
void* pParam);
356 static void CheckInterruptOfCurrentThread(
void);
357 static void SetStateOfCurrentThread(
ThreadState state);
358 static String CreateDefaultThreadName(
void);
361 void CreateThreadInternal(
void);
363 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 364 void RunThread(ThreadBinaryCompatibilityExtensions* pBinCompat);
365 #else // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 366 void RunThread(
void);
367 #endif // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 374 ThreadStartDelegate threadStart;
376 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 377 ThreadBinaryCompatibilityExtensions* pBinaryCompatibilityExtensions;
378 #else // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 379 IThreadService* pThreadService;
380 #endif // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 382 std::atomic<ThreadState> state;
383 std::atomic<bool> interrupting;
384 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 385 #else // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 386 ThreadEventPtr eventWakeupPtr;
390 std::atomic<bool> isActive;
391 #endif // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 394 static thread_local
Thread* pCurrentThread;
399 template<
class TInstance,
class TFunction>
401 :
Thread(&instance, fn, pStartParam)
405 template<
class TInstance,
class TFunction>
406 inline Thread::Thread(
const TInstance& instance, TFunction fn,
void* pStartParam)
407 :
Thread(&instance, fn, pStartParam)
411 template<
class TInstance,
class TFunction>
414 , pStartParam(pStartParam)
415 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
416 , pBinaryCompatibilityExtensions(nullptr)
418 , pThreadService(nullptr)
421 , interrupting(false)
422 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
424 , eventWakeupPtr(nullptr)
431 this->CreateThreadInternal();
434 template<
class TInstance,
class TFunction>
435 inline Thread::Thread(
const TInstance* pInstance, TFunction fn,
void* pStartParam)
437 , pStartParam(pStartParam)
438 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
439 , pBinaryCompatibilityExtensions(nullptr)
441 , pThreadService(nullptr)
444 , interrupting(false)
445 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
447 , eventWakeupPtr(nullptr)
454 this->CreateThreadInternal();
458 : threadStart(
std::move(threadStart))
459 , pStartParam(pStartParam)
460 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
461 , pBinaryCompatibilityExtensions(nullptr)
463 , pThreadService(nullptr)
466 , interrupting(false)
467 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
469 , eventWakeupPtr(nullptr)
476 this->CreateThreadInternal();
481 , pStartParam(pStartParam)
482 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
483 , pBinaryCompatibilityExtensions(nullptr)
485 , pThreadService(nullptr)
488 , interrupting(false)
489 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE
491 , eventWakeupPtr(nullptr)
498 this->CreateThreadInternal();
501 template<
class TInstance,
class TFunction>
503 :
Thread(settings, &instance, fn, pStartParam)
507 template<
class TInstance,
class TFunction>
509 :
Thread(settings, &instance, fn, pStartParam)
513 template<
class TInstance,
class TFunction>
516 , pStartParam(pStartParam)
517 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
518 , pThreadService(nullptr)
520 , pBinaryCompatibilityExtensions(nullptr)
523 , interrupting(false)
524 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
525 , eventWakeupPtr(nullptr)
526 , semIsCreatedPtr(nullptr)
527 , semStartPtr(nullptr)
528 , semIsStartedPtr(nullptr)
531 this->CreateThreadInternal(settings);
534 template<
class TInstance,
class TFunction>
537 , pStartParam(pStartParam)
538 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
539 , pThreadService(nullptr)
541 , pBinaryCompatibilityExtensions(nullptr)
544 , interrupting(false)
545 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
546 , eventWakeupPtr(nullptr)
547 , semIsCreatedPtr(nullptr)
548 , semStartPtr(nullptr)
549 , semIsStartedPtr(nullptr)
552 this->CreateThreadInternal(settings);
566 : threadStart(
std::move(threadStart))
567 , pStartParam(pStartParam)
568 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
569 , pThreadService(nullptr)
571 , pBinaryCompatibilityExtensions(nullptr)
574 , interrupting(false)
575 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
576 , eventWakeupPtr(nullptr)
577 , semIsCreatedPtr(nullptr)
578 , semStartPtr(nullptr)
579 , semIsStartedPtr(nullptr)
582 this->CreateThreadInternal(settings);
587 , pStartParam(pStartParam)
588 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
589 , pThreadService(nullptr)
591 , pBinaryCompatibilityExtensions(nullptr)
594 , interrupting(false)
595 #ifdef THREAD_DONT_PROVIDE_BINARY_COMPATIBILITY
596 , eventWakeupPtr(nullptr)
597 , semIsCreatedPtr(nullptr)
598 , semStartPtr(nullptr)
599 , semIsStartedPtr(nullptr)
602 this->CreateThreadInternal(settings);
612 return Thread::pCurrentThread;
615 #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 618 this->interrupting =
true;
620 #endif // #ifdef ARP_SYSTEM_COMMONS_THREADING_THREAD_BINARY_COMPAT_MODE 624 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:610
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:605
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:616
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:400
summary>Thread is up and running
ThreadState GetState() const
Returns the current state of the thread.
Definition: Thread.hpp:622