Mistake on this page?
Report an issue in GitHub or email us
LockGuard.h
1 /*
2  * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may
6  * 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, WITHOUT
13  * 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 
18 #ifndef MBEDMICRO_RTOS_MBED_THREADS_LOCK_GUARD
19 #define MBEDMICRO_RTOS_MBED_THREADS_LOCK_GUARD
20 
21 #include <rtos.h>
22 
23 /**
24  * RAII mutex locker.
25  * The mutex pass in the constructor will be locked for the lifetime of
26  * the LockGuard instance.
27  */
28 class LockGuard {
29 public:
30  /**
31  * Construct a LockGuard instance and ackire ownership of mutex in input.
32  * @param mutex The mutex to ackire ownership of.
33  */
34  LockGuard(rtos::Mutex &mutex) : _mutex(mutex)
35  {
36  _mutex.lock();
37  }
38 
39  /**
40  * Destruct the lock and release the inner mutex.
41  */
43  {
44  _mutex.unlock();
45  }
46 
47 private:
48  LockGuard(const LockGuard &);
49  LockGuard &operator=(const LockGuard &);
50  rtos::Mutex &_mutex;
51 };
52 
53 #endif /* MBEDMICRO_RTOS_MBED_THREADS_LOCK_GUARD */
RAII mutex locker.
Definition: LockGuard.h:28
~LockGuard()
Destruct the lock and release the inner mutex.
Definition: LockGuard.h:42
LockGuard(rtos::Mutex &mutex)
Construct a LockGuard instance and ackire ownership of mutex in input.
Definition: LockGuard.h:34
void unlock()
Unlock the mutex that has previously been locked by the same thread.
Definition: Mutex.h:221
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:68
void lock()
Wait until a Mutex becomes available.
Definition: Mutex.h:202
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.