ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
trigger.cpp
1//##################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6//##################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14//========================================= Global Fragment ========================================
15#include "alib/alib.inl"
16//============================================== Module ============================================
17#if ALIB_C20_MODULES
18 module ALib.ThreadModel;
19# if ALIB_MONOMEM
20 import ALib.Monomem;
21# endif
22#else
23# include "ALib.Monomem.H"
24# include "ALib.ThreadModel.H"
25#endif
26//========================================== Implementation ========================================
27using namespace std::literals::chrono_literals;
28namespace alib::threadmodel {
29
30
32: Thread (A_CHAR("Trigger"))
33, TCondition (ALIB_DBG(A_CHAR("Trigger")))
34, triggerList (monomem::GLOBAL_ALLOCATOR) {}
35
37 if( state <= State::Started ) {
38 ALIB_WARNING("TMOD", "Trigger destroyed without having run" )
39 }
40 else if( state != State::Terminated ) {
41 ALIB_ERROR("TMOD", "Trigger destroyed without being terminated" )
42 Start();
43} }
44
45void Trigger::Add(Triggered& triggered, bool initialWakeup) {
47
48 // find if not already registered
49 bool isExisting= false;
50 for ( auto& it : triggerList )
51 if ( it.Target == &triggered ) {
52 isExisting= true;
53 break;
54 }
55
56 Ticks now;
57 if(!isExisting )
59 triggerList.emplace_back( &triggered,
60 now + ( initialWakeup ? Ticks::Duration()
61 : triggered.triggerPeriod() ) );
62 }
63 else
64#if ALIB_STRINGS
65 ALIB_WARNING( "TMOD", "Duplicate registration of triggered object \"{}\".",
66 triggered.Name )
67#endif
68 ALIB_WARNING( "TMOD", "Duplicate registration of triggered object." )
69
70 // Wake us (thread manager) up and perform a first trigger
71 wakeUpCondition= true;
73}
74
75void Trigger::Remove(Triggered &triggered) {
76 bool found= false;
77
78 { ALIB_LOCK
79 for(auto it= triggerList.begin() ; it != triggerList.end() ; it++) {
80 if(it->Target == &triggered) {
81 found= true;
82 triggerList.erase(it);
83 break;
84 } } }
85
86 // check
87 if(!found) {
88 #if ALIB_STRINGS
89 ALIB_MESSAGE("TMOD",
90 "Managed thread \"{}\" not found for de-registering with trigger list", triggered.Name)
91 #else
92 ALIB_MESSAGE("TMOD", "Managed thread not found for de-registering with trigger list")
93 #endif
94} }
95
97 ALIB_MESSAGE( "TMOD", "Internal trigger-thread started" )
99
100 while(internalThreadMode)
102
103 internalThreadMode= false;
104
105 ALIB_MESSAGE( "TMOD", "Internal trigger-thread exiting" )
106}
107
115
116
117
119{ ALIB_LOCK
120 ALIB_ASSERT_ERROR( lang::IsNull(Dbg.AssertExclusiveWaiter), "TMOD",
121 "Trigger::Do() called twice!\n"
122 "Hint: Did you start the trigger thread and in addition 'manually' invoked Do()?\n"
123 " Only on sort of execution is allowed." )
124
125 ALIB_DBG( Dbg.AssertExclusiveWaiter= std::this_thread::get_id(); )
126
127 // wait loop
128 bool calledByInternalThread= internalThreadMode;
130 while( (!calledByInternalThread || internalThreadMode )
131 && now.Reset() < until )
132 {
133 // trigger loop
134 Ticks nextTriggerTime= until;
135 for( auto& it : triggerList ) {
136 // Trigger current?
137 if( it.NextWakeup <= now ) {
138 it.Target->trigger();
139 now.Reset(); // first we increase now, then we calculate the next wakeup
140 it.NextWakeup= now + it.Target->triggerPeriod();
141 }
142
143 // If earlier than others, use this as the new trigger time
144 nextTriggerTime= (std::min)(nextTriggerTime, it.NextWakeup);
145 }
146
147 // sleep
148 wakeUpCondition= false;
150 }
151
152 ALIB_DBG( lang::SetNull(Dbg.AssertExclusiveWaiter); )
153}
154
155
156
157
158} // namespace [alib::threadmodel]
ALIB_DLL void Add(Triggered &triggered, bool initialWakeup=false)
Definition trigger.cpp:45
bool internalThreadMode
Denotes whether or not the trigger is currently used in internal thread mode.
Definition trigger.inl:126
ListMA< TriggerEntry > triggerList
The list of registered triggered objects.
Definition trigger.inl:119
virtual ALIB_DLL void Run() override
Implementation of the parent interface (virtual abstract).
Definition trigger.cpp:96
virtual ALIB_DLL void Stop()
Stops the trigger thread and joins it.
Definition trigger.cpp:108
ALIB_DLL void Do(Ticks until)
Definition trigger.cpp:118
ALIB_DLL void Remove(Triggered &triggered)
Definition trigger.cpp:75
ALIB_DLL Trigger()
Constructor.
Definition trigger.cpp:31
virtual ALIB_DLL void Start()
Definition thread.cpp:285
virtual ALIB_DLL ~Trigger() override
Destructor.
Definition trigger.cpp:36
virtual Ticks::Duration triggerPeriod()=0
@ Started
Method Start was invoked but not running, yet.
Definition thread.inl:140
@ Terminated
The thread is terminated.
Definition thread.inl:144
Thread(const character *pName=A_CHAR(""))
Definition thread.inl:181
virtual ALIB_DLL void Join()
Definition thread.cpp:244
State state
Internal flag to indicate if the thread is alive.
Definition thread.inl:168
#define ALIB_MESSAGE(domain,...)
Definition alib.inl:1064
#define ALIB_COMMA_CALLER_PRUNED
Definition alib.inl:1025
#define A_CHAR(STR)
#define ALIB_WARNING(domain,...)
Definition alib.inl:1063
#define ALIB_ERROR(domain,...)
Definition alib.inl:1062
#define ALIB_LOCK_RECURSIVE_WITH(lock)
Definition alib.inl:1340
#define ALIB_LOCK
Definition alib.inl:1336
#define ALIB_DBG(...)
Definition alib.inl:853
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1066
#define ALIB_CALLER_PRUNED
Definition alib.inl:1024
constexpr void SetNull(T &t)
Definition tmp.inl:63
constexpr bool IsNull(const T &t)
Definition tmp.inl:48
ALIB_DLL RecursiveLock GLOBAL_ALLOCATOR_LOCK
time::Ticks Ticks
Type alias in namespace alib.
Definition ticks.inl:79
DbgConditionAsserter Dbg
Definition condition.inl:93
void WaitForNotification(ALIB_DBG_TAKE_CI)
TCondition(const character *dbgName)
void Acquire(ALIB_DBG_TAKE_CI)
void ReleaseAndNotify(ALIB_DBG_TAKE_CI)