Debian Codegraph @

crt/aws-c-common/include/aws/common/atomics_msvc.inl

raw
1 #ifndef AWS_COMMON_ATOMICS_MSVC_INL
2 #define AWS_COMMON_ATOMICS_MSVC_INL
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 /* These are implicitly included, but helps with editor highlighting */
10 #include <aws/common/atomics.h>
11 #include <aws/common/common.h>
12
13 /* This file generates level 4 compiler warnings in Visual Studio 2017 and older */
14 #pragma warning(push, 3)
15 #include <intrin.h>
16 #pragma warning(pop)
17
18 #include <stdint.h>
19 #include <stdlib.h>
20
21 AWS_EXTERN_C_BEGIN
22
23 #if !(defined(_M_IX86) || defined(_M_X64) || defined(_M_ARM64))
24 # error Atomics are not currently supported for non-x86 or ARM64 MSVC platforms
25
26 /*
27 * In particular, it's not clear that seq_cst will work properly on non-x86
28 * memory models. We may need to make use of platform-specific intrinsics.
29 *
30 * NOTE: Before removing this #error, please make use of the Interlocked*[Acquire|Release]
31 * variants (if applicable for the new platform)! This will (hopefully) help ensure that
32 * code breaks before people take too much of a dependency on it.
33 */
34
35 #endif
36
37 /**
38 * Some general notes:
39 *
40 * On x86/x86_64, by default, windows uses acquire/release semantics for volatile accesses;
41 * however, this is not the case on ARM, and on x86/x86_64 it can be disabled using the
42 * /volatile:iso compile flag.
43 *
44 * Interlocked* functions implicitly have acq_rel semantics; there are ones with weaker
45 * semantics as well, but because windows is generally used on x86, where there's not a lot
46 * of performance difference between different ordering modes anyway, we just use the stronger
47 * forms for now. Further, on x86, they actually have seq_cst semantics as they use locked instructions.
48 * It is unclear if Interlocked functions guarantee seq_cst on non-x86 platforms.
49 *
50 * Since all loads and stores are acq and/or rel already, we can do non-seq_cst loads and stores
51 * as just volatile variable accesses, but add the appropriate barriers for good measure.
52 *
53 * For seq_cst accesses, we take advantage of the facts that (on x86):
54 * 1. Loads are not reordered with other loads
55 * 2. Stores are not reordered with other stores
56 * 3. Locked instructions (including swaps) have a total order
57 * 4. Non-locked accesses are not reordered with locked instructions
58 *
59 * Therefore, if we ensure that all seq_cst stores are locked, we can establish
60 * a total order on stores, and the intervening ordinary loads will not violate that total
61 * order.
62 * See http://www.cs.cmu.edu/~410-f10/doc/Intel_Reordering_318147.pdf 2.7, which covers
63 * this use case.
64 */
65
66 /**
67 * Some general notes about ARM environments:
68 * ARM processors uses a weak memory model as opposed to the strong memory model used by Intel processors
69 * This means more permissible memory ordering allowed between stores and loads.
70 *
71 * Thus ARM port will need more hardware fences/barriers to assure developer intent.
72 * Memory barriers will prevent reordering stores and loads accross them depending on their type
73 * (read write, write only, read only ...)
74 *
75 * For more information about ARM64 memory ordering,
76 * see https://developer.arm.com/documentation/102336/0100/Memory-ordering
77 * For more information about Memory barriers,
78 * see https://developer.arm.com/documentation/102336/0100/Memory-barriers
79 * For more information about Miscosoft Interensic ARM64 APIs,
80 * see https://learn.microsoft.com/en-us/cpp/intrinsics/arm64-intrinsics?view=msvc-170
81 * Note: wrt _Interlocked[Op]64 is the same for ARM64 and x64 processors
82 */
83
84 #ifdef _M_IX86
85 # define AWS_INTERLOCKED_INT(x) _Interlocked##x
86 typedef long aws_atomic_impl_int_t;
87 #else
88 # define AWS_INTERLOCKED_INT(x) _Interlocked##x##64
89 typedef long long aws_atomic_impl_int_t;
90 #endif
91
92 #ifdef _M_ARM64
93 /* Hardware Read Write barrier, prevents all memory operations to cross the barrier in both directions */
94 # define AWS_RW_BARRIER() __dmb(_ARM64_BARRIER_SY)
95 /* Hardware Read barrier, prevents all memory operations to cross the barrier upwards */
96 # define AWS_R_BARRIER() __dmb(_ARM64_BARRIER_LD)
97 /* Hardware Write barrier, prevents all memory operations to cross the barrier downwards */
98 # define AWS_W_BARRIER() __dmb(_ARM64_BARRIER_ST)
99 /* Software barrier, prevents the compiler from reodering the operations across the barrier */
100 # define AWS_SW_BARRIER() _ReadWriteBarrier();
101 #else
102 /* hardware barriers, do nothing on x86 since it has a strong memory model
103 * as described in the section above: some general notes
104 */
105 # define AWS_RW_BARRIER()
106 # define AWS_R_BARRIER()
107 # define AWS_W_BARRIER()
108 /*
109 * x86: only a compiler barrier is required. For seq_cst, we must use some form of interlocked operation for
110 * writes, but that's the caller's responsibility.
111 *
112 * Volatile ops may or may not imply this barrier, depending on the /volatile: switch, but adding an extra
113 * barrier doesn't hurt.
114 */
115 # define AWS_SW_BARRIER() _ReadWriteBarrier(); /* software barrier */
116 #endif
117
118 static inline void aws_atomic_priv_check_order(enum aws_memory_order order) {
119 #ifndef NDEBUG
120 switch (order) {
121 case aws_memory_order_relaxed:
122 return;
123 case aws_memory_order_acquire:
124 return;
125 case aws_memory_order_release:
126 return;
127 case aws_memory_order_acq_rel:
128 return;
129 case aws_memory_order_seq_cst:
130 return;
131 default: /* Unknown memory order */
132 abort();
133 }
134 #endif
135 (void)order;
136 }
137
138 enum aws_atomic_mode_priv { aws_atomic_priv_load, aws_atomic_priv_store };
139
140 static inline void aws_atomic_priv_barrier_before(enum aws_memory_order order, enum aws_atomic_mode_priv mode) {
141 aws_atomic_priv_check_order(order);
142 AWS_ASSERT(mode != aws_atomic_priv_load || order != aws_memory_order_release);
143
144 if (order == aws_memory_order_relaxed) {
145 /* no barriers required for relaxed mode */
146 return;
147 }
148
149 if (order == aws_memory_order_acquire || mode == aws_atomic_priv_load) {
150 /* for acquire, we need only use a barrier afterward */
151 return;
152 }
153
154 AWS_RW_BARRIER();
155 AWS_SW_BARRIER();
156 }
157
158 static inline void aws_atomic_priv_barrier_after(enum aws_memory_order order, enum aws_atomic_mode_priv mode) {
159 aws_atomic_priv_check_order(order);
160 AWS_ASSERT(mode != aws_atomic_priv_store || order != aws_memory_order_acquire);
161
162 if (order == aws_memory_order_relaxed) {
163 /* no barriers required for relaxed mode */
164 return;
165 }
166
167 if (order == aws_memory_order_release || mode == aws_atomic_priv_store) {
168 /* for release, we need only use a barrier before */
169 return;
170 }
171
172 AWS_RW_BARRIER();
173 AWS_SW_BARRIER();
174 }
175
176 /**
177 * Initializes an atomic variable with an integer value. This operation should be done before any
178 * other operations on this atomic variable, and must be done before attempting any parallel operations.
179 */
180 AWS_STATIC_IMPL
181 void aws_atomic_init_int(volatile struct aws_atomic_var *var, size_t n) {
182 AWS_ATOMIC_VAR_INTVAL(var) = (aws_atomic_impl_int_t)n;
183 }
184
185 /**
186 * Initializes an atomic variable with a pointer value. This operation should be done before any
187 * other operations on this atomic variable, and must be done before attempting any parallel operations.
188 */
189 AWS_STATIC_IMPL
190 void aws_atomic_init_ptr(volatile struct aws_atomic_var *var, void *p) {
191 AWS_ATOMIC_VAR_PTRVAL(var) = p;
192 }
193
194 /**
195 * Reads an atomic var as an integer, using the specified ordering, and returns the result.
196 */
197 AWS_STATIC_IMPL
198 size_t aws_atomic_load_int_explicit(volatile const struct aws_atomic_var *var, enum aws_memory_order memory_order) {
199 aws_atomic_priv_barrier_before(memory_order, aws_atomic_priv_load);
200 size_t result = (size_t)AWS_ATOMIC_VAR_INTVAL(var);
201 aws_atomic_priv_barrier_after(memory_order, aws_atomic_priv_load);
202 return result;
203 }
204
205 /**
206 * Reads an atomic var as an pointer, using the specified ordering, and returns the result.
207 */
208 AWS_STATIC_IMPL
209 void *aws_atomic_load_ptr_explicit(volatile const struct aws_atomic_var *var, enum aws_memory_order memory_order) {
210 aws_atomic_priv_barrier_before(memory_order, aws_atomic_priv_load);
211 void *result = AWS_ATOMIC_VAR_PTRVAL(var);
212 aws_atomic_priv_barrier_after(memory_order, aws_atomic_priv_load);
213 return result;
214 }
215
216 /**
217 * Stores an integer into an atomic var, using the specified ordering.
218 */
219 AWS_STATIC_IMPL
220 void aws_atomic_store_int_explicit(volatile struct aws_atomic_var *var, size_t n, enum aws_memory_order memory_order) {
221 if (memory_order != aws_memory_order_seq_cst) {
222 aws_atomic_priv_barrier_before(memory_order, aws_atomic_priv_store);
223 AWS_ATOMIC_VAR_INTVAL(var) = (aws_atomic_impl_int_t)n;
224 aws_atomic_priv_barrier_after(memory_order, aws_atomic_priv_store);
225 } else {
226 AWS_INTERLOCKED_INT(Exchange)(&AWS_ATOMIC_VAR_INTVAL(var), (aws_atomic_impl_int_t)n);
227 }
228 }
229
230 /**
231 * Stores an pointer into an atomic var, using the specified ordering.
232 */
233 AWS_STATIC_IMPL
234 void aws_atomic_store_ptr_explicit(volatile struct aws_atomic_var *var, void *p, enum aws_memory_order memory_order) {
235 aws_atomic_priv_check_order(memory_order);
236 if (memory_order != aws_memory_order_seq_cst) {
237 aws_atomic_priv_barrier_before(memory_order, aws_atomic_priv_store);
238 AWS_ATOMIC_VAR_PTRVAL(var) = p;
239 aws_atomic_priv_barrier_after(memory_order, aws_atomic_priv_store);
240 } else {
241 _InterlockedExchangePointer(&AWS_ATOMIC_VAR_PTRVAL(var), p);
242 }
243 }
244
245 /**
246 * Exchanges an integer with the value in an atomic_var, using the specified ordering.
247 * Returns the value that was previously in the atomic_var.
248 */
249 AWS_STATIC_IMPL
250 size_t aws_atomic_exchange_int_explicit(
251 volatile struct aws_atomic_var *var,
252 size_t n,
253 enum aws_memory_order memory_order) {
254 aws_atomic_priv_check_order(memory_order);
255 return (size_t)AWS_INTERLOCKED_INT(Exchange)(&AWS_ATOMIC_VAR_INTVAL(var), (aws_atomic_impl_int_t)n);
256 }
257
258 /**
259 * Exchanges a pointer with the value in an atomic_var, using the specified ordering.
260 * Returns the value that was previously in the atomic_var.
261 */
262 AWS_STATIC_IMPL
263 void *aws_atomic_exchange_ptr_explicit(
264 volatile struct aws_atomic_var *var,
265 void *p,
266 enum aws_memory_order memory_order) {
267 aws_atomic_priv_check_order(memory_order);
268 return _InterlockedExchangePointer(&AWS_ATOMIC_VAR_PTRVAL(var), p);
269 }
270
271 /**
272 * Atomically compares *var to *expected; if they are equal, atomically sets *var = desired. Otherwise, *expected is set
273 * to the value in *var. On success, the memory ordering used was order_success; otherwise, it was order_failure.
274 * order_failure must be no stronger than order_success, and must not be release or acq_rel.
275 */
276 AWS_STATIC_IMPL
277 bool aws_atomic_compare_exchange_int_explicit(
278 volatile struct aws_atomic_var *var,
279 size_t *expected,
280 size_t desired,
281 enum aws_memory_order order_success,
282 enum aws_memory_order order_failure) {
283 aws_atomic_priv_check_order(order_success);
284 aws_atomic_priv_check_order(order_failure);
285
286 size_t oldval = (size_t)AWS_INTERLOCKED_INT(CompareExchange)(
287 &AWS_ATOMIC_VAR_INTVAL(var), (aws_atomic_impl_int_t)desired, (aws_atomic_impl_int_t)*expected);
288 bool successful = oldval == *expected;
289 *expected = oldval;
290
291 return successful;
292 }
293
294 /**
295 * Atomically compares *var to *expected; if they are equal, atomically sets *var = desired. Otherwise, *expected is set
296 * to the value in *var. On success, the memory ordering used was order_success; otherwise, it was order_failure.
297 * order_failure must be no stronger than order_success, and must not be release or acq_rel.
298 */
299 AWS_STATIC_IMPL
300 bool aws_atomic_compare_exchange_ptr_explicit(
301 volatile struct aws_atomic_var *var,
302 void **expected,
303 void *desired,
304 enum aws_memory_order order_success,
305 enum aws_memory_order order_failure) {
306 aws_atomic_priv_check_order(order_success);
307 aws_atomic_priv_check_order(order_failure);
308
309 void *oldval = _InterlockedCompareExchangePointer(&AWS_ATOMIC_VAR_PTRVAL(var), desired, *expected);
310 bool successful = oldval == *expected;
311 *expected = oldval;
312
313 return successful;
314 }
315
316 /**
317 * Atomically adds n to *var, and returns the previous value of *var.
318 */
319 AWS_STATIC_IMPL
320 size_t aws_atomic_fetch_add_explicit(volatile struct aws_atomic_var *var, size_t n, enum aws_memory_order order) {
321 aws_atomic_priv_check_order(order);
322
323 return (size_t)AWS_INTERLOCKED_INT(ExchangeAdd)(&AWS_ATOMIC_VAR_INTVAL(var), (aws_atomic_impl_int_t)n);
324 }
325
326 /**
327 * Atomically subtracts n from *var, and returns the previous value of *var.
328 */
329 AWS_STATIC_IMPL
330 size_t aws_atomic_fetch_sub_explicit(volatile struct aws_atomic_var *var, size_t n, enum aws_memory_order order) {
331 aws_atomic_priv_check_order(order);
332
333 return (size_t)AWS_INTERLOCKED_INT(ExchangeAdd)(&AWS_ATOMIC_VAR_INTVAL(var), -(aws_atomic_impl_int_t)n);
334 }
335
336 /**
337 * Atomically ORs n with *var, and returns the previous value of *var.
338 */
339 AWS_STATIC_IMPL
340 size_t aws_atomic_fetch_or_explicit(volatile struct aws_atomic_var *var, size_t n, enum aws_memory_order order) {
341 aws_atomic_priv_check_order(order);
342
343 return (size_t)AWS_INTERLOCKED_INT(Or)(&AWS_ATOMIC_VAR_INTVAL(var), (aws_atomic_impl_int_t)n);
344 }
345
346 /**
347 * Atomically ANDs n with *var, and returns the previous value of *var.
348 */
349 AWS_STATIC_IMPL
350 size_t aws_atomic_fetch_and_explicit(volatile struct aws_atomic_var *var, size_t n, enum aws_memory_order order) {
351 aws_atomic_priv_check_order(order);
352
353 return (size_t)AWS_INTERLOCKED_INT(And)(&AWS_ATOMIC_VAR_INTVAL(var), (aws_atomic_impl_int_t)n);
354 }
355
356 /**
357 * Atomically XORs n with *var, and returns the previous value of *var.
358 */
359 AWS_STATIC_IMPL
360 size_t aws_atomic_fetch_xor_explicit(volatile struct aws_atomic_var *var, size_t n, enum aws_memory_order order) {
361 aws_atomic_priv_check_order(order);
362
363 return (size_t)AWS_INTERLOCKED_INT(Xor)(&AWS_ATOMIC_VAR_INTVAL(var), (aws_atomic_impl_int_t)n);
364 }
365
366 /**
367 * Provides the same reordering guarantees as an atomic operation with the specified memory order, without
368 * needing to actually perform an atomic operation.
369 */
370 AWS_STATIC_IMPL
371 void aws_atomic_thread_fence(enum aws_memory_order order) {
372 volatile aws_atomic_impl_int_t x = 0;
373 aws_atomic_priv_check_order(order);
374
375 /* On x86: A compiler barrier is sufficient for anything short of seq_cst */
376
377 switch (order) {
378 case aws_memory_order_seq_cst:
379 AWS_INTERLOCKED_INT(Exchange)(&x, 1);
380 break;
381 case aws_memory_order_release:
382 AWS_W_BARRIER();
383 AWS_SW_BARRIER();
384 break;
385 case aws_memory_order_acquire:
386 AWS_R_BARRIER();
387 AWS_SW_BARRIER();
388 break;
389 case aws_memory_order_acq_rel:
390 AWS_RW_BARRIER();
391 AWS_SW_BARRIER();
392 break;
393 case aws_memory_order_relaxed:
394 /* no-op */
395 break;
396 }
397 }
398
399 /* prevent conflicts with other files that might pick the same names */
400 #undef AWS_RW_BARRIER
401 #undef AWS_R_BARRIER
402 #undef AWS_W_BARRIER
403 #undef AWS_SW_BARRIER
404
405 #define AWS_ATOMICS_HAVE_THREAD_FENCE
406 AWS_EXTERN_C_END
407 #endif