13 std::atomic<bool> closingDown;
17 std::function<void()> callback;
20 std::mutex entryMutex;
35 void removeEntry(TimerEntry& entry);
44 std::chrono::milliseconds ms(
interval);
58 return std::chrono::duration_cast<std::chrono::seconds>(Clock::now() -
started).count();
60 return std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() -
started).count();
62 return std::chrono::duration_cast<std::chrono::microseconds>(Clock::now() -
started).count();
64 return std::chrono::duration_cast<std::chrono::nanoseconds>(Clock::now() -
started).count();
88 return std::chrono::duration_cast<std::chrono::milliseconds>(
elapsedImpl()).count();
90 return std::chrono::duration_cast<std::chrono::microseconds>(
elapsedImpl()).count();
98 const std::function<
void(
void)>& callback,
108 thread = std::thread([
this]() { this->runLoop(); });
119 instance = makeAuto<TimerThread>();
125 std::unique_lock<std::mutex> lock(entryMutex);
126 entries.
push(TimerEntry{ timer, callback });
129 void TimerThread::runLoop() {
131 while (!closingDown) {
133 std::unique_lock<std::mutex> lock(entryMutex);
136 for (TimerEntry& entry : copies) {
138 if (
auto timerPtr = entry.timer.lock()) {
139 if (timerPtr->isExpired()) {
142 if (timerPtr->isPeriodic()) {
146 this->removeEntry(entry);
150 this->removeEntry(entry);
153 std::this_thread::sleep_for(std::chrono::milliseconds(50));
157 void TimerThread::removeEntry(TimerEntry& entry) {
170 std::stringstream ss;
172 ss << time /
DAY <<
"d ";
176 ss << std::setw(2) << std::setfill(
'0') << time /
HOUR <<
"h ";
180 ss << std::setw(2) << std::setfill(
'0') << time /
MINUTE <<
"min ";
183 ss << std::setw(2) << std::setfill(
'0') << time /
SECOND;
184 ss <<
"." << std::setw(3) << std::setfill(
'0') << time %
SECOND <<
"s";
INLINE CopyableArray< T, TAllocator, TCounter > copyable(const Array< T, TAllocator, TCounter > &array)
#define SPH_ASSERT(x,...)
#define NOT_IMPLEMENTED
Helper macro marking missing implementation.
#define NAMESPACE_SPH_END
SharedPtr< Timer > makeTimer(const int64_t interval, const std::function< void(void)> &callback, const Flags< TimerFlags > flags)
Creates timer with given interval and callback when time interval is finished.
std::string getFormattedTime(int64_t time)
Returns the human-readable formatted time in suitable units.
Measuring time intervals and executing periodic events.
@ START_EXPIRED
Creates expired timer, calling elapsed immediately after creating will return the timer interval.
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
constexpr INLINE bool has(const TEnum flag) const
Checks if the object has a given flag.
int64_t elapsed(const TimerUnit unit) const
Returns elapsed time in timer units. Does not reset the timer.
void stop()
Stops the timer. Function getElapsed() will report the same value from now on.
void resume()
Resumes stopped timer.
void registerTimer(const SharedPtr< Timer > &timer, const std::function< void(void)> &callback)
static TimerThread & getInstance()
int64_t elapsed(const TimerUnit unit) const
Returns elapsed time in timer units. Does not reset the timer.
void restart()
Reset elapsed duration to zero.
Timer(const int64_t interval=0, const Flags< TimerFlags > flags=EMPTY_FLAGS)
Creates timer with given expiration duration.
Flags< TimerFlags > flags
Object with deleted copy constructor and copy operator.