diff --git a/rtos/Mutex.cpp b/rtos/Mutex.cpp index edc2cdcb52..61c1d45d22 100644 --- a/rtos/Mutex.cpp +++ b/rtos/Mutex.cpp @@ -50,11 +50,25 @@ void Mutex::constructor(const char *name) MBED_ASSERT(_id); } +void Mutex::lock(void) { + osStatus status = osMutexAcquire(_id, osWaitForever); + if (osOK == status) { + _count++; + } + + MBED_ASSERT(status == osOK); +} + osStatus Mutex::lock(uint32_t millisec) { osStatus status = osMutexAcquire(_id, millisec); if (osOK == status) { _count++; } + + MBED_ASSERT(status == osOK || + (status == osErrorResource && millisec == 0) || + (status == osErrorTimeout && millisec != osWaitForever)); + return status; } @@ -63,12 +77,14 @@ bool Mutex::trylock() { } bool Mutex::trylock_for(uint32_t millisec) { - osStatus status = lock(millisec); + osStatus status = osMutexAcquire(_id, millisec); if (status == osOK) { return true; } - MBED_ASSERT(status == osErrorTimeout || status == osErrorResource); + MBED_ASSERT(status == osOK || + (status == osErrorResource && millisec == 0) || + (status == osErrorTimeout && millisec != osWaitForever)); return false; } diff --git a/rtos/Mutex.h b/rtos/Mutex.h index 39fb4f84e7..dedb9a8f3a 100644 --- a/rtos/Mutex.h +++ b/rtos/Mutex.h @@ -78,7 +78,18 @@ public: */ Mutex(const char *name); - /** Wait until a Mutex becomes available. + /** + Wait until a Mutex becomes available. + + @note You cannot call this function from ISR context. + */ + void lock(void); + + /** + For backwards compatibility. + @deprecated Do not use this function. This function has been replaced with trylock_for and lock(void) functions. + + Wait until a Mutex becomes available. @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever) @return status code that indicates the execution status of the function: @a osOK the mutex has been obtained. @@ -89,6 +100,7 @@ public: @note You cannot call this function from ISR context. */ + MBED_DEPRECATED_SINCE("mbed-os-5.10.0", "Replaced with trylock_for and lock(void) functions") osStatus lock(uint32_t millisec=osWaitForever); /** Try to lock the mutex, and return immediately