Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ScopedLock.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2018 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef MBED_SCOPEDLOCK_H 00017 #define MBED_SCOPEDLOCK_H 00018 00019 #include "platform/NonCopyable.h" 00020 00021 namespace mbed { 00022 00023 /** \addtogroup platform */ 00024 /** @{*/ 00025 /** 00026 * \defgroup platform_ScopedLock ScopedLock functions 00027 * @{ 00028 */ 00029 00030 /** RAII-style mechanism for owning a lock of Lockable object for the duration of a scoped block 00031 * 00032 * @tparam Lockable The type implementing BasicLockable concept 00033 * 00034 * @note For type Lockable to be BasicLockable, the following conditions have to be satisfied: 00035 * - has public member function @a lock which blocks until a lock can be obtained for the current execution context 00036 * - has public member function @a unlock which releases the lock 00037 * 00038 * Usage: 00039 * 00040 * Example with rtos::Mutex 00041 * 00042 * @code 00043 * void foo(Mutex &m) { 00044 * ScopedLock<Mutex> lock(m); 00045 * // Mutex lock protects code in this block 00046 * } 00047 * @endcode 00048 * 00049 * 00050 * More generic example 00051 * 00052 * @code 00053 * template<typename Lockable> 00054 * void foo(Lockable& lockable) { 00055 * ScopedLock<Lockable> lock(lockable); 00056 * // Code in this block runs under lock 00057 * } 00058 * @endcode 00059 */ 00060 template <typename Lockable> 00061 class ScopedLock : private NonCopyable<ScopedLock<Lockable> > { 00062 public: 00063 /** Locks given locable object 00064 * 00065 * @param lockable reference to the instance of Lockable object 00066 * @note lockable object should outlive the ScopedLock object 00067 */ 00068 ScopedLock(Lockable& lockable): _lockable(lockable) 00069 { 00070 _lockable.lock(); 00071 } 00072 00073 ~ScopedLock() 00074 { 00075 _lockable.unlock(); 00076 } 00077 private: 00078 Lockable& _lockable; 00079 }; 00080 00081 /**@}*/ 00082 00083 /**@}*/ 00084 00085 } // embed 00086 00087 #endif // MBED_SCOPEDLOCK_H
Generated on Tue Jul 12 2022 12:22:19 by
