init

Dependencies:   mbed

Committer:
Nathan Yonkee
Date:
Fri Mar 02 07:16:49 2018 -0700
Revision:
10:46a4cf51ee38
Parent:
9:d58e77ebd769
remove mbed-os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nathan Yonkee 9:d58e77ebd769 1 /*
Nathan Yonkee 9:d58e77ebd769 2 * PackageLicenseDeclared: Apache-2.0
Nathan Yonkee 9:d58e77ebd769 3 * Copyright (c) 2017 ARM Limited
Nathan Yonkee 9:d58e77ebd769 4 *
Nathan Yonkee 9:d58e77ebd769 5 * Licensed under the Apache License, Version 2.0 (the "License");
Nathan Yonkee 9:d58e77ebd769 6 * you may not use this file except in compliance with the License.
Nathan Yonkee 9:d58e77ebd769 7 * You may obtain a copy of the License at
Nathan Yonkee 9:d58e77ebd769 8 *
Nathan Yonkee 9:d58e77ebd769 9 * http://www.apache.org/licenses/LICENSE-2.0
Nathan Yonkee 9:d58e77ebd769 10 *
Nathan Yonkee 9:d58e77ebd769 11 * Unless required by applicable law or agreed to in writing, software
Nathan Yonkee 9:d58e77ebd769 12 * distributed under the License is distributed on an "AS IS" BASIS,
Nathan Yonkee 9:d58e77ebd769 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Nathan Yonkee 9:d58e77ebd769 14 * See the License for the specific language governing permissions and
Nathan Yonkee 9:d58e77ebd769 15 * limitations under the License.
Nathan Yonkee 9:d58e77ebd769 16 */
Nathan Yonkee 9:d58e77ebd769 17
Nathan Yonkee 9:d58e77ebd769 18 #ifndef MBED_CRITICALSECTIONLOCK_H
Nathan Yonkee 9:d58e77ebd769 19 #define MBED_CRITICALSECTIONLOCK_H
Nathan Yonkee 9:d58e77ebd769 20
Nathan Yonkee 9:d58e77ebd769 21 #include "platform/mbed_critical.h"
Nathan Yonkee 9:d58e77ebd769 22 #include "platform/mbed_toolchain.h"
Nathan Yonkee 9:d58e77ebd769 23
Nathan Yonkee 9:d58e77ebd769 24 namespace mbed {
Nathan Yonkee 9:d58e77ebd769 25
Nathan Yonkee 9:d58e77ebd769 26 /** \addtogroup platform */
Nathan Yonkee 9:d58e77ebd769 27 /** @{*/
Nathan Yonkee 9:d58e77ebd769 28 /**
Nathan Yonkee 9:d58e77ebd769 29 * \defgroup platform_CriticalSectionLock CriticalSectionLock functions
Nathan Yonkee 9:d58e77ebd769 30 * @{
Nathan Yonkee 9:d58e77ebd769 31 */
Nathan Yonkee 9:d58e77ebd769 32
Nathan Yonkee 9:d58e77ebd769 33 /** RAII object for disabling, then restoring, interrupt state
Nathan Yonkee 9:d58e77ebd769 34 * Usage:
Nathan Yonkee 9:d58e77ebd769 35 * @code
Nathan Yonkee 9:d58e77ebd769 36 *
Nathan Yonkee 9:d58e77ebd769 37 * // RAII style usage
Nathan Yonkee 9:d58e77ebd769 38 * unsigned int atomic_counter_increment(unsigned int &counter) {
Nathan Yonkee 9:d58e77ebd769 39 * CriticalSectionLock lock;
Nathan Yonkee 9:d58e77ebd769 40 * // Code in this block will run with interrupts disabled
Nathan Yonkee 9:d58e77ebd769 41 * // Interrupts will be restored to their previous state automatically
Nathan Yonkee 9:d58e77ebd769 42 * // at the end of function scope
Nathan Yonkee 9:d58e77ebd769 43 * return ++counter;
Nathan Yonkee 9:d58e77ebd769 44 * }
Nathan Yonkee 9:d58e77ebd769 45 *
Nathan Yonkee 9:d58e77ebd769 46 * // free locking usage
Nathan Yonkee 9:d58e77ebd769 47 * unsigned int atomic_counter_decrement(unsigned int &counter) {
Nathan Yonkee 9:d58e77ebd769 48 * CriticalSectionLock::enable();
Nathan Yonkee 9:d58e77ebd769 49 * // Code in this block will run with interrupts disabled
Nathan Yonkee 9:d58e77ebd769 50 * counter--;
Nathan Yonkee 9:d58e77ebd769 51 * CriticalSectionLock::disable(); // need explicitly to disable critical section lock
Nathan Yonkee 9:d58e77ebd769 52 * // interrupts will be restored to their previous state here
Nathan Yonkee 9:d58e77ebd769 53 * return counter;
Nathan Yonkee 9:d58e77ebd769 54 * }
Nathan Yonkee 9:d58e77ebd769 55 *
Nathan Yonkee 9:d58e77ebd769 56 * @endcode
Nathan Yonkee 9:d58e77ebd769 57 */
Nathan Yonkee 9:d58e77ebd769 58 class CriticalSectionLock {
Nathan Yonkee 9:d58e77ebd769 59 public:
Nathan Yonkee 9:d58e77ebd769 60 CriticalSectionLock()
Nathan Yonkee 9:d58e77ebd769 61 {
Nathan Yonkee 9:d58e77ebd769 62 core_util_critical_section_enter();
Nathan Yonkee 9:d58e77ebd769 63 }
Nathan Yonkee 9:d58e77ebd769 64
Nathan Yonkee 9:d58e77ebd769 65 ~CriticalSectionLock()
Nathan Yonkee 9:d58e77ebd769 66 {
Nathan Yonkee 9:d58e77ebd769 67 core_util_critical_section_exit();
Nathan Yonkee 9:d58e77ebd769 68 }
Nathan Yonkee 9:d58e77ebd769 69
Nathan Yonkee 9:d58e77ebd769 70 /** Mark the start of a critical section
Nathan Yonkee 9:d58e77ebd769 71 *
Nathan Yonkee 9:d58e77ebd769 72 */
Nathan Yonkee 9:d58e77ebd769 73 MBED_DEPRECATED_SINCE("mbed-os-5.8",
Nathan Yonkee 9:d58e77ebd769 74 "This function is inconsistent with RAII and is being removed in the future."
Nathan Yonkee 9:d58e77ebd769 75 "Replaced by static function CriticalSectionLock::enable.")
Nathan Yonkee 9:d58e77ebd769 76 void lock()
Nathan Yonkee 9:d58e77ebd769 77 {
Nathan Yonkee 9:d58e77ebd769 78 core_util_critical_section_enter();
Nathan Yonkee 9:d58e77ebd769 79 }
Nathan Yonkee 9:d58e77ebd769 80
Nathan Yonkee 9:d58e77ebd769 81 /** Mark the end of a critical section
Nathan Yonkee 9:d58e77ebd769 82 *
Nathan Yonkee 9:d58e77ebd769 83 */
Nathan Yonkee 9:d58e77ebd769 84 MBED_DEPRECATED_SINCE("mbed-os-5.8",
Nathan Yonkee 9:d58e77ebd769 85 "This function is inconsistent with RAII and is being removed in the future."
Nathan Yonkee 9:d58e77ebd769 86 "Replaced by static function CriticalSectionLock::disable.")
Nathan Yonkee 9:d58e77ebd769 87 void unlock()
Nathan Yonkee 9:d58e77ebd769 88 {
Nathan Yonkee 9:d58e77ebd769 89 core_util_critical_section_exit();
Nathan Yonkee 9:d58e77ebd769 90 }
Nathan Yonkee 9:d58e77ebd769 91
Nathan Yonkee 9:d58e77ebd769 92 /** Mark the start of a critical section
Nathan Yonkee 9:d58e77ebd769 93 */
Nathan Yonkee 9:d58e77ebd769 94 static void enable()
Nathan Yonkee 9:d58e77ebd769 95 {
Nathan Yonkee 9:d58e77ebd769 96 core_util_critical_section_enter();
Nathan Yonkee 9:d58e77ebd769 97 }
Nathan Yonkee 9:d58e77ebd769 98
Nathan Yonkee 9:d58e77ebd769 99 /** Mark the end of a critical section
Nathan Yonkee 9:d58e77ebd769 100 */
Nathan Yonkee 9:d58e77ebd769 101 static void disable()
Nathan Yonkee 9:d58e77ebd769 102 {
Nathan Yonkee 9:d58e77ebd769 103 core_util_critical_section_exit();
Nathan Yonkee 9:d58e77ebd769 104 }
Nathan Yonkee 9:d58e77ebd769 105 };
Nathan Yonkee 9:d58e77ebd769 106
Nathan Yonkee 9:d58e77ebd769 107 /**@}*/
Nathan Yonkee 9:d58e77ebd769 108
Nathan Yonkee 9:d58e77ebd769 109 /**@}*/
Nathan Yonkee 9:d58e77ebd769 110
Nathan Yonkee 9:d58e77ebd769 111 } // namespace mbed
Nathan Yonkee 9:d58e77ebd769 112
Nathan Yonkee 9:d58e77ebd769 113 #endif