Skip to content

Commit 7442da3

Browse files
committed
equeue - avoid Kernel::get_ms_count from IRQ
Kernel::get_ms_count is documented as not working from IRQ. In RTOS builds it can return misleading answers - see ARM-software/CMSIS_5#625 In non-RTOS builds, it can trigger an assert, as it upsets the sleep logic. Modified code is still not ideal - could be improved further if there was a fast path for "post now" that didn't bother looking at timers (both at post time and dispatch time).
1 parent c98b0d0 commit 7442da3

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

Diff for: events/equeue/equeue_mbed.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,24 @@ using namespace mbed;
3737
#if MBED_CONF_RTOS_API_PRESENT
3838

3939
#include "rtos/Kernel.h"
40+
#include "platform/mbed_os_timer.h"
4041

4142
unsigned equeue_tick()
4243
{
43-
return rtos::Kernel::get_ms_count();
44+
// It is not safe to call get_ms_count from ISRs, both
45+
// because documentation says so, and because it will give
46+
// a stale value from the RTOS if the interrupt has woken
47+
// us out of sleep - the RTOS will not have updated its
48+
// ticks yet.
49+
if (core_util_is_isr_active()) {
50+
// And the documentation further says that this
51+
// should not be called from critical sections, for
52+
// performance reasons, but I don't have a good
53+
// current alternative!
54+
return mbed::internal::os_timer->get_time() / 1000;
55+
} else {
56+
return rtos::Kernel::get_ms_count();
57+
}
4458
}
4559

4660
#else

0 commit comments

Comments
 (0)