-
Notifications
You must be signed in to change notification settings - Fork 117
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
||
|
@@ -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(); | ||
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 | ||
|
@@ -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_ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should make this a There was a problem hiding this comment. Choose a reason for hiding this commentThe 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_ |
There was a problem hiding this comment.
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?