Skip to content

Commit 4ccff69

Browse files
authored
Merge pull request #4962 from 0xc0170/dev_critical_section
platform: add CriticalSectionLock
2 parents 5b4e0b3 + be8174f commit 4ccff69

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

mbed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
#include "platform/FileSystemHandle.h"
9090
#include "platform/FileHandle.h"
9191
#include "platform/DirHandle.h"
92+
#include "platform/CriticalSectionLock.h"
9293

9394
// mbed Non-hardware components
9495
#include "platform/Callback.h"

platform/CriticalSectionLock.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* PackageLicenseDeclared: Apache-2.0
3+
* Copyright (c) 2017 ARM Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef MBED_CRITICALSECTIONLOCK_H
19+
#define MBED_CRITICALSECTIONLOCK_H
20+
21+
#include "platform/mbed_critical.h"
22+
23+
namespace mbed {
24+
25+
/** RAII object for disabling, then restoring, interrupt state
26+
* Usage:
27+
* @code
28+
*
29+
* void f() {
30+
* // some code here
31+
* {
32+
* CriticalSectionLock lock;
33+
* // Code in this block will run with interrupts disabled
34+
* }
35+
* // interrupts will be restored to their previous state
36+
* }
37+
* @endcode
38+
*/
39+
class CriticalSectionLock {
40+
public:
41+
CriticalSectionLock()
42+
{
43+
core_util_critical_section_enter();
44+
}
45+
46+
~CriticalSectionLock()
47+
{
48+
core_util_critical_section_exit();
49+
}
50+
51+
/** Mark the start of a critical section
52+
*
53+
*/
54+
void lock()
55+
{
56+
core_util_critical_section_enter();
57+
}
58+
59+
/** Mark the end of a critical section
60+
*
61+
*/
62+
void unlock()
63+
{
64+
core_util_critical_section_exit();
65+
}
66+
};
67+
68+
69+
} // namespace mbed
70+
71+
#endif

0 commit comments

Comments
 (0)