mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /*
elessair 0:f269e3021894 2 * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved
elessair 0:f269e3021894 3 * SPDX-License-Identifier: Apache-2.0
elessair 0:f269e3021894 4 *
elessair 0:f269e3021894 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
elessair 0:f269e3021894 6 * not use this file except in compliance with the License.
elessair 0:f269e3021894 7 * You may obtain a copy of the License at
elessair 0:f269e3021894 8 *
elessair 0:f269e3021894 9 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 10 *
elessair 0:f269e3021894 11 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
elessair 0:f269e3021894 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 14 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 15 * limitations under the License.
elessair 0:f269e3021894 16 */
elessair 0:f269e3021894 17
elessair 0:f269e3021894 18 #ifndef MBEDMICRO_RTOS_MBED_THREADS_LOCK_GUARD
elessair 0:f269e3021894 19 #define MBEDMICRO_RTOS_MBED_THREADS_LOCK_GUARD
elessair 0:f269e3021894 20
elessair 0:f269e3021894 21 #include <rtos.h>
elessair 0:f269e3021894 22
elessair 0:f269e3021894 23 /**
elessair 0:f269e3021894 24 * RAII mutex locker.
elessair 0:f269e3021894 25 * The mutex pass in the constructor will be locked for the lifetime of
elessair 0:f269e3021894 26 * the LockGuard instance.
elessair 0:f269e3021894 27 */
elessair 0:f269e3021894 28 class LockGuard {
elessair 0:f269e3021894 29 public:
elessair 0:f269e3021894 30 /**
elessair 0:f269e3021894 31 * Construct a LockGuard instance and ackire ownership of mutex in input.
elessair 0:f269e3021894 32 * @param mutex The mutex to ackire ownership of.
elessair 0:f269e3021894 33 */
elessair 0:f269e3021894 34 LockGuard(rtos::Mutex& mutex) : _mutex(mutex) {
elessair 0:f269e3021894 35 _mutex.lock();
elessair 0:f269e3021894 36 }
elessair 0:f269e3021894 37
elessair 0:f269e3021894 38 /**
elessair 0:f269e3021894 39 * Destruct the lock and release the inner mutex.
elessair 0:f269e3021894 40 */
elessair 0:f269e3021894 41 ~LockGuard() {
elessair 0:f269e3021894 42 _mutex.unlock();
elessair 0:f269e3021894 43 }
elessair 0:f269e3021894 44
elessair 0:f269e3021894 45 private:
elessair 0:f269e3021894 46 LockGuard(const LockGuard&);
elessair 0:f269e3021894 47 LockGuard& operator=(const LockGuard&);
elessair 0:f269e3021894 48 rtos::Mutex& _mutex;
elessair 0:f269e3021894 49 };
elessair 0:f269e3021894 50
elessair 0:f269e3021894 51 #endif /* MBEDMICRO_RTOS_MBED_THREADS_LOCK_GUARD */