Skip to content

Commit f738f9c

Browse files
Fix rmw_wait lock (#200) (#202)
* Fix rmw_wait lock * Fix * Lock memory usage in ping Lock fixes Fix fix * Do not wait if no contexts * Fix * Uncrustify * Refactor cleaning * Unlock timing API (cherry picked from commit 50cdbf8) Co-authored-by: Pablo Garrido <pablogs9@gmail.com>
1 parent 498773b commit f738f9c

4 files changed

Lines changed: 23 additions & 10 deletions

File tree

rmw_microxrcedds_c/src/rmw_microros/ping.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ rmw_ret_t rmw_uros_ping_agent(
3333
{
3434
bool success = false;
3535

36+
UXR_LOCK(&session_memory.mutex);
37+
3638
if (NULL == session_memory.allocateditems) {
3739
#ifdef RMW_UXRCE_TRANSPORT_SERIAL
3840
uxrSerialTransport transport;
@@ -52,6 +54,7 @@ rmw_ret_t rmw_uros_ping_agent(
5254
rmw_ret_t ret = rmw_uxrce_transport_init(NULL, NULL, (void *)&transport);
5355

5456
if (RMW_RET_OK != ret) {
57+
UXR_UNLOCK(&session_memory.mutex);
5558
return ret;
5659
}
5760

@@ -67,6 +70,8 @@ rmw_ret_t rmw_uros_ping_agent(
6770
} while (NULL != item && !success);
6871
}
6972

73+
UXR_UNLOCK(&session_memory.mutex);
74+
7075
return success ? RMW_RET_OK : RMW_RET_ERROR;
7176
}
7277

rmw_microxrcedds_c/src/rmw_microros/time_sync.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ bool rmw_uros_epoch_synchronized()
3030
rmw_uxrce_mempool_item_t * item = session_memory.allocateditems;
3131
rmw_context_impl_t * context = (rmw_context_impl_t *)item->data;
3232

33-
return context->session.synchronized;
33+
bool ret = context->session.synchronized;
34+
return ret;
3435
}
3536

3637
int64_t rmw_uros_epoch_millis()
@@ -44,7 +45,8 @@ int64_t rmw_uros_epoch_millis()
4445
rmw_uxrce_mempool_item_t * item = session_memory.allocateditems;
4546
rmw_context_impl_t * context = (rmw_context_impl_t *)item->data;
4647

47-
return uxr_epoch_millis(&context->session);
48+
int64_t ret = uxr_epoch_millis(&context->session);
49+
return ret;
4850
}
4951

5052
int64_t rmw_uros_epoch_nanos()
@@ -58,7 +60,8 @@ int64_t rmw_uros_epoch_nanos()
5860
rmw_uxrce_mempool_item_t * item = session_memory.allocateditems;
5961
rmw_context_impl_t * context = (rmw_context_impl_t *)item->data;
6062

61-
return uxr_epoch_nanos(&context->session);
63+
int64_t ret = uxr_epoch_nanos(&context->session);
64+
return ret;
6265
}
6366

6467
rmw_ret_t rmw_uros_sync_session(
@@ -77,8 +80,7 @@ rmw_ret_t rmw_uros_sync_session(
7780

7881
if (!uxr_sync_session(&context->session, timeout_ms)) {
7982
RMW_SET_ERROR_MSG("Time synchronization failed.");
80-
return RMW_RET_ERROR;
83+
ret = RMW_RET_ERROR;
8184
}
82-
8385
return ret;
8486
}

rmw_microxrcedds_c/src/rmw_wait.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,12 @@ rmw_wait(
5151
timeout.i32 = (timeout.i64 > INT32_MAX) ? INT32_MAX : timeout.i64;
5252
}
5353

54-
// Clean expired buffers
5554
rmw_uxrce_clean_expired_static_input_buffer();
5655

57-
rmw_uxrce_mempool_item_t * item = NULL;
56+
UXR_LOCK(&session_memory.mutex);
5857

5958
// Clear run flag for all sessions
60-
item = session_memory.allocateditems;
59+
rmw_uxrce_mempool_item_t * item = session_memory.allocateditems;
6160
while (item != NULL) {
6261
rmw_context_impl_t * custom_context = (rmw_context_impl_t *)item->data;
6362
custom_context->need_to_be_ran = false;
@@ -90,7 +89,11 @@ rmw_wait(
9089
item = item->next;
9190
}
9291

93-
rmw_uxrce_clean_expired_static_input_buffer();
92+
// There is no context that contais any of the wait set entities. Nothing to wait here.
93+
if (available_contexts == 0) {
94+
UXR_UNLOCK(&session_memory.mutex);
95+
return RMW_RET_OK;
96+
}
9497

9598
int32_t per_session_timeout =
9699
(timeout.i32 == UXR_TIMEOUT_INF) ? UXR_TIMEOUT_INF :
@@ -105,6 +108,8 @@ rmw_wait(
105108
item = item->next;
106109
}
107110

111+
UXR_UNLOCK(&session_memory.mutex);
112+
108113
bool buffered_status = false;
109114

110115
// Check services

rmw_microxrcedds_c/src/types.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,10 @@ size_t rmw_uxrce_count_static_input_buffer_for_entity(
225225
void * entity)
226226
{
227227
size_t count = 0;
228-
rmw_uxrce_mempool_item_t * item = static_buffer_memory.allocateditems;
229228

230229
UXR_LOCK(&static_buffer_memory.mutex);
230+
rmw_uxrce_mempool_item_t * item = static_buffer_memory.allocateditems;
231+
231232
while (item != NULL) {
232233
rmw_uxrce_static_input_buffer_t * data = (rmw_uxrce_static_input_buffer_t *)item->data;
233234
if (data->owner == entity) {

0 commit comments

Comments
 (0)