Skip to content

Commit 00ed575

Browse files
authored
fix: make locks not unlock immediately (#31)
replace temporaries with scope variables
1 parent 866a71f commit 00ed575

File tree

7 files changed

+50
-50
lines changed

7 files changed

+50
-50
lines changed

include/rtps/entities/StatefulReader.tpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void StatefulReaderT<NetworkDriver>::newChange(
6868
if (m_callback_count == 0 || !m_is_initialized_) {
6969
return;
7070
}
71-
Lock{m_proxies_mutex};
71+
Lock lock{m_proxies_mutex};
7272
for (auto &proxy : m_proxies) {
7373
if (proxy.remoteWriterGuid == cacheChange.writerGuid) {
7474
if (proxy.expectedSN == cacheChange.sn) {
@@ -112,7 +112,7 @@ bool StatefulReaderT<NetworkDriver>::addNewMatchedWriter(
112112
template <class NetworkDriver>
113113
bool StatefulReaderT<NetworkDriver>::onNewGapMessage(
114114
const SubmessageGap &msg, const GuidPrefix_t &remotePrefix) {
115-
Lock lock(m_proxies_mutex);
115+
Lock lock{m_proxies_mutex};
116116
if (!m_is_initialized_) {
117117
return false;
118118
}
@@ -209,7 +209,7 @@ bool StatefulReaderT<NetworkDriver>::onNewGapMessage(
209209
template <class NetworkDriver>
210210
bool StatefulReaderT<NetworkDriver>::onNewHeartbeat(
211211
const SubmessageHeartbeat &msg, const GuidPrefix_t &sourceGuidPrefix) {
212-
Lock lock(m_proxies_mutex);
212+
Lock lock{m_proxies_mutex};
213213
if (!m_is_initialized_) {
214214
return false;
215215
}
@@ -257,7 +257,7 @@ bool StatefulReaderT<NetworkDriver>::onNewHeartbeat(
257257
template <class NetworkDriver>
258258
bool StatefulReaderT<NetworkDriver>::sendPreemptiveAckNack(
259259
const WriterProxy &writer) {
260-
Lock lock(m_proxies_mutex);
260+
Lock lock{m_proxies_mutex};
261261
if (!m_is_initialized_) {
262262
return false;
263263
}

include/rtps/entities/StatefulWriter.tpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ const rtps::CacheChange *StatefulWriterT<NetworkDriver>::newChange(
159159

160160
template <class NetworkDriver> void StatefulWriterT<NetworkDriver>::progress() {
161161
INIT_GUARD()
162-
Lock{m_mutex};
162+
Lock lock{m_mutex};
163163
CacheChange *next = m_history.getChangeBySN(m_nextSequenceNumberToSend);
164164
if (next != nullptr) {
165165
uint32_t i = 0;
@@ -210,7 +210,7 @@ template <class NetworkDriver> void StatefulWriterT<NetworkDriver>::progress() {
210210
template <class NetworkDriver>
211211
void StatefulWriterT<NetworkDriver>::setAllChangesToUnsent() {
212212
INIT_GUARD()
213-
Lock lock(m_mutex);
213+
Lock lock{m_mutex};
214214

215215
m_nextSequenceNumberToSend = m_history.getCurrentSeqNumMin();
216216

@@ -223,7 +223,7 @@ template <class NetworkDriver>
223223
void StatefulWriterT<NetworkDriver>::onNewAckNack(
224224
const SubmessageAckNack &msg, const GuidPrefix_t &sourceGuidPrefix) {
225225
INIT_GUARD()
226-
Lock lock(m_mutex);
226+
Lock lock{m_mutex};
227227
if (!m_is_initialized_) {
228228
return;
229229
}
@@ -512,7 +512,7 @@ void StatefulWriterT<NetworkDriver>::sendHeartBeat() {
512512
MessageFactory::addHeader(info.buffer, m_attributes.endpointGuid.prefix);
513513

514514
{
515-
Lock lock(m_mutex);
515+
Lock lock{m_mutex};
516516

517517
if (!m_history.isEmpty()) {
518518
firstSN = m_history.getCurrentSeqNumMin();

src/discovery/SEDPAgent.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ void SEDPAgent::removeUnmatchedEntity(const Guid_t &guid) {
156156

157157
void SEDPAgent::removeUnmatchedEntitiesOfParticipant(
158158
const GuidPrefix_t &guidPrefix) {
159-
Lock{m_mutex};
159+
Lock lock{m_mutex};
160160
auto isElementToRemove = [&](const TopicDataCompressed &topicData) {
161161
return topicData.endpointGuid.prefix == guidPrefix;
162162
};

src/entities/Domain.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ void Domain::registerMulticastPort(FullLengthLocator mcastLocator) {
240240

241241
rtps::Reader *Domain::readerExists(Participant &part, const char *topicName,
242242
const char *typeName, bool reliable) {
243-
Lock{m_mutex};
243+
Lock lock{m_mutex};
244244
if (reliable) {
245245
for (unsigned int i = 0; i < m_statefulReaders.size(); i++) {
246246
if (m_statefulReaders[i].isInitialized()) {
@@ -285,7 +285,7 @@ rtps::Reader *Domain::readerExists(Participant &part, const char *topicName,
285285

286286
rtps::Writer *Domain::writerExists(Participant &part, const char *topicName,
287287
const char *typeName, bool reliable) {
288-
Lock{m_mutex};
288+
Lock lock{m_mutex};
289289
if (reliable) {
290290
for (unsigned int i = 0; i < m_statefulWriters.size(); i++) {
291291
if (m_statefulWriters[i].isInitialized()) {
@@ -330,7 +330,7 @@ rtps::Writer *Domain::writerExists(Participant &part, const char *topicName,
330330
rtps::Writer *Domain::createWriter(Participant &part, const char *topicName,
331331
const char *typeName, bool reliable,
332332
bool enforceUnicast) {
333-
Lock{m_mutex};
333+
Lock lock{m_mutex};
334334
StatelessWriter *statelessWriter =
335335
getNextUnusedEndpoint<decltype(m_statelessWriters), StatelessWriter>(
336336
m_statelessWriters);
@@ -391,7 +391,7 @@ rtps::Writer *Domain::createWriter(Participant &part, const char *topicName,
391391
rtps::Reader *Domain::createReader(Participant &part, const char *topicName,
392392
const char *typeName, bool reliable,
393393
ip4_addr_t mcastaddress) {
394-
Lock{m_mutex};
394+
Lock lock{m_mutex};
395395
StatelessReader *statelessReader =
396396
getNextUnusedEndpoint<decltype(m_statelessReaders), StatelessReader>(
397397
m_statelessReaders);
@@ -468,7 +468,7 @@ rtps::Reader *Domain::createReader(Participant &part, const char *topicName,
468468
}
469469

470470
bool rtps::Domain::deleteReader(Participant &part, Reader *reader) {
471-
Lock{m_mutex};
471+
Lock lock{m_mutex};
472472
if(reader == nullptr || !reader->isInitialized()){
473473
return false;
474474
}
@@ -481,7 +481,7 @@ bool rtps::Domain::deleteReader(Participant &part, Reader *reader) {
481481
}
482482

483483
bool rtps::Domain::deleteWriter(Participant &part, Writer *writer) {
484-
Lock{m_mutex};
484+
Lock lock{m_mutex};
485485
if(writer == nullptr || !writer->isInitialized()){
486486
return false;
487487
}

src/entities/Participant.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ bool Participant::registerOnNewSubscriberMatchedCallback(
105105
}
106106

107107
rtps::Writer *Participant::addWriter(Writer *pWriter) {
108-
Lock{m_mutex};
108+
Lock lock{m_mutex};
109109
for (unsigned int i = 0; i < m_writers.size(); i++) {
110110
if (m_writers[i] == nullptr) {
111111
m_writers[i] = pWriter;
@@ -119,7 +119,7 @@ rtps::Writer *Participant::addWriter(Writer *pWriter) {
119119
}
120120

121121
bool Participant::isWritersFull() {
122-
Lock{m_mutex};
122+
Lock lock{m_mutex};
123123
for (unsigned int i = 0; i < m_writers.size(); i++) {
124124
if (m_writers[i] == nullptr) {
125125
return false;
@@ -130,7 +130,7 @@ bool Participant::isWritersFull() {
130130
}
131131

132132
rtps::Reader *Participant::addReader(Reader *pReader) {
133-
Lock{m_mutex};
133+
Lock lock{m_mutex};
134134
for (unsigned int i = 0; i < m_readers.size(); i++) {
135135
if (m_readers[i] == nullptr) {
136136
m_readers[i] = pReader;
@@ -145,7 +145,7 @@ rtps::Reader *Participant::addReader(Reader *pReader) {
145145
}
146146

147147
bool Participant::deleteReader(Reader *reader) {
148-
Lock{m_mutex};
148+
Lock lock{m_mutex};
149149
for (unsigned int i = 0; i < m_readers.size(); i++) {
150150
if (m_readers[i]->getSEDPSequenceNumber() ==
151151
reader->getSEDPSequenceNumber()) {
@@ -160,7 +160,7 @@ bool Participant::deleteReader(Reader *reader) {
160160
}
161161

162162
bool Participant::deleteWriter(Writer *writer) {
163-
Lock{m_mutex};
163+
Lock lock{m_mutex};
164164
for (unsigned int i = 0; i < m_writers.size(); i++) {
165165
if (m_writers[i]->getSEDPSequenceNumber() ==
166166
writer->getSEDPSequenceNumber()) {
@@ -175,7 +175,7 @@ bool Participant::deleteWriter(Writer *writer) {
175175
}
176176

177177
bool Participant::isReadersFull() {
178-
Lock{m_mutex};
178+
Lock lock{m_mutex};
179179
for (unsigned int i = 0; i < m_readers.size(); i++) {
180180
if (m_readers[i] == nullptr) {
181181
return false;
@@ -186,7 +186,7 @@ bool Participant::isReadersFull() {
186186
}
187187

188188
rtps::Writer *Participant::getWriter(EntityId_t id) {
189-
Lock{m_mutex};
189+
Lock lock{m_mutex};
190190
for (uint8_t i = 0; i < m_writers.size(); ++i) {
191191
if (m_writers[i] == nullptr) {
192192
continue;
@@ -199,7 +199,7 @@ rtps::Writer *Participant::getWriter(EntityId_t id) {
199199
}
200200

201201
rtps::Reader *Participant::getReader(EntityId_t id) {
202-
Lock{m_mutex};
202+
Lock lock{m_mutex};
203203
for (uint8_t i = 0; i < m_readers.size(); ++i) {
204204
if (m_readers[i] == nullptr) {
205205
continue;
@@ -225,7 +225,7 @@ rtps::Reader *Participant::getReaderByWriterId(const Guid_t &guid) {
225225
}
226226

227227
rtps::Writer *Participant::getMatchingWriter(const TopicData &readerTopicData) {
228-
Lock{m_mutex};
228+
Lock lock{m_mutex};
229229
for (uint8_t i = 0; i < m_writers.size(); ++i) {
230230
if (m_writers[i] == nullptr) {
231231
continue;
@@ -241,7 +241,7 @@ rtps::Writer *Participant::getMatchingWriter(const TopicData &readerTopicData) {
241241
}
242242

243243
rtps::Reader *Participant::getMatchingReader(const TopicData &writerTopicData) {
244-
Lock{m_mutex};
244+
Lock lock{m_mutex};
245245
for (uint8_t i = 0; i < m_readers.size(); ++i) {
246246
if (m_readers[i] == nullptr) {
247247
continue;
@@ -258,7 +258,7 @@ rtps::Reader *Participant::getMatchingReader(const TopicData &writerTopicData) {
258258

259259
rtps::Writer *
260260
Participant::getMatchingWriter(const TopicDataCompressed &readerTopicData) {
261-
Lock{m_mutex};
261+
Lock lock{m_mutex};
262262
for (uint8_t i = 0; i < m_writers.size(); ++i) {
263263
if (m_writers[i] == nullptr) {
264264
continue;
@@ -275,7 +275,7 @@ Participant::getMatchingWriter(const TopicDataCompressed &readerTopicData) {
275275

276276
rtps::Reader *
277277
Participant::getMatchingReader(const TopicDataCompressed &writerTopicData) {
278-
Lock{m_mutex};
278+
Lock lock{m_mutex};
279279
for (uint8_t i = 0; i < m_readers.size(); ++i) {
280280
if (m_readers[i] == nullptr) {
281281
continue;
@@ -292,12 +292,12 @@ Participant::getMatchingReader(const TopicDataCompressed &writerTopicData) {
292292

293293
bool Participant::addNewRemoteParticipant(
294294
const ParticipantProxyData &remotePart) {
295-
Lock{m_mutex};
295+
Lock lock{m_mutex};
296296
return m_remoteParticipants.add(remotePart);
297297
}
298298

299299
bool Participant::removeRemoteParticipant(const GuidPrefix_t &prefix) {
300-
Lock{m_mutex};
300+
Lock lock{m_mutex};
301301
auto isElementToRemove = [&](const ParticipantProxyData &proxy) {
302302
return proxy.m_guid.prefix == prefix;
303303
};
@@ -310,7 +310,7 @@ bool Participant::removeRemoteParticipant(const GuidPrefix_t &prefix) {
310310
}
311311

312312
void Participant::removeAllProxiesOfParticipant(const GuidPrefix_t &prefix) {
313-
Lock{m_mutex};
313+
Lock lock{m_mutex};
314314
for (unsigned int i = 0; i < m_readers.size(); i++) {
315315
if (m_readers[i] == nullptr) {
316316
continue;
@@ -327,7 +327,7 @@ void Participant::removeAllProxiesOfParticipant(const GuidPrefix_t &prefix) {
327327
}
328328

329329
void Participant::removeProxyFromAllEndpoints(const Guid_t &guid) {
330-
Lock{m_mutex};
330+
Lock lock{m_mutex};
331331
for (unsigned int i = 0; i < m_writers.size(); i++) {
332332
if (m_writers[i] == nullptr) {
333333
continue;
@@ -355,7 +355,7 @@ void Participant::removeProxyFromAllEndpoints(const Guid_t &guid) {
355355

356356
const rtps::ParticipantProxyData *
357357
Participant::findRemoteParticipant(const GuidPrefix_t &prefix) {
358-
Lock{m_mutex};
358+
Lock lock{m_mutex};
359359
auto isElementToFind = [&](const ParticipantProxyData &proxy) {
360360
return proxy.m_guid.prefix == prefix;
361361
};
@@ -367,7 +367,7 @@ Participant::findRemoteParticipant(const GuidPrefix_t &prefix) {
367367

368368
void Participant::refreshRemoteParticipantLiveliness(
369369
const GuidPrefix_t &prefix) {
370-
Lock{m_mutex};
370+
Lock lock{m_mutex};
371371
auto isElementToFind = [&](const ParticipantProxyData &proxy) {
372372
return proxy.m_guid.prefix == prefix;
373373
};
@@ -382,7 +382,7 @@ void Participant::refreshRemoteParticipantLiveliness(
382382
}
383383

384384
bool Participant::hasReaderWithMulticastLocator(ip4_addr_t address) {
385-
Lock{m_mutex};
385+
Lock lock{m_mutex};
386386
for (uint8_t i = 0; i < m_readers.size(); i++) {
387387
if (m_readers[i] == nullptr) {
388388
continue;
@@ -395,15 +395,15 @@ bool Participant::hasReaderWithMulticastLocator(ip4_addr_t address) {
395395
}
396396

397397
uint32_t Participant::getRemoteParticipantCount() {
398-
Lock{m_mutex};
398+
Lock lock{m_mutex};
399399
return m_remoteParticipants.getNumElements();
400400
}
401401

402402
rtps::MessageReceiver *Participant::getMessageReceiver() { return &m_receiver; }
403403

404404
bool Participant::checkAndResetHeartbeats() {
405-
Lock{m_mutex};
406-
Lock{m_spdpAgent.m_mutex};
405+
Lock lock1{m_mutex};
406+
Lock lock2{m_spdpAgent.m_mutex};
407407
PARTICIPANT_LOG("Have %u remote participants",
408408
(unsigned int)m_remoteParticipants.getNumElements());
409409
PARTICIPANT_LOG(
@@ -521,7 +521,7 @@ void Participant::printInfo() {
521521
rtps::SPDPAgent &Participant::getSPDPAgent() { return m_spdpAgent; }
522522

523523
void Participant::addBuiltInEndpoints(BuiltInEndpoints &endpoints) {
524-
Lock{m_mutex};
524+
Lock lock{m_mutex};
525525
m_hasBuilInEndpoints = true;
526526
m_spdpAgent.init(*this, endpoints);
527527
m_sedpAgent.init(*this, endpoints);

0 commit comments

Comments
 (0)