ALib C++ Library
Library Version: 2510 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{}
36
38{
39 if( state <= State::Started )
40 {
41 ALIB_WARNING("MGTHR", "Trigger destroyed without having run" )
42 }
43 else if( state != State::Terminated )
44 {
45 ALIB_ERROR("MGTHR", "Trigger destroyed without being terminated" )
46 Start();
47 }
48}
49
50void Trigger::Add(Triggered& triggered, bool initialWakeup)
51{
53
54 // find if not already registered
55 bool isExisting= false;
56 for ( auto& it : triggerList )
57 if ( it.Target == &triggered )
58 {
59 isExisting= true;
60 break;
61 }
62
63 Ticks now;
64 if(!isExisting )
66 triggerList.emplace_back( &triggered,
67 now + ( initialWakeup ? Ticks::Duration()
68 : triggered.triggerPeriod() ) );
69 }
70 else
71#if ALIB_STRINGS
72 ALIB_WARNING( "MGTHR", "Duplicate registration of triggered object \"{}\".",
73 triggered.Name )
74#endif
75 ALIB_WARNING( "MGTHR", "Duplicate registration of triggered object." )
76
77 // Wake us (thread manager) up and perform a first trigger
78 wakeUpCondition= true;
80}
81
82void Trigger::Remove(Triggered &triggered)
83{
84 bool found= false;
85
86 { ALIB_LOCK
87 for(auto it= triggerList.begin() ; it != triggerList.end() ; it++)
88 {
89 if(it->Target == &triggered)
90 {
91 found= true;
92 triggerList.erase(it);
93 break;
94 }
95 }
96 }
97
98 // check
99 if(!found)
100 {
101 #if ALIB_STRINGS
102 ALIB_MESSAGE("MGTHR",
103 "Managed thread \"{}\" not found for de-registering with trigger list", triggered.Name)
104 #else
105 ALIB_MESSAGE("MGTHR", "Managed thread not found for de-registering with trigger list")
106 #endif
107 }
108}
109
111{
112 ALIB_MESSAGE( "MGTHR", "Internal trigger-thread started" )
113 internalThreadMode= true;
114
115 while(internalThreadMode)
117
118 internalThreadMode= false;
119
120 ALIB_MESSAGE( "MGTHR", "Internal trigger-thread exiting" )
121}
122
131
132
133
135{ ALIB_LOCK
136 ALIB_ASSERT_ERROR( lang::IsNull(Dbg.AssertExclusiveWaiter), "MGTHR",
137 "Trigger::Do() called twice!\n"
138 "Hint: Did you start the trigger thread and in addition 'manually' invoked Do()?\n"
139 " Only on sort of execution is allowed." )
140
141 ALIB_DBG( Dbg.AssertExclusiveWaiter= std::this_thread::get_id(); )
142
143 // wait loop
144 bool calledByInternalThread= internalThreadMode;
146 while( (!calledByInternalThread || internalThreadMode )
147 && now.Reset() < until )
148 {
149 // trigger loop
150 Ticks nextTriggerTime= until;
151 for( auto& it : triggerList )
152 {
153 // Trigger current?
154 if( it.NextWakeup <= now )
155 {
156 it.Target->trigger();
157 now.Reset(); // first we increase now, then we calculate the next wakeup
158 it.NextWakeup= now + it.Target->triggerPeriod();
159 }
160
161 // If earlier than others, use this as the new trigger time
162 nextTriggerTime= (std::min)(nextTriggerTime, it.NextWakeup);
163 }
164
165 // sleep
166 wakeUpCondition= false;
168 }
169
170 ALIB_DBG( lang::SetNull(Dbg.AssertExclusiveWaiter); )
171}
172
173
174
175
176} // namespace [alib::threadmodel]
177
178
ALIB_DLL void Add(Triggered &triggered, bool initialWakeup=false)
Definition trigger.cpp:50
bool internalThreadMode
Denotes whether or not the trigger is currently used in internal thread mode.
Definition trigger.inl:124
virtual ALIB_DLL void Run() override
Implementation of the parent interface (virtual abstract).
Definition trigger.cpp:110
virtual ALIB_DLL void Stop()
Stops the trigger thread and joins it.
Definition trigger.cpp:123
ALIB_DLL void Do(Ticks until)
Definition trigger.cpp:134
List< MonoAllocator, TriggerEntry > triggerList
The list of registered triggered objects.
Definition trigger.inl:117
ALIB_DLL void Remove(Triggered &triggered)
Definition trigger.cpp:82
ALIB_DLL Trigger()
Constructor.
Definition trigger.cpp:31
virtual ALIB_DLL void Start()
Definition thread.cpp:296
virtual ALIB_DLL ~Trigger() override
Destructor.
Definition trigger.cpp:37
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:251
State state
Internal flag to indicate if the thread is alive.
Definition thread.inl:168
#define ALIB_MESSAGE(domain,...)
Definition alib.inl:1047
#define ALIB_COMMA_CALLER_PRUNED
Definition alib.inl:1008
#define A_CHAR(STR)
#define ALIB_WARNING(domain,...)
Definition alib.inl:1046
#define ALIB_ERROR(domain,...)
Definition alib.inl:1045
#define ALIB_LOCK_RECURSIVE_WITH(lock)
Definition alib.inl:1323
#define ALIB_LOCK
Definition alib.inl:1319
#define ALIB_DBG(...)
Definition alib.inl:836
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1049
#define ALIB_CALLER_PRUNED
Definition alib.inl:1007
constexpr void SetNull(T &t)
Definition tmp.inl:64
constexpr bool IsNull(const T &t)
Definition tmp.inl:49
ALIB_DLL RecursiveLock GLOBAL_ALLOCATOR_LOCK
time::Ticks Ticks
Type alias in namespace alib.
Definition ticks.inl:109
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)