takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SynchronizedIntegral.h Source File

SynchronizedIntegral.h

00001 /*
00002  * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * 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, WITHOUT
00013  * 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 
00018 #ifndef MBEDMICRO_RTOS_MBED_THREADS_SYNCHRONIZED_INTEGRAL
00019 #define MBEDMICRO_RTOS_MBED_THREADS_SYNCHRONIZED_INTEGRAL
00020 
00021 #include <rtos.h>
00022 #include "LockGuard.h"
00023 
00024 /**
00025  * Thread safe wrapper for integral types.
00026  * @tparam T type of the integral
00027  */
00028 template<typename T>
00029 class SynchronizedIntegral {
00030 public:
00031     SynchronizedIntegral(T value) : _mutex(), _value(value) { }
00032 
00033     // preincrement operator
00034     T operator++()
00035     {
00036         LockGuard lock(_mutex);
00037         return ++_value;
00038     }
00039 
00040     // predecrement operator
00041     T operator--()
00042     {
00043         LockGuard lock(_mutex);
00044         return --_value;
00045     }
00046 
00047     // post increment operator
00048     T operator++(int)
00049     {
00050         LockGuard lock(_mutex);
00051         return _value++;
00052     }
00053 
00054     // post decrement operator
00055     T operator--(int)
00056     {
00057         LockGuard lock(_mutex);
00058         return _value--;
00059     }
00060 
00061     // conversion operator, used also for <,>,<=,>=,== and !=
00062     operator T()  const
00063     {
00064         LockGuard lock(_mutex);
00065         return _value;
00066     }
00067 
00068     // access to the internal mutex
00069     rtos::Mutex &internal_mutex()
00070     {
00071         return _mutex;
00072     }
00073 
00074 private:
00075     mutable rtos::Mutex _mutex;
00076     T _value;
00077 };
00078 
00079 #endif /* MBEDMICRO_RTOS_MBED_THREADS_SYNCHRONIZED_INTEGRAL */