Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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