|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2018 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#ifndef MBED_SCOPEDLOCK_H |
| 17 | +#define MBED_SCOPEDLOCK_H |
| 18 | + |
| 19 | +#include "platform/NonCopyable.h" |
| 20 | + |
| 21 | +namespace mbed { |
| 22 | + |
| 23 | +/** \addtogroup platform */ |
| 24 | +/** @{*/ |
| 25 | +/** |
| 26 | + * \defgroup platform_ScopedLock ScopedLock functions |
| 27 | + * @{ |
| 28 | + */ |
| 29 | + |
| 30 | +/** RAII-style mechanism for owning a lock of Lockable object for the duration of a scoped block |
| 31 | + * |
| 32 | + * @tparam Lockable The type implementing BasicLockable concept |
| 33 | + * |
| 34 | + * @note For type Lockable to be BasicLockable, the following conditions have to be satisfied: |
| 35 | + * - has public member function @a lock which blocks until a lock can be obtained for the current execution context |
| 36 | + * - has public member function @a unlock which releases the lock |
| 37 | + * |
| 38 | + * Usage: |
| 39 | + * |
| 40 | + * Example with rtos::Mutex |
| 41 | + * |
| 42 | + * @code |
| 43 | + * void foo(Mutex &m) { |
| 44 | + * ScopedLock<Mutex> lock(m); |
| 45 | + * // Mutex lock protects code in this block |
| 46 | + * } |
| 47 | + * @endcode |
| 48 | + * |
| 49 | + * |
| 50 | + * More generic example |
| 51 | + * |
| 52 | + * @code |
| 53 | + * template<typename Lockable> |
| 54 | + * void foo(Lockable& lockable) { |
| 55 | + * ScopedLock<Lockable> lock(lockable); |
| 56 | + * // Code in this block runs under lock |
| 57 | + * } |
| 58 | + * @endcode |
| 59 | + */ |
| 60 | +template <typename Lockable> |
| 61 | +class ScopedLock : private NonCopyable<ScopedLock<Lockable> > { |
| 62 | +public: |
| 63 | + /** Locks given locable object |
| 64 | + * |
| 65 | + * @param lockable reference to the instance of Lockable object |
| 66 | + * @note lockable object should outlive the ScopedLock object |
| 67 | + */ |
| 68 | + ScopedLock(Lockable& lockable): _lockable(lockable) |
| 69 | + { |
| 70 | + _lockable.lock(); |
| 71 | + } |
| 72 | + |
| 73 | + ~ScopedLock() |
| 74 | + { |
| 75 | + _lockable.unlock(); |
| 76 | + } |
| 77 | +private: |
| 78 | + Lockable& _lockable; |
| 79 | +}; |
| 80 | + |
| 81 | +/**@}*/ |
| 82 | + |
| 83 | +/**@}*/ |
| 84 | + |
| 85 | +} // embed |
| 86 | + |
| 87 | +#endif // MBED_SCOPEDLOCK_H |
0 commit comments