Debian Codegraph @

lib/atomic.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2018 - 2022, Stephan Mueller <[email protected]>
3 *
4 * License: see COPYING file in root directory
5 *
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 */
19
20 #ifndef _ATOMIC_H
21 #define _ATOMIC_H
22
23 /*
24 * Atomic operations only work on:
25 * GCC >= 4.1
26 * Clang / LLVM
27 */
28
29 /**
30 * Atomic type and operations equivalent to the Linux kernel.
31 */
32 typedef struct {
33 volatile int counter;
34 } atomic_t;
35
36 /**
37 * Memory barrier
38 */
39 static inline void mb(void)
40 {
41 __sync_synchronize();
42 }
43
44 #define ATOMIC_INIT(i) { (i) }
45
46 /**
47 * Read atomic variable
48 * @param v atomic variable
49 * @return variable content
50 */
51 static inline int atomic_read(const atomic_t *v)
52 {
53 int i;
54
55 mb();
56 i = ((v)->counter);
57 mb();
58
59 return i;
60 }
61
62 /**
63 * Set atomic variable
64 * @param i value to be set
65 * @param v atomic variable
66 */
67 static inline void atomic_set(int i, atomic_t *v)
68 {
69 mb();
70 ((v)->counter) = i;
71 mb();
72 }
73
74 /**
75 * Atomic add operation
76 * @param i integer value to add
77 * @param v atomic variable
78 * @return variable content after operation
79 */
80 static inline int atomic_add(int i, atomic_t *v)
81 {
82 return __sync_add_and_fetch(&v->counter, i);
83 }
84
85 /**
86 * Atomic add value from variable and test for zero
87 * @param i integer value to add
88 * @param v atomic variable
89 * @return true if the result is zero, or false for all other cases.
90 */
91 static inline int atomic_add_and_test(int i, atomic_t *v)
92 {
93 return !(__sync_add_and_fetch(&v->counter, i));
94 }
95
96 /**
97 * Atomic increment by 1
98 * @param v atomic variable
99 * @return variable content after operation
100 */
101 static inline int atomic_inc(atomic_t *v)
102 {
103 return atomic_add(1, v);
104 }
105
106 /**
107 * Atomic increment and test for zero
108 * @param v pointer of type atomic_t
109 * @return true if the result is zero, or false for all other cases.
110 */
111 static inline int atomic_inc_and_test(atomic_t *v)
112 {
113 return atomic_add_and_test(1, v);
114 }
115
116 /**
117 * Atomic subtract operation
118 * @param i integer value to subtract
119 * @param v atomic variable
120 * @return variable content after operation
121 */
122 static inline int atomic_sub(int i, atomic_t *v)
123 {
124 return __sync_sub_and_fetch(&v->counter, i);
125 }
126
127 /**
128 * Atomic subtract value from variable and test for zero
129 * @param i integer value to subtract
130 * @param v atomic variable
131 * @return true if the result is zero, or false for all other cases.
132 */
133 static inline int atomic_sub_and_test(int i, atomic_t *v)
134 {
135 return !(__sync_sub_and_fetch(&v->counter, i));
136 }
137
138 /**
139 * Atomic decrement by 1
140 * @param v: atomic variable
141 * @return variable content after operation
142 */
143 static inline int atomic_dec(atomic_t *v)
144 {
145 return atomic_sub(1, v);
146 }
147
148 /**
149 * Atomic decrement by 1 and test for zero
150 * @param v atomic variable
151 * @return true if the result is zero, or false for all other cases.
152 */
153 static inline int atomic_dec_and_test(atomic_t *v)
154 {
155 return atomic_sub_and_test(1, v);
156 }
157
158 /**
159 * Atomic or operation
160 * @param i integer value to or
161 * @param v atomic variable
162 * @return variable content after operation
163 */
164 static inline int atomic_or(int i, atomic_t *v)
165 {
166 return __sync_or_and_fetch(&v->counter, i);
167 }
168
169 /**
170 * Atomic xor operation
171 * @param i integer value to xor
172 * @param v atomic variable
173 * @return variable content after operation
174 */
175 static inline int atomic_xor(int i, atomic_t *v)
176 {
177 return __sync_xor_and_fetch(&v->counter, i);
178 }
179
180 /**
181 * Atomic and operation
182 * @param i integer value to and
183 * @param v atomic variable
184 * @return variable content after operation
185 */
186 static inline int atomic_and(int i, atomic_t *v)
187 {
188 return __sync_and_and_fetch(&v->counter, i);
189 }
190
191 #ifdef __clang__
192 #pragma clang diagnostic push
193 #pragma clang diagnostic ignored "-Wsync-fetch-and-nand-semantics-changed"
194 #endif
195 /**
196 * Atomic nand operation
197 * @param i integer value to nand
198 * @param v atomic variable
199 * @return variable content after operation
200 */
201 static inline int atomic_nand(int i, atomic_t *v)
202 {
203 return __sync_nand_and_fetch(&v->counter, i);
204 }
205 #ifdef __clang__
206 #pragma clang diagnostic pop
207 #endif
208
209 /**
210 * Atomic compare and exchange operation (if current value of atomic
211 * variable is equal to the old value, set the new value)
212 * @param v atomic variable
213 * @param old integer value to compare with
214 * @param new integer value to set atomic variable to
215 * @return true if comparison is successful and new was written
216 */
217 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
218 {
219 return __sync_bool_compare_and_swap(&v->counter, old, new);
220 }
221
222 #endif