Fix for issue #6872 - Mutex lock has possibility to fail at runtime (returning status flag)

Add assertions for statuses returned by osMutexAcquire().
Add void Mutex::lock(void) member function - lock function which does not return status and waits for the mutex forever.
Make osStatus Mutex::lock(uint32_t millisec=osWaitForever) member function deprecated in favour of bool Mutex::trylock_for(uint32_t millisec).
pull/7423/head
Przemyslaw Stekiel 2018-07-05 10:37:40 +02:00
parent 2f8e679183
commit ca8070a482
2 changed files with 31 additions and 3 deletions

View File

@ -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;
}

View File

@ -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