Line data Source code
1 : #ifndef AWS_COMMON_ATOMICS_INL
2 : #define AWS_COMMON_ATOMICS_INL
3 :
4 : /**
5 : * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 : * SPDX-License-Identifier: Apache-2.0.
7 : */
8 :
9 : #include <aws/common/atomics.h>
10 : #include <aws/common/common.h>
11 :
12 : AWS_EXTERN_C_BEGIN
13 :
14 : /**
15 : * Reads an atomic var as an integer, using sequentially consistent ordering, and returns the result.
16 : */
17 : AWS_STATIC_IMPL
18 0 : size_t aws_atomic_load_int(volatile const struct aws_atomic_var *var) {
19 0 : return aws_atomic_load_int_explicit(var, aws_memory_order_seq_cst);
20 0 : }
21 :
22 : /**
23 : * Reads an atomic var as a pointer, using sequentially consistent ordering, and returns the result.
24 : */
25 : AWS_STATIC_IMPL
26 104756 : void *aws_atomic_load_ptr(volatile const struct aws_atomic_var *var) {
27 104756 : return aws_atomic_load_ptr_explicit(var, aws_memory_order_seq_cst);
28 104756 : }
29 :
30 : /**
31 : * Stores an integer into an atomic var, using sequentially consistent ordering.
32 : */
33 : AWS_STATIC_IMPL
34 0 : void aws_atomic_store_int(volatile struct aws_atomic_var *var, size_t n) {
35 0 : aws_atomic_store_int_explicit(var, n, aws_memory_order_seq_cst);
36 0 : }
37 :
38 : /**
39 : * Stores a pointer into an atomic var, using sequentially consistent ordering.
40 : */
41 : AWS_STATIC_IMPL
42 25244 : void aws_atomic_store_ptr(volatile struct aws_atomic_var *var, void *p) {
43 25244 : aws_atomic_store_ptr_explicit(var, p, aws_memory_order_seq_cst);
44 25244 : }
45 :
46 : /**
47 : * Exchanges an integer with the value in an atomic_var, using sequentially consistent ordering.
48 : * Returns the value that was previously in the atomic_var.
49 : */
50 : AWS_STATIC_IMPL
51 0 : size_t aws_atomic_exchange_int(volatile struct aws_atomic_var *var, size_t n) {
52 0 : return aws_atomic_exchange_int_explicit(var, n, aws_memory_order_seq_cst);
53 0 : }
54 :
55 : /**
56 : * Exchanges an integer with the value in an atomic_var, using sequentially consistent ordering.
57 : * Returns the value that was previously in the atomic_var.
58 : */
59 : AWS_STATIC_IMPL
60 0 : void *aws_atomic_exchange_ptr(volatile struct aws_atomic_var *var, void *p) {
61 0 : return aws_atomic_exchange_ptr_explicit(var, p, aws_memory_order_seq_cst);
62 0 : }
63 :
64 : /**
65 : * Atomically compares *var to *expected; if they are equal, atomically sets *var = desired. Otherwise, *expected is set
66 : * to the value in *var. Uses sequentially consistent memory ordering, regardless of success or failure.
67 : * Returns true if the compare was successful and the variable updated to desired.
68 : */
69 : AWS_STATIC_IMPL
70 0 : bool aws_atomic_compare_exchange_int(volatile struct aws_atomic_var *var, size_t *expected, size_t desired) {
71 0 : return aws_atomic_compare_exchange_int_explicit(
72 0 : var, expected, desired, aws_memory_order_seq_cst, aws_memory_order_seq_cst);
73 0 : }
74 :
75 : /**
76 : * Atomically compares *var to *expected; if they are equal, atomically sets *var = desired. Otherwise, *expected is set
77 : * to the value in *var. Uses sequentially consistent memory ordering, regardless of success or failure.
78 : * Returns true if the compare was successful and the variable updated to desired.
79 : */
80 : AWS_STATIC_IMPL
81 0 : bool aws_atomic_compare_exchange_ptr(volatile struct aws_atomic_var *var, void **expected, void *desired) {
82 0 : return aws_atomic_compare_exchange_ptr_explicit(
83 0 : var, expected, desired, aws_memory_order_seq_cst, aws_memory_order_seq_cst);
84 0 : }
85 :
86 : /**
87 : * Atomically adds n to *var, and returns the previous value of *var.
88 : * Uses sequentially consistent ordering.
89 : */
90 : AWS_STATIC_IMPL
91 0 : size_t aws_atomic_fetch_add(volatile struct aws_atomic_var *var, size_t n) {
92 0 : return aws_atomic_fetch_add_explicit(var, n, aws_memory_order_seq_cst);
93 0 : }
94 :
95 : /**
96 : * Atomically subtracts n from *var, and returns the previous value of *var.
97 : * Uses sequentially consistent ordering.
98 : */
99 : AWS_STATIC_IMPL
100 0 : size_t aws_atomic_fetch_sub(volatile struct aws_atomic_var *var, size_t n) {
101 0 : return aws_atomic_fetch_sub_explicit(var, n, aws_memory_order_seq_cst);
102 0 : }
103 :
104 : /**
105 : * Atomically ands n into *var, and returns the previous value of *var.
106 : * Uses sequentially consistent ordering.
107 : */
108 : AWS_STATIC_IMPL
109 0 : size_t aws_atomic_fetch_and(volatile struct aws_atomic_var *var, size_t n) {
110 0 : return aws_atomic_fetch_and_explicit(var, n, aws_memory_order_seq_cst);
111 0 : }
112 :
113 : /**
114 : * Atomically ors n into *var, and returns the previous value of *var.
115 : * Uses sequentially consistent ordering.
116 : */
117 : AWS_STATIC_IMPL
118 0 : size_t aws_atomic_fetch_or(volatile struct aws_atomic_var *var, size_t n) {
119 0 : return aws_atomic_fetch_or_explicit(var, n, aws_memory_order_seq_cst);
120 0 : }
121 :
122 : /**
123 : * Atomically xors n into *var, and returns the previous value of *var.
124 : * Uses sequentially consistent ordering.
125 : */
126 : AWS_STATIC_IMPL
127 0 : size_t aws_atomic_fetch_xor(volatile struct aws_atomic_var *var, size_t n) {
128 0 : return aws_atomic_fetch_xor_explicit(var, n, aws_memory_order_seq_cst);
129 0 : }
130 :
131 : /* Include the backend implementation now, because we'll use its typedefs and #defines below */
132 : #if defined(__GNUC__) || defined(__clang__)
133 : # if defined(__ATOMIC_RELAXED)
134 : # include <aws/common/atomics_gnu.inl>
135 : # else
136 : # include <aws/common/atomics_gnu_old.inl>
137 : # endif /* __ATOMIC_RELAXED */
138 : #elif defined(_MSC_VER)
139 : # include <aws/common/atomics_msvc.inl>
140 : #else
141 : # error No atomics implementation for your compiler is available
142 : #endif
143 :
144 : #include <aws/common/atomics_fallback.inl>
145 :
146 : AWS_EXTERN_C_END
147 :
148 : #endif /* AWS_COMMON_ATOMICS_INL */
|