1 #ifndef _ARCH_ATOMIC_X86_H
2 #define _ARCH_ATOMIC_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 libatomic_ops-1.2, inherited in part from the
20 * Boehm-Demers-Weiser conservative garbage collector.
26 #define BITS_PER_LONG (__SIZEOF_LONG__ * 8)
29 #ifndef _INCLUDE_API_H
32 * Derived from AO_compare_and_swap() and AO_test_and_set_full().
35 struct __atomic_dummy
{
38 #define __hp(x) ((struct __atomic_dummy *)(x))
40 #define atomic_set(addr, v) \
42 ACCESS_ONCE(*(addr)) = (v); \
45 #define atomic_read(addr) ACCESS_ONCE(*(addr))
49 static inline __attribute__((always_inline
))
50 unsigned long _atomic_cmpxchg(volatile void *addr
, unsigned long old
,
51 unsigned long _new
, int len
)
56 unsigned char result
= old
;
59 "lock; cmpxchgb %2, %1"
60 : "+a"(result
), "+m"(*__hp(addr
))
61 : "q"((unsigned char)_new
)
67 unsigned short result
= old
;
70 "lock; cmpxchgw %2, %1"
71 : "+a"(result
), "+m"(*__hp(addr
))
72 : "r"((unsigned short)_new
)
78 unsigned int result
= old
;
81 "lock; cmpxchgl %2, %1"
82 : "+a"(result
), "+m"(*__hp(addr
))
83 : "r"((unsigned int)_new
)
87 #if (BITS_PER_LONG == 64)
90 unsigned long result
= old
;
93 "lock; cmpxchgq %2, %1"
94 : "+a"(result
), "+m"(*__hp(addr
))
95 : "r"((unsigned long)_new
)
101 /* generate an illegal instruction. Cannot catch this with linker tricks
102 * when optimizations are disabled. */
103 __asm__
__volatile__("ud2");
107 #define cmpxchg(addr, old, _new) \
108 ((__typeof__(*(addr))) _atomic_cmpxchg((addr), (unsigned long)(old),\
109 (unsigned long)(_new), \
114 static inline __attribute__((always_inline
))
115 unsigned long _atomic_exchange(volatile void *addr
, unsigned long val
, int len
)
117 /* Note: the "xchg" instruction does not need a "lock" prefix. */
121 unsigned char result
;
122 __asm__
__volatile__(
124 : "=q"(result
), "+m"(*__hp(addr
))
125 : "0" ((unsigned char)val
)
131 unsigned short result
;
132 __asm__
__volatile__(
134 : "=r"(result
), "+m"(*__hp(addr
))
135 : "0" ((unsigned short)val
)
142 __asm__
__volatile__(
144 : "=r"(result
), "+m"(*__hp(addr
))
145 : "0" ((unsigned int)val
)
149 #if (BITS_PER_LONG == 64)
152 unsigned long result
;
153 __asm__
__volatile__(
155 : "=r"(result
), "+m"(*__hp(addr
))
156 : "0" ((unsigned long)val
)
162 /* generate an illegal instruction. Cannot catch this with linker tricks
163 * when optimizations are disabled. */
164 __asm__
__volatile__("ud2");
168 #define xchg(addr, v) \
169 ((__typeof__(*(addr))) _atomic_exchange((addr), (unsigned long)(v), \
172 /* atomic_add_return, atomic_sub_return */
174 static inline __attribute__((always_inline
))
175 unsigned long _atomic_add_return(volatile void *addr
, unsigned long val
,
181 unsigned char result
= val
;
183 __asm__
__volatile__(
185 : "+m"(*__hp(addr
)), "+q" (result
)
188 return result
+ (unsigned char)val
;
192 unsigned short result
= val
;
194 __asm__
__volatile__(
196 : "+m"(*__hp(addr
)), "+r" (result
)
199 return result
+ (unsigned short)val
;
203 unsigned int result
= val
;
205 __asm__
__volatile__(
207 : "+m"(*__hp(addr
)), "+r" (result
)
210 return result
+ (unsigned int)val
;
212 #if (BITS_PER_LONG == 64)
215 unsigned long result
= val
;
217 __asm__
__volatile__(
219 : "+m"(*__hp(addr
)), "+r" (result
)
222 return result
+ (unsigned long)val
;
226 /* generate an illegal instruction. Cannot catch this with linker tricks
227 * when optimizations are disabled. */
228 __asm__
__volatile__("ud2");
232 #define atomic_add_return(addr, v) \
233 ((__typeof__(*(addr))) _atomic_add_return((addr), \
234 (unsigned long)(v), \
237 #define atomic_sub_return(addr, v) atomic_add_return((addr), -(v))
239 /* atomic_add, atomic_sub */
241 static inline __attribute__((always_inline
))
242 void _atomic_add(volatile void *addr
, unsigned long val
, int len
)
247 __asm__
__volatile__(
250 : "iq" ((unsigned char)val
)
256 __asm__
__volatile__(
259 : "ir" ((unsigned short)val
)
265 __asm__
__volatile__(
268 : "ir" ((unsigned int)val
)
272 #if (BITS_PER_LONG == 64)
275 __asm__
__volatile__(
278 : "er" ((unsigned long)val
)
284 /* generate an illegal instruction. Cannot catch this with linker tricks
285 * when optimizations are disabled. */
286 __asm__
__volatile__("ud2");
290 #define atomic_add(addr, v) \
291 (_atomic_add((addr), (unsigned long)(v), sizeof(*(addr))))
293 #define atomic_sub(addr, v) atomic_add((addr), -(v))
298 static inline __attribute__((always_inline
))
299 void _atomic_inc(volatile void *addr
, int len
)
304 __asm__
__volatile__(
313 __asm__
__volatile__(
322 __asm__
__volatile__(
329 #if (BITS_PER_LONG == 64)
332 __asm__
__volatile__(
341 /* generate an illegal instruction. Cannot catch this with linker tricks
342 * when optimizations are disabled. */
343 __asm__
__volatile__("ud2");
347 #define atomic_inc(addr) (_atomic_inc((addr), sizeof(*(addr))))
351 static inline __attribute__((always_inline
))
352 void _atomic_dec(volatile void *addr
, int len
)
357 __asm__
__volatile__(
366 __asm__
__volatile__(
375 __asm__
__volatile__(
382 #if (BITS_PER_LONG == 64)
385 __asm__
__volatile__(
394 /* generate an illegal instruction. Cannot catch this with linker tricks
395 * when optimizations are disabled. */
396 __asm__
__volatile__("ud2");
400 #define atomic_dec(addr) (_atomic_dec((addr), sizeof(*(addr))))
402 #endif /* #ifndef _INCLUDE_API_H */
404 #endif /* ARCH_ATOMIC_X86_H */
This page took 0.039068 seconds and 5 git commands to generate.