forked from ros2/rclcpp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backport mutex_two_priorities ros2#1516
Signed-off-by: hsgwa <hasegawa@isp.co.jp>
- Loading branch information
hsgwa
committed
Apr 16, 2021
1 parent
234b5e4
commit 4870b77
Showing
5 changed files
with
169 additions
and
3 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,75 @@ | ||
// 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 <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: | ||
// Implementation detail: the idea here is that only one low priority thread can be | ||
// trying to take the data_ mutex while the others are excluded by the barrier_ mutex. | ||
// All high priority threads are already waiting for the data_ mutex. | ||
std::mutex barrier_; | ||
std::mutex data_; | ||
}; | ||
|
||
} // 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,75 @@ | ||
// 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() | ||
{ | ||
parent_.data_.lock(); | ||
} | ||
|
||
void | ||
HighPriorityLockable::unlock() | ||
{ | ||
parent_.data_.unlock(); | ||
} | ||
|
||
LowPriorityLockable::LowPriorityLockable(MutexTwoPriorities & parent) | ||
: parent_(parent) | ||
{} | ||
|
||
void | ||
LowPriorityLockable::lock() | ||
{ | ||
std::unique_lock<std::mutex> barrier_guard{parent_.barrier_}; | ||
parent_.data_.lock(); | ||
barrier_guard.release(); | ||
} | ||
|
||
void | ||
LowPriorityLockable::unlock() | ||
{ | ||
std::lock_guard<std::mutex> barrier_guard{parent_.barrier_, std::adopt_lock}; | ||
parent_.data_.unlock(); | ||
} | ||
|
||
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