mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CriticalSectionLock.h Source File

CriticalSectionLock.h

00001 /*
00002  * Copyright (c) 2017 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
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_CRITICALSECTIONLOCK_H
00019 #define MBED_CRITICALSECTIONLOCK_H
00020 
00021 #include "platform/mbed_critical.h"
00022 #include "platform/mbed_toolchain.h"
00023 
00024 namespace mbed {
00025 
00026 /** \addtogroup platform */
00027 /** @{*/
00028 /**
00029  * \defgroup platform_CriticalSectionLock CriticalSectionLock functions
00030  * @{
00031  */
00032 
00033 /** RAII object for disabling, then restoring, interrupt state
00034   * Usage:
00035   * @code
00036   *
00037   * // RAII style usage
00038   * unsigned int atomic_counter_increment(unsigned int &counter) {
00039   *     CriticalSectionLock lock;
00040   *     // Code in this block will run with interrupts disabled
00041   *     // Interrupts will be restored to their previous state automatically
00042   *     // at the end of function scope
00043   *     return ++counter;
00044   * }
00045   *
00046   * // free locking usage
00047   * unsigned int atomic_counter_decrement(unsigned int &counter) {
00048   *     CriticalSectionLock::enable();
00049   *     // Code in this block will run with interrupts disabled
00050   *     counter--;
00051   *     CriticalSectionLock::disable(); // need explicitly to disable critical section lock
00052   *     // interrupts will be restored to their previous state here
00053   *     return counter;
00054   * }
00055   *
00056   * @endcode
00057   */
00058 class CriticalSectionLock {
00059 public:
00060     CriticalSectionLock()
00061     {
00062         core_util_critical_section_enter();
00063     }
00064 
00065     ~CriticalSectionLock()
00066     {
00067         core_util_critical_section_exit();
00068     }
00069 
00070     /** Mark the start of a critical section
00071      *  @deprecated This function is inconsistent with RAII and is being removed in the future. Replaced by static function CriticalSectionLock::enable.
00072      *
00073      */
00074     MBED_DEPRECATED_SINCE("mbed-os-5.8",
00075                           "This function is inconsistent with RAII and is being removed in the future."
00076                           "Replaced by static function CriticalSectionLock::enable.")
00077     void lock()
00078     {
00079         core_util_critical_section_enter();
00080     }
00081 
00082     /** Mark the end of a critical section
00083      *  @deprecated This function is inconsistent with RAII and is being removed in the future. Replaced by static function CriticalSectionLock::enable.
00084      *
00085      */
00086     MBED_DEPRECATED_SINCE("mbed-os-5.8",
00087                           "This function is inconsistent with RAII and is being removed in the future."
00088                           "Replaced by static function CriticalSectionLock::disable.")
00089     void unlock()
00090     {
00091         core_util_critical_section_exit();
00092     }
00093 
00094     /** Mark the start of a critical section
00095      */
00096     static void enable()
00097     {
00098         core_util_critical_section_enter();
00099     }
00100 
00101     /** Mark the end of a critical section
00102      */
00103     static void disable()
00104     {
00105         core_util_critical_section_exit();
00106     }
00107 };
00108 
00109 /**@}*/
00110 
00111 /**@}*/
00112 
00113 } // namespace mbed
00114 
00115 #endif