From bd3fb642a6c7e1101c5908214a65de9fea977d91 Mon Sep 17 00:00:00 2001 From: AssemblyJohn Date: Mon, 21 Oct 2024 12:46:31 +0300 Subject: [PATCH] Updated class for sync mutex usage Signed-off-by: AssemblyJohn --- include/everest/timer.hpp | 132 +++++++++++++++++++------------------- 1 file changed, 67 insertions(+), 65 deletions(-) diff --git a/include/everest/timer.hpp b/include/everest/timer.hpp index 3affcb8..ce3d48b 100644 --- a/include/everest/timer.hpp +++ b/include/everest/timer.hpp @@ -67,7 +67,7 @@ template class this->callback = callback; } - ~Timer() { + ~Timer() { if (this->timer != nullptr) { // stop asio timer this->timer->cancel(); @@ -85,16 +85,12 @@ template class /// Executes the given callback at the given timepoint template void at(const std::function& callback, const std::chrono::time_point& time_point) { - if constexpr(ThreadSafe) { - OptionalGuard optional_guard(this->mutex); - this->stop(); - this->callback = callback; - } else { - this->stop(); - this->callback = callback; - } + OptionalGuard optional_guard(this->mutex); + + this->stop(); + this->callback = callback; - this->at(time_point); + this->at_internal(time_point); } /// Executes the at the given timepoint @@ -102,6 +98,65 @@ template class void at(const std::chrono::time_point& time_point) { OptionalGuard optional_guard(this->mutex); + at_internal(time_point); + } + + /// Execute the given callback peridically from now in the given interval + template + void interval(const std::function& callback, const std::chrono::duration& interval) { + OptionalGuard optional_guard(this->mutex); + + this->stop(); + this->callback = callback; + + this->interval_internal(interval); + } + + /// Execute peridically from now in the given interval + template void interval(const std::chrono::duration& interval) { + OptionalGuard optional_guard(this->mutex); + + this->interval_internal(interval); + } + + // Execute the given callback once after the given interval + template + void timeout(const std::function& callback, const std::chrono::duration& interval) { + OptionalGuard optional_guard(this->mutex); + + this->stop(); + this->callback = callback; + + this->timeout_internal(interval); + } + + // Execute the given callback once after the given interval + template void timeout(const std::chrono::duration& interval) { + OptionalGuard optional_guard(this->mutex); + + this->timeout_internal(interval); + } + + /// Stop timer from excuting its callback + void stop() { + OptionalGuard optional_guard(this->mutex); + + if (this->timer != nullptr) { + // asio based timer + this->timer->cancel(); + } + + running = false; + } + + bool is_running() { + return running; + } + +private: + /// Executes the at the given timepoint + template + void at_internal(const std::chrono::time_point& time_point) { this->stop(); if (this->callback == nullptr) { @@ -124,27 +179,7 @@ template class } } - /// Execute the given callback peridically from now in the given interval - template - void interval(const std::function& callback, const std::chrono::duration& interval) { - if constexpr(ThreadSafe) { - OptionalGuard optional_guard(this->mutex); - - this->stop(); - this->callback = callback; - } else { - this->stop(); - this->callback = callback; - } - - - this->interval(interval); - } - - /// Execute peridically from now in the given interval - template void interval(const std::chrono::duration& interval) { - OptionalGuard optional_guard(this->mutex); - + template void interval_internal(const std::chrono::duration& interval) { this->stop(); this->interval_nanoseconds = interval; if (interval_nanoseconds == std::chrono::nanoseconds(0)) { @@ -176,24 +211,7 @@ template class } } - // Execute the given callback once after the given interval - template - void timeout(const std::function& callback, const std::chrono::duration& interval) { - if constexpr (ThreadSafe) { - OptionalGuard optional_guard(this->mutex); - this->stop(); - this->callback = callback; - } else { - this->stop(); - this->callback = callback; - } - - this->timeout(interval); - } - - // Execute the given callback once after the given interval - template void timeout(const std::chrono::duration& interval) { - OptionalGuard optional_guard(this->mutex); + template void timeout_internal(const std::chrono::duration& interval) { this->stop(); if (this->callback == nullptr) { @@ -216,22 +234,6 @@ template class }); } } - - /// Stop timer from excuting its callback - void stop() { - OptionalGuard optional_guard(this->mutex); - - if (this->timer != nullptr) { - // asio based timer - this->timer->cancel(); - } - - running = false; - } - - bool is_running() { - return running; - } }; using SteadyTimer = Timer;