From 505969a09ec9d64e7c4effb5e07131914cbada52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Pouiller?= Date: Fri, 10 Oct 2025 09:13:02 +0200 Subject: [PATCH] portability: cmsis: Fix possible race in osEventFlagsSet() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The CMSIS-RTOS specification says "The function returns the event flags stored in the event control block". In the original code, osEventFlagsSet() called k_event_post() and then k_event_test(). It worked mostly fine if the thread has higher priority than the waiter thread. In the opposite case, the event was not reported to the user. With the last changes, the waiter thread use k_event_wait_safe(). So, the event is posted and consumed in an atomic way. So, the issue above happen even if the waiter thread has a lower priority then the poster. This patch fixes the both cases. Signed-off-by: Jérôme Pouiller --- subsys/portability/cmsis_rtos_v2/event_flags.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/subsys/portability/cmsis_rtos_v2/event_flags.c b/subsys/portability/cmsis_rtos_v2/event_flags.c index 7e48e163da9..a9eb3dfb0ef 100644 --- a/subsys/portability/cmsis_rtos_v2/event_flags.c +++ b/subsys/portability/cmsis_rtos_v2/event_flags.c @@ -57,13 +57,16 @@ osEventFlagsId_t osEventFlagsNew(const osEventFlagsAttr_t *attr) uint32_t osEventFlagsSet(osEventFlagsId_t ef_id, uint32_t flags) { struct cmsis_rtos_event_cb *events = (struct cmsis_rtos_event_cb *)ef_id; + uint32_t rv; + if ((ef_id == NULL) || (flags & osFlagsError)) { return osFlagsErrorParameter; } - k_event_post(&events->z_event, flags); + rv = k_event_test(&events->z_event, 0xFFFFFFFF); + k_event_post(&events->z_event, flags & ~rv); - return k_event_test(&events->z_event, 0xFFFFFFFF); + return flags & ~rv; } /**