Skip to content

Commit 8e7627d

Browse files
committed
Attempt to support semaphore use from within FreeRTOS ISR context (see cameron314#127)
1 parent b18136b commit 8e7627d

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

atomicops.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -613,19 +613,31 @@ namespace moodycamel
613613

614614
bool try_wait() AE_NO_TSAN
615615
{
616+
// Note: In an ISR context, if this causes a task to unblock,
617+
// the caller won't know about it
618+
if (xPortIsInsideInterrupt())
619+
return xSemaphoreTakeFromISR(m_sema, NULL) == pdTRUE;
616620
return xSemaphoreTake(m_sema, 0) == pdTRUE;
617621
}
618622

619623
bool timed_wait(std::uint64_t usecs) AE_NO_TSAN
620624
{
621625
std::uint64_t msecs = usecs / 1000;
622626
TickType_t ticks = static_cast<TickType_t>(msecs / portTICK_PERIOD_MS);
627+
if (ticks == 0)
628+
return try_wait();
623629
return xSemaphoreTake(m_sema, ticks) == pdTRUE;
624630
}
625631

626632
void signal() AE_NO_TSAN
627633
{
628-
BaseType_t rc = xSemaphoreGive(m_sema);
634+
// Note: In an ISR context, if this causes a task to unblock,
635+
// the caller won't know about it
636+
BaseType_t rc;
637+
if (xPortIsInsideInterrupt())
638+
rc = xSemaphoreGiveFromISR(m_sema, NULL);
639+
else
640+
rc = xSemaphoreGive(m_sema);
629641
assert(rc == pdTRUE);
630642
AE_UNUSED(rc);
631643
}

0 commit comments

Comments
 (0)