1 #ifndef _URCU_ARCH_UATOMIC_X86_H
2 #define _URCU_ARCH_UATOMIC_X86_H
5 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
6 * Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
7 * Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
8 * Copyright (c) 2009 Mathieu Desnoyers
10 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
11 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
13 * Permission is hereby granted to use or copy this program
14 * for any purpose, provided the above notices are retained on all copies.
15 * Permission to modify the code and to distribute modified code is granted,
16 * provided the above notices are retained, and a notice that the code was
17 * modified is included with the above copyright notice.
19 * Code inspired from libuatomic_ops-1.2, inherited in part from the
20 * Boehm-Demers-Weiser conservative garbage collector.
23 #include <urcu/compiler.h>
25 #ifndef __SIZEOF_LONG__
26 #if defined(__x86_64__) || defined(__amd64__)
27 #define __SIZEOF_LONG__ 8
29 #define __SIZEOF_LONG__ 4
34 #define BITS_PER_LONG (__SIZEOF_LONG__ * 8)
38 * Derived from AO_compare_and_swap() and AO_test_and_set_full().
41 struct __uatomic_dummy
{
44 #define __hp(x) ((struct __uatomic_dummy *)(x))
46 #define uatomic_set(addr, v) \
48 ACCESS_ONCE(*(addr)) = (v); \
51 #define uatomic_read(addr) ACCESS_ONCE(*(addr))
55 static inline __attribute__((always_inline
))
56 unsigned long _uatomic_cmpxchg(void *addr
, unsigned long old
,
57 unsigned long _new
, int len
)
62 unsigned char result
= old
;
65 "lock; cmpxchgb %2, %1"
66 : "+a"(result
), "+m"(*__hp(addr
))
67 : "q"((unsigned char)_new
)
73 unsigned short result
= old
;
76 "lock; cmpxchgw %2, %1"
77 : "+a"(result
), "+m"(*__hp(addr
))
78 : "r"((unsigned short)_new
)
84 unsigned int result
= old
;
87 "lock; cmpxchgl %2, %1"
88 : "+a"(result
), "+m"(*__hp(addr
))
89 : "r"((unsigned int)_new
)
93 #if (BITS_PER_LONG == 64)
96 unsigned long result
= old
;
99 "lock; cmpxchgq %2, %1"
100 : "+a"(result
), "+m"(*__hp(addr
))
101 : "r"((unsigned long)_new
)
107 /* generate an illegal instruction. Cannot catch this with linker tricks
108 * when optimizations are disabled. */
109 __asm__
__volatile__("ud2");
113 #define uatomic_cmpxchg(addr, old, _new) \
114 ((__typeof__(*(addr))) _uatomic_cmpxchg((addr), (unsigned long)(old),\
115 (unsigned long)(_new), \
120 static inline __attribute__((always_inline
))
121 unsigned long _uatomic_exchange(void *addr
, unsigned long val
, int len
)
123 /* Note: the "xchg" instruction does not need a "lock" prefix. */
127 unsigned char result
;
128 __asm__
__volatile__(
130 : "=q"(result
), "+m"(*__hp(addr
))
131 : "0" ((unsigned char)val
)
137 unsigned short result
;
138 __asm__
__volatile__(
140 : "=r"(result
), "+m"(*__hp(addr
))
141 : "0" ((unsigned short)val
)
148 __asm__
__volatile__(
150 : "=r"(result
), "+m"(*__hp(addr
))
151 : "0" ((unsigned int)val
)
155 #if (BITS_PER_LONG == 64)
158 unsigned long result
;
159 __asm__
__volatile__(
161 : "=r"(result
), "+m"(*__hp(addr
))
162 : "0" ((unsigned long)val
)
168 /* generate an illegal instruction. Cannot catch this with linker tricks
169 * when optimizations are disabled. */
170 __asm__
__volatile__("ud2");
174 #define uatomic_xchg(addr, v) \
175 ((__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \
178 /* uatomic_add_return, uatomic_sub_return */
180 static inline __attribute__((always_inline
))
181 unsigned long _uatomic_add_return(void *addr
, unsigned long val
,
187 unsigned char result
= val
;
189 __asm__
__volatile__(
191 : "+m"(*__hp(addr
)), "+q" (result
)
194 return result
+ (unsigned char)val
;
198 unsigned short result
= val
;
200 __asm__
__volatile__(
202 : "+m"(*__hp(addr
)), "+r" (result
)
205 return result
+ (unsigned short)val
;
209 unsigned int result
= val
;
211 __asm__
__volatile__(
213 : "+m"(*__hp(addr
)), "+r" (result
)
216 return result
+ (unsigned int)val
;
218 #if (BITS_PER_LONG == 64)
221 unsigned long result
= val
;
223 __asm__
__volatile__(
225 : "+m"(*__hp(addr
)), "+r" (result
)
228 return result
+ (unsigned long)val
;
232 /* generate an illegal instruction. Cannot catch this with linker tricks
233 * when optimizations are disabled. */
234 __asm__
__volatile__("ud2");
238 #define uatomic_add_return(addr, v) \
239 ((__typeof__(*(addr))) _uatomic_add_return((addr), \
240 (unsigned long)(v), \
243 #define uatomic_sub_return(addr, v) uatomic_add_return((addr), -(v))
245 /* uatomic_add, uatomic_sub */
247 static inline __attribute__((always_inline
))
248 void _uatomic_add(void *addr
, unsigned long val
, int len
)
253 __asm__
__volatile__(
256 : "iq" ((unsigned char)val
)
262 __asm__
__volatile__(
265 : "ir" ((unsigned short)val
)
271 __asm__
__volatile__(
274 : "ir" ((unsigned int)val
)
278 #if (BITS_PER_LONG == 64)
281 __asm__
__volatile__(
284 : "er" ((unsigned long)val
)
290 /* generate an illegal instruction. Cannot catch this with linker tricks
291 * when optimizations are disabled. */
292 __asm__
__volatile__("ud2");
296 #define uatomic_add(addr, v) \
297 (_uatomic_add((addr), (unsigned long)(v), sizeof(*(addr))))
299 #define uatomic_sub(addr, v) uatomic_add((addr), -(v))
304 static inline __attribute__((always_inline
))
305 void _uatomic_inc(void *addr
, int len
)
310 __asm__
__volatile__(
319 __asm__
__volatile__(
328 __asm__
__volatile__(
335 #if (BITS_PER_LONG == 64)
338 __asm__
__volatile__(
347 /* generate an illegal instruction. Cannot catch this with linker tricks
348 * when optimizations are disabled. */
349 __asm__
__volatile__("ud2");
353 #define uatomic_inc(addr) (_uatomic_inc((addr), sizeof(*(addr))))
357 static inline __attribute__((always_inline
))
358 void _uatomic_dec(void *addr
, int len
)
363 __asm__
__volatile__(
372 __asm__
__volatile__(
381 __asm__
__volatile__(
388 #if (BITS_PER_LONG == 64)
391 __asm__
__volatile__(
400 /* generate an illegal instruction. Cannot catch this with linker tricks
401 * when optimizations are disabled. */
402 __asm__
__volatile__("ud2");
406 #define uatomic_dec(addr) (_uatomic_dec((addr), sizeof(*(addr))))
408 #if (BITS_PER_LONG == 64)
409 #define URCU_CAS_AVAIL() 1
410 #define compat_uatomic_cmpxchg(ptr, old, _new) uatomic_cmpxchg(ptr, old, _new)
412 extern int __urcu_cas_avail
;
413 extern int __urcu_cas_init(void);
414 #define URCU_CAS_AVAIL() \
415 ((likely(__urcu_cas_avail > 0)) ? \
417 ((unlikely(__urcu_cas_avail < 0) ? \
418 (__urcu_cas_init()) : \
421 extern unsigned long _compat_uatomic_cmpxchg(void *addr
, unsigned long old
,
422 unsigned long _new
, int len
);
424 #define compat_uatomic_cmpxchg(addr, old, _new) \
425 ((__typeof__(*(addr))) _uatomic_cmpxchg((addr), (unsigned long)(old),\
426 (unsigned long)(_new), \
430 #endif /* _URCU_ARCH_UATOMIC_X86_H */
This page took 0.040572 seconds and 4 git commands to generate.