The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Thu Nov 08 11:45:42 2018 +0000
Revision:
171:3a7713b1edbc
Parent:
170:e95d10626187
Child:
172:65be27845400
mbed library. Release version 164

Who changed what in which revision?

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