Skip to content

Commit 07fce41

Browse files
authored
Merge pull request #5852 from maciejbocianski/scoped_lock
ScopedLock implementation
2 parents 3d815de + 1d35cce commit 07fce41

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
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+
* // Mutex lock protects code in this block
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 runs 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

rtos/Mutex.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,25 @@
2828
#include "mbed_rtos_storage.h"
2929

3030
#include "platform/NonCopyable.h"
31+
#include "platform/ScopedLock.h"
3132

3233
namespace rtos {
3334
/** \addtogroup rtos */
3435
/** @{*/
36+
37+
class Mutex;
38+
/** Typedef for the mutex lock
39+
*
40+
* Usage:
41+
* @code
42+
* void foo(Mutex &m) {
43+
* ScopedMutexLock lock(m);
44+
* // Mutex lock protects code in this block
45+
* }
46+
* @endcode
47+
*/
48+
typedef mbed::ScopedLock<Mutex> ScopedMutexLock;
49+
3550
/**
3651
* \defgroup rtos_Mutex Mutex class
3752
* @{
@@ -44,7 +59,7 @@ namespace rtos {
4459
ISR handler, consider using @a Semaphore.
4560
4661
@note
47-
Memory considerations: The mutex control structures will be created on current thread's stack, both for the mbed OS
62+
Memory considerations: The mutex control structures will be created on current thread's stack, both for the Mbed OS
4863
and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
4964
*/
5065
class Mutex : private mbed::NonCopyable<Mutex> {

0 commit comments

Comments
 (0)