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 93 of file trigger.inl.
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, TriggerEntry > | triggerList |
The list of registered triggered objects. | |
bool | wakeUpCondition = false |
![]() | |
std::thread * | c11Thread =nullptr |
The internal C++ thread object. | |
ThreadID | id =0 |
The id of the thread. | |
const character * | name =nullptr |
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. | |
![]() | |
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 |
![]() | |
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 character * | GetName () const |
std::thread::id | GetNativeID () const |
State | GetState () |
bool | IsAlive () |
virtual ALIB_DLL void | Join () |
virtual void | SetName (const character *newName) |
![]() | |
virtual | ~Runnable () |
Virtual destructor. | |
![]() | |
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 | |
![]() | |
enum class | State { Unstarted = 0 , Started = 1 , Running = 2 , Done = 3 , Terminated = 4 } |
![]() | |
static ALIB_DLL Thread * | Get (std::thread::id nativeID) |
static Thread * | GetCurrent () |
static ALIB_DLL 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 60 of file trigger.inl.
|
friend |
Definition at line 60 of file trigger.inl.
|
protected |
Denotes whether or not the trigger is currently used in internal thread mode.
Definition at line 124 of file trigger.inl.
|
protected |
The list of registered triggered objects.
Definition at line 117 of file trigger.inl.
|
protected |
The condition requested by parent class TCondition via a call to isConditionMet.
Definition at line 121 of file trigger.inl.
alib::threadmodel::Trigger::Trigger | ( | ) |
|
overridevirtual |
void alib::threadmodel::Trigger::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 50 of file trigger.cpp.
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.
until | Defines the future point in time until triggering is performed. |
Definition at line 134 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 154 of file trigger.inl.
|
inlineprotectednoexcept |
Implementation of the interface needed by parent TCondition.
Definition at line 128 of file trigger.inl.
void alib::threadmodel::Trigger::Remove | ( | Triggered & | triggered | ) |
Remove a previously added triggered object.
triggered | The object to be removed from the list. |
Definition at line 82 of file trigger.cpp.
|
overridevirtual |
Implementation of the parent interface (virtual abstract).
Reimplemented from alib::threads::Thread.
Definition at line 110 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 alib::threads::Thread.
Definition at line 273 of file thread.cpp.
|
virtual |
Stops the trigger thread and joins it.
Definition at line 123 of file trigger.cpp.