Skip to content
This repository was archived by the owner on Aug 19, 2021. It is now read-only.

Commit b5a79ea

Browse files
committed
Fixed race-conditions in non-rtos mbed wfi implementation
Before, an interrupt that changed the state of the queue could occur before the wfi was executed. To fix this, a flag was added to catch these events, and wfi must be dispatched with interrupts counter-intuitively disabled. A thanks to @c1728p9 for noting this
1 parent f8dd3b0 commit b5a79ea

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

equeue_mbed.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,30 @@ bool equeue_sema_wait(equeue_sema_t *s, int ms) {
8888
#else
8989

9090
// Semaphore operations
91-
int equeue_sema_create(equeue_sema_t *s) { return 0; }
92-
void equeue_sema_destroy(equeue_sema_t *s) {}
93-
void equeue_sema_signal(equeue_sema_t *s) {}
91+
int equeue_sema_create(equeue_sema_t *s) {
92+
*s = false;
93+
return 0;
94+
}
95+
96+
void equeue_sema_destroy(equeue_sema_t *s) {
97+
}
9498

95-
static void equeue_sema_wakeup() {}
99+
void equeue_sema_signal(equeue_sema_t *s) {
100+
*s = true;
101+
}
96102

97103
bool equeue_sema_wait(equeue_sema_t *s, int ms) {
98104
Timeout timeout;
99-
timeout.attach_us(equeue_sema_wakeup, ms*1000);
105+
timeout.attach_us(s, equeue_sema_signal, ms*1000);
100106

101-
__WFI();
107+
core_util_critical_section_enter();
108+
while (!*(volatile equeue_sema_t *)s) {
109+
__WFI();
110+
core_util_critical_section_exit();
111+
core_util_critical_section_enter();
112+
}
113+
*s = false;
114+
core_util_critical_section_exit();
102115

103116
return true;
104117
}

equeue_sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ typedef sem_t equeue_sema_t;
2525
#ifdef MBED_CONF_RTOS_PRESENT
2626
typedef void *equeue_sema_t;
2727
#else
28-
typedef struct {} equeue_sema_t;
28+
typedef bool equeue_sema_t;
2929
#endif
3030
#endif
3131

0 commit comments

Comments
 (0)