Skip to content

Commit

Permalink
Remove mutex from TimedEventImpl (#3745) (#3750)
Browse files Browse the repository at this point in the history
* [19256] Remove mutex from TimedEventImpl (#3745)

* Removes mutex from TimedEventImpl

Signed-off-by: Vilas Chitrakaran <cvilas@gmail.com>

* Please linters.

Signed-off-by: Miguel Company <MiguelCompany@eprosima.com>

---------

Signed-off-by: Vilas Chitrakaran <cvilas@gmail.com>
Signed-off-by: Miguel Company <MiguelCompany@eprosima.com>
Co-authored-by: Miguel Company <MiguelCompany@eprosima.com>
Co-authored-by: Holger Schulz <holger.schulz@durham.ac.uk>
(cherry picked from commit a7697c7)

* Added atomic time_point initialization (#3760)

Signed-off-by: Javier Santiago <javiersantiago@eprosima.com>

---------

Signed-off-by: Javier Santiago <javiersantiago@eprosima.com>
Co-authored-by: Vilas Kumar Chitrakaran <cvilas@gmail.com>
Co-authored-by: jsantiago-eProsima <90755661+jsantiago-eProsima@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 10, 2023
1 parent 85abd71 commit 46a96ff
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
15 changes: 4 additions & 11 deletions src/cpp/rtps/resources/TimedEventImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@

#include "TimedEventImpl.h"

#include <chrono>
#include <fastrtps/utils/TimeConversion.h>

#include <atomic>
#include <functional>

namespace eprosima {
namespace fastrtps {
namespace rtps {
Expand All @@ -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)
{
Expand Down Expand Up @@ -73,12 +72,10 @@ bool TimedEventImpl::update(

if (set_time)
{
std::lock_guard<std::mutex> 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<std::mutex> lock(mutex_);
next_trigger_time_ = cancel_time;
}

Expand All @@ -103,30 +100,26 @@ void TimedEventImpl::trigger(
expected = StateCode::INACTIVE;
if (state_.compare_exchange_strong(expected, StateCode::WAITING))
{
std::lock_guard<std::mutex> lock(mutex_);
next_trigger_time_ = current_time + interval_microsec_;
next_trigger_time_ = current_time + interval_microsec_.load();
return;
}
}
}

std::lock_guard<std::mutex> lock(mutex_);
next_trigger_time_ = cancel_time;
}
}

bool TimedEventImpl::update_interval(
const eprosima::fastrtps::Duration_t& interval)
{
std::lock_guard<std::mutex> lock(mutex_);
interval_microsec_ = std::chrono::microseconds(TimeConv::Duration_t2MicroSecondsInt64(interval));
return true;
}

bool TimedEventImpl::update_interval_millisec(
double interval)
{
std::lock_guard<std::mutex> lock(mutex_);
interval_microsec_ = std::chrono::microseconds(static_cast<int64_t>(interval * 1000));
return true;
}
Expand Down
25 changes: 7 additions & 18 deletions src/cpp/rtps/resources/TimedEventImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@
#include <fastdds/rtps/resources/TimedEvent.h>

#include <atomic>
#include <thread>
#include <memory>
#include <functional>
#include <mutex>
#include <condition_variable>

namespace eprosima {
namespace fastrtps {
Expand Down Expand Up @@ -91,9 +87,8 @@ class TimedEventImpl
*/
double getIntervalMsec()
{
std::unique_lock<std::mutex> lock(mutex_);
auto total_milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(interval_microsec_);
return static_cast<double>(total_milliseconds.count());
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(interval_microsec_.load());
return static_cast<double>(ms.count());
}

/*!
Expand All @@ -102,11 +97,9 @@ class TimedEventImpl
*/
double getRemainingTimeMilliSec()
{
std::unique_lock<std::mutex> lock(mutex_);
return static_cast<double>(
std::chrono::duration_cast<std::chrono::milliseconds>(
next_trigger_time_ - std::chrono::steady_clock::now()).
count());
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
next_trigger_time_.load() - std::chrono::steady_clock::now());
return static_cast<double>(ms.count());
}

/*!
Expand All @@ -115,7 +108,6 @@ class TimedEventImpl
*/
std::chrono::steady_clock::time_point next_trigger_time()
{
std::unique_lock<std::mutex> lock(mutex_);
return next_trigger_time_;
}

Expand Down Expand Up @@ -154,19 +146,16 @@ class TimedEventImpl
private:

//! Expiration time in microseconds of the event.
std::chrono::microseconds interval_microsec_;
std::atomic<std::chrono::microseconds> interval_microsec_;

//! Next time this event should be triggered
std::chrono::steady_clock::time_point next_trigger_time_;
std::atomic<std::chrono::steady_clock::time_point> next_trigger_time_;

//! User function to be called when this event is triggered
Callback callback_;

//! Current state of this event
std::atomic<StateCode> state_;

//! Protects interval_microsec_ and next_trigger_time_
std::mutex mutex_;
};

} // namespace rtps
Expand Down

0 comments on commit 46a96ff

Please sign in to comment.