Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update guard condition by managing multiple items #527

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rmw_fastrtps_shared_cpp/src/rmw_wait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ __rmw_wait(
for (size_t i = 0; i < guard_conditions->guard_condition_count; ++i) {
void * data = guard_conditions->guard_conditions[i];
auto guard_condition = static_cast<GuardCondition *>(data);
guard_condition->detachCondition();
guard_condition->detachCondition(conditionMutex, conditionVariable);
if (!guard_condition->getHasTriggered()) {
guard_conditions->guard_conditions[i] = 0;
}
Expand Down
40 changes: 21 additions & 19 deletions rmw_fastrtps_shared_cpp/src/types/guard_condition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
#ifndef TYPES__GUARD_CONDITION_HPP_
#define TYPES__GUARD_CONDITION_HPP_

#include <algorithm>
#include <array>
#include <atomic>
#include <cassert>
#include <condition_variable>
#include <list>
#include <mutex>
#include <utility>

Expand All @@ -28,41 +30,41 @@ class GuardCondition
{
public:
GuardCondition()
: hasTriggered_(false),
conditionMutex_(nullptr), conditionVariable_(nullptr) {}
: hasTriggered_(false) {}

void
trigger()
{
std::lock_guard<std::mutex> lock(internalMutex_);

if (conditionMutex_ != nullptr) {
std::unique_lock<std::mutex> clock(*conditionMutex_);
// the change to hasTriggered_ needs to be mutually exclusive with
// rmw_wait() which checks hasTriggered() and decides if wait() needs to
// be called
hasTriggered_ = true;
hasTriggered_ = true;
for (auto & cond : conditions_) {
// TODO(iuhilnehc-ynos): conditionMutex is not used, remove it
std::unique_lock<std::mutex> clock(*cond.first);
clock.unlock();
Comment on lines +41 to 43
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick question, are these actually needed? just curious why not removing now?

conditionVariable_->notify_one();
} else {
hasTriggered_ = true;
cond.second->notify_one();
}
}

void
attachCondition(std::mutex * conditionMutex, std::condition_variable * conditionVariable)
{
std::lock_guard<std::mutex> lock(internalMutex_);
conditionMutex_ = conditionMutex;
conditionVariable_ = conditionVariable;
conditions_.push_back({conditionMutex, conditionVariable});
}

void
detachCondition()
detachCondition(std::mutex * conditionMutex, std::condition_variable * conditionVariable)
{
std::lock_guard<std::mutex> lock(internalMutex_);
conditionMutex_ = nullptr;
conditionVariable_ = nullptr;
auto it = std::find_if(
conditions_.begin(),
conditions_.end(),
[conditionMutex, conditionVariable](const auto & cond) {
return cond.first == conditionMutex && cond.second == conditionVariable;
});
if (it != conditions_.end()) {
conditions_.erase(it);
}
}

bool
Expand All @@ -80,8 +82,8 @@ class GuardCondition
private:
std::mutex internalMutex_;
std::atomic_bool hasTriggered_;
std::mutex * conditionMutex_ RCPPUTILS_TSA_GUARDED_BY(internalMutex_);
std::condition_variable * conditionVariable_ RCPPUTILS_TSA_GUARDED_BY(internalMutex_);
std::list<std::pair<std::mutex *, std::condition_variable *>> conditions_
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make this a std::vector? The most common operations (in my opinion) will be trigger() which has to loop over this sequence of pairs. If removing a pair is expensive (inside of detachCondition()), I don't think that's as important.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I'll update it.

RCPPUTILS_TSA_GUARDED_BY(internalMutex_);
};

#endif // TYPES__GUARD_CONDITION_HPP_