-
Notifications
You must be signed in to change notification settings - Fork 55
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
Avoid priority inversions when using FIFO scheduling #174
Open
WideAwakeTN
wants to merge
17
commits into
ros2:rolling
Choose a base branch
from
WideAwakeTN:pimutex
base: rolling
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8cee5f7
added priority inheritance mutex + tests
WideAwakeTN b48991e
simplify testcase
WideAwakeTN f8bcfbe
added priority inversion test
WideAwakeTN 814c4a5
code style fix
WideAwakeTN a51ac25
fix for priority inversion testcase
WideAwakeTN 027f5e8
moved thread functionality to rcutils
WideAwakeTN 01bd5a8
modified return type
WideAwakeTN b111400
comment change
WideAwakeTN aee9d8d
style fix
WideAwakeTN c56cf12
comment
WideAwakeTN 0cbf3a3
reworked rcpputils unittest
WideAwakeTN d455916
uncrustify
WideAwakeTN 4e81d58
Thread configuration now part of the test implementation
WideAwakeTN 54db1d3
added CMake option USE_PI_MUTEX
WideAwakeTN 92cf9dc
changed OS detection in CMakeLists.txt
WideAwakeTN 35855fc
Fix linter complaints
WideAwakeTN e75a512
changed doxygen comments
WideAwakeTN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,68 @@ | ||
// Copyright 2023 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 RCPPUTILS__MUTEX_HPP_ | ||
#define RCPPUTILS__MUTEX_HPP_ | ||
|
||
#include <mutex> | ||
|
||
#include "rcpputils/visibility_control.hpp" | ||
|
||
namespace rcpputils | ||
{ | ||
#ifndef RCPPUTILS_USE_PIMUTEX | ||
|
||
// Fallback code path | ||
using PIMutex = std::mutex; | ||
using RecursivePIMutex = std::recursive_mutex; | ||
|
||
#else | ||
|
||
/** | ||
* Mutex with priority inheritance on systems that support it. | ||
* This class derives from std::mutex to be fully compatible with standard C++. | ||
* This implementation is needed because the C++ standard library doesn't support priority inheritance. | ||
**/ | ||
class PIMutex : public std::mutex | ||
{ | ||
public: | ||
/// Creates a mutex with priority inheritance enabled | ||
RCPPUTILS_PUBLIC | ||
PIMutex(); | ||
|
||
/// Releases the mutex | ||
RCPPUTILS_PUBLIC | ||
~PIMutex(); | ||
}; | ||
|
||
/** | ||
* Recursive mutex with priority inheritance on systems that support it. | ||
* This class derives from std::recursive_mutex to be fully compatible with standard C++. | ||
* This implementation is needed because the C++ standard library doesn't support priority inheritance. | ||
**/ | ||
class RecursivePIMutex : public std::recursive_mutex | ||
{ | ||
public: | ||
/// Creates a recursive mutex with priority inheritance enabled | ||
RCPPUTILS_PUBLIC | ||
RecursivePIMutex(); | ||
|
||
/// Releases the mutex | ||
RCPPUTILS_PUBLIC | ||
~RecursivePIMutex(); | ||
}; | ||
|
||
#endif // RCPPUTILS_USE_PIMUTEX | ||
} // namespace rcpputils | ||
#endif // RCPPUTILS__MUTEX_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2023 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 "rcpputils/mutex.hpp" | ||
|
||
#ifdef RCPPUTILS_USE_PIMUTEX | ||
|
||
#include <pthread.h> | ||
|
||
namespace rcpputils | ||
{ | ||
|
||
PIMutex::PIMutex() | ||
{ | ||
// Destroy the underlying mutex | ||
pthread_mutex_destroy(native_handle()); | ||
|
||
// Create mutex attribute with desired settings | ||
pthread_mutexattr_t attr; | ||
pthread_mutexattr_init(&attr); | ||
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); | ||
|
||
// Initialize the underlying mutex | ||
if (pthread_mutex_init(native_handle(), &attr) != 0) { | ||
throw std::runtime_error("Mutex initialization failed."); | ||
} | ||
|
||
// The attribute object isn't needed any more | ||
pthread_mutexattr_destroy(&attr); | ||
} | ||
|
||
RecursivePIMutex::RecursivePIMutex() | ||
{ | ||
// Destroy the underlying mutex | ||
pthread_mutex_destroy(native_handle()); | ||
|
||
// Create mutex attribute with desired settings | ||
pthread_mutexattr_t attr; | ||
pthread_mutexattr_init(&attr); | ||
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); | ||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); | ||
|
||
// Initialize the underlying mutex | ||
if (pthread_mutex_init(native_handle(), &attr) != 0) { | ||
throw std::runtime_error("Recursive mutex initialization failed."); | ||
} | ||
|
||
// The attribute object isn't needed any more | ||
pthread_mutexattr_destroy(&attr); | ||
} | ||
|
||
PIMutex::~PIMutex() | ||
{ | ||
} | ||
|
||
RecursivePIMutex::~RecursivePIMutex() | ||
{ | ||
} | ||
|
||
} // namespace rcpputils | ||
#endif // RCPPUTILS_USE_PIMUTEX |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I would do it the other way around. I would list systems with PI_MUTEX support and fail if the system is not in the list. With this approach you will get an error for unknown systems and for new systems you explicitly add them when you know it is supported.
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.
Makes sense. Better now?
Unfortunately I could not find much info on the CMAKE_SYSTEM_NAME variable for VxWorks and QNX systems. I hope I got the right strings.
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.
@razr FYI