init

Dependencies:   mbed

Committer:
Nathan Yonkee
Date:
Fri Mar 02 07:16:49 2018 -0700
Revision:
10:46a4cf51ee38
Parent:
9:d58e77ebd769
remove mbed-os

Who changed what in which revision?

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