1 #ifndef _URCU_UATOMIC_GENERIC_H
2 #define _URCU_UATOMIC_GENERIC_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
9 * Copyright (c) 2010 Paolo Bonzini
11 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
12 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
14 * Permission is hereby granted to use or copy this program
15 * for any purpose, provided the above notices are retained on all copies.
16 * Permission to modify the code and to distribute modified code is granted,
17 * provided the above notices are retained, and a notice that the code was
18 * modified is included with the above copyright notice.
20 * Code inspired from libuatomic_ops-1.2, inherited in part from the
21 * Boehm-Demers-Weiser conservative garbage collector.
24 #include <urcu/compiler.h>
25 #include <urcu/system.h>
32 #define BITS_PER_LONG (__SIZEOF_LONG__ * 8)
36 #define uatomic_set(addr, v) STORE_SHARED(*(addr), (v))
40 #define uatomic_read(addr) LOAD_SHARED(*(addr))
43 #if !defined __OPTIMIZE__ || defined UATOMIC_NO_LINK_ERROR
44 static inline __attribute__((always_inline
))
45 void _uatomic_link_error()
48 /* generate an illegal instruction. Cannot catch this with linker tricks
49 * when optimizations are disabled. */
50 __asm__
__volatile__(ILLEGAL_INSTR
);
56 #else /* #if !defined __OPTIMIZE__ || defined UATOMIC_NO_LINK_ERROR */
57 extern void _uatomic_link_error ();
58 #endif /* #else #if !defined __OPTIMIZE__ || defined UATOMIC_NO_LINK_ERROR */
62 #ifndef uatomic_cmpxchg
63 static inline __attribute__((always_inline
))
64 unsigned long _uatomic_cmpxchg(void *addr
, unsigned long old
,
65 unsigned long _new
, int len
)
69 return __sync_val_compare_and_swap_4(addr
, old
, _new
);
70 #if (BITS_PER_LONG == 64)
72 return __sync_val_compare_and_swap_8(addr
, old
, _new
);
75 _uatomic_link_error();
80 #define uatomic_cmpxchg(addr, old, _new) \
81 ((__typeof__(*(addr))) _uatomic_cmpxchg((addr), (unsigned long)(old),\
82 (unsigned long)(_new), \
86 /* uatomic_add_return */
88 #ifndef uatomic_add_return
89 static inline __attribute__((always_inline
))
90 unsigned long _uatomic_add_return(void *addr
, unsigned long val
,
95 return __sync_add_and_fetch_4(addr
, val
);
96 #if (BITS_PER_LONG == 64)
98 return __sync_add_and_fetch_8(addr
, val
);
101 _uatomic_link_error();
106 #define uatomic_add_return(addr, v) \
107 ((__typeof__(*(addr))) _uatomic_add_return((addr), \
108 (unsigned long)(v), \
110 #endif /* #ifndef uatomic_add_return */
115 static inline __attribute__((always_inline
))
116 unsigned long _uatomic_exchange(void *addr
, unsigned long val
, int len
)
124 old
= uatomic_read((unsigned int *)addr
);
125 while (!__sync_bool_compare_and_swap_4(addr
, old
, val
));
129 #if (BITS_PER_LONG == 64)
135 old
= uatomic_read((unsigned long *)addr
);
136 } while (!__sync_bool_compare_and_swap_8(addr
, old
, val
));
142 _uatomic_link_error();
146 #define uatomic_xchg(addr, v) \
147 ((__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \
149 #endif /* #ifndef uatomic_xchg */
151 #else /* #ifndef uatomic_cmpxchg */
153 #ifndef uatomic_add_return
154 /* uatomic_add_return */
156 static inline __attribute__((always_inline
))
157 unsigned long _uatomic_add_return(void *addr
, unsigned long val
, int len
)
162 unsigned int old
, oldt
;
164 oldt
= uatomic_read((unsigned int *)addr
);
167 oldt
= _uatomic_cmpxchg(addr
, old
, old
+ val
, 4);
168 } while (oldt
!= old
);
172 #if (BITS_PER_LONG == 64)
175 unsigned long old
, oldt
;
177 oldt
= uatomic_read((unsigned long *)addr
);
180 oldt
= _uatomic_cmpxchg(addr
, old
, old
+ val
, 8);
181 } while (oldt
!= old
);
187 _uatomic_link_error();
191 #define uatomic_add_return(addr, v) \
192 ((__typeof__(*(addr))) _uatomic_add_return((addr), \
193 (unsigned long)(v), \
195 #endif /* #ifndef uatomic_add_return */
200 static inline __attribute__((always_inline
))
201 unsigned long _uatomic_exchange(void *addr
, unsigned long val
, int len
)
206 unsigned int old
, oldt
;
208 oldt
= uatomic_read((unsigned int *)addr
);
211 oldt
= _uatomic_cmpxchg(addr
, old
, val
, 4);
212 } while (oldt
!= old
);
216 #if (BITS_PER_LONG == 64)
219 unsigned long old
, oldt
;
221 oldt
= uatomic_read((unsigned long *)addr
);
224 oldt
= _uatomic_cmpxchg(addr
, old
, val
, 8);
225 } while (oldt
!= old
);
231 _uatomic_link_error();
235 #define uatomic_xchg(addr, v) \
236 ((__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \
238 #endif /* #ifndef uatomic_xchg */
240 #endif /* #else #ifndef uatomic_cmpxchg */
242 /* uatomic_sub_return, uatomic_add, uatomic_sub, uatomic_inc, uatomic_dec */
245 #define uatomic_add(addr, v) (void)uatomic_add_return((addr), (v))
248 #define uatomic_sub_return(addr, v) uatomic_add_return((addr), -(v))
249 #define uatomic_sub(addr, v) uatomic_add((addr), -(v))
252 #define uatomic_inc(addr) uatomic_add((addr), 1)
256 #define uatomic_dec(addr) uatomic_add((addr), -1)
263 #endif /* _URCU_UATOMIC_GENERIC_H */
This page took 0.036109 seconds and 4 git commands to generate.