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