From fccbabbdb4879732aac654c73131341767fd7650 Mon Sep 17 00:00:00 2001 From: Nicolas Rabault Date: Mon, 22 Jan 2024 18:36:36 +0100 Subject: [PATCH] Prevent multi-threading datarace on job allocation --- engine/IO/src/luos_phy.c | 2 ++ engine/core/src/service.c | 2 ++ examples/projects/native/gate_wscom/src/main.c | 8 ++++---- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/engine/IO/src/luos_phy.c b/engine/IO/src/luos_phy.c index d673fec48..8adb64a77 100644 --- a/engine/IO/src/luos_phy.c +++ b/engine/IO/src/luos_phy.c @@ -902,9 +902,11 @@ static void Phy_Dispatch(void) phy_job.phy_data = NULL; // Write the job in the phy queue and get back the pointer to it + MSGALLOC_MUTEX_LOCK phy_job_t *job_ptr = Phy_AddJob(&phy_ctx.phy[y], &phy_job); // Notify this phy that a job is available and give it the concerned job on his queue phy_ctx.phy[y].job_cb(&phy_ctx.phy[y], job_ptr); + MSGALLOC_MUTEX_UNLOCK } } Phy_SetIrqState(false); diff --git a/engine/core/src/service.c b/engine/core/src/service.c index bccf0e13c..a86c2b1da 100644 --- a/engine/core/src/service.c +++ b/engine/core/src/service.c @@ -271,8 +271,10 @@ error_return_t Service_Deliver(phy_job_t *job) // This means that this job already contain a service filter. // We just have to loop in the service list, filter it, call the callback and remove it from the service filter. error_return_t error = SUCCEED; + MSGALLOC_MUTEX_LOCK LUOS_ASSERT(job); service_filter_t *service_filter = (service_filter_t *)job->phy_data; + MSGALLOC_MUTEX_UNLOCK for (int i = 0; i < service_ctx.number; i++) { if (((*service_filter) >> i) & 0x01) diff --git a/examples/projects/native/gate_wscom/src/main.c b/examples/projects/native/gate_wscom/src/main.c index 65f52190c..61e0cc614 100644 --- a/examples/projects/native/gate_wscom/src/main.c +++ b/examples/projects/native/gate_wscom/src/main.c @@ -55,8 +55,8 @@ int main(void) Pipe_Init(); Gate_Init(); // Create a thread to convert messages into Json and steam them using Websocket - // pthread_t thread_id; - // pthread_create(&thread_id, NULL, Gate_Pipe_LoopThread, NULL); + pthread_t thread_id; + pthread_create(&thread_id, NULL, Gate_Pipe_LoopThread, NULL); while (1) { Luos_Loop(); @@ -65,7 +65,7 @@ int main(void) #else Ws_Loop(); #endif - Pipe_Loop(); - Gate_Loop(); + // Pipe_Loop(); + // Gate_Loop(); } }