Skip to content

Commit 3f692b2

Browse files
authored
Multithread initial approach (#126)
* Static incoming mempools Update Parametrize lenght Simplify xml/ref buffer Uncrustified Remove unused Indent Update Update rmw_microxrcedds_c/test/test_sizes.cpp Co-authored-by: Jose Antonio Moral <joseantoniomoralparras@gmail.com> Update default history value Initial multithread Update Add includes Lock memory pools Update Update * Uncrustified Co-authored-by: GitHub Actions Bot <>
1 parent 1b5a2bc commit 3f692b2

6 files changed

Lines changed: 38 additions & 2 deletions

File tree

rmw_microxrcedds_c/src/memory.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,23 @@
1818
#include <rmw_microxrcedds_c/config.h>
1919
#include <rmw/allocators.h>
2020

21+
#include <uxr/client/profile/multithread/multithread.h>
22+
2123
bool has_memory(
2224
rmw_uxrce_mempool_t* mem)
2325
{
24-
return mem->freeitems != NULL ? true : false;
26+
UXR_LOCK(&mem->mutex);
27+
bool rv = mem->freeitems != NULL ? true : false;
28+
UXR_UNLOCK(&mem->mutex);
29+
30+
return rv;
2531
}
2632

2733
rmw_uxrce_mempool_item_t* get_memory(
2834
rmw_uxrce_mempool_t* mem)
2935
{
36+
UXR_LOCK(&mem->mutex);
37+
3038
rmw_uxrce_mempool_item_t* item = NULL;
3139

3240
if (has_memory(mem))
@@ -62,13 +70,18 @@ rmw_uxrce_mempool_item_t* get_memory(
6270
item = get_memory(mem);
6371
#endif /* ifdef RMW_UXRCE_ALLOW_DYNAMIC_ALLOCATIONS */
6472
}
73+
74+
UXR_UNLOCK(&mem->mutex);
75+
6576
return item;
6677
}
6778

6879
void put_memory(
6980
rmw_uxrce_mempool_t* mem,
7081
rmw_uxrce_mempool_item_t* item)
7182
{
83+
UXR_LOCK(&mem->mutex);
84+
7285
// Gets item from allocated pool
7386
if (item->prev)
7487
{
@@ -101,4 +114,6 @@ void put_memory(
101114
}
102115
item->prev = NULL;
103116
mem->freeitems = item;
117+
118+
UXR_UNLOCK(&mem->mutex);
104119
}

rmw_microxrcedds_c/src/memory.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <stdbool.h>
1919
#include <stddef.h>
2020

21+
#include <uxr/client/profile/multithread/multithread.h>
22+
2123
typedef struct rmw_uxrce_mempool_item_t
2224
{
2325
bool is_dynamic_memory;
@@ -26,14 +28,18 @@ typedef struct rmw_uxrce_mempool_item_t
2628
void* data;
2729
} rmw_uxrce_mempool_item_t;
2830

29-
3031
typedef struct rmw_uxrce_mempool_t
3132
{
3233
struct rmw_uxrce_mempool_item_t* allocateditems;
3334
struct rmw_uxrce_mempool_item_t* freeitems;
3435

3536
bool is_initialized;
3637
size_t element_size;
38+
39+
#ifdef UCLIENT_PROFILE_MULTITHREAD
40+
uxrMutex mutex;
41+
#endif // UCLIENT_PROFILE_MULTITHREAD
42+
3743
} rmw_uxrce_mempool_t;
3844

3945
bool has_memory(

rmw_microxrcedds_c/src/rmw_publish.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include <rmw_uros/options.h>
2222

23+
#include <uxr/client/profile/multithread/multithread.h>
24+
2325
bool flush_session(
2426
uxrSession* session)
2527
{
@@ -83,6 +85,8 @@ rmw_publish(
8385
custom_publisher->cs_cb_serialization(&mb);
8486
}
8587

88+
UXR_UNLOCK_STREAM_ID(&custom_publisher->owner_node->context->session, custom_publisher->stream_id);
89+
8690
if (UXR_BEST_EFFORT_STREAM == custom_publisher->stream_id.type)
8791
{
8892
uxr_flash_output_streams(&custom_publisher->owner_node->context->session);

rmw_microxrcedds_c/src/rmw_request.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <rmw/rmw.h>
1818
#include <rmw/error_handling.h>
1919

20+
#include <uxr/client/profile/multithread/multithread.h>
21+
2022
rmw_ret_t
2123
rmw_send_request(
2224
const rmw_client_t* client,
@@ -48,6 +50,8 @@ rmw_send_request(
4850

4951
functions->cdr_serialize(ros_request, &mb);
5052

53+
UXR_UNLOCK_STREAM_ID(&custom_node->context->session, custom_client->stream_id);
54+
5155
if (UXR_INVALID_REQUEST_ID == *sequence_id)
5256
{
5357
RMW_SET_ERROR_MSG("Micro XRCE-DDS service request error.");

rmw_microxrcedds_c/src/rmw_response.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <rmw/rmw.h>
1818
#include <rmw/error_handling.h>
1919

20+
#include <uxr/client/profile/multithread/multithread.h>
21+
2022
rmw_ret_t
2123
rmw_send_response(
2224
const rmw_service_t* service,
@@ -61,6 +63,8 @@ rmw_send_response(
6163
uxr_serialize_SampleIdentity(&mb, &sample_id);
6264
functions->cdr_serialize(ros_response, &mb);
6365

66+
UXR_UNLOCK_STREAM_ID(&custom_node->context->session, custom_service->stream_id);
67+
6468
if (UXR_INVALID_REQUEST_ID == rc)
6569
{
6670
RMW_SET_ERROR_MSG("Micro XRCE-DDS service response error.");

rmw_microxrcedds_c/src/types.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include "rmw/allocators.h"
2929
#include <rmw/error_handling.h>
3030

31+
#include <uxr/client/profile/multithread/multithread.h>
32+
3133
// Static memory pools
3234

3335
char rmw_uxrce_entity_naming_buffer[RMW_UXRCE_ENTITY_NAMING_BUFFER_LENGTH];
@@ -65,6 +67,7 @@ rmw_uxrce_static_input_buffer_t custom_static_buffers[RMW_UXRCE_MAX_HISTORY];
6567
size_t size) \
6668
{ \
6769
if (size > 0 && !memory->is_initialized){ \
70+
UXR_INIT_LOCK(&memory->mutex); \
6871
memory->is_initialized = true; \
6972
memory->element_size = sizeof(*array); \
7073
memory->allocateditems = NULL; \

0 commit comments

Comments
 (0)