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