ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
alib::threadmodel::Trigger Class Reference

Description:

The Trigger class provides a mechanism to periodically "trigger" actions on objects that implement the Triggered interface without requiring dedicated threads per object or manual additions of actions in existing threads.

The class manages a collection of Triggered objects and ensures that their virtual trigger() methods are called periodically based on the time interval given with their respective method triggerPeriod().

Internally, Trigger operates its own thread to handle the timing and execution of the triggers, adhering to the specified conditions.

This design helps in simplifying periodic task management within an application, avoiding thread proliferation and minimizing resource overhead.

Key responsibilities of the class:
  • Maintain and manage a list of Triggered objects.
  • Schedule and execute periodic triggers for the registered objects.
  • Provide the ability to add or remove Triggered objects dynamically.
Usage:
  • Users register their implementations of the Triggered interface with the Add() method to begin periodic triggers.
  • Objects can be unregistered using the Remove() method.
  • The Stop() method terminates the execution of the internal thread.

Intended for scenarios where lightweight, periodic task scheduling is needed without creating additional complexity or significant overhead.

Definition at line 93 of file trigger.inl.

Inheritance diagram for alib::threadmodel::Trigger:
[legend]
Collaboration diagram for alib::threadmodel::Trigger:
[legend]

Inner Type Index:

struct  TriggerEntry
 The entry type used with field triggerList. More...
 

Public Method Index:

ALIB_DLL Trigger ()
 Constructor.
 
virtual ALIB_DLL ~Trigger () override
 Destructor.
 
ALIB_DLL void Add (Triggered &triggered, bool initialWakeup=false)
 
ALIB_DLL void Do (Ticks until)
 
void Do (Ticks::Duration until)
 
ALIB_DLL void Remove (Triggered &triggered)
 
virtual ALIB_DLL void Run () override
 Implementation of the parent interface (virtual abstract).
 
virtual ALIB_DLL void Start ()
 
virtual ALIB_DLL void Stop ()
 Stops the trigger thread and joins it.
 

Protected Field Index:

bool internalThreadMode = false
 Denotes whether or not the trigger is currently used in internal thread mode.
 
List< MonoAllocator, TriggerEntrytriggerList
 The list of registered triggered objects.
 
bool wakeUpCondition = false
 
- Protected Field Index: inherited from alib::threads::Thread
std::thread * c11Thread =nullptr
 The internal C++ thread object.
 
ThreadID id =0
 The id of the thread.
 
const charactername =nullptr
 The name of the thread.
 
std::thread::id nativeID
 The internal C++ thread id.
 
Runnablerunnable =nullptr
 The runnable to execute.
 
State state = State::Unstarted
 Internal flag to indicate if the thread is alive.
 
- Protected Field Index: inherited from alib::threads::TCondition< T >
std::condition_variable conditionVariable
 The condition variable used for blocking and notification.
 
std::condition_variable conditionVariable
 The condition variable used for blocking and notification.
 
DbgConditionAsserter Dbg
 The debug tool instance.
 
DbgConditionAsserter Dbg
 The debug tool instance.
 
std::mutex mutex
 The mutex used for locking this instance.
 
std::mutex mutex
 The mutex used for locking this instance.
 

Protected Method Index:

bool isConditionMet () const noexcept
 
- Protected Method Index: inherited from alib::threads::Thread
ALIB_DLL void destruct ()
 
 Thread (const character *pName=A_CHAR(""))
 
 Thread (const Thread &)=delete
 Deleted copy constructor.
 
ALIB_DLL Thread (Runnable *target, const character *pName=A_CHAR(""))
 
virtual ~Thread () override
 
ThreadID GetID () const
 
virtual const characterGetName () const
 
std::thread::id GetNativeID () const
 
State GetState ()
 
bool IsAlive ()
 
virtual ALIB_DLL void Join ()
 
virtual void SetName (const character *newName)
 
- Protected Method Index: inherited from alib::threads::Runnable
virtual ~Runnable ()
 Virtual destructor.
 
- Protected Method Index: inherited from alib::threads::TCondition< T >
 TCondition (const character *dbgName)
 
 TCondition (const character *dbgName)
 
void Acquire (ALIB_DBG_TAKE_CI)
 
void Acquire (ALIB_DBG_TAKE_CI)
 
T & cast ()
 
T & cast ()
 
void Release (ALIB_DBG_TAKE_CI)
 
void Release (ALIB_DBG_TAKE_CI)
 
void ReleaseAndNotify (ALIB_DBG_TAKE_CI)
 
void ReleaseAndNotify (ALIB_DBG_TAKE_CI)
 
void ReleaseAndNotifyAll (ALIB_DBG_TAKE_CI)
 
void ReleaseAndNotifyAll (ALIB_DBG_TAKE_CI)
 
void WaitForNotification (ALIB_DBG_TAKE_CI)
 
void WaitForNotification (ALIB_DBG_TAKE_CI)
 
void WaitForNotification (const Ticks &wakeUpTime, const CallerInfo &ci)
 
void WaitForNotification (const Ticks &wakeUpTime, const CallerInfo &ci)
 
void WaitForNotification (const Ticks::Duration &maxWaitTimeSpan, const CallerInfo &ci)
 
void WaitForNotification (const Ticks::Duration &maxWaitTimeSpan, const CallerInfo &ci)
 
void WaitForNotification (const Ticks::Duration::TDuration &maxWaitTimeSpan, const CallerInfo &ci)
 
void WaitForNotification (const Ticks::Duration::TDuration &maxWaitTimeSpan, const CallerInfo &ci)
 

Additional Inherited Members

- Protected Type Index: inherited from alib::threads::Thread
enum class  State {
  Unstarted = 0 , Started = 1 , Running = 2 , Done = 3 ,
  Terminated = 4
}
 
- Protected Static Method Index: inherited from alib::threads::Thread
static ALIB_DLL ThreadGet (std::thread::id nativeID)
 
static ThreadGetCurrent ()
 
static ALIB_DLL ThreadGetMain ()
 
static void Sleep (const Ticks::Duration &duration)
 
static void Sleep (const Ticks::Duration::TDuration &duration)
 
static void SleepMicros (int64_t microseconds)
 
static void SleepMillis (int milliseconds)
 
static void SleepNanos (int64_t nanoseconds)
 
static void SleepUntil (const Ticks &time)
 
static void YieldToSystem ()
 

Friends And Related Entity Details:

◆ lang::Owner< Trigger & >

friend class lang::Owner< Trigger & >
friend

Definition at line 60 of file trigger.inl.

◆ threads::TCondition< Trigger >

friend struct threads::TCondition< Trigger >
friend

Definition at line 60 of file trigger.inl.

Field Details:

◆ internalThreadMode

bool alib::threadmodel::Trigger::internalThreadMode = false
protected

Denotes whether or not the trigger is currently used in internal thread mode.

Definition at line 124 of file trigger.inl.

◆ triggerList

List<MonoAllocator, TriggerEntry> alib::threadmodel::Trigger::triggerList
protected

The list of registered triggered objects.

Definition at line 117 of file trigger.inl.

◆ wakeUpCondition

bool alib::threadmodel::Trigger::wakeUpCondition = false
protected

The condition requested by parent class TCondition via a call to isConditionMet.

Definition at line 121 of file trigger.inl.

Constructor(s) / Destructor Details:

◆ Trigger()

alib::threadmodel::Trigger::Trigger ( )

Constructor.

Definition at line 31 of file trigger.cpp.

Here is the call graph for this function:

◆ ~Trigger()

alib::threadmodel::Trigger::~Trigger ( )
overridevirtual

Destructor.

Definition at line 37 of file trigger.cpp.

Here is the call graph for this function:

Method Details:

◆ Add()

void alib::threadmodel::Trigger::Add ( Triggered & triggered,
bool initialWakeup = false )

Add an object to be triggered.

Parameters
triggeredThe object to be triggered.
initialWakeupIf true, the first wakeup is scheduled right away. Defaults to false.

Definition at line 50 of file trigger.cpp.

Here is the call graph for this function:

◆ Do() [1/2]

void alib::threadmodel::Trigger::Do ( Ticks until)

Executes the processing of triggers for up to a given maximum time. If the internal thread is not used, this method may be called manually inside an external loop to execute triggering operations within the specified timeframe.

If the internal thread was created and is running, with debug-compilations, an ALib error will be raised.

Parameters
untilDefines the future point in time until triggering is performed.

Definition at line 134 of file trigger.cpp.

Here is the call graph for this function:

◆ Do() [2/2]

void alib::threadmodel::Trigger::Do ( Ticks::Duration until)
inline

Invokes Do(Ticks) by adding the given duration to the current point in time.

Parameters
untilDefines the maximum duration for which this method will execute the trigger logic.

Definition at line 154 of file trigger.inl.

Here is the call graph for this function:

◆ isConditionMet()

bool alib::threadmodel::Trigger::isConditionMet ( ) const
inlineprotectednoexcept

Implementation of the interface needed by parent TCondition.

Returns
Member wakeUpCondition

Definition at line 128 of file trigger.inl.

◆ Remove()

void alib::threadmodel::Trigger::Remove ( Triggered & triggered)

Remove a previously added triggered object.

Parameters
triggeredThe object to be removed from the list.

Definition at line 82 of file trigger.cpp.

◆ Run()

void alib::threadmodel::Trigger::Run ( )
overridevirtual

Implementation of the parent interface (virtual abstract).

Reimplemented from alib::threads::Thread.

Definition at line 110 of file trigger.cpp.

Here is the call graph for this function:

◆ Start()

void alib::threads::Thread::Start ( )
virtual

Starts the execution of the thread. Method Run is invoked by the new system thread, which - if not overwritten - invokes the method Runnable::Run. Of course, this method immediately returns, and after invocation, method IsAlive will usually return true (unless the executed thread is not finished already).

Reimplemented from alib::threads::Thread.

Definition at line 273 of file thread.cpp.

◆ Stop()

void alib::threadmodel::Trigger::Stop ( )
virtual

Stops the trigger thread and joins it.

Definition at line 123 of file trigger.cpp.

Here is the call graph for this function:

The documentation for this class was generated from the following files: