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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.