Mistake on this page?
Report an issue in GitHub or email us
ScopedLock.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2018 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 */
25 /** @{*/
26 /**
27  * \defgroup platform_ScopedLock ScopedLock functions
28  * @{
29  */
30 
31 /** RAII-style mechanism for owning a lock of Lockable object for the duration of a scoped block
32  *
33  * @tparam Lockable The type implementing BasicLockable concept
34  *
35  * @note For type Lockable to be BasicLockable, the following conditions have to be satisfied:
36  * - has public member function @a lock which blocks until a lock can be obtained for the current execution context
37  * - has public member function @a unlock which releases the lock
38  *
39  * Usage:
40  *
41  * Example with rtos::Mutex
42  *
43  * @code
44  * void foo(Mutex &m) {
45  * ScopedLock<Mutex> lock(m);
46  * // Mutex lock protects code in this block
47  * }
48  * @endcode
49  *
50  *
51  * More generic example
52  *
53  * @code
54  * template<typename Lockable>
55  * void foo(Lockable& lockable) {
56  * ScopedLock<Lockable> lock(lockable);
57  * // Code in this block runs under lock
58  * }
59  * @endcode
60  */
61 template <typename Lockable>
62 class ScopedLock : private NonCopyable<ScopedLock<Lockable> > {
63 public:
64  /** Locks given lockable object
65  *
66  * @param lockable reference to the instance of Lockable object
67  * @note lockable object should outlive the ScopedLock object
68  */
69  ScopedLock(Lockable &lockable): _lockable(lockable)
70  {
71  _lockable.lock();
72  }
73 
74  ~ScopedLock()
75  {
76  _lockable.unlock();
77  }
78 private:
79  Lockable &_lockable;
80 };
81 
82 /**@}*/
83 
84 /**@}*/
85 
86 } // embed
87 
88 #endif // MBED_SCOPEDLOCK_H
RAII-style mechanism for owning a lock of Lockable object for the duration of a scoped block...
Definition: ScopedLock.h:62
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:168
ScopedLock(Lockable &lockable)
Locks given lockable object.
Definition: ScopedLock.h:69
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.