Skip to content

Commit a7ff058

Browse files
committed
Merge pull request #1804 from c1728p9/dev_critical
Add a c based re-entrant critical section API
2 parents 60c862c + 462c917 commit a7ff058

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

hal/api/critical.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* 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, WITHOUT
13+
* 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_UTIL_CRITICAL_H__
19+
#define __MBED_UTIL_CRITICAL_H__
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
/** Mark the start of a critical section
26+
*
27+
* This function should be called to mark the start of a critical section of code.
28+
* \note
29+
* NOTES:
30+
* 1) The use of this style of critical section is targetted at C based implementations.
31+
* 2) These critical sections can be nested.
32+
* 3) The interrupt enable state on entry to the first critical section (of a nested set, or single
33+
* section) will be preserved on exit from the section.
34+
* 4) This implementation will currently only work on code running in privileged mode.
35+
*/
36+
void core_util_critical_section_enter();
37+
38+
/** Mark the end of a critical section
39+
*
40+
* This function should be called to mark the end of a critical section of code.
41+
* \note
42+
* NOTES:
43+
* 1) The use of this style of critical section is targetted at C based implementations.
44+
* 2) These critical sections can be nested.
45+
* 3) The interrupt enable state on entry to the first critical section (of a nested set, or single
46+
* section) will be preserved on exit from the section.
47+
* 4) This implementation will currently only work on code running in privileged mode.
48+
*/
49+
void core_util_critical_section_exit();
50+
51+
#ifdef __cplusplus
52+
} // extern "C"
53+
#endif
54+
55+
56+
#endif // __MBED_UTIL_CRITICAL_H__

hal/common/critical.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* 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, WITHOUT
13+
* 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+
19+
#include <stdint.h>
20+
#include <stddef.h>
21+
#include "cmsis.h"
22+
#include <assert.h>
23+
24+
// Module include
25+
#include "critical.h"
26+
27+
static volatile uint32_t interrupt_enable_counter = 0;
28+
static volatile uint32_t critical_primask = 0;
29+
30+
void core_util_critical_section_enter()
31+
{
32+
uint32_t primask = __get_PRIMASK(); /* get the current interrupt enabled state */
33+
__disable_irq();
34+
35+
/* Save the interrupt enabled state as it was prior to any nested critical section lock use */
36+
if (!interrupt_enable_counter) {
37+
critical_primask = primask & 0x1;
38+
}
39+
40+
/* If the interrupt_enable_counter overflows or we are in a nested critical section and interrupts
41+
are enabled, then something has gone badly wrong thus assert an error.
42+
*/
43+
assert(interrupt_enable_counter < UINT32_MAX);
44+
if (interrupt_enable_counter > 0) {
45+
assert(primask & 0x1);
46+
}
47+
interrupt_enable_counter++;
48+
}
49+
50+
void core_util_critical_section_exit()
51+
{
52+
/* If critical_section_enter has not previously been called, do nothing */
53+
if (interrupt_enable_counter) {
54+
55+
uint32_t primask = __get_PRIMASK(); /* get the current interrupt enabled state */
56+
57+
assert(primask & 0x1); /* Interrupts must be disabled on invoking an exit from a critical section */
58+
59+
interrupt_enable_counter--;
60+
61+
/* Only re-enable interrupts if we are exiting the last of the nested critical sections and
62+
interrupts were enabled on entry to the first critical section.
63+
*/
64+
if (!interrupt_enable_counter && !critical_primask) {
65+
__enable_irq();
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)