4#include "Stroika/Foundation/StroikaPreComp.h"
10#include "Stroika/Foundation/Containers/Collection.h"
11#include "Stroika/Foundation/Debug/Main.h"
23using namespace Stroika::Foundation::Time;
34 sb <<
"Callback: "sv << fCallback;
35 sb <<
", CallNextAt: "sv << fCallNextAt;
36 sb <<
", Frequency: "sv << fFrequency;
37 sb <<
", Hysteresis: "sv << fHysteresis;
49 virtual ~Rep_ () =
default;
53 auto lk = fData_.rwget ();
54 lk->Add (RegisteredTask{intervalTimer, Time::GetTickCount () + when});
57 void AddRepeating (
const TimerCallback& intervalTimer,
const Time::Duration& repeatInterval,
const optional<Time::Duration>& hysteresis)
60 auto lk = fData_.rwget ();
61 lk->Add ({intervalTimer, Time::GetTickCount () + repeatInterval, repeatInterval, hysteresis});
64 void RemoveRepeating (
const TimerCallback& intervalTimer)
noexcept
67 auto lk = fData_.rwget ();
68 RegisteredTaskCollection x = lk.cref ();
69 lk->Remove (intervalTimer);
72 RegisteredTaskCollection GetAllRegisteredTasks ()
const
75 return fData_.load ();
88#if qStroika_Foundation_Debug_AssertionsChecked
89 auto dataLock = fData_.
cget ();
93 for (
const RegisteredTask& i : dataLock.cref ()) {
94 r = min (r, i.fCallNextAt);
96 Assert (r == funResult);
106 Require (Debug::AppearsDuringMainLifetime ());
107 fDataChanged_.WaitUntilQuietly (GetNextWakeupTime_ ());
108 fDataChanged_.Reset ();
115 for (
const RegisteredTask& i : elts2Run) {
116 IgnoreExceptionsExceptThreadAbortForCall (i.fCallback ());
119 now = Time::GetTickCount ();
120 auto rwDataLock = fData_.
rwget ();
121 for (
const RegisteredTask& i : elts2Run) {
122 if (i.fFrequency.has_value ()) {
123 RegisteredTask newE = i;
124 newE.fCallNextAt = now + *i.fFrequency;
126 uniform_real_distribution<> dis{-i.fHysteresis->count (), i.fHysteresis->count ()};
129 rwDataLock->Add (newE);
132 rwDataLock->Remove (i);
139 auto rwThreadLk = fThread_.rwget ();
140 if (fData_.
cget ()->empty ()) {
141 rwThreadLk.store (
nullptr);
144 auto lk = fThread_.rwget ();
145 if (lk.cref () ==
nullptr) {
146 using namespace Execution;
147 lk.store (make_shared<Thread::CleanupPtr> (Thread::CleanupPtr::eAbortBeforeWaiting,
148 Thread::New ([
this] () { RunnerLoop_ (); }, Thread::eAutoStart,
"Default-Interval-Timer"sv)));
151 fDataChanged_.Set ();
157IntervalTimer::Manager::DefaultRep::DefaultRep ()
158 : fHiddenRep_{make_shared<Rep_> ()}
165 fHiddenRep_->AddOneShot (intervalTimer, when);
169 const optional<Time::Duration>& hysteresis)
172 fHiddenRep_->AddRepeating (intervalTimer, repeatInterval, hysteresis);
175void IntervalTimer::Manager::DefaultRep::RemoveRepeating (
const TimerCallback& intervalTimer)
noexcept
178 fHiddenRep_->RemoveRepeating (intervalTimer);
181auto IntervalTimer::Manager::DefaultRep::GetAllRegisteredTasks () const -> RegisteredTaskCollection
184 return fHiddenRep_->GetAllRegisteredTasks ();
192IntervalTimer::Manager::Activator::Activator ()
196 Require (Debug::AppearsDuringMainLifetime ());
197 Manager::sThe = Manager{make_shared<IntervalTimer::Manager::DefaultRep> ()};
200IntervalTimer::Manager::Activator::~Activator ()
204 Require (Debug::AppearsDuringMainLifetime ());
214 RunImmediatelyFlag runImmediately,
const optional<Time::Duration>& hysteresis)
215 : fRepeatInterval_{repeatInterval}
216 , fHysteresis_{hysteresis}
217 , fManager_{&manager}
221 if (runImmediately == RunImmediatelyFlag::eRunImmediately) {
222 IgnoreExceptionsExceptThreadAbortForCall (fFunction_ ());
#define RequireNotNull(p)
time_point< RealtimeClock, DurationSeconds > TimePointSeconds
TimePointSeconds is a simpler approach to chrono::time_point, which doesn't require using templates e...
constexpr DurationSeconds kInfinity
chrono::duration< double > DurationSeconds
chrono::duration<double> - a time span (length of time) measured in seconds, but high precision.
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
String is like std::u32string, except it is much easier to use, often much more space efficient,...
A Collection<T> is a container to manage an un-ordered collection of items, without equality defined ...
nonvirtual void AddRepeating(const TimerCallback &intervalTimer, const Time::Duration &repeatInterval, const optional< Time::Duration > &hysteresis=nullopt)
Add a timer to be called repeatedly after duration repeatInterval.
Function< void()> TimerCallback
Wrap any object with Synchronized<> and it can be used similarly to the base type,...
nonvirtual WritableReference rwget()
get a read-write smart pointer to the underlying Synchronized<> object, holding the full lock the who...
nonvirtual ReadableReference cget() const
get a read-only smart pointer to the underlying Synchronized<> object, holding the readlock the whole...
Duration is a chrono::duration<double> (=.
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
Ptr New(const function< void()> &fun2CallOnce, const optional< Characters::String > &name, const optional< Configuration > &configuration)
nonvirtual Characters::String ToString() const