Roy Want / Mbed OS beaconCompileReadyFork
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CriticalSectionLock.h Source File

CriticalSectionLock.h

00001 /*
00002  * PackageLicenseDeclared: Apache-2.0
00003  * Copyright (c) 2015 ARM Limited
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef __MBED_UTIL_CRITICAL_SECTION_LOCK_H__
00019 #define __MBED_UTIL_CRITICAL_SECTION_LOCK_H__
00020 
00021 #include <stdint.h>
00022 #include "cmsis.h"
00023 
00024 namespace mbed {
00025 namespace util {
00026 
00027 /** RAII object for disabling, then restoring, interrupt state
00028   * Usage:
00029   * @code
00030   *
00031   * void f() {
00032   *     // some code here
00033   *     {
00034   *         CriticalSectionLock lock;
00035   *         // Code in this block will run with interrupts disabled
00036   *     }
00037   *     // interrupts will be restored to their previous state
00038   * }
00039   * @endcode
00040   */
00041 class CriticalSectionLock {
00042 public:
00043     CriticalSectionLock() {
00044         _state = __get_PRIMASK();
00045         __disable_irq();
00046     }
00047 
00048     ~CriticalSectionLock() {
00049         __set_PRIMASK(_state);
00050     }
00051 
00052 private:
00053     uint32_t _state;
00054 };
00055 
00056 } // namespace util
00057 } // namespace mbed
00058 
00059 #endif // #ifndef __MBED_UTIL_CRITICAL_SECTION_LOCK_H__