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.
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>
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, TriggerEntry > | triggerList |
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. | |
Runnable * | runnable =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 Thread * | Get (std::thread::id nativeID) |
static Thread * | GetCurrent () |
static ALIB_API Thread * | GetMain () |
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 () |
|
friend |
Definition at line 67 of file trigger.hpp.
|
friend |
Definition at line 67 of file trigger.hpp.
|
protected |
Denotes whether or not the trigger is currently used in internal thread mode.
Definition at line 131 of file trigger.hpp.
|
protected |
The list of registered triggered objects.
Definition at line 124 of file trigger.hpp.
|
protected |
The condition requested by parent class TCondition via a call to isConditionMet.
Definition at line 128 of file trigger.hpp.
Trigger | ( | ) |
Constructor.
Definition at line 20 of file trigger.cpp.
|
overridevirtual |
void Add | ( | Triggered & | triggered, |
bool | initialWakeup = false ) |
Add an object to be triggered.
triggered | The object to be triggered. |
initialWakeup | If true , the first wakeup is scheduled right away. Defaults to false . |
Definition at line 39 of file trigger.cpp.
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.
until | Defines the future point in time until triggering is performed. |
Definition at line 118 of file trigger.cpp.
|
inline |
Invokes Do(Ticks) by adding the given duration to the current point in time.
until | Defines the maximum duration for which this method will execute the trigger logic. |
Definition at line 161 of file trigger.hpp.
|
inlineprotectednoexcept |
Implementation of the interface needed by parent TCondition.
Definition at line 135 of file trigger.hpp.
void Remove | ( | Triggered & | triggered | ) |
Remove a previously added triggered object.
triggered | The object to be removed from the list. |
Definition at line 69 of file trigger.cpp.
|
overridevirtual |
Implementation of the parent interface (virtual abstract).
Reimplemented from Thread.
Definition at line 94 of file trigger.cpp.
|
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.
|
virtual |
Stops the trigger thread and joins it.
Definition at line 107 of file trigger.cpp.