xref: /titanic_44/usr/src/common/atomic/atomic.c (revision b5a2d8455dfa3190fc977c4bec53e91c99012767)
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
38 atomic_inc_8(volatile uint8_t *target)
39 { (*target)++; }
40 
41 void
42 atomic_inc_uchar(volatile uchar_t *target)
43 { (*target)++; }
44 
45 void
46 atomic_inc_16(volatile uint16_t *target)
47 { (*target)++; }
48 
49 void
50 atomic_inc_ushort(volatile ushort_t *target)
51 { (*target)++; }
52 
53 void
54 atomic_inc_32(volatile uint32_t *target)
55 { (*target)++; }
56 
57 void
58 atomic_inc_uint(volatile uint_t *target)
59 { (*target)++; }
60 
61 void
62 atomic_inc_ulong(volatile ulong_t *target)
63 { (*target)++; }
64 
65 void
66 atomic_inc_64(volatile uint64_t *target)
67 { (*target)++; }
68 
69 void
70 atomic_dec_8(volatile uint8_t *target)
71 { (*target)--; }
72 
73 void
74 atomic_dec_uchar(volatile uchar_t *target)
75 { (*target)--; }
76 
77 void
78 atomic_dec_16(volatile uint16_t *target)
79 { (*target)--; }
80 
81 void
82 atomic_dec_ushort(volatile ushort_t *target)
83 { (*target)--; }
84 
85 void
86 atomic_dec_32(volatile uint32_t *target)
87 { (*target)--; }
88 
89 void
90 atomic_dec_uint(volatile uint_t *target)
91 { (*target)--; }
92 
93 void
94 atomic_dec_ulong(volatile ulong_t *target)
95 { (*target)--; }
96 
97 void
98 atomic_dec_64(volatile uint64_t *target)
99 { (*target)--; }
100 
101 void
102 atomic_add_8(volatile uint8_t *target, int8_t value)
103 { *target += value; }
104 
105 void
106 atomic_add_char(volatile uchar_t *target, signed char value)
107 { *target += value; }
108 
109 void
110 atomic_add_16(volatile uint16_t *target, int16_t delta)
111 { *target += delta; }
112 
113 void
114 atomic_add_ushort(volatile ushort_t *target, short value)
115 { *target += value; }
116 
117 void
118 atomic_add_32(volatile uint32_t *target, int32_t delta)
119 { *target += delta; }
120 
121 void
122 atomic_add_ptr(volatile void *target, ssize_t value)
123 { *(caddr_t *)target += value; }
124 
125 void
126 atomic_add_long(volatile ulong_t *target, long delta)
127 { *target += delta; }
128 
129 void
130 atomic_add_64(volatile uint64_t *target, int64_t delta)
131 { *target += delta; }
132 
133 void
134 atomic_or_8(volatile uint8_t *target, uint8_t bits)
135 { *target |= bits; }
136 
137 void
138 atomic_or_uchar(volatile uchar_t *target, uchar_t bits)
139 { *target |= bits; }
140 
141 void
142 atomic_or_16(volatile uint16_t *target, uint16_t bits)
143 { *target |= bits; }
144 
145 void
146 atomic_or_ushort(volatile ushort_t *target, ushort_t bits)
147 { *target |= bits; }
148 
149 void
150 atomic_or_32(volatile uint32_t *target, uint32_t bits)
151 { *target |= bits; }
152 
153 void
154 atomic_or_uint(volatile uint_t *target, uint_t bits)
155 { *target |= bits; }
156 
157 void
158 atomic_or_ulong(volatile ulong_t *target, ulong_t bits)
159 { *target |= bits; }
160 
161 void
162 atomic_or_64(volatile uint64_t *target, uint64_t bits)
163 { *target |= bits; }
164 
165 void
166 atomic_and_8(volatile uint8_t *target, uint8_t bits)
167 { *target &= bits; }
168 
169 void
170 atomic_and_uchar(volatile uchar_t *target, uchar_t bits)
171 { *target &= bits; }
172 
173 void
174 atomic_and_16(volatile uint16_t *target, uint16_t bits)
175 { *target &= bits; }
176 
177 void
178 atomic_and_ushort(volatile ushort_t *target, ushort_t bits)
179 { *target &= bits; }
180 
181 void
182 atomic_and_32(volatile uint32_t *target, uint32_t bits)
183 { *target &= bits; }
184 
185 void
186 atomic_and_uint(volatile uint_t *target, uint_t bits)
187 { *target &= bits; }
188 
189 void
190 atomic_and_ulong(volatile ulong_t *target, ulong_t bits)
191 { *target &= bits; }
192 
193 void
194 atomic_and_64(volatile uint64_t *target, uint64_t bits)
195 { *target &= bits; }
196 
197 uint8_t
198 atomic_inc_8_nv(volatile uint8_t *target)
199 { return (++(*target)); }
200 
201 uchar_t
202 atomic_inc_uchar_nv(volatile uchar_t *target)
203 { return (++(*target)); }
204 
205 uint16_t
206 atomic_inc_16_nv(volatile uint16_t *target)
207 { return (++(*target)); }
208 
209 ushort_t
210 atomic_inc_ushort_nv(volatile ushort_t *target)
211 { return (++(*target)); }
212 
213 uint32_t
214 atomic_inc_32_nv(volatile uint32_t *target)
215 { return (++(*target)); }
216 
217 uint_t
218 atomic_inc_uint_nv(volatile uint_t *target)
219 { return (++(*target)); }
220 
221 ulong_t
222 atomic_inc_ulong_nv(volatile ulong_t *target)
223 { return (++(*target)); }
224 
225 uint64_t
226 atomic_inc_64_nv(volatile uint64_t *target)
227 { return (++(*target)); }
228 
229 uint8_t
230 atomic_dec_8_nv(volatile uint8_t *target)
231 { return (--(*target)); }
232 
233 uchar_t
234 atomic_dec_uchar_nv(volatile uchar_t *target)
235 { return (--(*target)); }
236 
237 uint16_t
238 atomic_dec_16_nv(volatile uint16_t *target)
239 { return (--(*target)); }
240 
241 ushort_t
242 atomic_dec_ushort_nv(volatile ushort_t *target)
243 { return (--(*target)); }
244 
245 uint32_t
246 atomic_dec_32_nv(volatile uint32_t *target)
247 { return (--(*target)); }
248 
249 uint_t
250 atomic_dec_uint_nv(volatile uint_t *target)
251 { return (--(*target)); }
252 
253 ulong_t
254 atomic_dec_ulong_nv(volatile ulong_t *target)
255 { return (--(*target)); }
256 
257 uint64_t
258 atomic_dec_64_nv(volatile uint64_t *target)
259 { return (--(*target)); }
260 
261 uint8_t
262 atomic_add_8_nv(volatile uint8_t *target, int8_t value)
263 { return (*target += value); }
264 
265 uchar_t
266 atomic_add_char_nv(volatile uchar_t *target, signed char value)
267 { return (*target += value); }
268 
269 uint16_t
270 atomic_add_16_nv(volatile uint16_t *target, int16_t delta)
271 { return (*target += delta); }
272 
273 ushort_t
274 atomic_add_short_nv(volatile ushort_t *target, short value)
275 { return (*target += value); }
276 
277 uint32_t
278 atomic_add_32_nv(volatile uint32_t *target, int32_t delta)
279 { return (*target += delta); }
280 
281 uint_t
282 atomic_add_int_nv(volatile uint_t *target, int delta)
283 { return (*target += delta); }
284 
285 void *
286 atomic_add_ptr_nv(volatile void *target, ssize_t value)
287 { return (*(caddr_t *)target += value); }
288 
289 ulong_t
290 atomic_add_long_nv(volatile ulong_t *target, long delta)
291 { return (*target += delta); }
292 
293 uint64_t
294 atomic_add_64_nv(volatile uint64_t *target, int64_t delta)
295 { return (*target += delta); }
296 
297 uint8_t
298 atomic_or_8_nv(volatile uint8_t *target, uint8_t value)
299 { return (*target |= value); }
300 
301 uchar_t
302 atomic_or_uchar_nv(volatile uchar_t *target, uchar_t value)
303 { return (*target |= value); }
304 
305 uint16_t
306 atomic_or_16_nv(volatile uint16_t *target, uint16_t value)
307 { return (*target |= value); }
308 
309 ushort_t
310 atomic_or_ushort_nv(volatile ushort_t *target, ushort_t value)
311 { return (*target |= value); }
312 
313 uint32_t
314 atomic_or_32_nv(volatile uint32_t *target, uint32_t value)
315 { return (*target |= value); }
316 
317 uint_t
318 atomic_or_uint_nv(volatile uint_t *target, uint_t value)
319 { return (*target |= value); }
320 
321 ulong_t
322 atomic_or_ulong_nv(volatile ulong_t *target, ulong_t value)
323 { return (*target |= value); }
324 
325 uint64_t
326 atomic_or_64_nv(volatile uint64_t *target, uint64_t value)
327 { return (*target |= value); }
328 
329 uint8_t
330 atomic_and_8_nv(volatile uint8_t *target, uint8_t value)
331 { return (*target &= value); }
332 
333 uchar_t
334 atomic_and_uchar_nv(volatile uchar_t *target, uchar_t value)
335 { return (*target &= value); }
336 
337 uint16_t
338 atomic_and_16_nv(volatile uint16_t *target, uint16_t value)
339 { return (*target &= value); }
340 
341 ushort_t
342 atomic_and_ushort_nv(volatile ushort_t *target, ushort_t value)
343 { return (*target &= value); }
344 
345 uint32_t
346 atomic_and_32_nv(volatile uint32_t *target, uint32_t value)
347 { return (*target &= value); }
348 
349 uint_t
350 atomic_and_uint_nv(volatile uint_t *target, uint_t value)
351 { return (*target &= value); }
352 
353 ulong_t
354 atomic_and_ulong_nv(volatile ulong_t *target, ulong_t value)
355 { return (*target &= value); }
356 
357 uint64_t
358 atomic_and_64_nv(volatile uint64_t *target, uint64_t value)
359 { return (*target &= value); }
360 
361 uint8_t
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
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
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
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
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
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
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
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 *
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
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
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
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
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
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
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
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 *
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
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
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
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
537 membar_enter(void)
538 {}
539 
540 void
541 membar_exit(void)
542 {}
543 
544 void
545 membar_producer(void)
546 {}
547 
548 void
549 membar_consumer(void)
550 {}
551 
552 #endif	/* _KERNEL */
553 
554 #endif	/* __lint */
555