-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow timers to keep up the intended rate in MultiThreadedExecutor #1516
(#1636) Backports #1516 and follow-up fix #1628 Patched to keep ABI compatibility by using static class variables to store the mutex two priorities instances. Signed-off-by: hsgwa <hasegawa@isp.co.jp>
- Loading branch information
hsgwa
authored
May 11, 2021
1 parent
791c23a
commit d12ed36
Showing
5 changed files
with
208 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2021 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef RCLCPP__DETAIL__MUTEX_TWO_PRIORITIES_HPP_ | ||
#define RCLCPP__DETAIL__MUTEX_TWO_PRIORITIES_HPP_ | ||
|
||
#include <condition_variable> | ||
#include <mutex> | ||
|
||
namespace rclcpp | ||
{ | ||
namespace detail | ||
{ | ||
/// \internal A mutex that has two locking mechanism, one with higher priority than the other. | ||
/** | ||
* After the current mutex owner release the lock, a thread that used the high | ||
* priority mechanism will have priority over threads that used the low priority mechanism. | ||
*/ | ||
class MutexTwoPriorities | ||
{ | ||
public: | ||
class HighPriorityLockable | ||
{ | ||
public: | ||
explicit HighPriorityLockable(MutexTwoPriorities & parent); | ||
|
||
void lock(); | ||
|
||
void unlock(); | ||
|
||
private: | ||
MutexTwoPriorities & parent_; | ||
}; | ||
|
||
class LowPriorityLockable | ||
{ | ||
public: | ||
explicit LowPriorityLockable(MutexTwoPriorities & parent); | ||
|
||
void lock(); | ||
|
||
void unlock(); | ||
|
||
private: | ||
MutexTwoPriorities & parent_; | ||
}; | ||
|
||
HighPriorityLockable | ||
get_high_priority_lockable(); | ||
|
||
LowPriorityLockable | ||
get_low_priority_lockable(); | ||
|
||
private: | ||
std::condition_variable hp_cv_; | ||
std::condition_variable lp_cv_; | ||
std::mutex cv_mutex_; | ||
size_t hp_waiting_count_{0u}; | ||
bool data_taken_{false}; | ||
}; | ||
|
||
} // namespace detail | ||
} // namespace rclcpp | ||
|
||
#endif // RCLCPP__DETAIL__MUTEX_TWO_PRIORITIES_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2021 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "rclcpp/detail/mutex_two_priorities.hpp" | ||
|
||
#include <mutex> | ||
|
||
namespace rclcpp | ||
{ | ||
namespace detail | ||
{ | ||
|
||
using LowPriorityLockable = MutexTwoPriorities::LowPriorityLockable; | ||
using HighPriorityLockable = MutexTwoPriorities::HighPriorityLockable; | ||
|
||
HighPriorityLockable::HighPriorityLockable(MutexTwoPriorities & parent) | ||
: parent_(parent) | ||
{} | ||
|
||
void | ||
HighPriorityLockable::lock() | ||
{ | ||
std::unique_lock<std::mutex> guard{parent_.cv_mutex_}; | ||
if (parent_.data_taken_) { | ||
++parent_.hp_waiting_count_; | ||
while (parent_.data_taken_) { | ||
parent_.hp_cv_.wait(guard); | ||
} | ||
--parent_.hp_waiting_count_; | ||
} | ||
parent_.data_taken_ = true; | ||
} | ||
|
||
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(); | ||
} | ||
} | ||
|
||
LowPriorityLockable::LowPriorityLockable(MutexTwoPriorities & parent) | ||
: parent_(parent) | ||
{} | ||
|
||
void | ||
LowPriorityLockable::lock() | ||
{ | ||
std::unique_lock<std::mutex> guard{parent_.cv_mutex_}; | ||
while (parent_.data_taken_ || parent_.hp_waiting_count_) { | ||
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(); | ||
} | ||
} | ||
|
||
HighPriorityLockable | ||
MutexTwoPriorities::get_high_priority_lockable() | ||
{ | ||
return HighPriorityLockable{*this}; | ||
} | ||
|
||
LowPriorityLockable | ||
MutexTwoPriorities::get_low_priority_lockable() | ||
{ | ||
return LowPriorityLockable{*this}; | ||
} | ||
|
||
} // namespace detail | ||
} // namespace rclcpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters