ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
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 100 of file trigger.hpp.

#include <trigger.hpp>

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

Inner Type Index:

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

Public Method Index:

ALIB_API Trigger ()
 Constructor.
 
virtual ALIB_API ~Trigger () override
 Destructor.
 
ALIB_API void Add (Triggered &triggered, bool initialWakeup=false)
 
ALIB_API void Do (Ticks until)
 
void Do (Ticks::Duration until)
 
ALIB_API void Remove (Triggered &triggered)
 
virtual ALIB_API void Run () override
 Implementation of the parent interface (virtual abstract).
 
virtual ALIB_API void Start ()
 
virtual ALIB_API 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 Thread
std::thread * c11Thread =nullptr
 The internal C++ thread object.
 
ThreadID id =0
 The id of the thread.
 
AString name
 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 TCondition< TDerived >
std::condition_variable conditionVariable
 The condition variable used for blocking and notification.
 
DbgConditionAsserter Dbg
 The debug tool instance.
 
std::mutex mutex
 The mutex used for locking this instance.
 

Protected Method Index:

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

Additional Inherited Members

- Protected Type Index: inherited from Thread
enum class  State {
  Unstarted = 0 , Started = 1 , Running = 2 , Done = 3 ,
  Terminated = 4
}
 
- Protected Static Method Index: inherited from Thread
static ALIB_API ThreadGet (std::thread::id nativeID)
 
static ThreadGetCurrent ()
 
static ALIB_API 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 67 of file trigger.hpp.

◆ threads::TCondition< Trigger >

friend struct threads::TCondition< Trigger >
friend

Definition at line 67 of file trigger.hpp.

Field Details:

◆ internalThreadMode

bool internalThreadMode = false
protected

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

Definition at line 131 of file trigger.hpp.

◆ triggerList

List<MonoAllocator, TriggerEntry> triggerList
protected

The list of registered triggered objects.

Definition at line 124 of file trigger.hpp.

◆ wakeUpCondition

bool wakeUpCondition = false
protected

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

Definition at line 128 of file trigger.hpp.

Constructor(s) / Destructor Details:

◆ Trigger()

Trigger ( )

Constructor.

Definition at line 20 of file trigger.cpp.

◆ ~Trigger()

~Trigger ( )
overridevirtual

Destructor.

Definition at line 26 of file trigger.cpp.

Here is the call graph for this function:

Method Details:

◆ Add()

void 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 39 of file trigger.cpp.

Here is the call graph for this function:

◆ Do() [1/2]

void 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 118 of file trigger.cpp.

Here is the call graph for this function:

◆ Do() [2/2]

void 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 161 of file trigger.hpp.

Here is the call graph for this function:

◆ isConditionMet()

bool isConditionMet ( ) const
inlineprotectednoexcept

Implementation of the interface needed by parent TCondition.

Returns
Member wakeUpCondition

Definition at line 135 of file trigger.hpp.

◆ Remove()

void Remove ( Triggered & triggered)

Remove a previously added triggered object.

Parameters
triggeredThe object to be removed from the list.

Definition at line 69 of file trigger.cpp.

◆ Run()

void Run ( )
overridevirtual

Implementation of the parent interface (virtual abstract).

Reimplemented from Thread.

Definition at line 94 of file trigger.cpp.

Here is the call graph for this function:

◆ Start()

void 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 Thread.

Definition at line 265 of file thread.cpp.

◆ Stop()

void Stop ( )
virtual

Stops the trigger thread and joins it.

Definition at line 107 of file trigger.cpp.

Here is the call graph for this function:

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