Skip to content

Commit 54db1d3

Browse files
committed
added CMake option USE_PI_MUTEX
Signed-off-by: Martin Mayer <[email protected]>
1 parent 4e81d58 commit 54db1d3

File tree

6 files changed

+50
-22
lines changed

6 files changed

+50
-22
lines changed

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ if(WIN32)
3737
target_compile_definitions(${PROJECT_NAME}
3838
PRIVATE "RCPPUTILS_BUILDING_LIBRARY")
3939
endif()
40+
41+
option(USE_PI_MUTEX "Enables priority inheritance mutexes." ON)
42+
if(USE_PI_MUTEX)
43+
if(WIN32 OR APPLE)
44+
message("Mutexes with priority inheritance are not supported on your system. Using std mutexes.")
45+
else()
46+
target_compile_definitions(${PROJECT_NAME} PUBLIC "RCPPUTILS_USE_PIMUTEX")
47+
endif()
48+
endif()
49+
4050
ament_target_dependencies(${PROJECT_NAME} rcutils)
4151

4252
# Export old-style CMake variables

Doxyfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ EXPAND_ONLY_PREDEF = YES
2323
PREDEFINED += __declspec(x)=
2424
PREDEFINED += RCPPUTILS_PUBLIC=
2525
PREDEFINED += RCPPUTILS_PUBLIC_TYPE=
26+
PREDEFINED += RCPPUTILS_USE_PIMUTEX
2627

2728
# Tag files that do not exist will produce a warning and cross-project linking will not work.
2829
TAGFILES += "../../../doxygen_tag_files/cppreference-doxygen-web.tag.xml=http://en.cppreference.com/w/"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This package currently contains:
1717
* String helpers
1818
* File system helpers
1919
* Type traits helpers
20+
* Mutex classes that support thread priority inheritance
2021
* Class that dynamically loads, unloads and get symbols from shared libraries at run-time.
2122

2223
Features are described in more detail at [docs/FEATURES.md](docs/FEATURES.md)

include/rcpputils/mutex.hpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020 Open Source Robotics Foundation, Inc.
1+
// Copyright 2023 Open Source Robotics Foundation, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -21,11 +21,18 @@
2121

2222
namespace rcpputils
2323
{
24+
#ifndef RCPPUTILS_USE_PIMUTEX
25+
26+
// Fallback code path
27+
using PIMutex = std::mutex;
28+
using RecursivePIMutex = std::recursive_mutex;
29+
30+
#else
2431

2532
/**
2633
* Mutex with priority inheritance on systems that support it.
2734
* This class derives from std::mutex to be fully compatible with standard C++.
28-
* This implementation is a workaround that is needed until the C++ standard library offers the same mutex functionality.
35+
* This implementation is needed because the C++ standard library doesn't support priority inheritance.
2936
**/
3037
class PIMutex : public std::mutex
3138
{
@@ -42,7 +49,7 @@ class PIMutex : public std::mutex
4249
/**
4350
* Recursive mutex with priority inheritance on systems that support it.
4451
* This class derives from std::recursive_mutex to be fully compatible with standard C++.
45-
* This implementation is a workaround that is needed until the C++ standard library offers the same mutex functionality.
52+
* This implementation is needed because the C++ standard library doesn't support priority inheritance.
4653
**/
4754
class RecursivePIMutex : public std::recursive_mutex
4855
{
@@ -56,6 +63,6 @@ class RecursivePIMutex : public std::recursive_mutex
5663
~RecursivePIMutex();
5764
};
5865

66+
#endif // RCPPUTILS_USE_PIMUTEX
5967
} // namespace rcpputils
60-
6168
#endif // RCPPUTILS__MUTEX_HPP_

src/mutex.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020 Open Source Robotics Foundation, Inc.
1+
// Copyright 2023 Open Source Robotics Foundation, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -14,14 +14,13 @@
1414

1515
#include "rcpputils/mutex.hpp"
1616

17-
#ifndef _WIN32
17+
#ifdef RCPPUTILS_USE_PIMUTEX
18+
1819
#include <pthread.h>
19-
#endif
2020

2121
namespace rcpputils
2222
{
2323

24-
#ifndef _WIN32
2524
PIMutex::PIMutex()
2625
{
2726
// Destroy the underlying mutex
@@ -60,15 +59,6 @@ RecursivePIMutex::RecursivePIMutex()
6059
// The attribute object isn't needed any more
6160
pthread_mutexattr_destroy(&attr);
6261
}
63-
#else
64-
PIMutex::PIMutex()
65-
{
66-
}
67-
68-
RecursivePIMutex::RecursivePIMutex()
69-
{
70-
}
71-
#endif // _WIN32
7262

7363
PIMutex::~PIMutex()
7464
{
@@ -77,4 +67,6 @@ PIMutex::~PIMutex()
7767
RecursivePIMutex::~RecursivePIMutex()
7868
{
7969
}
70+
8071
} // namespace rcpputils
72+
#endif // RCPPUTILS_USE_PIMUTEX

test/test_mutex.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 Open Source Robotics Foundation, Inc.
1+
// Copyright 2023 Open Source Robotics Foundation, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -19,10 +19,14 @@
1919

2020
#include "rcpputils/mutex.hpp"
2121

22-
#ifdef __linux__
22+
#ifdef RCPPUTILS_USE_PIMUTEX
2323
#include <pthread.h>
2424
#include "rcutils/types/rcutils_ret.h"
25-
#endif // __linux__
25+
#ifdef __QNXNTO__
26+
#include <sys/neutrino.h>
27+
#include <sys/syspage.h>
28+
#endif // __QNXNTO__
29+
#endif // RCPPUTILS_USE_PIMUTEX
2630

2731
using namespace std::chrono_literals;
2832

@@ -99,7 +103,7 @@ TEST(test_mutex, pimutex_lockthread) {
99103
test_thread.join();
100104
}
101105

102-
#ifdef __linux__
106+
#ifdef RCPPUTILS_USE_PIMUTEX
103107
//
104108
// The test cases pimutex_priority_inversion & rpimutex_priority_inversion provoke
105109
// a thread priority inversion. To do so they need to configure the cpu priority,
@@ -160,14 +164,27 @@ rcutils_ret_t configure_native_realtime_thread(
160164
RCUTILS_RET_OK ? 1 : 0);
161165
success &= (pthread_setschedparam(native_handle, SCHED_FIFO, &params) == 0);
162166

167+
#ifdef __QNXNTO__
168+
// run_mask is a bit mask to set which cpu a thread runs on
169+
// where each bit corresponds to a cpu core
170+
int64_t run_mask = cpu_bitmask;
171+
172+
// change thread affinity of thread associated with native_handle
173+
success &= (ThreadCtlExt(
174+
0, native_handle, _NTO_TCTL_RUNMASK,
175+
reinterpret_cast<void *>(run_mask)) != -1);
176+
#else // __QNXNTO__
163177
cpu_set_t cpuset;
164178
CPU_ZERO(&cpuset);
165179
for (unsigned int i = 0; i < sizeof(cpu_bitmask) * 8; i++) {
166180
if ( (cpu_bitmask & (1 << i)) != 0) {
167181
CPU_SET(i, &cpuset);
168182
}
169183
}
184+
185+
// change thread affinity of thread associated with native_handle
170186
success &= (pthread_setaffinity_np(native_handle, sizeof(cpu_set_t), &cpuset) == 0);
187+
#endif // __QNXNTO__
171188

172189
return success ? RCUTILS_RET_OK : RCUTILS_RET_ERROR;
173190
}
@@ -285,4 +302,4 @@ TEST(test_mutex, rpimutex_priority_inversion) {
285302
priority_inheritance_test<rcpputils::RecursivePIMutex>();
286303
}
287304

288-
#endif // __linux__
305+
#endif // RCPPUTILS_USE_PIMUTEX

0 commit comments

Comments
 (0)