Skip to content

Commit ca8070a

Browse files
committed
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).
1 parent 2f8e679 commit ca8070a

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

rtos/Mutex.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,25 @@ void Mutex::constructor(const char *name)
5050
MBED_ASSERT(_id);
5151
}
5252

53+
void Mutex::lock(void) {
54+
osStatus status = osMutexAcquire(_id, osWaitForever);
55+
if (osOK == status) {
56+
_count++;
57+
}
58+
59+
MBED_ASSERT(status == osOK);
60+
}
61+
5362
osStatus Mutex::lock(uint32_t millisec) {
5463
osStatus status = osMutexAcquire(_id, millisec);
5564
if (osOK == status) {
5665
_count++;
5766
}
67+
68+
MBED_ASSERT(status == osOK ||
69+
(status == osErrorResource && millisec == 0) ||
70+
(status == osErrorTimeout && millisec != osWaitForever));
71+
5872
return status;
5973
}
6074

@@ -63,12 +77,14 @@ bool Mutex::trylock() {
6377
}
6478

6579
bool Mutex::trylock_for(uint32_t millisec) {
66-
osStatus status = lock(millisec);
80+
osStatus status = osMutexAcquire(_id, millisec);
6781
if (status == osOK) {
6882
return true;
6983
}
7084

71-
MBED_ASSERT(status == osErrorTimeout || status == osErrorResource);
85+
MBED_ASSERT(status == osOK ||
86+
(status == osErrorResource && millisec == 0) ||
87+
(status == osErrorTimeout && millisec != osWaitForever));
7288

7389
return false;
7490
}

rtos/Mutex.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,18 @@ class Mutex : private mbed::NonCopyable<Mutex> {
7878
*/
7979
Mutex(const char *name);
8080

81-
/** Wait until a Mutex becomes available.
81+
/**
82+
Wait until a Mutex becomes available.
83+
84+
@note You cannot call this function from ISR context.
85+
*/
86+
void lock(void);
87+
88+
/**
89+
For backwards compatibility.
90+
@deprecated Do not use this function. This function has been replaced with trylock_for and lock(void) functions.
91+
92+
Wait until a Mutex becomes available.
8293
@param millisec timeout value or 0 in case of no time-out. (default: osWaitForever)
8394
@return status code that indicates the execution status of the function:
8495
@a osOK the mutex has been obtained.
@@ -89,6 +100,7 @@ class Mutex : private mbed::NonCopyable<Mutex> {
89100
90101
@note You cannot call this function from ISR context.
91102
*/
103+
MBED_DEPRECATED_SINCE("mbed-os-5.10.0", "Replaced with trylock_for and lock(void) functions")
92104
osStatus lock(uint32_t millisec=osWaitForever);
93105

94106
/** Try to lock the mutex, and return immediately

0 commit comments

Comments
 (0)