Skip to content

Commit

Permalink
Updated class for sync mutex usage
Browse files Browse the repository at this point in the history
Signed-off-by: AssemblyJohn <[email protected]>
  • Loading branch information
AssemblyJohn committed Oct 21, 2024
1 parent 1e8c2bd commit bd3fb64
Showing 1 changed file with 67 additions and 65 deletions.
132 changes: 67 additions & 65 deletions include/everest/timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ template <typename TimerClock = date::utc_clock, bool ThreadSafe = false> class
this->callback = callback;
}

~Timer() {
~Timer() {
if (this->timer != nullptr) {
// stop asio timer
this->timer->cancel();
Expand All @@ -85,23 +85,78 @@ template <typename TimerClock = date::utc_clock, bool ThreadSafe = false> class
/// Executes the given callback at the given timepoint
template <class Clock, class Duration = typename Clock::duration>
void at(const std::function<void()>& callback, const std::chrono::time_point<Clock, Duration>& time_point) {
if constexpr(ThreadSafe) {
OptionalGuard<std::unique_lock, std::mutex, ThreadSafe> optional_guard(this->mutex);
this->stop();
this->callback = callback;
} else {
this->stop();
this->callback = callback;
}
OptionalGuard<std::unique_lock, std::mutex, ThreadSafe> optional_guard(this->mutex);

Check notice on line 88 in include/everest/timer.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

include/everest/timer.hpp#L88

Variable 'optional_guard' is assigned a value that is never used.

this->stop();
this->callback = callback;

this->at(time_point);
this->at_internal(time_point);
}

/// Executes the at the given timepoint
template <class Clock, class Duration = typename Clock::duration>
void at(const std::chrono::time_point<Clock, Duration>& time_point) {
OptionalGuard<std::unique_lock, std::mutex, ThreadSafe> optional_guard(this->mutex);

at_internal<Clock, Duration>(time_point);
}

/// Execute the given callback peridically from now in the given interval
template <class Rep, class Period>
void interval(const std::function<void()>& callback, const std::chrono::duration<Rep, Period>& interval) {
OptionalGuard<std::unique_lock, std::mutex, ThreadSafe> optional_guard(this->mutex);

this->stop();
this->callback = callback;

this->interval_internal(interval);
}

/// Execute peridically from now in the given interval
template <class Rep, class Period> void interval(const std::chrono::duration<Rep, Period>& interval) {
OptionalGuard<std::unique_lock, std::mutex, ThreadSafe> optional_guard(this->mutex);

this->interval_internal(interval);
}

// Execute the given callback once after the given interval
template <class Rep, class Period>
void timeout(const std::function<void()>& callback, const std::chrono::duration<Rep, Period>& interval) {
OptionalGuard<std::unique_lock, std::mutex, ThreadSafe> optional_guard(this->mutex);

this->stop();
this->callback = callback;

this->timeout_internal(interval);
}

// Execute the given callback once after the given interval
template <class Rep, class Period> void timeout(const std::chrono::duration<Rep, Period>& interval) {
OptionalGuard<std::unique_lock, std::mutex, ThreadSafe> optional_guard(this->mutex);

this->timeout_internal(interval);
}

/// Stop timer from excuting its callback
void stop() {
OptionalGuard<std::unique_lock, std::mutex, ThreadSafe> 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 <class Clock, class Duration = typename Clock::duration>
void at_internal(const std::chrono::time_point<Clock, Duration>& time_point) {
this->stop();

if (this->callback == nullptr) {
Expand All @@ -124,27 +179,7 @@ template <typename TimerClock = date::utc_clock, bool ThreadSafe = false> class
}
}

/// Execute the given callback peridically from now in the given interval
template <class Rep, class Period>
void interval(const std::function<void()>& callback, const std::chrono::duration<Rep, Period>& interval) {
if constexpr(ThreadSafe) {
OptionalGuard<std::unique_lock, std::mutex, ThreadSafe> 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 <class Rep, class Period> void interval(const std::chrono::duration<Rep, Period>& interval) {
OptionalGuard<std::unique_lock, std::mutex, ThreadSafe> optional_guard(this->mutex);

template <class Rep, class Period> void interval_internal(const std::chrono::duration<Rep, Period>& interval) {
this->stop();
this->interval_nanoseconds = interval;
if (interval_nanoseconds == std::chrono::nanoseconds(0)) {
Expand Down Expand Up @@ -176,24 +211,7 @@ template <typename TimerClock = date::utc_clock, bool ThreadSafe = false> class
}
}

// Execute the given callback once after the given interval
template <class Rep, class Period>
void timeout(const std::function<void()>& callback, const std::chrono::duration<Rep, Period>& interval) {
if constexpr (ThreadSafe) {
OptionalGuard<std::unique_lock, std::mutex, ThreadSafe> 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 <class Rep, class Period> void timeout(const std::chrono::duration<Rep, Period>& interval) {
OptionalGuard<std::unique_lock, std::mutex, ThreadSafe> optional_guard(this->mutex);
template <class Rep, class Period> void timeout_internal(const std::chrono::duration<Rep, Period>& interval) {
this->stop();

if (this->callback == nullptr) {
Expand All @@ -216,22 +234,6 @@ template <typename TimerClock = date::utc_clock, bool ThreadSafe = false> class
});
}
}

/// Stop timer from excuting its callback
void stop() {
OptionalGuard<std::unique_lock, std::mutex, ThreadSafe> optional_guard(this->mutex);

if (this->timer != nullptr) {
// asio based timer
this->timer->cancel();
}

running = false;
}

bool is_running() {
return running;
}
};

using SteadyTimer = Timer<date::utc_clock>;
Expand Down

0 comments on commit bd3fb64

Please sign in to comment.