Skip to content

Commit ad880bf

Browse files
Change default mutex of RealtimeThreadSafeBox and add more aliases (#342)
1 parent 3f46a8d commit ad880bf

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

realtime_tools/include/realtime_tools/realtime_box.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,18 @@
4242
namespace realtime_tools
4343
{
4444

45+
// Provide backward-compatibility for the old RealtimeBox class
46+
template <class T, typename mutex_type = std::mutex>
47+
using RealtimeBoxBase = RealtimeThreadSafeBox<T, mutex_type>;
48+
49+
template <typename T>
50+
using RealtimeBoxStandard = RealtimeBoxBase<T, std::mutex>;
51+
52+
template <typename T>
53+
using RealtimeBoxRecursive = RealtimeBoxBase<T, std::recursive_mutex>;
54+
4555
template <typename T>
46-
using RealtimeBox = RealtimeThreadSafeBox<T>;
56+
using RealtimeBox = RealtimeBoxStandard<T>;
4757

4858
} // namespace realtime_tools
4959

realtime_tools/include/realtime_tools/realtime_thread_safe_box.hpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@
4343
#ifndef _WIN32
4444
#include "realtime_tools/mutex.hpp"
4545
#define DEFAULT_MUTEX realtime_tools::prio_inherit_mutex
46+
#define RECURSIVE_MUTEX realtime_tools::prio_inherit_recursive_mutex
4647
#else
4748
#define DEFAULT_MUTEX std::mutex
49+
#define RECURSIVE_MUTEX std::recursive_mutex
4850
#endif
4951

5052
namespace realtime_tools
@@ -62,19 +64,19 @@ constexpr auto is_ptr_or_smart_ptr = rcpputils::is_pointer<T>::value;
6264
Only use the get/set methods that take function pointer for accessing the internal value.
6365
*/
6466
template <class T, typename mutex_type = DEFAULT_MUTEX>
65-
class RealtimeThreadSafeBoxBase
67+
class RealtimeThreadSafeBox
6668
{
6769
static_assert(std::is_copy_constructible_v<T>, "Passed type must be copy constructible");
6870

6971
public:
7072
using mutex_t = mutex_type;
7173
using type = T;
7274
// Provide various constructors
73-
constexpr explicit RealtimeThreadSafeBoxBase(const T & init = T{}) : value_(init) {}
74-
constexpr explicit RealtimeThreadSafeBoxBase(const T && init) : value_(std::move(init)) {}
75+
constexpr explicit RealtimeThreadSafeBox(const T & init = T{}) : value_(init) {}
76+
constexpr explicit RealtimeThreadSafeBox(const T && init) : value_(std::move(init)) {}
7577

7678
// Copy constructor
77-
constexpr RealtimeThreadSafeBoxBase(const RealtimeThreadSafeBoxBase & o)
79+
constexpr RealtimeThreadSafeBox(const RealtimeThreadSafeBox & o)
7880
{
7981
// Lock the other box mutex
8082
std::unique_lock<mutex_t> lock(o.lock_);
@@ -83,7 +85,7 @@ class RealtimeThreadSafeBoxBase
8385
}
8486

8587
// Copy assignment constructor
86-
constexpr RealtimeThreadSafeBoxBase & operator=(const RealtimeThreadSafeBoxBase & o)
88+
constexpr RealtimeThreadSafeBox & operator=(const RealtimeThreadSafeBox & o)
8789
{
8890
// Check for self assignment (and a potential deadlock)
8991
if (&o != this) {
@@ -96,7 +98,7 @@ class RealtimeThreadSafeBoxBase
9698
return *this;
9799
}
98100

99-
constexpr RealtimeThreadSafeBoxBase(RealtimeThreadSafeBoxBase && o)
101+
constexpr RealtimeThreadSafeBox(RealtimeThreadSafeBox && o)
100102
{
101103
// Lock the other box mutex
102104
std::unique_lock<mutex_t> lock(o.lock_);
@@ -106,14 +108,14 @@ class RealtimeThreadSafeBoxBase
106108

107109
// Only enabled for types that can be constructed from an initializer list
108110
template <typename U = T>
109-
constexpr RealtimeThreadSafeBoxBase(
111+
constexpr RealtimeThreadSafeBox(
110112
const std::initializer_list<U> & init,
111113
std::enable_if_t<std::is_constructible_v<U, std::initializer_list<U>>>)
112114
: value_(init)
113115
{
114116
}
115117

116-
constexpr RealtimeThreadSafeBoxBase & operator=(RealtimeThreadSafeBoxBase && o)
118+
constexpr RealtimeThreadSafeBox & operator=(RealtimeThreadSafeBox && o)
117119
{
118120
// Check for self assignment (and a potential deadlock)
119121
if (&o != this) {
@@ -299,18 +301,10 @@ class RealtimeThreadSafeBoxBase
299301
mutable mutex_t lock_;
300302
};
301303

302-
// Introduce some easier to use names
304+
// Provide specialisations for other mutex types
303305

304-
// Provide specialisations for different mutex types
305306
template <typename T>
306-
using RealtimeThreadSafeBoxStandard = RealtimeThreadSafeBoxBase<T, std::mutex>;
307-
308-
template <typename T>
309-
using RealtimeThreadSafeBoxRecursive = RealtimeThreadSafeBoxBase<T, std::recursive_mutex>;
310-
311-
// This is the specialisation we recommend to use in the end
312-
template <typename T>
313-
using RealtimeThreadSafeBox = RealtimeThreadSafeBoxStandard<T>;
307+
using RealtimeThreadSafeBoxRecursive = RealtimeThreadSafeBox<T, RECURSIVE_MUTEX>;
314308

315309
} // namespace realtime_tools
316310

0 commit comments

Comments
 (0)