mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
ScopedLock.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2018 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 #ifndef MBED_SCOPEDLOCK_H 00018 #define MBED_SCOPEDLOCK_H 00019 00020 #include "platform/NonCopyable.h" 00021 00022 namespace mbed { 00023 00024 /** \addtogroup platform */ 00025 /** @{*/ 00026 /** 00027 * \defgroup platform_ScopedLock ScopedLock functions 00028 * @{ 00029 */ 00030 00031 /** RAII-style mechanism for owning a lock of Lockable object for the duration of a scoped block 00032 * 00033 * @tparam Lockable The type implementing BasicLockable concept 00034 * 00035 * @note For type Lockable to be BasicLockable, the following conditions have to be satisfied: 00036 * - has public member function @a lock which blocks until a lock can be obtained for the current execution context 00037 * - has public member function @a unlock which releases the lock 00038 * 00039 * Usage: 00040 * 00041 * Example with rtos::Mutex 00042 * 00043 * @code 00044 * void foo(Mutex &m) { 00045 * ScopedLock<Mutex> lock(m); 00046 * // Mutex lock protects code in this block 00047 * } 00048 * @endcode 00049 * 00050 * 00051 * More generic example 00052 * 00053 * @code 00054 * template<typename Lockable> 00055 * void foo(Lockable& lockable) { 00056 * ScopedLock<Lockable> lock(lockable); 00057 * // Code in this block runs under lock 00058 * } 00059 * @endcode 00060 */ 00061 template <typename Lockable> 00062 class ScopedLock : private NonCopyable<ScopedLock<Lockable> > { 00063 public: 00064 /** Locks given lockable object 00065 * 00066 * @param lockable reference to the instance of Lockable object 00067 * @note lockable object should outlive the ScopedLock object 00068 */ 00069 ScopedLock(Lockable &lockable): _lockable(lockable) 00070 { 00071 _lockable.lock(); 00072 } 00073 00074 ~ScopedLock() 00075 { 00076 _lockable.unlock(); 00077 } 00078 private: 00079 Lockable &_lockable; 00080 }; 00081 00082 /**@}*/ 00083 00084 /**@}*/ 00085 00086 } // embed 00087 00088 #endif // MBED_SCOPEDLOCK_H
Generated on Tue Jul 12 2022 20:41:15 by 1.7.2