4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2009 by Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
30 * These are the void returning variants
33 #define ATOMIC_INC(name, type) \
34 void atomic_inc_##name(volatile type *target) \
36 (void) __atomic_add_fetch(target, 1, __ATOMIC_SEQ_CST); \
39 ATOMIC_INC(8, uint8_t)
40 ATOMIC_INC(uchar, uchar_t)
41 ATOMIC_INC(16, uint16_t)
42 ATOMIC_INC(ushort, ushort_t)
43 ATOMIC_INC(32, uint32_t)
44 ATOMIC_INC(uint, uint_t)
45 ATOMIC_INC(ulong, ulong_t)
46 ATOMIC_INC(64, uint64_t)
49 #define ATOMIC_DEC(name, type) \
50 void atomic_dec_##name(volatile type *target) \
52 (void) __atomic_sub_fetch(target, 1, __ATOMIC_SEQ_CST); \
55 ATOMIC_DEC(8, uint8_t)
56 ATOMIC_DEC(uchar, uchar_t)
57 ATOMIC_DEC(16, uint16_t)
58 ATOMIC_DEC(ushort, ushort_t)
59 ATOMIC_DEC(32, uint32_t)
60 ATOMIC_DEC(uint, uint_t)
61 ATOMIC_DEC(ulong, ulong_t)
62 ATOMIC_DEC(64, uint64_t)
65 #define ATOMIC_ADD(name, type1, type2) \
66 void atomic_add_##name(volatile type1 *target, type2 bits) \
68 (void) __atomic_add_fetch(target, bits, __ATOMIC_SEQ_CST); \
71 ATOMIC_ADD(8, uint8_t, int8_t)
72 ATOMIC_ADD(char, uchar_t, signed char)
73 ATOMIC_ADD(16, uint16_t, int16_t)
74 ATOMIC_ADD(short, ushort_t, short)
75 ATOMIC_ADD(32, uint32_t, int32_t)
76 ATOMIC_ADD(int, uint_t, int)
77 ATOMIC_ADD(long, ulong_t, long)
78 ATOMIC_ADD(64, uint64_t, int64_t)
81 atomic_add_ptr(volatile void *target, ssize_t bits)
83 (void) __atomic_add_fetch((void **)target, bits, __ATOMIC_SEQ_CST);
87 #define ATOMIC_SUB(name, type1, type2) \
88 void atomic_sub_##name(volatile type1 *target, type2 bits) \
90 (void) __atomic_sub_fetch(target, bits, __ATOMIC_SEQ_CST); \
93 ATOMIC_SUB(8, uint8_t, int8_t)
94 ATOMIC_SUB(char, uchar_t, signed char)
95 ATOMIC_SUB(16, uint16_t, int16_t)
96 ATOMIC_SUB(short, ushort_t, short)
97 ATOMIC_SUB(32, uint32_t, int32_t)
98 ATOMIC_SUB(int, uint_t, int)
99 ATOMIC_SUB(long, ulong_t, long)
100 ATOMIC_SUB(64, uint64_t, int64_t)
103 atomic_sub_ptr(volatile void *target, ssize_t bits)
105 (void) __atomic_sub_fetch((void **)target, bits, __ATOMIC_SEQ_CST);
109 #define ATOMIC_OR(name, type) \
110 void atomic_or_##name(volatile type *target, type bits) \
112 (void) __atomic_or_fetch(target, bits, __ATOMIC_SEQ_CST); \
115 ATOMIC_OR(8, uint8_t)
116 ATOMIC_OR(uchar, uchar_t)
117 ATOMIC_OR(16, uint16_t)
118 ATOMIC_OR(ushort, ushort_t)
119 ATOMIC_OR(32, uint32_t)
120 ATOMIC_OR(uint, uint_t)
121 ATOMIC_OR(ulong, ulong_t)
122 ATOMIC_OR(64, uint64_t)
125 #define ATOMIC_AND(name, type) \
126 void atomic_and_##name(volatile type *target, type bits) \
128 (void) __atomic_and_fetch(target, bits, __ATOMIC_SEQ_CST); \
131 ATOMIC_AND(8, uint8_t)
132 ATOMIC_AND(uchar, uchar_t)
133 ATOMIC_AND(16, uint16_t)
134 ATOMIC_AND(ushort, ushort_t)
135 ATOMIC_AND(32, uint32_t)
136 ATOMIC_AND(uint, uint_t)
137 ATOMIC_AND(ulong, ulong_t)
138 ATOMIC_AND(64, uint64_t)
142 * New value returning variants
145 #define ATOMIC_INC_NV(name, type) \
146 type atomic_inc_##name##_nv(volatile type *target) \
148 return (__atomic_add_fetch(target, 1, __ATOMIC_SEQ_CST)); \
151 ATOMIC_INC_NV(8, uint8_t)
152 ATOMIC_INC_NV(uchar, uchar_t)
153 ATOMIC_INC_NV(16, uint16_t)
154 ATOMIC_INC_NV(ushort, ushort_t)
155 ATOMIC_INC_NV(32, uint32_t)
156 ATOMIC_INC_NV(uint, uint_t)
157 ATOMIC_INC_NV(ulong, ulong_t)
158 ATOMIC_INC_NV(64, uint64_t)
161 #define ATOMIC_DEC_NV(name, type) \
162 type atomic_dec_##name##_nv(volatile type *target) \
164 return (__atomic_sub_fetch(target, 1, __ATOMIC_SEQ_CST)); \
167 ATOMIC_DEC_NV(8, uint8_t)
168 ATOMIC_DEC_NV(uchar, uchar_t)
169 ATOMIC_DEC_NV(16, uint16_t)
170 ATOMIC_DEC_NV(ushort, ushort_t)
171 ATOMIC_DEC_NV(32, uint32_t)
172 ATOMIC_DEC_NV(uint, uint_t)
173 ATOMIC_DEC_NV(ulong, ulong_t)
174 ATOMIC_DEC_NV(64, uint64_t)
177 #define ATOMIC_ADD_NV(name, type1, type2) \
178 type1 atomic_add_##name##_nv(volatile type1 *target, type2 bits) \
180 return (__atomic_add_fetch(target, bits, __ATOMIC_SEQ_CST)); \
183 ATOMIC_ADD_NV(8, uint8_t, int8_t)
184 ATOMIC_ADD_NV(char, uchar_t, signed char)
185 ATOMIC_ADD_NV(16, uint16_t, int16_t)
186 ATOMIC_ADD_NV(short, ushort_t, short)
187 ATOMIC_ADD_NV(32, uint32_t, int32_t)
188 ATOMIC_ADD_NV(int, uint_t, int)
189 ATOMIC_ADD_NV(long, ulong_t, long)
190 ATOMIC_ADD_NV(64, uint64_t, int64_t)
193 atomic_add_ptr_nv(volatile void *target, ssize_t bits)
195 return (__atomic_add_fetch((void **)target, bits, __ATOMIC_SEQ_CST));
199 #define ATOMIC_SUB_NV(name, type1, type2) \
200 type1 atomic_sub_##name##_nv(volatile type1 *target, type2 bits) \
202 return (__atomic_sub_fetch(target, bits, __ATOMIC_SEQ_CST)); \
205 ATOMIC_SUB_NV(8, uint8_t, int8_t)
206 ATOMIC_SUB_NV(char, uchar_t, signed char)
207 ATOMIC_SUB_NV(16, uint16_t, int16_t)
208 ATOMIC_SUB_NV(short, ushort_t, short)
209 ATOMIC_SUB_NV(32, uint32_t, int32_t)
210 ATOMIC_SUB_NV(int, uint_t, int)
211 ATOMIC_SUB_NV(long, ulong_t, long)
212 ATOMIC_SUB_NV(64, uint64_t, int64_t)
215 atomic_sub_ptr_nv(volatile void *target, ssize_t bits)
217 return (__atomic_sub_fetch((void **)target, bits, __ATOMIC_SEQ_CST));
221 #define ATOMIC_OR_NV(name, type) \
222 type atomic_or_##name##_nv(volatile type *target, type bits) \
224 return (__atomic_or_fetch(target, bits, __ATOMIC_SEQ_CST)); \
227 ATOMIC_OR_NV(8, uint8_t)
228 ATOMIC_OR_NV(uchar, uchar_t)
229 ATOMIC_OR_NV(16, uint16_t)
230 ATOMIC_OR_NV(ushort, ushort_t)
231 ATOMIC_OR_NV(32, uint32_t)
232 ATOMIC_OR_NV(uint, uint_t)
233 ATOMIC_OR_NV(ulong, ulong_t)
234 ATOMIC_OR_NV(64, uint64_t)
237 #define ATOMIC_AND_NV(name, type) \
238 type atomic_and_##name##_nv(volatile type *target, type bits) \
240 return (__atomic_and_fetch(target, bits, __ATOMIC_SEQ_CST)); \
243 ATOMIC_AND_NV(8, uint8_t)
244 ATOMIC_AND_NV(uchar, uchar_t)
245 ATOMIC_AND_NV(16, uint16_t)
246 ATOMIC_AND_NV(ushort, ushort_t)
247 ATOMIC_AND_NV(32, uint32_t)
248 ATOMIC_AND_NV(uint, uint_t)
249 ATOMIC_AND_NV(ulong, ulong_t)
250 ATOMIC_AND_NV(64, uint64_t)
254 * If *tgt == exp, set *tgt = des; return old value
256 * This may not look right on the first pass (or the sixteenth), but,
257 * from https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html:
258 * > If they are not equal, the operation is a read
259 * > and the current contents of *ptr are written into *expected.
260 * And, in the converse case, exp is already *target by definition.
263 #define ATOMIC_CAS(name, type) \
264 type atomic_cas_##name(volatile type *target, type exp, type des) \
266 __atomic_compare_exchange_n(target, &exp, des, B_FALSE, \
267 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \
271 ATOMIC_CAS(8, uint8_t)
272 ATOMIC_CAS(uchar, uchar_t)
273 ATOMIC_CAS(16, uint16_t)
274 ATOMIC_CAS(ushort, ushort_t)
275 ATOMIC_CAS(32, uint32_t)
276 ATOMIC_CAS(uint, uint_t)
277 ATOMIC_CAS(ulong, ulong_t)
278 ATOMIC_CAS(64, uint64_t)
281 atomic_cas_ptr(volatile void *target, void *exp, void *des)
284 __atomic_compare_exchange_n((void **)target, &exp, des, B_FALSE,
285 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
291 * Swap target and return old value
294 #define ATOMIC_SWAP(name, type) \
295 type atomic_swap_##name(volatile type *target, type bits) \
297 return (__atomic_exchange_n(target, bits, __ATOMIC_SEQ_CST)); \
300 ATOMIC_SWAP(8, uint8_t)
301 ATOMIC_SWAP(uchar, uchar_t)
302 ATOMIC_SWAP(16, uint16_t)
303 ATOMIC_SWAP(ushort, ushort_t)
304 ATOMIC_SWAP(32, uint32_t)
305 ATOMIC_SWAP(uint, uint_t)
306 ATOMIC_SWAP(ulong, ulong_t)
307 ATOMIC_SWAP(64, uint64_t)
311 atomic_swap_ptr(volatile void *target, void *bits)
313 return (__atomic_exchange_n((void **)target, bits, __ATOMIC_SEQ_CST));
318 atomic_set_long_excl(volatile ulong_t *target, uint_t value)
320 ulong_t bit = 1UL << value;
321 ulong_t old = __atomic_fetch_or(target, bit, __ATOMIC_SEQ_CST);
322 return ((old & bit) ? -1 : 0);
326 atomic_clear_long_excl(volatile ulong_t *target, uint_t value)
328 ulong_t bit = 1UL << value;
329 ulong_t old = __atomic_fetch_and(target, ~bit, __ATOMIC_SEQ_CST);
330 return ((old & bit) ? 0 : -1);
336 __atomic_thread_fence(__ATOMIC_SEQ_CST);
342 __atomic_thread_fence(__ATOMIC_SEQ_CST);
346 membar_producer(void)
348 __atomic_thread_fence(__ATOMIC_RELEASE);
352 membar_consumer(void)
354 __atomic_thread_fence(__ATOMIC_ACQUIRE);