Embedded Template Library 1.0
Loading...
Searching...
No Matches
atomic_std.h
1/******************************************************************************
2The MIT License(MIT)
3
4Embedded Template Library.
5https://github.com/ETLCPP/etl
6https://www.etlcpp.com
7
8Copyright(c) 2018 John Wellbelove
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files(the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions :
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26SOFTWARE.
27******************************************************************************/
28
29#ifndef ETL_ATOMIC_STD_INCLUDED
30#define ETL_ATOMIC_STD_INCLUDED
31
32#include "../platform.h"
33#include "../char_traits.h"
34#include "../nullptr.h"
35
36#include <atomic>
37#include <stdint.h>
38
39namespace etl
40{
41 //***************************************************************************
42 // ETL Atomic type for compilers that support std::atomic.
43 // etl::atomic is a wrapper around std::atomic that adds fetch_max and
44 // fetch_min if the standard library does not yet provide them.
45 //***************************************************************************
46
47#if ETL_HAS_STD_ATOMIC_MIN_MAX
48 // The standard library already provides fetch_max/fetch_min.
49 template <typename T>
50 using atomic = std::atomic<T>;
51#else
52 template <typename T>
53 class atomic : public std::atomic<T>
54 {
55 using base_type = std::atomic<T>;
56
57 public:
58
59 atomic() ETL_NOEXCEPT = default;
60
61 ETL_CONSTEXPR atomic(T desired) ETL_NOEXCEPT
62 : base_type(desired)
63 {
64 }
65
66 atomic(const atomic&) = delete;
67 atomic& operator=(const atomic&) = delete;
68 atomic& operator=(const atomic&) volatile = delete;
69
70 using base_type::operator=;
71
72 // Fetch max
73 T fetch_max(T v, std::memory_order order = std::memory_order_seq_cst)
74 {
75 T old = this->load(order);
76
77 while (v > old)
78 {
79 if (this->compare_exchange_weak(old, v, order))
80 {
81 break;
82 }
83 }
84
85 return old;
86 }
87
88 T fetch_max(T v, std::memory_order order = std::memory_order_seq_cst) volatile
89 {
90 T old = this->load(order);
91
92 while (v > old)
93 {
94 if (this->compare_exchange_weak(old, v, order))
95 {
96 break;
97 }
98 }
99
100 return old;
101 }
102
103 // Fetch min
104 T fetch_min(T v, std::memory_order order = std::memory_order_seq_cst)
105 {
106 T old = this->load(order);
107
108 while (v < old)
109 {
110 if (this->compare_exchange_weak(old, v, order))
111 {
112 break;
113 }
114 }
115
116 return old;
117 }
118
119 T fetch_min(T v, std::memory_order order = std::memory_order_seq_cst) volatile
120 {
121 T old = this->load(order);
122
123 while (v < old)
124 {
125 if (this->compare_exchange_weak(old, v, order))
126 {
127 break;
128 }
129 }
130
131 return old;
132 }
133 };
134#endif
135
136 using memory_order = std::memory_order;
137
138 static ETL_CONSTANT etl::memory_order memory_order_relaxed = std::memory_order_relaxed;
139 static ETL_CONSTANT etl::memory_order memory_order_consume = std::memory_order_consume;
140 static ETL_CONSTANT etl::memory_order memory_order_acquire = std::memory_order_acquire;
141 static ETL_CONSTANT etl::memory_order memory_order_release = std::memory_order_release;
142 static ETL_CONSTANT etl::memory_order memory_order_acq_rel = std::memory_order_acq_rel;
143 static ETL_CONSTANT etl::memory_order memory_order_seq_cst = std::memory_order_seq_cst;
144
145 using atomic_bool = etl::atomic<bool>;
146 using atomic_char = etl::atomic<char>;
147 using atomic_schar = etl::atomic<signed char>;
148 using atomic_uchar = etl::atomic<unsigned char>;
149 using atomic_short = etl::atomic<short>;
150 using atomic_ushort = etl::atomic<unsigned short>;
151 using atomic_int = etl::atomic<int>;
152 using atomic_uint = etl::atomic<unsigned int>;
153 using atomic_long = etl::atomic<long>;
154 using atomic_ulong = etl::atomic<unsigned long>;
155 using atomic_llong = etl::atomic<long long>;
156 using atomic_ullong = etl::atomic<unsigned long long>;
157 using atomic_wchar_t = etl::atomic<wchar_t>;
158#if ETL_HAS_NATIVE_CHAR8_T
159 using atomic_char8_t = etl::atomic<char8_t>;
160#endif
161#if ETL_HAS_NATIVE_CHAR16_T
162 using atomic_char16_t = etl::atomic<char16_t>;
163#endif
164#if ETL_HAS_NATIVE_CHAR32_T
165 using atomic_char32_t = etl::atomic<char32_t>;
166#endif
167#if ETL_USING_8BIT_TYPES
168 using atomic_uint8_t = etl::atomic<uint8_t>;
169 using atomic_int8_t = etl::atomic<int8_t>;
170#endif
171 using atomic_uint16_t = etl::atomic<uint16_t>;
172 using atomic_int16_t = etl::atomic<int16_t>;
173 using atomic_uint32_t = etl::atomic<uint32_t>;
174 using atomic_int32_t = etl::atomic<int32_t>;
175#if ETL_USING_64BIT_TYPES
176 using atomic_uint64_t = etl::atomic<uint64_t>;
177 using atomic_int64_t = etl::atomic<int64_t>;
178#endif
179 using atomic_int_least8_t = etl::atomic<int_least8_t>;
180 using atomic_uint_least8_t = etl::atomic<uint_least8_t>;
181 using atomic_int_least16_t = etl::atomic<int_least16_t>;
182 using atomic_uint_least16_t = etl::atomic<uint_least16_t>;
183 using atomic_int_least32_t = etl::atomic<int_least32_t>;
184 using atomic_uint_least32_t = etl::atomic<uint_least32_t>;
185#if ETL_USING_64BIT_TYPES
186 using atomic_int_least64_t = etl::atomic<int_least64_t>;
187 using atomic_uint_least64_t = etl::atomic<uint_least64_t>;
188#endif
189 using atomic_int_fast8_t = etl::atomic<int_fast8_t>;
190 using atomic_uint_fast8_t = etl::atomic<uint_fast8_t>;
191 using atomic_int_fast16_t = etl::atomic<int_fast16_t>;
192 using atomic_uint_fast16_t = etl::atomic<uint_fast16_t>;
193 using atomic_int_fast32_t = etl::atomic<int_fast32_t>;
194 using atomic_uint_fast32_t = etl::atomic<uint_fast32_t>;
195#if ETL_USING_64BIT_TYPES
196 using atomic_int_fast64_t = etl::atomic<int_fast64_t>;
197 using atomic_uint_fast64_t = etl::atomic<uint_fast64_t>;
198#endif
199 using atomic_intptr_t = etl::atomic<intptr_t>;
200 using atomic_uintptr_t = etl::atomic<uintptr_t>;
201 using atomic_size_t = etl::atomic<size_t>;
202 using atomic_ptrdiff_t = etl::atomic<ptrdiff_t>;
203 using atomic_intmax_t = etl::atomic<intmax_t>;
204 using atomic_uintmax_t = etl::atomic<uintmax_t>;
205} // namespace etl
206
207#endif
Definition atomic_std.h:54
Definition absolute.h:40