Skip to content

Commit ad67904

Browse files
platform: CriticalSectionLock class improvement
1 parent 9cce4d2 commit ad67904

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

platform/CriticalSectionLock.h

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,25 @@ namespace mbed {
3333
* Usage:
3434
* @code
3535
*
36-
* void f() {
37-
* // some code here
38-
* {
39-
* CriticalSectionLock lock;
40-
* // Code in this block will run with interrupts disabled
41-
* }
42-
* // interrupts will be restored to their previous state
36+
* // RAII style usage
37+
* unsigned int atomic_counter_increment(unsigned int &counter) {
38+
* CriticalSectionLock lock;
39+
* // Code in this block will run with interrupts disabled
40+
* // Interrupts will be restored to their previous state automatically
41+
* // at the end of function scope
42+
* return ++counter;
4343
* }
44+
*
45+
* // free locking usage
46+
* unsigned int atomic_counter_decrement(unsigned int &counter) {
47+
* CriticalSectionLock::enable();
48+
* // Code in this block will run with interrupts disabled
49+
* counter--;
50+
* CriticalSectionLock::disable(); // need explicitly to disable critical section lock
51+
* // interrupts will be restored to their previous state here
52+
* return counter;
53+
* }
54+
*
4455
* @endcode
4556
*/
4657
class CriticalSectionLock {
@@ -56,20 +67,40 @@ class CriticalSectionLock {
5667
}
5768

5869
/** Mark the start of a critical section
59-
*
70+
*
6071
*/
72+
MBED_DEPRECATED_SINCE("mbed-os-5.8",
73+
"This function is inconsistent with RAII and is being removed in the future."
74+
"Replaced by static function CriticalSectionLock::enable.")
6175
void lock()
6276
{
6377
core_util_critical_section_enter();
6478
}
6579

6680
/** Mark the end of a critical section
67-
*
81+
*
6882
*/
83+
MBED_DEPRECATED_SINCE("mbed-os-5.8",
84+
"This function is inconsistent with RAII and is being removed in the future."
85+
"Replaced by static function CriticalSectionLock::disable.")
6986
void unlock()
7087
{
7188
core_util_critical_section_exit();
7289
}
90+
91+
/** Mark the start of a critical section
92+
*/
93+
static void enable()
94+
{
95+
core_util_critical_section_enter();
96+
}
97+
98+
/** Mark the end of a critical section
99+
*/
100+
static void disable()
101+
{
102+
core_util_critical_section_exit();
103+
}
73104
};
74105

75106
/**@}*/

0 commit comments

Comments
 (0)