Skip to content

Commit

Permalink
Attempt to support semaphore use from within FreeRTOS ISR context (see
Browse files Browse the repository at this point in the history
  • Loading branch information
cameron314 committed Aug 14, 2021
1 parent b18136b commit 8e7627d
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion atomicops.h
Original file line number Diff line number Diff line change
Expand Up @@ -613,19 +613,31 @@ 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;
}

bool timed_wait(std::uint64_t usecs) AE_NO_TSAN
{
std::uint64_t msecs = usecs / 1000;
TickType_t ticks = static_cast<TickType_t>(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);
}
Expand Down

0 comments on commit 8e7627d

Please sign in to comment.