diff --git a/src/concurrency/Lock.cpp b/src/concurrency/Lock.cpp index 9068cee433..29d6bd5e46 100644 --- a/src/concurrency/Lock.cpp +++ b/src/concurrency/Lock.cpp @@ -1,6 +1,7 @@ #include "Lock.h" #include "configuration.h" #include +#include namespace concurrency { @@ -38,18 +39,41 @@ void Lock::unlock() } } #else -Lock::Lock() {} +Lock::Lock() +{ + pthread_mutex_init(&mutex, NULL); +} -Lock::~Lock() {} +void Lock::lock() +{ + if (locked) { + LOG_INFO("Attempt to lock an already locked Lock!"); + } + pthread_mutex_lock(&mutex); + locked = true; -void Lock::lock() {} + if (console) + LOG_WARN("Lock"); +} bool Lock::lock(uint32_t) { + // No portable timed pthread lock across Linux and macOS, so block instead: returning true + // without acquiring would leave callers such as SPILock unlocking a mutex they never took. + lock(); return true; } -void Lock::unlock() {} +void Lock::unlock() +{ + pthread_mutex_unlock(&mutex); + locked = false; +} + +Lock::~Lock() +{ + pthread_mutex_destroy(&mutex); +} #endif } // namespace concurrency diff --git a/src/concurrency/Lock.h b/src/concurrency/Lock.h index 342747ae7f..d30667e12c 100644 --- a/src/concurrency/Lock.h +++ b/src/concurrency/Lock.h @@ -35,6 +35,9 @@ class Lock private: #ifdef HAS_FREE_RTOS SemaphoreHandle_t handle; +#else + pthread_mutex_t mutex; + bool locked = false; #endif };