The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
Anna Bridge
Date:
Fri Jun 22 15:38:59 2018 +0100
Revision:
169:a7c7b631e539
Parent:
165:d1b4690b3f8b
Child:
170:e95d10626187
mbed library. Release version 162

Who changed what in which revision?

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