Blender  V2.93
atomic_ops_unix.h
Go to the documentation of this file.
1 /*
2  * Original code from jemalloc with this license:
3  *
4  * Copyright (C) 2002-2013 Jason Evans <jasone@canonware.com>.
5  * All rights reserved.
6  * Copyright (C) 2007-2012 Mozilla Foundation. All rights reserved.
7  * Copyright (C) 2009-2013 Facebook, Inc. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * 1. Redistributions of source code must retain the above copyright notice(s),
12  * this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice(s),
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20  * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  * This program is free software; you can redistribute it and/or
28  * modify it under the terms of the GNU General Public License
29  * as published by the Free Software Foundation; either version 2
30  * of the License, or (at your option) any later version.
31  *
32  * This program is distributed in the hope that it will be useful,
33  * but WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35  * GNU General Public License for more details.
36  *
37  * You should have received a copy of the GNU General Public License
38  * along with this program; if not, write to the Free Software Foundation,
39  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
40  *
41  * The Original Code is Copyright (C) 2016 Blender Foundation.
42  * All rights reserved.
43  *
44  * The Original Code is: adapted from jemalloc.
45  */
46 
47 #ifndef __ATOMIC_OPS_UNIX_H__
48 #define __ATOMIC_OPS_UNIX_H__
49 
50 #include "atomic_ops_utils.h"
51 
52 #if defined(__arm__)
53 /* Attempt to fix compilation error on Debian armel kernel.
54  * arm7 architecture does have both 32 and 64bit atomics, however
55  * its gcc doesn't have __GCC_HAVE_SYNC_COMPARE_AND_SWAP_n defined.
56  */
57 # define JE_FORCE_SYNC_COMPARE_AND_SWAP_1
58 # define JE_FORCE_SYNC_COMPARE_AND_SWAP_2
59 # define JE_FORCE_SYNC_COMPARE_AND_SWAP_4
60 # define JE_FORCE_SYNC_COMPARE_AND_SWAP_8
61 #endif
62 
63 /******************************************************************************/
64 /* 64-bit operations. */
65 #if (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8) || defined(JE_FORCE_SYNC_COMPARE_AND_SWAP_8))
66 /* Unsigned */
68 {
69  return __sync_add_and_fetch(p, x);
70 }
71 
73 {
74  return __sync_sub_and_fetch(p, x);
75 }
76 
78 {
79  return __sync_fetch_and_add(p, x);
80 }
81 
83 {
84  return __sync_fetch_and_sub(p, x);
85 }
86 
88 {
89  return __sync_val_compare_and_swap(v, old, _new);
90 }
91 
92 /* Signed */
94 {
95  return __sync_add_and_fetch(p, x);
96 }
97 
99 {
100  return __sync_sub_and_fetch(p, x);
101 }
102 
104 {
105  return __sync_fetch_and_add(p, x);
106 }
107 
109 {
110  return __sync_fetch_and_sub(p, x);
111 }
112 
114 {
115  return __sync_val_compare_and_swap(v, old, _new);
116 }
117 
118 #elif (defined(__amd64__) || defined(__x86_64__))
119 /* Unsigned */
121 {
122  asm volatile("lock; xaddq %0, %1;"
123  : "+r"(x), "=m"(*p) /* Outputs. */
124  : "m"(*p) /* Inputs. */
125  );
126  return x;
127 }
128 
130 {
131  x = (uint64_t)(-(int64_t)x);
132  asm volatile("lock; xaddq %0, %1;"
133  : "+r"(x), "=m"(*p) /* Outputs. */
134  : "m"(*p) /* Inputs. */
135  );
136  return x;
137 }
138 
140 {
141  return atomic_fetch_and_add_uint64(p, x) + x;
142 }
143 
145 {
146  return atomic_fetch_and_sub_uint64(p, x) - x;
147 }
148 
150 {
151  uint64_t ret;
152  asm volatile("lock; cmpxchgq %2,%1" : "=a"(ret), "+m"(*v) : "r"(_new), "0"(old) : "memory");
153  return ret;
154 }
155 
156 /* Signed */
158 {
159  asm volatile("lock; xaddq %0, %1;"
160  : "+r"(x), "=m"(*p) /* Outputs. */
161  : "m"(*p) /* Inputs. */
162  );
163  return x;
164 }
165 
167 {
168  x = -x;
169  asm volatile("lock; xaddq %0, %1;"
170  : "+r"(x), "=m"(*p) /* Outputs. */
171  : "m"(*p) /* Inputs. */
172  );
173  return x;
174 }
175 
177 {
178  return atomic_fetch_and_add_int64(p, x) + x;
179 }
180 
182 {
183  return atomic_fetch_and_sub_int64(p, x) - x;
184 }
185 
187 {
188  int64_t ret;
189  asm volatile("lock; cmpxchgq %2,%1" : "=a"(ret), "+m"(*v) : "r"(_new), "0"(old) : "memory");
190  return ret;
191 }
192 #else
193 # error "Missing implementation for 64-bit atomic operations"
194 #endif
195 
196 /******************************************************************************/
197 /* 32-bit operations. */
198 #if (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) || defined(JE_FORCE_SYNC_COMPARE_AND_SWAP_4))
199 /* Unsigned */
201 {
202  return __sync_add_and_fetch(p, x);
203 }
204 
206 {
207  return __sync_sub_and_fetch(p, x);
208 }
209 
211 {
212  return __sync_val_compare_and_swap(v, old, _new);
213 }
214 
215 /* Signed */
217 {
218  return __sync_add_and_fetch(p, x);
219 }
220 
222 {
223  return __sync_sub_and_fetch(p, x);
224 }
225 
227 {
228  return __sync_val_compare_and_swap(v, old, _new);
229 }
230 
231 #elif (defined(__i386__) || defined(__amd64__) || defined(__x86_64__))
232 /* Unsigned */
234 {
235  uint32_t ret = x;
236  asm volatile("lock; xaddl %0, %1;"
237  : "+r"(ret), "=m"(*p) /* Outputs. */
238  : "m"(*p) /* Inputs. */
239  );
240  return ret + x;
241 }
242 
244 {
245  uint32_t ret = (uint32_t)(-(int32_t)x);
246  asm volatile("lock; xaddl %0, %1;"
247  : "+r"(ret), "=m"(*p) /* Outputs. */
248  : "m"(*p) /* Inputs. */
249  );
250  return ret - x;
251 }
252 
254 {
255  uint32_t ret;
256  asm volatile("lock; cmpxchgl %2,%1" : "=a"(ret), "+m"(*v) : "r"(_new), "0"(old) : "memory");
257  return ret;
258 }
259 
260 /* Signed */
262 {
263  int32_t ret = x;
264  asm volatile("lock; xaddl %0, %1;"
265  : "+r"(ret), "=m"(*p) /* Outputs. */
266  : "m"(*p) /* Inputs. */
267  );
268  return ret + x;
269 }
270 
272 {
273  int32_t ret = -x;
274  asm volatile("lock; xaddl %0, %1;"
275  : "+r"(ret), "=m"(*p) /* Outputs. */
276  : "m"(*p) /* Inputs. */
277  );
278  return ret - x;
279 }
280 
282 {
283  int32_t ret;
284  asm volatile("lock; cmpxchgl %2,%1" : "=a"(ret), "+m"(*v) : "r"(_new), "0"(old) : "memory");
285  return ret;
286 }
287 
288 #else
289 # error "Missing implementation for 32-bit atomic operations"
290 #endif
291 
292 #if (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) || defined(JE_FORCE_SYNC_COMPARE_AND_SWAP_4))
293 /* Unsigned */
295 {
296  return __sync_fetch_and_add(p, x);
297 }
298 
300 {
301  return __sync_fetch_and_or(p, x);
302 }
303 
305 {
306  return __sync_fetch_and_and(p, x);
307 }
308 
309 /* Signed */
311 {
312  return __sync_fetch_and_add(p, x);
313 }
314 
316 {
317  return __sync_fetch_and_or(p, x);
318 }
319 
321 {
322  return __sync_fetch_and_and(p, x);
323 }
324 
325 #else
326 # error "Missing implementation for 32-bit atomic operations"
327 #endif
328 
329 /******************************************************************************/
330 /* 16-bit operations. */
331 #if (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2) || defined(JE_FORCE_SYNC_COMPARE_AND_SWAP_2))
332 
333 /* Signed */
335 {
336  return __sync_fetch_and_and(p, b);
337 }
339 {
340  return __sync_fetch_and_or(p, b);
341 }
342 
343 #else
344 # error "Missing implementation for 16-bit atomic operations"
345 #endif
346 
347 /******************************************************************************/
348 /* 8-bit operations. */
349 #if (defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1) || defined(JE_FORCE_SYNC_COMPARE_AND_SWAP_1))
350 /* Unsigned */
352 {
353  return __sync_fetch_and_and(p, b);
354 }
356 {
357  return __sync_fetch_and_or(p, b);
358 }
359 
360 /* Signed */
362 {
363  return __sync_fetch_and_and(p, b);
364 }
366 {
367  return __sync_fetch_and_or(p, b);
368 }
369 
370 #else
371 # error "Missing implementation for 8-bit atomic operations"
372 #endif
373 
374 #endif /* __ATOMIC_OPS_UNIX_H__ */
ATOMIC_INLINE uint32_t atomic_fetch_and_or_uint32(uint32_t *p, uint32_t x)
ATOMIC_INLINE int32_t atomic_add_and_fetch_int32(int32_t *p, int32_t x)
ATOMIC_INLINE int16_t atomic_fetch_and_or_int16(int16_t *p, int16_t b)
ATOMIC_INLINE uint64_t atomic_fetch_and_sub_uint64(uint64_t *p, uint64_t x)
ATOMIC_INLINE int64_t atomic_sub_and_fetch_int64(int64_t *p, int64_t x)
ATOMIC_INLINE uint8_t atomic_fetch_and_and_uint8(uint8_t *p, uint8_t b)
ATOMIC_INLINE int64_t atomic_cas_int64(int64_t *v, int64_t old, int64_t _new)
ATOMIC_INLINE uint32_t atomic_fetch_and_add_uint32(uint32_t *p, uint32_t x)
ATOMIC_INLINE uint8_t atomic_fetch_and_or_uint8(uint8_t *p, uint8_t b)
ATOMIC_INLINE int32_t atomic_fetch_and_or_int32(int32_t *p, int32_t x)
ATOMIC_INLINE uint32_t atomic_fetch_and_and_uint32(uint32_t *p, uint32_t x)
ATOMIC_INLINE int64_t atomic_fetch_and_add_int64(int64_t *p, int64_t x)
ATOMIC_INLINE uint32_t atomic_add_and_fetch_uint32(uint32_t *p, uint32_t x)
ATOMIC_INLINE int32_t atomic_fetch_and_add_int32(int32_t *p, int32_t x)
ATOMIC_INLINE uint64_t atomic_cas_uint64(uint64_t *v, uint64_t old, uint64_t _new)
ATOMIC_INLINE uint64_t atomic_fetch_and_add_uint64(uint64_t *p, uint64_t x)
ATOMIC_INLINE uint32_t atomic_sub_and_fetch_uint32(uint32_t *p, uint32_t x)
ATOMIC_INLINE int16_t atomic_fetch_and_and_int16(int16_t *p, int16_t b)
ATOMIC_INLINE int64_t atomic_add_and_fetch_int64(int64_t *p, int64_t x)
ATOMIC_INLINE uint64_t atomic_add_and_fetch_uint64(uint64_t *p, uint64_t x)
ATOMIC_INLINE uint64_t atomic_sub_and_fetch_uint64(uint64_t *p, uint64_t x)
ATOMIC_INLINE int32_t atomic_cas_int32(int32_t *v, int32_t old, int32_t _new)
ATOMIC_INLINE int32_t atomic_fetch_and_and_int32(int32_t *p, int32_t x)
ATOMIC_INLINE int8_t atomic_fetch_and_or_int8(int8_t *p, int8_t b)
ATOMIC_INLINE int32_t atomic_sub_and_fetch_int32(int32_t *p, int32_t x)
ATOMIC_INLINE int8_t atomic_fetch_and_and_int8(int8_t *p, int8_t b)
ATOMIC_INLINE int64_t atomic_fetch_and_sub_int64(int64_t *p, int64_t x)
ATOMIC_INLINE uint32_t atomic_cas_uint32(uint32_t *v, uint32_t old, uint32_t _new)
#define ATOMIC_INLINE
ATTR_WARN_UNUSED_RESULT const BMVert * v
return ret
signed short int16_t
Definition: stdint.h:79
unsigned int uint32_t
Definition: stdint.h:83
__int64 int64_t
Definition: stdint.h:92
signed int int32_t
Definition: stdint.h:80
unsigned char uint8_t
Definition: stdint.h:81
unsigned __int64 uint64_t
Definition: stdint.h:93
signed char int8_t
Definition: stdint.h:78