Mistake on this page?
Report an issue in GitHub or email us
SynchronizedIntegral.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_SYNCHRONIZED_INTEGRAL
19 #define MBEDMICRO_RTOS_MBED_THREADS_SYNCHRONIZED_INTEGRAL
20 
21 #include <rtos.h>
22 #include <mstd_mutex>
23 
24 using mstd::lock_guard;
25 
26 /**
27  * Thread safe wrapper for integral types.
28  * @tparam T type of the integral
29  */
30 template<typename T>
32 public:
33  SynchronizedIntegral(T value) : _mutex(), _value(value) { }
34 
35  // preincrement operator
36  T operator++()
37  {
38  lock_guard<rtos::Mutex> lock(_mutex);
39  return ++_value;
40  }
41 
42  // predecrement operator
43  T operator--()
44  {
45  lock_guard<rtos::Mutex> lock(_mutex);
46  return --_value;
47  }
48 
49  // post increment operator
50  T operator++(int)
51  {
52  lock_guard<rtos::Mutex> lock(_mutex);
53  return _value++;
54  }
55 
56  // post decrement operator
57  T operator--(int)
58  {
59  lock_guard<rtos::Mutex> lock(_mutex);
60  return _value--;
61  }
62 
63  // conversion operator, used also for <,>,<=,>=,== and !=
64  operator T() const
65  {
66  lock_guard<rtos::Mutex> lock(_mutex);
67  return _value;
68  }
69 
70  // access to the internal mutex
71  rtos::Mutex &internal_mutex()
72  {
73  return _mutex;
74  }
75 
76 private:
77  mutable rtos::Mutex _mutex;
78  T _value;
79 };
80 #endif /* MBEDMICRO_RTOS_MBED_THREADS_SYNCHRONIZED_INTEGRAL */
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:70
Thread safe wrapper for integral types.
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.