diff --git a/include/sta/rtos/mutex.hpp b/include/sta/rtos/mutex.hpp index c946185..c4208ed 100644 --- a/include/sta/rtos/mutex.hpp +++ b/include/sta/rtos/mutex.hpp @@ -9,6 +9,7 @@ #include + namespace sta { /** @@ -30,10 +31,17 @@ namespace sta */ RtosMutex(const char* name); + /** + * @brief Method for checking mutex ownership. + * + * @return Returns true if the currently running thread is the owner of the mutex. + */ + bool isCurrentThreadOwner() override; + /** * @brief Acquire the mutex. */ - void acquire() override; + void acquire(uint32_t timeout = osWaitForever) override; /** * @brief Release the mutex. diff --git a/src/mutex.cpp b/src/mutex.cpp index 854b356..2607e49 100644 --- a/src/mutex.cpp +++ b/src/mutex.cpp @@ -17,9 +17,17 @@ namespace sta STA_ASSERT(handle_ != NULL); } - void RtosMutex::acquire() + bool RtosMutex::isCurrentThreadOwner() { - osMutexAcquire(handle_, osWaitForever); + osThreadId_t id = osThreadGetId(); + STA_ASSERT_MSG(id != NULL, "Failed to get current thread id."); + + return osMutexGetOwner(handle_) == id; + } + + void RtosMutex::acquire(uint32_t timeout /* = osWaitForever */) + { + osMutexAcquire(handle_, timeout); } void RtosMutex::release()