Skip to content

Commit fba9c4b

Browse files
committed
Address review comments.
Perform the following changes: - change definition of `void Mutex::lock(void)` to `osStatus Mutex::lock(void)`. - change definition of `void Mutex::unlock()` to `osStatus Mutex::unlock()`. - use MBED_ERROR1 macro to check the lock/unlock operation status. - add notes in the description of lock/unlock functions: "This function asserts status of the lock/unlock operation (will not return in case of failure). Use of the return value is deprecated, as the return is expected to become void in the future.". - modify/add description of the return value. - remove reference to Mbed 6. - make `lock(millisec)` deprecated in favour of lock(), trylock() and trylock_for() functions.
1 parent ac2db7c commit fba9c4b

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

rtos/Mutex.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,18 @@ void Mutex::constructor(const char *name)
5151
MBED_ASSERT(_id);
5252
}
5353

54-
void Mutex::lock(void)
54+
osStatus Mutex::lock(void)
5555
{
5656
osStatus status = osMutexAcquire(_id, osWaitForever);
5757
if (osOK == status) {
5858
_count++;
5959
}
6060

61-
MBED_ASSERT(status == osOK);
61+
if (status != osOK) {
62+
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_MUTEX_LOCK_FAILED), "Mutex lock failed", status);
63+
}
64+
65+
return osOK;
6266
}
6367

6468
osStatus Mutex::lock(uint32_t millisec)
@@ -68,9 +72,13 @@ osStatus Mutex::lock(uint32_t millisec)
6872
_count++;
6973
}
7074

71-
MBED_ASSERT(status == osOK ||
72-
(status == osErrorResource && millisec == 0) ||
73-
(status == osErrorTimeout && millisec != osWaitForever));
75+
bool success = (status == osOK ||
76+
(status == osErrorResource && millisec == 0) ||
77+
(status == osErrorTimeout && millisec != osWaitForever));
78+
79+
if (!success) {
80+
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_MUTEX_LOCK_FAILED), "Mutex lock failed", status);
81+
}
7482

7583
return status;
7684
}
@@ -87,9 +95,13 @@ bool Mutex::trylock_for(uint32_t millisec)
8795
return true;
8896
}
8997

90-
MBED_ASSERT(status == osOK ||
91-
(status == osErrorResource && millisec == 0) ||
92-
(status == osErrorTimeout && millisec != osWaitForever));
98+
bool success = (status == osOK ||
99+
(status == osErrorResource && millisec == 0) ||
100+
(status == osErrorTimeout && millisec != osWaitForever));
101+
102+
if (!success) {
103+
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_MUTEX_LOCK_FAILED), "Mutex lock failed", status);
104+
}
93105

94106
return false;
95107
}
@@ -108,13 +120,17 @@ bool Mutex::trylock_until(uint64_t millisec)
108120
}
109121
}
110122

111-
void Mutex::unlock()
123+
osStatus Mutex::unlock()
112124
{
113125
_count--;
114126

115127
osStatus status = osMutexRelease(_id);
116128

117-
MBED_ASSERT(status == osOK);
129+
if (status != osOK) {
130+
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_MUTEX_UNLOCK_FAILED), "Mutex unlock failed", status);
131+
}
132+
133+
return osOK;
118134
}
119135

120136
osThreadId Mutex::get_owner()

rtos/Mutex.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,34 +74,38 @@ class Mutex : private mbed::NonCopyable<Mutex> {
7474
/** Create and Initialize a Mutex object
7575
7676
@param name name to be used for this mutex. It has to stay allocated for the lifetime of the thread.
77-
7877
@note You cannot call this function from ISR context.
7978
*/
8079
Mutex(const char *name);
8180

8281
/**
8382
Wait until a Mutex becomes available.
8483
84+
@return status code that indicates the execution status of the function:
85+
@a osOK the mutex has been obtained.
86+
8587
@note You cannot call this function from ISR context.
88+
@note This function treats RTOS errors as fatal system errors, so can only return osOK.
89+
Use of the return value is deprecated, as the return is expected to become void in the future.
8690
*/
87-
void lock(void);
91+
osStatus lock(void);
8892

8993
/**
9094
For backwards compatibility.
91-
@deprecated Do not use this function. This function has been replaced with trylock_for and lock(void) functions.
95+
@deprecated Do not use this function. This function has been replaced with lock(), trylock() and trylock_for() functions.
9296
9397
Wait until a Mutex becomes available.
9498
@param millisec timeout value or 0 in case of no time-out.
9599
@return status code that indicates the execution status of the function:
96100
@a osOK the mutex has been obtained.
97101
@a osErrorTimeout the mutex could not be obtained in the given time.
98-
@a osErrorParameter internal error.
99102
@a osErrorResource the mutex could not be obtained when no timeout was specified.
100-
@a osErrorISR this function cannot be called from the interrupt service routine.
101103
102104
@note You cannot call this function from ISR context.
105+
@note This function treats RTOS errors as fatal system errors, so can only return osOK or
106+
osErrorResource in case when millisec is 0 or osErrorTimeout if millisec is not osWaitForever.
103107
*/
104-
MBED_DEPRECATED_SINCE("mbed-os-5.10.0", "Replaced with trylock_for and lock(void) functions")
108+
MBED_DEPRECATED_SINCE("mbed-os-5.10.0", "Replaced with lock(), trylock() and trylock_for() functions")
105109
osStatus lock(uint32_t millisec);
106110

107111
/** Try to lock the mutex, and return immediately
@@ -139,9 +143,14 @@ class Mutex : private mbed::NonCopyable<Mutex> {
139143
/**
140144
Unlock the mutex that has previously been locked by the same thread
141145
146+
@return status code that indicates the execution status of the function:
147+
@a osOK the mutex has been released.
148+
142149
@note You cannot call this function from ISR context.
150+
@note This function treats RTOS errors as fatal system errors, so can only return osOK.
151+
Use of the return value is deprecated, as the return is expected to become void in the future.
143152
*/
144-
void unlock();
153+
osStatus unlock();
145154

146155
/** Get the owner the this mutex
147156
@return the current owner of this mutex.

0 commit comments

Comments
 (0)