ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
trigger.cpp
1using namespace std::literals::chrono_literals;
2namespace alib::threadmodel {
3
4
6: Thread (A_CHAR("Trigger"))
7, TCondition (ALIB_DBG(A_CHAR("Trigger")))
8, triggerList (monomem::GLOBAL_ALLOCATOR) {}
9
11 if( state <= State::Started ) {
12 ALIB_WARNING("TMOD", "Trigger destroyed without having run" )
13 }
14 else if( state != State::Terminated ) {
15 ALIB_ERROR("TMOD", "Trigger destroyed without being terminated" )
16 Start();
17} }
18
19void Trigger::Add(Triggered& triggered, bool initialWakeup) {
21
22 // find if not already registered
23 bool isExisting= false;
24 for ( auto& it : triggerList )
25 if ( it.Target == &triggered ) {
26 isExisting= true;
27 break;
28 }
29
30 Ticks now;
31 if(!isExisting )
33 triggerList.emplace_back( &triggered,
34 now + ( initialWakeup ? Ticks::Duration()
35 : triggered.triggerPeriod() ) );
36 }
37 else
38#if ALIB_STRINGS
39 ALIB_WARNING( "TMOD", "Duplicate registration of triggered object \"{}\".",
40 triggered.Name )
41#endif
42 ALIB_WARNING( "TMOD", "Duplicate registration of triggered object." )
43
44 // Wake us (thread manager) up and perform a first trigger
45 wakeUpCondition= true;
47}
48
49void Trigger::Remove(Triggered &triggered) {
50 bool found= false;
51
52 { ALIB_LOCK
53 for(auto it= triggerList.begin() ; it != triggerList.end() ; it++) {
54 if(it->Target == &triggered) {
55 found= true;
56 triggerList.erase(it);
57 break;
58 } } }
59
60 // check
61 if(!found) {
62 #if ALIB_STRINGS
63 ALIB_MESSAGE("TMOD",
64 "Managed thread \"{}\" not found for de-registering with trigger list", triggered.Name)
65 #else
66 ALIB_MESSAGE("TMOD", "Managed thread not found for de-registering with trigger list")
67 #endif
68} }
69
71 ALIB_MESSAGE( "TMOD", "Internal trigger-thread started" )
73
76
77 internalThreadMode= false;
78
79 ALIB_MESSAGE( "TMOD", "Internal trigger-thread exiting" )
80}
81
89
90
91
92void Trigger::Do(Ticks until)
94 ALIB_ASSERT_ERROR( lang::IsNull(Dbg.AssertExclusiveWaiter), "TMOD",
95 "Trigger::Do() called twice!\n"
96 "Hint: Did you start the trigger thread and in addition 'manually' invoked Do()?\n"
97 " Only on sort of execution is allowed." )
98
99 ALIB_DBG( Dbg.AssertExclusiveWaiter= std::this_thread::get_id(); )
100
101 // wait loop
102 bool calledByInternalThread= internalThreadMode;
104 while( (!calledByInternalThread || internalThreadMode )
105 && now.Reset() < until )
106 {
107 // trigger loop
108 Ticks nextTriggerTime= until;
109 for( auto& it : triggerList ) {
110 // Trigger current?
111 if( it.NextWakeup <= now ) {
112 it.Target->trigger();
113 now.Reset(); // first we increase now, then we calculate the next wakeup
114 it.NextWakeup= now + it.Target->triggerPeriod();
115 }
116
117 // If earlier than others, use this as the new trigger time
118 nextTriggerTime= (std::min)(nextTriggerTime, it.NextWakeup);
119 }
120
121 // sleep
122 wakeUpCondition= false;
124 }
125
126 ALIB_DBG( lang::SetNull(Dbg.AssertExclusiveWaiter); )
127}
128
129
130
131
132} // namespace [alib::threadmodel]
#define ALIB_MESSAGE(domain,...)
#define ALIB_COMMA_CALLER_PRUNED
#define A_CHAR(STR)
#define ALIB_WARNING(domain,...)
#define ALIB_ERROR(domain,...)
#define ALIB_LOCK_RECURSIVE_WITH(lock)
#define ALIB_LOCK
#define ALIB_DBG(...)
#define ALIB_ASSERT_ERROR(cond, domain,...)
#define ALIB_CALLER_PRUNED
void Add(Triggered &triggered, bool initialWakeup=false)
Definition trigger.cpp:19
bool internalThreadMode
Denotes whether or not the trigger is currently used in internal thread mode.
Definition trigger.hpp:126
ListMA< TriggerEntry > triggerList
The list of registered triggered objects.
Definition trigger.hpp:119
virtual void Run() override
Implementation of the parent interface (virtual abstract).
Definition trigger.cpp:70
virtual void Stop()
Stops the trigger thread and joins it.
Definition trigger.cpp:82
void Do(Ticks until)
Definition trigger.cpp:92
void Remove(Triggered &triggered)
Definition trigger.cpp:49
Trigger()
Constructor.
Definition trigger.cpp:5
virtual ~Trigger() override
Destructor.
Definition trigger.cpp:10
virtual Ticks::Duration triggerPeriod()=0
@ Started
Method #".Start" was invoked but not running, yet.
Definition thread.hpp:137
@ Terminated
The thread is terminated.
Definition thread.hpp:141
Thread(const character *pName=A_CHAR(""))
Definition thread.hpp:178
virtual void Join()
Definition thread.cpp:183
State state
Internal flag to indicate if the thread is alive.
Definition thread.hpp:165
constexpr void SetNull(T &t)
Definition tmp.hpp:63
constexpr bool IsNull(const T &t)
Definition tmp.hpp:48
RecursiveLock GLOBAL_ALLOCATOR_LOCK
time::Ticks Ticks
Type alias in namespace #"%alib".
Definition ticks.hpp:86
DbgConditionAsserter Dbg
Definition condition.hpp:99
void WaitForNotification(ALIB_DBG_TAKE_CI)
TCondition(const character *dbgName)
void Acquire(ALIB_DBG_TAKE_CI)
void ReleaseAndNotify(ALIB_DBG_TAKE_CI)