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