From 8e7627d18c2108aca178888d88514179899a044f Mon Sep 17 00:00:00 2001 From: Cameron Date: Sat, 14 Aug 2021 16:41:41 -0400 Subject: [PATCH] Attempt to support semaphore use from within FreeRTOS ISR context (see #127) --- atomicops.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/atomicops.h b/atomicops.h index ffd215a..b103bc6 100644 --- a/atomicops.h +++ b/atomicops.h @@ -613,6 +613,10 @@ namespace moodycamel bool try_wait() AE_NO_TSAN { + // Note: In an ISR context, if this causes a task to unblock, + // the caller won't know about it + if (xPortIsInsideInterrupt()) + return xSemaphoreTakeFromISR(m_sema, NULL) == pdTRUE; return xSemaphoreTake(m_sema, 0) == pdTRUE; } @@ -620,12 +624,20 @@ namespace moodycamel { std::uint64_t msecs = usecs / 1000; TickType_t ticks = static_cast(msecs / portTICK_PERIOD_MS); + if (ticks == 0) + return try_wait(); return xSemaphoreTake(m_sema, ticks) == pdTRUE; } void signal() AE_NO_TSAN { - BaseType_t rc = xSemaphoreGive(m_sema); + // Note: In an ISR context, if this causes a task to unblock, + // the caller won't know about it + BaseType_t rc; + if (xPortIsInsideInterrupt()) + rc = xSemaphoreGiveFromISR(m_sema, NULL); + else + rc = xSemaphoreGive(m_sema); assert(rc == pdTRUE); AE_UNUSED(rc); }