Added mutex timeout and mutex ownership

This commit is contained in:
dario 2024-05-01 13:56:38 +02:00
parent 4ce4653f71
commit 33a1f757a2
2 changed files with 19 additions and 3 deletions

View File

@ -9,6 +9,7 @@
#include <cmsis_os2.h> #include <cmsis_os2.h>
namespace sta namespace sta
{ {
/** /**
@ -30,10 +31,17 @@ namespace sta
*/ */
RtosMutex(const char* name); 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. * @brief Acquire the mutex.
*/ */
void acquire() override; void acquire(uint32_t timeout = osWaitForever) override;
/** /**
* @brief Release the mutex. * @brief Release the mutex.

View File

@ -17,9 +17,17 @@ namespace sta
STA_ASSERT(handle_ != NULL); 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() void RtosMutex::release()