Roy Want / Mbed OS beaconCompileReadyFork
Committer:
roywant
Date:
Mon Sep 19 00:59:11 2016 +0000
Revision:
0:ed0152b5c495
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
roywant 0:ed0152b5c495 1 /*
roywant 0:ed0152b5c495 2 * PackageLicenseDeclared: Apache-2.0
roywant 0:ed0152b5c495 3 * Copyright (c) 2015 ARM Limited
roywant 0:ed0152b5c495 4 *
roywant 0:ed0152b5c495 5 * Licensed under the Apache License, Version 2.0 (the "License");
roywant 0:ed0152b5c495 6 * you may not use this file except in compliance with the License.
roywant 0:ed0152b5c495 7 * You may obtain a copy of the License at
roywant 0:ed0152b5c495 8 *
roywant 0:ed0152b5c495 9 * http://www.apache.org/licenses/LICENSE-2.0
roywant 0:ed0152b5c495 10 *
roywant 0:ed0152b5c495 11 * Unless required by applicable law or agreed to in writing, software
roywant 0:ed0152b5c495 12 * distributed under the License is distributed on an "AS IS" BASIS,
roywant 0:ed0152b5c495 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
roywant 0:ed0152b5c495 14 * See the License for the specific language governing permissions and
roywant 0:ed0152b5c495 15 * limitations under the License.
roywant 0:ed0152b5c495 16 */
roywant 0:ed0152b5c495 17
roywant 0:ed0152b5c495 18 #ifndef __MBED_UTIL_CRITICAL_SECTION_LOCK_H__
roywant 0:ed0152b5c495 19 #define __MBED_UTIL_CRITICAL_SECTION_LOCK_H__
roywant 0:ed0152b5c495 20
roywant 0:ed0152b5c495 21 #include <stdint.h>
roywant 0:ed0152b5c495 22 #include "cmsis.h"
roywant 0:ed0152b5c495 23
roywant 0:ed0152b5c495 24 namespace mbed {
roywant 0:ed0152b5c495 25 namespace util {
roywant 0:ed0152b5c495 26
roywant 0:ed0152b5c495 27 /** RAII object for disabling, then restoring, interrupt state
roywant 0:ed0152b5c495 28 * Usage:
roywant 0:ed0152b5c495 29 * @code
roywant 0:ed0152b5c495 30 *
roywant 0:ed0152b5c495 31 * void f() {
roywant 0:ed0152b5c495 32 * // some code here
roywant 0:ed0152b5c495 33 * {
roywant 0:ed0152b5c495 34 * CriticalSectionLock lock;
roywant 0:ed0152b5c495 35 * // Code in this block will run with interrupts disabled
roywant 0:ed0152b5c495 36 * }
roywant 0:ed0152b5c495 37 * // interrupts will be restored to their previous state
roywant 0:ed0152b5c495 38 * }
roywant 0:ed0152b5c495 39 * @endcode
roywant 0:ed0152b5c495 40 */
roywant 0:ed0152b5c495 41 class CriticalSectionLock {
roywant 0:ed0152b5c495 42 public:
roywant 0:ed0152b5c495 43 CriticalSectionLock() {
roywant 0:ed0152b5c495 44 _state = __get_PRIMASK();
roywant 0:ed0152b5c495 45 __disable_irq();
roywant 0:ed0152b5c495 46 }
roywant 0:ed0152b5c495 47
roywant 0:ed0152b5c495 48 ~CriticalSectionLock() {
roywant 0:ed0152b5c495 49 __set_PRIMASK(_state);
roywant 0:ed0152b5c495 50 }
roywant 0:ed0152b5c495 51
roywant 0:ed0152b5c495 52 private:
roywant 0:ed0152b5c495 53 uint32_t _state;
roywant 0:ed0152b5c495 54 };
roywant 0:ed0152b5c495 55
roywant 0:ed0152b5c495 56 } // namespace util
roywant 0:ed0152b5c495 57 } // namespace mbed
roywant 0:ed0152b5c495 58
roywant 0:ed0152b5c495 59 #endif // #ifndef __MBED_UTIL_CRITICAL_SECTION_LOCK_H__