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 <jerome.pouiller@silabs.com>
diff --git a/subsys/portability/cmsis_rtos_v2/event_flags.c b/subsys/portability/cmsis_rtos_v2/event_flags.c
index 7e48e16..a9eb3df 100644
--- a/subsys/portability/cmsis_rtos_v2/event_flags.c
+++ b/subsys/portability/cmsis_rtos_v2/event_flags.c
@@ -57,13 +57,16 @@
 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;
 }
 
 /**