Skip to content

Commit 505969a

Browse files
jerome-pouillercfriedt
authored andcommitted
portability: cmsis: Fix possible race in osEventFlagsSet()
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 <[email protected]>
1 parent 316452c commit 505969a

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

subsys/portability/cmsis_rtos_v2/event_flags.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,16 @@ osEventFlagsId_t osEventFlagsNew(const osEventFlagsAttr_t *attr)
5757
uint32_t osEventFlagsSet(osEventFlagsId_t ef_id, uint32_t flags)
5858
{
5959
struct cmsis_rtos_event_cb *events = (struct cmsis_rtos_event_cb *)ef_id;
60+
uint32_t rv;
61+
6062
if ((ef_id == NULL) || (flags & osFlagsError)) {
6163
return osFlagsErrorParameter;
6264
}
6365

64-
k_event_post(&events->z_event, flags);
66+
rv = k_event_test(&events->z_event, 0xFFFFFFFF);
67+
k_event_post(&events->z_event, flags & ~rv);
6568

66-
return k_event_test(&events->z_event, 0xFFFFFFFF);
69+
return flags & ~rv;
6770
}
6871

6972
/**

0 commit comments

Comments
 (0)