Skip to content

Commit a3c9c12

Browse files
platform: Add ScopedLock
1 parent a5403fd commit a3c9c12

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

mbed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
// mbed Non-hardware components
9696
#include "platform/Callback.h"
9797
#include "platform/FunctionPointer.h"
98+
#include "platform/ScopedLock.h"
9899

99100
using namespace mbed;
100101
using namespace std;

platform/ScopedLock.h

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

0 commit comments

Comments
 (0)