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