xref: /illumos-gate/usr/src/common/atomic/atomic.c (revision c40a6cd7)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/atomic.h>
28 
29 /*
30  * This file exists only for the purpose of running lint.
31  */
32 
33 #if defined(__lint)
34 
35 void
atomic_inc_8(volatile uint8_t * target)36 atomic_inc_8(volatile uint8_t *target)
37 { (*target)++; }
38 
39 void
atomic_inc_uchar(volatile uchar_t * target)40 atomic_inc_uchar(volatile uchar_t *target)
41 { (*target)++; }
42 
43 void
atomic_inc_16(volatile uint16_t * target)44 atomic_inc_16(volatile uint16_t *target)
45 { (*target)++; }
46 
47 void
atomic_inc_ushort(volatile ushort_t * target)48 atomic_inc_ushort(volatile ushort_t *target)
49 { (*target)++; }
50 
51 void
atomic_inc_32(volatile uint32_t * target)52 atomic_inc_32(volatile uint32_t *target)
53 { (*target)++; }
54 
55 void
atomic_inc_uint(volatile uint_t * target)56 atomic_inc_uint(volatile uint_t *target)
57 { (*target)++; }
58 
59 void
atomic_inc_ulong(volatile ulong_t * target)60 atomic_inc_ulong(volatile ulong_t *target)
61 { (*target)++; }
62 
63 void
atomic_inc_64(volatile uint64_t * target)64 atomic_inc_64(volatile uint64_t *target)
65 { (*target)++; }
66 
67 void
atomic_dec_8(volatile uint8_t * target)68 atomic_dec_8(volatile uint8_t *target)
69 { (*target)--; }
70 
71 void
atomic_dec_uchar(volatile uchar_t * target)72 atomic_dec_uchar(volatile uchar_t *target)
73 { (*target)--; }
74 
75 void
atomic_dec_16(volatile uint16_t * target)76 atomic_dec_16(volatile uint16_t *target)
77 { (*target)--; }
78 
79 void
atomic_dec_ushort(volatile ushort_t * target)80 atomic_dec_ushort(volatile ushort_t *target)
81 { (*target)--; }
82 
83 void
atomic_dec_32(volatile uint32_t * target)84 atomic_dec_32(volatile uint32_t *target)
85 { (*target)--; }
86 
87 void
atomic_dec_uint(volatile uint_t * target)88 atomic_dec_uint(volatile uint_t *target)
89 { (*target)--; }
90 
91 void
atomic_dec_ulong(volatile ulong_t * target)92 atomic_dec_ulong(volatile ulong_t *target)
93 { (*target)--; }
94 
95 void
atomic_dec_64(volatile uint64_t * target)96 atomic_dec_64(volatile uint64_t *target)
97 { (*target)--; }
98 
99 void
atomic_add_8(volatile uint8_t * target,int8_t value)100 atomic_add_8(volatile uint8_t *target, int8_t value)
101 { *target += value; }
102 
103 void
atomic_add_char(volatile uchar_t * target,signed char value)104 atomic_add_char(volatile uchar_t *target, signed char value)
105 { *target += value; }
106 
107 void
atomic_add_16(volatile uint16_t * target,int16_t delta)108 atomic_add_16(volatile uint16_t *target, int16_t delta)
109 { *target += delta; }
110 
111 void
atomic_add_ushort(volatile ushort_t * target,short value)112 atomic_add_ushort(volatile ushort_t *target, short value)
113 { *target += value; }
114 
115 void
atomic_add_32(volatile uint32_t * target,int32_t delta)116 atomic_add_32(volatile uint32_t *target, int32_t delta)
117 { *target += delta; }
118 
119 void
atomic_add_ptr(volatile void * target,ssize_t value)120 atomic_add_ptr(volatile void *target, ssize_t value)
121 { *(caddr_t *)target += value; }
122 
123 void
atomic_add_long(volatile ulong_t * target,long delta)124 atomic_add_long(volatile ulong_t *target, long delta)
125 { *target += delta; }
126 
127 void
atomic_add_64(volatile uint64_t * target,int64_t delta)128 atomic_add_64(volatile uint64_t *target, int64_t delta)
129 { *target += delta; }
130 
131 void
atomic_or_8(volatile uint8_t * target,uint8_t bits)132 atomic_or_8(volatile uint8_t *target, uint8_t bits)
133 { *target |= bits; }
134 
135 void
atomic_or_uchar(volatile uchar_t * target,uchar_t bits)136 atomic_or_uchar(volatile uchar_t *target, uchar_t bits)
137 { *target |= bits; }
138 
139 void
atomic_or_16(volatile uint16_t * target,uint16_t bits)140 atomic_or_16(volatile uint16_t *target, uint16_t bits)
141 { *target |= bits; }
142 
143 void
atomic_or_ushort(volatile ushort_t * target,ushort_t bits)144 atomic_or_ushort(volatile ushort_t *target, ushort_t bits)
145 { *target |= bits; }
146 
147 void
atomic_or_32(volatile uint32_t * target,uint32_t bits)148 atomic_or_32(volatile uint32_t *target, uint32_t bits)
149 { *target |= bits; }
150 
151 void
atomic_or_uint(volatile uint_t * target,uint_t bits)152 atomic_or_uint(volatile uint_t *target, uint_t bits)
153 { *target |= bits; }
154 
155 void
atomic_or_ulong(volatile ulong_t * target,ulong_t bits)156 atomic_or_ulong(volatile ulong_t *target, ulong_t bits)
157 { *target |= bits; }
158 
159 void
atomic_or_64(volatile uint64_t * target,uint64_t bits)160 atomic_or_64(volatile uint64_t *target, uint64_t bits)
161 { *target |= bits; }
162 
163 void
atomic_and_8(volatile uint8_t * target,uint8_t bits)164 atomic_and_8(volatile uint8_t *target, uint8_t bits)
165 { *target &= bits; }
166 
167 void
atomic_and_uchar(volatile uchar_t * target,uchar_t bits)168 atomic_and_uchar(volatile uchar_t *target, uchar_t bits)
169 { *target &= bits; }
170 
171 void
atomic_and_16(volatile uint16_t * target,uint16_t bits)172 atomic_and_16(volatile uint16_t *target, uint16_t bits)
173 { *target &= bits; }
174 
175 void
atomic_and_ushort(volatile ushort_t * target,ushort_t bits)176 atomic_and_ushort(volatile ushort_t *target, ushort_t bits)
177 { *target &= bits; }
178 
179 void
atomic_and_32(volatile uint32_t * target,uint32_t bits)180 atomic_and_32(volatile uint32_t *target, uint32_t bits)
181 { *target &= bits; }
182 
183 void
atomic_and_uint(volatile uint_t * target,uint_t bits)184 atomic_and_uint(volatile uint_t *target, uint_t bits)
185 { *target &= bits; }
186 
187 void
atomic_and_ulong(volatile ulong_t * target,ulong_t bits)188 atomic_and_ulong(volatile ulong_t *target, ulong_t bits)
189 { *target &= bits; }
190 
191 void
atomic_and_64(volatile uint64_t * target,uint64_t bits)192 atomic_and_64(volatile uint64_t *target, uint64_t bits)
193 { *target &= bits; }
194 
195 uint8_t
atomic_inc_8_nv(volatile uint8_t * target)196 atomic_inc_8_nv(volatile uint8_t *target)
197 { return (++(*target)); }
198 
199 uchar_t
atomic_inc_uchar_nv(volatile uchar_t * target)200 atomic_inc_uchar_nv(volatile uchar_t *target)
201 { return (++(*target)); }
202 
203 uint16_t
atomic_inc_16_nv(volatile uint16_t * target)204 atomic_inc_16_nv(volatile uint16_t *target)
205 { return (++(*target)); }
206 
207 ushort_t
atomic_inc_ushort_nv(volatile ushort_t * target)208 atomic_inc_ushort_nv(volatile ushort_t *target)
209 { return (++(*target)); }
210 
211 uint32_t
atomic_inc_32_nv(volatile uint32_t * target)212 atomic_inc_32_nv(volatile uint32_t *target)
213 { return (++(*target)); }
214 
215 uint_t
atomic_inc_uint_nv(volatile uint_t * target)216 atomic_inc_uint_nv(volatile uint_t *target)
217 { return (++(*target)); }
218 
219 ulong_t
atomic_inc_ulong_nv(volatile ulong_t * target)220 atomic_inc_ulong_nv(volatile ulong_t *target)
221 { return (++(*target)); }
222 
223 uint64_t
atomic_inc_64_nv(volatile uint64_t * target)224 atomic_inc_64_nv(volatile uint64_t *target)
225 { return (++(*target)); }
226 
227 uint8_t
atomic_dec_8_nv(volatile uint8_t * target)228 atomic_dec_8_nv(volatile uint8_t *target)
229 { return (--(*target)); }
230 
231 uchar_t
atomic_dec_uchar_nv(volatile uchar_t * target)232 atomic_dec_uchar_nv(volatile uchar_t *target)
233 { return (--(*target)); }
234 
235 uint16_t
atomic_dec_16_nv(volatile uint16_t * target)236 atomic_dec_16_nv(volatile uint16_t *target)
237 { return (--(*target)); }
238 
239 ushort_t
atomic_dec_ushort_nv(volatile ushort_t * target)240 atomic_dec_ushort_nv(volatile ushort_t *target)
241 { return (--(*target)); }
242 
243 uint32_t
atomic_dec_32_nv(volatile uint32_t * target)244 atomic_dec_32_nv(volatile uint32_t *target)
245 { return (--(*target)); }
246 
247 uint_t
atomic_dec_uint_nv(volatile uint_t * target)248 atomic_dec_uint_nv(volatile uint_t *target)
249 { return (--(*target)); }
250 
251 ulong_t
atomic_dec_ulong_nv(volatile ulong_t * target)252 atomic_dec_ulong_nv(volatile ulong_t *target)
253 { return (--(*target)); }
254 
255 uint64_t
atomic_dec_64_nv(volatile uint64_t * target)256 atomic_dec_64_nv(volatile uint64_t *target)
257 { return (--(*target)); }
258 
259 uint8_t
atomic_add_8_nv(volatile uint8_t * target,int8_t value)260 atomic_add_8_nv(volatile uint8_t *target, int8_t value)
261 { return (*target += value); }
262 
263 uchar_t
atomic_add_char_nv(volatile uchar_t * target,signed char value)264 atomic_add_char_nv(volatile uchar_t *target, signed char value)
265 { return (*target += value); }
266 
267 uint16_t
atomic_add_16_nv(volatile uint16_t * target,int16_t delta)268 atomic_add_16_nv(volatile uint16_t *target, int16_t delta)
269 { return (*target += delta); }
270 
271 ushort_t
atomic_add_short_nv(volatile ushort_t * target,short value)272 atomic_add_short_nv(volatile ushort_t *target, short value)
273 { return (*target += value); }
274 
275 uint32_t
atomic_add_32_nv(volatile uint32_t * target,int32_t delta)276 atomic_add_32_nv(volatile uint32_t *target, int32_t delta)
277 { return (*target += delta); }
278 
279 uint_t
atomic_add_int_nv(volatile uint_t * target,int delta)280 atomic_add_int_nv(volatile uint_t *target, int delta)
281 { return (*target += delta); }
282 
283 void *
atomic_add_ptr_nv(volatile void * target,ssize_t value)284 atomic_add_ptr_nv(volatile void *target, ssize_t value)
285 { return (*(caddr_t *)target += value); }
286 
287 ulong_t
atomic_add_long_nv(volatile ulong_t * target,long delta)288 atomic_add_long_nv(volatile ulong_t *target, long delta)
289 { return (*target += delta); }
290 
291 uint64_t
atomic_add_64_nv(volatile uint64_t * target,int64_t delta)292 atomic_add_64_nv(volatile uint64_t *target, int64_t delta)
293 { return (*target += delta); }
294 
295 uint8_t
atomic_or_8_nv(volatile uint8_t * target,uint8_t value)296 atomic_or_8_nv(volatile uint8_t *target, uint8_t value)
297 { return (*target |= value); }
298 
299 uchar_t
atomic_or_uchar_nv(volatile uchar_t * target,uchar_t value)300 atomic_or_uchar_nv(volatile uchar_t *target, uchar_t value)
301 { return (*target |= value); }
302 
303 uint16_t
atomic_or_16_nv(volatile uint16_t * target,uint16_t value)304 atomic_or_16_nv(volatile uint16_t *target, uint16_t value)
305 { return (*target |= value); }
306 
307 ushort_t
atomic_or_ushort_nv(volatile ushort_t * target,ushort_t value)308 atomic_or_ushort_nv(volatile ushort_t *target, ushort_t value)
309 { return (*target |= value); }
310 
311 uint32_t
atomic_or_32_nv(volatile uint32_t * target,uint32_t value)312 atomic_or_32_nv(volatile uint32_t *target, uint32_t value)
313 { return (*target |= value); }
314 
315 uint_t
atomic_or_uint_nv(volatile uint_t * target,uint_t value)316 atomic_or_uint_nv(volatile uint_t *target, uint_t value)
317 { return (*target |= value); }
318 
319 ulong_t
atomic_or_ulong_nv(volatile ulong_t * target,ulong_t value)320 atomic_or_ulong_nv(volatile ulong_t *target, ulong_t value)
321 { return (*target |= value); }
322 
323 uint64_t
atomic_or_64_nv(volatile uint64_t * target,uint64_t value)324 atomic_or_64_nv(volatile uint64_t *target, uint64_t value)
325 { return (*target |= value); }
326 
327 uint8_t
atomic_and_8_nv(volatile uint8_t * target,uint8_t value)328 atomic_and_8_nv(volatile uint8_t *target, uint8_t value)
329 { return (*target &= value); }
330 
331 uchar_t
atomic_and_uchar_nv(volatile uchar_t * target,uchar_t value)332 atomic_and_uchar_nv(volatile uchar_t *target, uchar_t value)
333 { return (*target &= value); }
334 
335 uint16_t
atomic_and_16_nv(volatile uint16_t * target,uint16_t value)336 atomic_and_16_nv(volatile uint16_t *target, uint16_t value)
337 { return (*target &= value); }
338 
339 ushort_t
atomic_and_ushort_nv(volatile ushort_t * target,ushort_t value)340 atomic_and_ushort_nv(volatile ushort_t *target, ushort_t value)
341 { return (*target &= value); }
342 
343 uint32_t
atomic_and_32_nv(volatile uint32_t * target,uint32_t value)344 atomic_and_32_nv(volatile uint32_t *target, uint32_t value)
345 { return (*target &= value); }
346 
347 uint_t
atomic_and_uint_nv(volatile uint_t * target,uint_t value)348 atomic_and_uint_nv(volatile uint_t *target, uint_t value)
349 { return (*target &= value); }
350 
351 ulong_t
atomic_and_ulong_nv(volatile ulong_t * target,ulong_t value)352 atomic_and_ulong_nv(volatile ulong_t *target, ulong_t value)
353 { return (*target &= value); }
354 
355 uint64_t
atomic_and_64_nv(volatile uint64_t * target,uint64_t value)356 atomic_and_64_nv(volatile uint64_t *target, uint64_t value)
357 { return (*target &= value); }
358 
359 uint8_t
atomic_cas_8(volatile uint8_t * target,uint8_t cmp,uint8_t new)360 atomic_cas_8(volatile uint8_t *target, uint8_t cmp, uint8_t new)
361 {
362 	uint8_t old = *target;
363 	if (old == cmp)
364 		*target = new;
365 	return (old);
366 }
367 
368 uchar_t
atomic_cas_uchar(volatile uchar_t * target,uchar_t cmp,uchar_t new)369 atomic_cas_uchar(volatile uchar_t *target, uchar_t cmp, uchar_t new)
370 {
371 	uchar_t old = *target;
372 	if (old == cmp)
373 		*target = new;
374 	return (old);
375 }
376 
377 uint16_t
atomic_cas_16(volatile uint16_t * target,uint16_t cmp,uint16_t new)378 atomic_cas_16(volatile uint16_t *target, uint16_t cmp, uint16_t new)
379 {
380 	uint16_t old = *target;
381 	if (old == cmp)
382 		*target = new;
383 	return (old);
384 }
385 
386 ushort_t
atomic_cas_ushort(volatile ushort_t * target,ushort_t cmp,ushort_t new)387 atomic_cas_ushort(volatile ushort_t *target, ushort_t cmp, ushort_t new)
388 {
389 	ushort_t old = *target;
390 	if (old == cmp)
391 		*target = new;
392 	return (old);
393 }
394 
395 uint32_t
atomic_cas_32(volatile uint32_t * target,uint32_t cmp,uint32_t new)396 atomic_cas_32(volatile uint32_t *target, uint32_t cmp, uint32_t new)
397 {
398 	uint32_t old = *target;
399 	if (old == cmp)
400 		*target = new;
401 	return (old);
402 }
403 
404 uint_t
atomic_cas_uint(volatile uint_t * target,uint_t cmp,uint_t new)405 atomic_cas_uint(volatile uint_t *target, uint_t cmp, uint_t new)
406 {
407 	uint_t old = *target;
408 	if (old == cmp)
409 		*target = new;
410 	return (old);
411 }
412 
413 ulong_t
atomic_cas_ulong(volatile ulong_t * target,ulong_t cmp,ulong_t new)414 atomic_cas_ulong(volatile ulong_t *target, ulong_t cmp, ulong_t new)
415 {
416 	ulong_t old = *target;
417 	if (old == cmp)
418 		*target = new;
419 	return (old);
420 }
421 
422 uint64_t
atomic_cas_64(volatile uint64_t * target,uint64_t cmp,uint64_t new)423 atomic_cas_64(volatile uint64_t *target, uint64_t cmp, uint64_t new)
424 {
425 	uint64_t old = *target;
426 	if (old == cmp)
427 		*target = new;
428 	return (old);
429 }
430 
431 void *
atomic_cas_ptr(volatile void * target,void * cmp,void * new)432 atomic_cas_ptr(volatile void *target, void *cmp, void *new)
433 {
434 	void *old = *(void **)target;
435 	if (old == cmp)
436 		*(void **)target = new;
437 	return (old);
438 }
439 
440 uint8_t
atomic_swap_8(volatile uint8_t * target,uint8_t new)441 atomic_swap_8(volatile uint8_t *target, uint8_t new)
442 {
443 	uint8_t old = *target;
444 	*target = new;
445 	return (old);
446 }
447 
448 uchar_t
atomic_swap_char(volatile uchar_t * target,uchar_t new)449 atomic_swap_char(volatile uchar_t *target, uchar_t new)
450 {
451 	uchar_t old = *target;
452 	*target = new;
453 	return (old);
454 }
455 
456 uint16_t
atomic_swap_16(volatile uint16_t * target,uint16_t new)457 atomic_swap_16(volatile uint16_t *target, uint16_t new)
458 {
459 	uint16_t old = *target;
460 	*target = new;
461 	return (old);
462 }
463 
464 ushort_t
atomic_swap_ushort(volatile ushort_t * target,ushort_t new)465 atomic_swap_ushort(volatile ushort_t *target, ushort_t new)
466 {
467 	ushort_t old = *target;
468 	*target = new;
469 	return (old);
470 }
471 
472 uint32_t
atomic_swap_32(volatile uint32_t * target,uint32_t new)473 atomic_swap_32(volatile uint32_t *target, uint32_t new)
474 {
475 	uint32_t old = *target;
476 	*target = new;
477 	return (old);
478 }
479 
480 uint_t
atomic_swap_uint(volatile uint_t * target,uint_t new)481 atomic_swap_uint(volatile uint_t *target, uint_t new)
482 {
483 	uint_t old = *target;
484 	*target = new;
485 	return (old);
486 }
487 
488 uint64_t
atomic_swap_64(volatile uint64_t * target,uint64_t new)489 atomic_swap_64(volatile uint64_t *target, uint64_t new)
490 {
491 	uint64_t old = *target;
492 	*target = new;
493 	return (old);
494 }
495 
496 void *
atomic_swap_ptr(volatile void * target,void * new)497 atomic_swap_ptr(volatile void *target, void *new)
498 {
499 	void *old = *(void **)target;
500 	*(void **)target = new;
501 	return (old);
502 }
503 
504 ulong_t
atomic_swap_ulong(volatile ulong_t * target,ulong_t new)505 atomic_swap_ulong(volatile ulong_t *target, ulong_t new)
506 {
507 	ulong_t old = *target;
508 	*target = new;
509 	return (old);
510 }
511 
512 int
atomic_set_long_excl(volatile ulong_t * target,uint_t value)513 atomic_set_long_excl(volatile ulong_t *target, uint_t value)
514 {
515 	ulong_t bit = (1UL << value);
516 	if ((*target & bit) != 0)
517 		return (-1);
518 	*target |= bit;
519 	return (0);
520 }
521 
522 int
atomic_clear_long_excl(volatile ulong_t * target,uint_t value)523 atomic_clear_long_excl(volatile ulong_t *target, uint_t value)
524 {
525 	ulong_t bit = (1UL << value);
526 	if ((*target & bit) == 0)
527 		return (-1);
528 	*target &= ~bit;
529 	return (0);
530 }
531 
532 #if !defined(_KERNEL)
533 
534 void
membar_enter(void)535 membar_enter(void)
536 {}
537 
538 void
membar_exit(void)539 membar_exit(void)
540 {}
541 
542 void
membar_producer(void)543 membar_producer(void)
544 {}
545 
546 void
membar_consumer(void)547 membar_consumer(void)
548 {}
549 
550 #endif	/* _KERNEL */
551 
552 #endif	/* __lint */
553