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