Denislam Valeev / Mbed OS Nucleo_rtos_basic
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CriticalSectionLock.h Source File

CriticalSectionLock.h

00001 /*
00002  * PackageLicenseDeclared: Apache-2.0
00003  * Copyright (c) 2017 ARM Limited
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      *
00072      */
00073     MBED_DEPRECATED_SINCE("mbed-os-5.8",
00074             "This function is inconsistent with RAII and is being removed in the future."
00075             "Replaced by static function CriticalSectionLock::enable.")
00076     void lock()
00077     {
00078         core_util_critical_section_enter();
00079     }
00080 
00081     /** Mark the end of a critical section
00082      *
00083      */
00084     MBED_DEPRECATED_SINCE("mbed-os-5.8",
00085             "This function is inconsistent with RAII and is being removed in the future."
00086             "Replaced by static function CriticalSectionLock::disable.")
00087     void unlock()
00088     {
00089         core_util_critical_section_exit();
00090     }
00091 
00092     /** Mark the start of a critical section
00093      */
00094     static void enable()
00095     {
00096         core_util_critical_section_enter();
00097     }
00098 
00099     /** Mark the end of a critical section
00100      */
00101     static void disable()
00102     {
00103         core_util_critical_section_exit();
00104     }
00105 };
00106 
00107 /**@}*/
00108 
00109 /**@}*/
00110 
00111 } // namespace mbed
00112 
00113 #endif