Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ScopedLock.h Source File

ScopedLock.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018-2019 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #ifndef MBED_SCOPEDLOCK_H
00018 #define MBED_SCOPEDLOCK_H
00019 
00020 #include "platform/NonCopyable.h"
00021 
00022 namespace mbed {
00023 
00024 /** \addtogroup platform-public-api */
00025 /** @{*/
00026 
00027 /**
00028  * \defgroup platform_ScopedLock ScopedLock functions
00029  * @{
00030  */
00031 
00032 /** RAII-style mechanism for owning a lock of Lockable object for the duration of a scoped block
00033  *
00034  * @tparam Lockable The type implementing BasicLockable concept
00035  *
00036  * @note For type Lockable to be BasicLockable, the following conditions have to be satisfied:
00037  *        - has public member function @a lock which blocks until a lock can be obtained for the current execution context
00038  *        - has public member function @a unlock which releases the lock
00039  *
00040  * Usage:
00041  *
00042  * Example with rtos::Mutex
00043  *
00044  * @code
00045  * void foo(Mutex &m) {
00046  *     ScopedLock<Mutex> lock(m);
00047  *     // Mutex lock protects code in this block
00048  * }
00049  * @endcode
00050  *
00051  *
00052  * More generic example
00053  *
00054  * @code
00055  * template<typename Lockable>
00056  * void foo(Lockable& lockable) {
00057  *     ScopedLock<Lockable> lock(lockable);
00058  *     // Code in this block runs under lock
00059  * }
00060  * @endcode
00061  */
00062 template <typename Lockable>
00063 class ScopedLock : private NonCopyable<ScopedLock<Lockable> > {
00064 public:
00065     /** Locks given lockable object
00066      *
00067      * @param lockable reference to the instance of Lockable object
00068      * @note lockable object should outlive the ScopedLock object
00069      */
00070     ScopedLock(Lockable &lockable): _lockable(lockable)
00071     {
00072         _lockable.lock();
00073     }
00074 
00075     ~ScopedLock()
00076     {
00077         _lockable.unlock();
00078     }
00079 private:
00080     Lockable &_lockable;
00081 };
00082 
00083 /**@}*/
00084 
00085 /**@}*/
00086 
00087 } // embed
00088 
00089 #endif // MBED_SCOPEDLOCK_H