Skip to content

Commit 1fd03aa

Browse files
akampmannAlexandru Kampmann
andauthored
Consistent Mutex Usage (#19)
This commit replaces calls to sys_mutex_* and replaces them with direct calls to FreeRTOS API. The reason behind this change is that some lwip abstraction layers use recursive mutexes, and others do not. embeddedRTPS hangs if non-recursive mutexes are used. This change ensures that only recursive mutexes are used. Co-authored-by: Alexandru Kampmann <kampmann@embedded.rwth-aachen.de>
1 parent c05ca9f commit 1fd03aa

File tree

16 files changed

+23
-38
lines changed

16 files changed

+23
-38
lines changed

include/rtps/discovery/SEDPAgent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class SEDPAgent {
5959

6060
private:
6161
Participant *m_part;
62-
sys_mutex_t m_mutex;
62+
SemaphoreHandle_t m_mutex;
6363
uint8_t m_buffer[600]; // TODO check size, currently changed from 300 to 600
6464
// (FastDDS gives too many options)
6565
BuiltInEndpoints m_endpoints;

include/rtps/discovery/SPDPAgent.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class ReaderCacheChange;
5252

5353
class SPDPAgent {
5454
public:
55-
~SPDPAgent();
5655
void init(Participant &participant, BuiltInEndpoints &endpoints);
5756
void start();
5857
void stop();
@@ -67,7 +66,7 @@ class SPDPAgent {
6766
ucdrBuffer m_microbuffer{};
6867
uint8_t m_cycleHB = 0;
6968

70-
sys_mutex_t m_mutex;
69+
SemaphoreHandle_t m_mutex;
7170
bool initialized = false;
7271
static void receiveCallback(void *callee,
7372
const ReaderCacheChange &cacheChange);

include/rtps/entities/Domain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class Domain {
8484
}
8585

8686
bool m_initComplete = false;
87-
sys_mutex_t m_mutex;
87+
SemaphoreHandle_t m_mutex;
8888

8989
void receiveCallback(const PacketInfo &packet);
9090
GuidPrefix_t generateGuidPrefix(ParticipantId_t id) const;

include/rtps/entities/Participant.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class Participant {
115115
std::array<Reader *, Config::NUM_READERS_PER_PARTICIPANT> m_readers = {
116116
nullptr};
117117

118-
sys_mutex_t m_mutex;
118+
SemaphoreHandle_t m_mutex;
119119
MemoryPool<ParticipantProxyData, Config::SPDP_MAX_NUMBER_FOUND_PARTICIPANTS>
120120
m_remoteParticipants;
121121

include/rtps/entities/Reader.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Author: i11 - Embedded Software, RWTH Aachen University
3131
#include "rtps/entities/WriterProxy.h"
3232
#include "rtps/storages/MemoryPool.h"
3333
#include "rtps/storages/PBufWrapper.h"
34+
#include "semphr.h"
3435
#include <cstring>
3536

3637
namespace rtps {
@@ -135,10 +136,10 @@ class Reader {
135136
std::array<callbackElement_t, Config::MAX_NUM_READER_CALLBACKS> m_callbacks;
136137

137138
// Guards manipulation of the proxies array
138-
sys_mutex_t m_proxies_mutex = nullptr;
139+
SemaphoreHandle_t m_proxies_mutex = nullptr;
139140

140141
// Guards manipulation of callback array
141-
sys_mutex_t m_callback_mutex = nullptr;
142+
SemaphoreHandle_t m_callback_mutex = nullptr;
142143
};
143144
} // namespace rtps
144145

include/rtps/entities/StatefulReader.tpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,16 @@ void StatefulReaderT<NetworkDriver>::newChange(
6666
if (m_callback_count == 0 || !m_is_initialized_) {
6767
return;
6868
}
69-
sys_mutex_lock(&m_proxies_mutex);
69+
Lock{m_proxies_mutex};
7070
for (auto &proxy : m_proxies) {
7171
if (proxy.remoteWriterGuid == cacheChange.writerGuid) {
7272
if (proxy.expectedSN == cacheChange.sn) {
73-
sys_mutex_unlock(&m_proxies_mutex);
7473
executeCallbacks(cacheChange);
7574
++proxy.expectedSN;
7675
return;
7776
}
7877
}
7978
}
80-
sys_mutex_unlock(&m_proxies_mutex);
8179
}
8280

8381
template <class NetworkDriver>

include/rtps/entities/StatelessWriter.tpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ bool StatelessWriterT<NetworkDriver>::init(TopicData attributes,
6767
m_attributes = attributes;
6868

6969
if (m_mutex == nullptr) {
70-
if (sys_mutex_new(&m_mutex) != ERR_OK) {
70+
if (!createMutex(&m_mutex)) {
7171
#if SLW_VERBOSE
7272
SLW_LOG("Failed to create mutex \n");
7373
#endif

include/rtps/entities/Writer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class Writer {
8282
protected:
8383
SequenceNumber_t m_sedp_sequence_number;
8484

85-
sys_mutex_t m_mutex = nullptr;
85+
SemaphoreHandle_t m_mutex = nullptr;
8686
ThreadPool *mp_threadPool = nullptr;
8787

8888
Ip4Port_t m_srcPort;

include/rtps/storages/ThreadSafeCircularBuffer.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Author: i11 - Embedded Software, RWTH Aachen University
2626
#define RTPS_THREADSAFEQUEUE_H
2727

2828
#include "lwip/sys.h"
29+
#include "semphr.h"
2930

3031
#include <array>
3132
#include <limits>
@@ -37,8 +38,6 @@ template <typename T, uint16_t SIZE> class ThreadSafeCircularBuffer {
3738
public:
3839
bool init();
3940

40-
~ThreadSafeCircularBuffer();
41-
4241
bool moveElementIntoBuffer(T &&elem);
4342

4443
/**
@@ -57,7 +56,7 @@ template <typename T, uint16_t SIZE> class ThreadSafeCircularBuffer {
5756
static_assert(SIZE + 1 < std::numeric_limits<decltype(m_head)>::max(),
5857
"Iterator is large enough for given size");
5958

60-
sys_mutex_t m_mutex;
59+
SemaphoreHandle_t m_mutex;
6160
bool m_initialized = false;
6261

6362
inline bool isFull();

include/rtps/storages/ThreadSafeCircularBuffer.tpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ bool ThreadSafeCircularBuffer<T, SIZE>::init() {
2323
if (m_initialized) {
2424
return true;
2525
}
26-
if (sys_mutex_new(&m_mutex) != ERR_OK) {
26+
if (!createMutex(&m_mutex)) {
2727
TSCB_LOG("Failed to create mutex \n");
2828
return false;
2929
} else {
@@ -34,13 +34,6 @@ bool ThreadSafeCircularBuffer<T, SIZE>::init() {
3434
}
3535
}
3636

37-
template <typename T, uint16_t SIZE>
38-
ThreadSafeCircularBuffer<T, SIZE>::~ThreadSafeCircularBuffer() {
39-
if (m_initialized) {
40-
sys_mutex_free(&m_mutex);
41-
}
42-
}
43-
4437
template <typename T, uint16_t SIZE>
4538
bool ThreadSafeCircularBuffer<T, SIZE>::moveElementIntoBuffer(T &&elem) {
4639
Lock lock(m_mutex);

0 commit comments

Comments
 (0)