From 46a96ff4cd1a90c83fb1313bd50c63aee315875f Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 10 Aug 2023 16:51:28 +0200 Subject: [PATCH] Remove mutex from TimedEventImpl (#3745) (#3750) * [19256] Remove mutex from TimedEventImpl (#3745) * Removes mutex from TimedEventImpl Signed-off-by: Vilas Chitrakaran * Please linters. Signed-off-by: Miguel Company --------- Signed-off-by: Vilas Chitrakaran Signed-off-by: Miguel Company Co-authored-by: Miguel Company Co-authored-by: Holger Schulz (cherry picked from commit a7697c7dc9dce5b6aab46c93b21b0020804560ee) * Added atomic time_point initialization (#3760) Signed-off-by: Javier Santiago --------- Signed-off-by: Javier Santiago Co-authored-by: Vilas Kumar Chitrakaran Co-authored-by: jsantiago-eProsima <90755661+jsantiago-eProsima@users.noreply.github.com> --- src/cpp/rtps/resources/TimedEventImpl.cpp | 15 ++++---------- src/cpp/rtps/resources/TimedEventImpl.h | 25 +++++++---------------- 2 files changed, 11 insertions(+), 29 deletions(-) diff --git a/src/cpp/rtps/resources/TimedEventImpl.cpp b/src/cpp/rtps/resources/TimedEventImpl.cpp index ece0247dc81..3bcddc6e09f 100644 --- a/src/cpp/rtps/resources/TimedEventImpl.cpp +++ b/src/cpp/rtps/resources/TimedEventImpl.cpp @@ -20,11 +20,9 @@ #include "TimedEventImpl.h" +#include #include -#include -#include - namespace eprosima { namespace fastrtps { namespace rtps { @@ -33,6 +31,7 @@ TimedEventImpl::TimedEventImpl( Callback callback, std::chrono::microseconds interval) : interval_microsec_(interval) + , next_trigger_time_(std::chrono::steady_clock::now()) , callback_(std::move(callback)) , state_(StateCode::INACTIVE) { @@ -73,12 +72,10 @@ bool TimedEventImpl::update( if (set_time) { - std::lock_guard lock(mutex_); - next_trigger_time_ = current_time + interval_microsec_; + next_trigger_time_ = current_time + interval_microsec_.load(); } else if (expected == StateCode::INACTIVE) { - std::lock_guard lock(mutex_); next_trigger_time_ = cancel_time; } @@ -103,14 +100,12 @@ void TimedEventImpl::trigger( expected = StateCode::INACTIVE; if (state_.compare_exchange_strong(expected, StateCode::WAITING)) { - std::lock_guard lock(mutex_); - next_trigger_time_ = current_time + interval_microsec_; + next_trigger_time_ = current_time + interval_microsec_.load(); return; } } } - std::lock_guard lock(mutex_); next_trigger_time_ = cancel_time; } } @@ -118,7 +113,6 @@ void TimedEventImpl::trigger( bool TimedEventImpl::update_interval( const eprosima::fastrtps::Duration_t& interval) { - std::lock_guard lock(mutex_); interval_microsec_ = std::chrono::microseconds(TimeConv::Duration_t2MicroSecondsInt64(interval)); return true; } @@ -126,7 +120,6 @@ bool TimedEventImpl::update_interval( bool TimedEventImpl::update_interval_millisec( double interval) { - std::lock_guard lock(mutex_); interval_microsec_ = std::chrono::microseconds(static_cast(interval * 1000)); return true; } diff --git a/src/cpp/rtps/resources/TimedEventImpl.h b/src/cpp/rtps/resources/TimedEventImpl.h index 1c7bc1ce3d9..01ab3c8cdbb 100644 --- a/src/cpp/rtps/resources/TimedEventImpl.h +++ b/src/cpp/rtps/resources/TimedEventImpl.h @@ -26,11 +26,7 @@ #include #include -#include -#include #include -#include -#include namespace eprosima { namespace fastrtps { @@ -91,9 +87,8 @@ class TimedEventImpl */ double getIntervalMsec() { - std::unique_lock lock(mutex_); - auto total_milliseconds = std::chrono::duration_cast(interval_microsec_); - return static_cast(total_milliseconds.count()); + const auto ms = std::chrono::duration_cast(interval_microsec_.load()); + return static_cast(ms.count()); } /*! @@ -102,11 +97,9 @@ class TimedEventImpl */ double getRemainingTimeMilliSec() { - std::unique_lock lock(mutex_); - return static_cast( - std::chrono::duration_cast( - next_trigger_time_ - std::chrono::steady_clock::now()). - count()); + const auto ms = std::chrono::duration_cast( + next_trigger_time_.load() - std::chrono::steady_clock::now()); + return static_cast(ms.count()); } /*! @@ -115,7 +108,6 @@ class TimedEventImpl */ std::chrono::steady_clock::time_point next_trigger_time() { - std::unique_lock lock(mutex_); return next_trigger_time_; } @@ -154,19 +146,16 @@ class TimedEventImpl private: //! Expiration time in microseconds of the event. - std::chrono::microseconds interval_microsec_; + std::atomic interval_microsec_; //! Next time this event should be triggered - std::chrono::steady_clock::time_point next_trigger_time_; + std::atomic next_trigger_time_; //! User function to be called when this event is triggered Callback callback_; //! Current state of this event std::atomic state_; - - //! Protects interval_microsec_ and next_trigger_time_ - std::mutex mutex_; }; } // namespace rtps