ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
trigger.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2024 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
8
12
13
14using namespace std::literals::chrono_literals;
15
16
17namespace alib::threadmodel {
18
19
21: Thread (A_CHAR("Trigger"))
22, TCondition (ALIB_DBG(A_CHAR("Trigger")))
23, triggerList (monomem::GLOBAL_ALLOCATOR)
24{}
25
27{
28 if( state <= State::Started )
29 {
30 ALIB_WARNING("MGTHR", "Trigger destroyed without having run" )
31 }
32 else if( state != State::Terminated )
33 {
34 ALIB_ERROR("MGTHR", "Trigger destroyed without being terminated" )
35 Start();
36 }
37}
38
39void Trigger::Add(Triggered& triggered, bool initialWakeup)
40{
42
43 // find if not already registered
44 bool isExisting= false;
45 for ( auto& it : triggerList )
46 if ( it.Target == &triggered )
47 {
48 isExisting= true;
49 break;
50 }
51
52 Ticks now;
53 if(!isExisting )
55 triggerList.EmplaceBack( &triggered,
56 now + ( initialWakeup ? Ticks::Duration()
57 : triggered.triggerPeriod() ) );
58 }
59 else
60 ALIB_WARNING( "MGTHR", NString256() <<
61 "Duplicate registration of triggered object \"" << triggered.Name << "\"." )
62
63
64 // Wake us (thread manager) up and perform a first trigger
65 wakeUpCondition= true;
67}
68
69void Trigger::Remove(Triggered &triggered)
70{
71 bool found= false;
72
73 { ALIB_LOCK
74 for(auto it= triggerList.begin() ; it != triggerList.end() ; it++)
75 {
76 if(it->Target == &triggered)
77 {
78 found= true;
79 triggerList.Erase(it);
80 break;
81 }
82 }
83 }
84
85 // check
86 if(!found)
87 {
88 ALIB_MESSAGE("MGTHR", NString256( "Managed thread \"" )
89 << triggered.Name
90 << "\" not found for de-registering with trigger list" )
91 }
92}
93
95{
96 ALIB_MESSAGE("MGTHR", "Internal trigger-thread started")
98
101
102 internalThreadMode= false;
103
104 ALIB_MESSAGE("MGTHR", "Internal trigger-thread exiting" )
105}
106
115
116
117
119{ ALIB_LOCK
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 {
137 // Trigger current?
138 if( it.NextWakeup <= now )
139 {
140 it.Target->trigger();
141 now.Reset(); // first we increase now, then we calculate the next wakeup
142 it.NextWakeup= now + it.Target->triggerPeriod();
143 }
144
145 // If earlier than others, use this as the new trigger time
146 nextTriggerTime= (std::min)(nextTriggerTime, it.NextWakeup);
147 }
148
149 // sleep
150 wakeUpCondition= false;
152 }
153
155}
156
157
158
159
160} // namespace [alib::threadmodel]
161
162
virtual ALIB_API void Start()
Definition thread.cpp:284
ALIB_API void Add(Triggered &triggered, bool initialWakeup=false)
Definition trigger.cpp:39
bool internalThreadMode
Denotes whether or not the trigger is currently used in internal thread mode.
Definition trigger.hpp:131
virtual ALIB_API void Stop()
Stops the trigger thread and joins it.
Definition trigger.cpp:107
virtual ALIB_API void Run() override
Implementation of the parent interface (virtual abstract).
Definition trigger.cpp:94
ALIB_API Trigger()
Constructor.
Definition trigger.cpp:20
virtual ALIB_API ~Trigger() override
Destructor.
Definition trigger.cpp:26
ALIB_API void Remove(Triggered &triggered)
Definition trigger.cpp:69
ALIB_API void Do(Ticks until)
Definition trigger.cpp:118
List< MonoAllocator, TriggerEntry > triggerList
The list of registered triggered objects.
Definition trigger.hpp:124
virtual Ticks::Duration triggerPeriod()=0
@ Started
Method Start was invoked but not running, yet.
@ Terminated
The thread is terminated.
State state
Internal flag to indicate if the thread is alive.
Definition thread.hpp:144
virtual ALIB_API void Join()
Definition thread.cpp:220
#define ALIB_WARNING(...)
Definition alib.hpp:1268
#define ALIB_COMMA_CALLER_PRUNED
Definition alib.hpp:1171
#define A_CHAR(STR)
#define ALIB_MESSAGE(...)
Definition alib.hpp:1269
#define ALIB_ERROR(...)
Definition alib.hpp:1267
#define ALIB_LOCK_RECURSIVE_WITH(lock)
Definition owner.hpp:457
#define ALIB_ASSERT_ERROR(cond,...)
Definition alib.hpp:1271
#define ALIB_LOCK
Definition owner.hpp:453
#define ALIB_DBG(...)
Definition alib.hpp:390
#define ALIB_CALLER_PRUNED
Definition alib.hpp:1170
constexpr bool IsNull(const T &t)
constexpr void SetNull(const T &t)
ALIB_API RecursiveLock GLOBAL_ALLOCATOR_LOCK
NLocalString< 256 > NString256
Type alias name for TLocalString<nchar,256>.
void WaitForNotification(ALIB_DBG_TAKE_CI)
DbgConditionAsserter Dbg
The debug tool instance.
void Acquire(ALIB_DBG_TAKE_CI)
void ReleaseAndNotify(ALIB_DBG_TAKE_CI)