mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

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_SYNCHRONIZED_INTEGRAL
elessair 0:f269e3021894 19 #define MBEDMICRO_RTOS_MBED_THREADS_SYNCHRONIZED_INTEGRAL
elessair 0:f269e3021894 20
elessair 0:f269e3021894 21 #include <rtos.h>
elessair 0:f269e3021894 22 #include "LockGuard.h"
elessair 0:f269e3021894 23
elessair 0:f269e3021894 24 /**
elessair 0:f269e3021894 25 * Thread safe wrapper for integral types.
elessair 0:f269e3021894 26 * @tparam T type of the integral
elessair 0:f269e3021894 27 */
elessair 0:f269e3021894 28 template<typename T>
elessair 0:f269e3021894 29 class SynchronizedIntegral {
elessair 0:f269e3021894 30 public:
elessair 0:f269e3021894 31 SynchronizedIntegral(T value) : _mutex(), _value(value) { }
elessair 0:f269e3021894 32
elessair 0:f269e3021894 33 // preincrement operator
elessair 0:f269e3021894 34 T operator++() {
elessair 0:f269e3021894 35 LockGuard lock(_mutex);
elessair 0:f269e3021894 36 return ++_value;
elessair 0:f269e3021894 37 }
elessair 0:f269e3021894 38
elessair 0:f269e3021894 39 // predecrement operator
elessair 0:f269e3021894 40 T operator--() {
elessair 0:f269e3021894 41 LockGuard lock(_mutex);
elessair 0:f269e3021894 42 return --_value;
elessair 0:f269e3021894 43 }
elessair 0:f269e3021894 44
elessair 0:f269e3021894 45 // post increment operator
elessair 0:f269e3021894 46 T operator++(int) {
elessair 0:f269e3021894 47 LockGuard lock(_mutex);
elessair 0:f269e3021894 48 return _value++;
elessair 0:f269e3021894 49 }
elessair 0:f269e3021894 50
elessair 0:f269e3021894 51 // post decrement operator
elessair 0:f269e3021894 52 T operator--(int) {
elessair 0:f269e3021894 53 LockGuard lock(_mutex);
elessair 0:f269e3021894 54 return _value--;
elessair 0:f269e3021894 55 }
elessair 0:f269e3021894 56
elessair 0:f269e3021894 57 // conversion operator, used also for <,>,<=,>=,== and !=
elessair 0:f269e3021894 58 operator T() const {
elessair 0:f269e3021894 59 LockGuard lock(_mutex);
elessair 0:f269e3021894 60 return _value;
elessair 0:f269e3021894 61 }
elessair 0:f269e3021894 62
elessair 0:f269e3021894 63 // access to the internal mutex
elessair 0:f269e3021894 64 rtos::Mutex& internal_mutex() {
elessair 0:f269e3021894 65 return _mutex;
elessair 0:f269e3021894 66 }
elessair 0:f269e3021894 67
elessair 0:f269e3021894 68 private:
elessair 0:f269e3021894 69 mutable rtos::Mutex _mutex;
elessair 0:f269e3021894 70 T _value;
elessair 0:f269e3021894 71 };
elessair 0:f269e3021894 72
elessair 0:f269e3021894 73 #endif /* MBEDMICRO_RTOS_MBED_THREADS_SYNCHRONIZED_INTEGRAL */