/** * serial_lock.cpp * * Implementation of the global serial write mutex. See serial_lock.h. */ #include "modules/serial_lock.h" namespace SerialLock { static SemaphoreHandle_t g_serial_mutex = NULL; void init() { if (g_serial_mutex == NULL) { g_serial_mutex = xSemaphoreCreateMutex(); } } bool lock(TickType_t timeout) { // Not initialized yet (early boot): don't block, just proceed. if (g_serial_mutex == NULL) { return true; } return xSemaphoreTake(g_serial_mutex, timeout) == pdTRUE; } void unlock() { if (g_serial_mutex != NULL) { xSemaphoreGive(g_serial_mutex); } } } // namespace SerialLock