BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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