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

Use a different implementation of mutex two priorities #1628

Merged
merged 3 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
3 changes: 2 additions & 1 deletion rclcpp/include/rclcpp/detail/mutex_two_priorities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class MutexTwoPriorities
get_low_priority_lockable();

private:
std::condition_variable cv_;
std::condition_variable hp_cv_;
std::condition_variable lp_cv_;
std::mutex cv_mutex_;
size_t hp_waiting_count_{0u};
bool data_taken_{false};
Expand Down
35 changes: 18 additions & 17 deletions rclcpp/src/rclcpp/detail/mutex_two_priorities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,12 @@ HighPriorityLockable::HighPriorityLockable(MutexTwoPriorities & parent)
void
HighPriorityLockable::lock()
{
bool did_aument_count{false};
std::unique_lock<std::mutex> guard{parent_.cv_mutex_};
if (parent_.data_taken_) {
++parent_.hp_waiting_count_;
did_aument_count = true;
}
while (parent_.data_taken_) {
parent_.cv_.wait(guard);
}
if (did_aument_count) {
while (parent_.data_taken_) {
parent_.hp_cv_.wait(guard);
}
--parent_.hp_waiting_count_;
}
parent_.data_taken_ = true;
Expand All @@ -49,16 +45,17 @@ HighPriorityLockable::lock()
void
HighPriorityLockable::unlock()
{
bool notify_lp{false};
{
std::lock_guard<std::mutex> guard{parent_.cv_mutex_};
parent_.data_taken_ = false;
notify_lp = 0u == parent_.hp_waiting_count_;
}
if (notify_lp) {
parent_.lp_cv_.notify_one();
} else {
parent_.hp_cv_.notify_one();
}
// We need to notify_all(), and not only one,
// because the notified thread might be low priority
// when a high priority thread is still waiting.
// In that case, the low priority thread will go to sleep again,
// release the mutex, and the high priority thread can continue.
parent_.cv_.notify_all();
}

LowPriorityLockable::LowPriorityLockable(MutexTwoPriorities & parent)
Expand All @@ -70,21 +67,25 @@ LowPriorityLockable::lock()
{
std::unique_lock<std::mutex> guard{parent_.cv_mutex_};
while (parent_.data_taken_ || parent_.hp_waiting_count_) {
parent_.cv_.wait(guard);
parent_.lp_cv_.wait(guard);
}
parent_.data_taken_ = true;
}

void
LowPriorityLockable::unlock()
{
bool notify_lp{false};
{
std::lock_guard<std::mutex> guard{parent_.cv_mutex_};
parent_.data_taken_ = false;
notify_lp = 0u == parent_.hp_waiting_count_;
}
if (notify_lp) {
parent_.lp_cv_.notify_one();
} else {
parent_.hp_cv_.notify_one();
}
// See comment in HighPriorityLockable::unlock() to understand
// why not using notify_one().
parent_.cv_.notify_all();
}

HighPriorityLockable
Expand Down