2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * from: src/sys/alpha/include/atomic.h,v 1.21.2.3 2005/10/06 18:12:05 jhb
32 #ifndef _MACHINE_ATOMIC_H_
33 #define _MACHINE_ATOMIC_H_
36 #error this file needs sys/cdefs.h as a prerequisite
39 #include <sys/atomic_common.h>
42 * Note: All the 64-bit atomic operations are only atomic when running
43 * in 64-bit mode. It is assumed that code compiled for n32 and n64
44 * fits into this definition and no further safeties are needed.
46 * It is also assumed that the add, subtract and other arithmetic is
47 * done on numbers not pointers. The special rules for n32 pointers
48 * do not have atomic operations defined for them, but generally shouldn't
49 * need atomic operations.
51 #ifndef __MIPS_PLATFORM_SYNC_NOPS
52 #define __MIPS_PLATFORM_SYNC_NOPS ""
58 __asm __volatile (".set noreorder\n"
60 __MIPS_PLATFORM_SYNC_NOPS
65 #define mb() mips_sync()
66 #define wmb() mips_sync()
67 #define rmb() mips_sync()
70 * Various simple arithmetic on memory which is atomic in the presence
71 * of interrupts and SMP safe.
74 void atomic_set_8(__volatile uint8_t *, uint8_t);
75 void atomic_clear_8(__volatile uint8_t *, uint8_t);
76 void atomic_add_8(__volatile uint8_t *, uint8_t);
77 void atomic_subtract_8(__volatile uint8_t *, uint8_t);
79 void atomic_set_16(__volatile uint16_t *, uint16_t);
80 void atomic_clear_16(__volatile uint16_t *, uint16_t);
81 void atomic_add_16(__volatile uint16_t *, uint16_t);
82 void atomic_subtract_16(__volatile uint16_t *, uint16_t);
84 static __inline int atomic_cmpset_8(__volatile uint8_t *, uint8_t, uint8_t);
85 static __inline int atomic_fcmpset_8(__volatile uint8_t *, uint8_t *, uint8_t);
86 static __inline int atomic_cmpset_16(__volatile uint16_t *, uint16_t, uint16_t);
87 static __inline int atomic_fcmpset_16(__volatile uint16_t *, uint16_t *, uint16_t);
90 atomic_set_32(__volatile uint32_t *p, uint32_t v)
95 "1:\tll %0, %3\n\t" /* load old value */
96 "or %0, %2, %0\n\t" /* calculate new value */
97 "sc %0, %1\n\t" /* attempt to store */
98 "beqz %0, 1b\n\t" /* spin if failed */
99 : "=&r" (temp), "=m" (*p)
106 atomic_clear_32(__volatile uint32_t *p, uint32_t v)
112 "1:\tll %0, %3\n\t" /* load old value */
113 "and %0, %2, %0\n\t" /* calculate new value */
114 "sc %0, %1\n\t" /* attempt to store */
115 "beqz %0, 1b\n\t" /* spin if failed */
116 : "=&r" (temp), "=m" (*p)
122 atomic_add_32(__volatile uint32_t *p, uint32_t v)
127 "1:\tll %0, %3\n\t" /* load old value */
128 "addu %0, %2, %0\n\t" /* calculate new value */
129 "sc %0, %1\n\t" /* attempt to store */
130 "beqz %0, 1b\n\t" /* spin if failed */
131 : "=&r" (temp), "=m" (*p)
137 atomic_subtract_32(__volatile uint32_t *p, uint32_t v)
142 "1:\tll %0, %3\n\t" /* load old value */
143 "subu %0, %2\n\t" /* calculate new value */
144 "sc %0, %1\n\t" /* attempt to store */
145 "beqz %0, 1b\n\t" /* spin if failed */
146 : "=&r" (temp), "=m" (*p)
151 static __inline uint32_t
152 atomic_readandclear_32(__volatile uint32_t *addr)
154 uint32_t result,temp;
157 "1:\tll %0,%3\n\t" /* load current value, asserting lock */
158 "li %1,0\n\t" /* value to store */
159 "sc %1,%2\n\t" /* attempt to store */
160 "beqz %1, 1b\n\t" /* if the store failed, spin */
161 : "=&r"(result), "=&r"(temp), "=m" (*addr)
168 static __inline uint32_t
169 atomic_readandset_32(__volatile uint32_t *addr, uint32_t value)
171 uint32_t result,temp;
174 "1:\tll %0,%3\n\t" /* load current value, asserting lock */
176 "sc %1,%2\n\t" /* attempt to store */
177 "beqz %1, 1b\n\t" /* if the store failed, spin */
178 : "=&r"(result), "=&r"(temp), "=m" (*addr)
179 : "m" (*addr), "r" (value)
185 #if defined(__mips_n64) || defined(__mips_n32)
187 atomic_set_64(__volatile uint64_t *p, uint64_t v)
193 "lld %0, %3\n\t" /* load old value */
194 "or %0, %2, %0\n\t" /* calculate new value */
195 "scd %0, %1\n\t" /* attempt to store */
196 "beqz %0, 1b\n\t" /* spin if failed */
197 : "=&r" (temp), "=m" (*p)
204 atomic_clear_64(__volatile uint64_t *p, uint64_t v)
211 "lld %0, %3\n\t" /* load old value */
212 "and %0, %2, %0\n\t" /* calculate new value */
213 "scd %0, %1\n\t" /* attempt to store */
214 "beqz %0, 1b\n\t" /* spin if failed */
215 : "=&r" (temp), "=m" (*p)
221 atomic_add_64(__volatile uint64_t *p, uint64_t v)
227 "lld %0, %3\n\t" /* load old value */
228 "daddu %0, %2, %0\n\t" /* calculate new value */
229 "scd %0, %1\n\t" /* attempt to store */
230 "beqz %0, 1b\n\t" /* spin if failed */
231 : "=&r" (temp), "=m" (*p)
237 atomic_subtract_64(__volatile uint64_t *p, uint64_t v)
243 "lld %0, %3\n\t" /* load old value */
244 "dsubu %0, %2\n\t" /* calculate new value */
245 "scd %0, %1\n\t" /* attempt to store */
246 "beqz %0, 1b\n\t" /* spin if failed */
247 : "=&r" (temp), "=m" (*p)
252 static __inline uint64_t
253 atomic_readandclear_64(__volatile uint64_t *addr)
255 uint64_t result,temp;
259 "lld %0, %3\n\t" /* load old value */
260 "li %1, 0\n\t" /* value to store */
261 "scd %1, %2\n\t" /* attempt to store */
262 "beqz %1, 1b\n\t" /* if the store failed, spin */
263 : "=&r"(result), "=&r"(temp), "=m" (*addr)
270 static __inline uint64_t
271 atomic_readandset_64(__volatile uint64_t *addr, uint64_t value)
273 uint64_t result,temp;
277 "lld %0,%3\n\t" /* Load old value*/
279 "scd %1,%2\n\t" /* attempt to store */
280 "beqz %1, 1b\n\t" /* if the store failed, spin */
281 : "=&r"(result), "=&r"(temp), "=m" (*addr)
282 : "m" (*addr), "r" (value)
289 #define ATOMIC_ACQ_REL(NAME, WIDTH) \
290 static __inline void \
291 atomic_##NAME##_acq_##WIDTH(__volatile uint##WIDTH##_t *p, uint##WIDTH##_t v)\
293 atomic_##NAME##_##WIDTH(p, v); \
297 static __inline void \
298 atomic_##NAME##_rel_##WIDTH(__volatile uint##WIDTH##_t *p, uint##WIDTH##_t v)\
301 atomic_##NAME##_##WIDTH(p, v); \
304 /* Variants of simple arithmetic with memory barriers. */
305 ATOMIC_ACQ_REL(set, 8)
306 ATOMIC_ACQ_REL(clear, 8)
307 ATOMIC_ACQ_REL(add, 8)
308 ATOMIC_ACQ_REL(subtract, 8)
309 ATOMIC_ACQ_REL(set, 16)
310 ATOMIC_ACQ_REL(clear, 16)
311 ATOMIC_ACQ_REL(add, 16)
312 ATOMIC_ACQ_REL(subtract, 16)
313 ATOMIC_ACQ_REL(set, 32)
314 ATOMIC_ACQ_REL(clear, 32)
315 ATOMIC_ACQ_REL(add, 32)
316 ATOMIC_ACQ_REL(subtract, 32)
317 #if defined(__mips_n64) || defined(__mips_n32)
318 ATOMIC_ACQ_REL(set, 64)
319 ATOMIC_ACQ_REL(clear, 64)
320 ATOMIC_ACQ_REL(add, 64)
321 ATOMIC_ACQ_REL(subtract, 64)
324 #undef ATOMIC_ACQ_REL
327 * We assume that a = b will do atomic loads and stores.
329 #define ATOMIC_STORE_LOAD(WIDTH) \
330 static __inline uint##WIDTH##_t \
331 atomic_load_acq_##WIDTH(__volatile uint##WIDTH##_t *p) \
340 static __inline void \
341 atomic_store_rel_##WIDTH(__volatile uint##WIDTH##_t *p, uint##WIDTH##_t v)\
347 ATOMIC_STORE_LOAD(32)
348 ATOMIC_STORE_LOAD(64)
349 #undef ATOMIC_STORE_LOAD
352 #define atomic_load_64 atomic_load_acq_64
356 * Atomically compare the value stored at *p with cmpval and if the
357 * two values are equal, update the value of *p with newval. Returns
358 * zero if the compare failed, nonzero otherwise.
361 atomic_cmpset_32(__volatile uint32_t *p, uint32_t cmpval, uint32_t newval)
366 "1:\tll %0, %4\n\t" /* load old value */
367 "bne %0, %2, 2f\n\t" /* compare */
368 "move %0, %3\n\t" /* value to store */
369 "sc %0, %1\n\t" /* attempt to store */
370 "beqz %0, 1b\n\t" /* if it failed, spin */
375 : "=&r" (ret), "=m" (*p)
376 : "r" (cmpval), "r" (newval), "m" (*p)
383 * Atomically compare the value stored at *p with cmpval and if the
384 * two values are equal, update the value of *p with newval. Returns
385 * zero if the compare failed, nonzero otherwise.
388 atomic_fcmpset_32(__volatile uint32_t *p, uint32_t *cmpval, uint32_t newval)
393 * The following sequence (similar to that in atomic_fcmpset_64) will
394 * attempt to update the value of *p with newval if the comparison
395 * succeeds. Note that they'll exit regardless of whether the store
396 * actually succeeded, leaving *cmpval untouched. This is in line with
397 * the documentation of atomic_fcmpset_<type>() in atomic(9) for ll/sc
401 "ll %0, %1\n\t" /* load old value */
402 "bne %0, %4, 1f\n\t" /* compare */
403 "move %0, %3\n\t" /* value to store */
404 "sc %0, %1\n\t" /* attempt to store */
405 "j 2f\n\t" /* exit regardless of success */
406 "nop\n\t" /* avoid delay slot accident */
408 "sw %0, %2\n\t" /* save old value */
411 : "=&r" (ret), "+m" (*p), "=m" (*cmpval)
412 : "r" (newval), "r" (*cmpval)
417 #define ATOMIC_CMPSET_ACQ_REL(WIDTH) \
418 static __inline int \
419 atomic_cmpset_acq_##WIDTH(__volatile uint##WIDTH##_t *p, \
420 uint##WIDTH##_t cmpval, uint##WIDTH##_t newval) \
424 retval = atomic_cmpset_##WIDTH(p, cmpval, newval); \
429 static __inline int \
430 atomic_cmpset_rel_##WIDTH(__volatile uint##WIDTH##_t *p, \
431 uint##WIDTH##_t cmpval, uint##WIDTH##_t newval) \
434 return (atomic_cmpset_##WIDTH(p, cmpval, newval)); \
437 #define ATOMIC_FCMPSET_ACQ_REL(WIDTH) \
438 static __inline int \
439 atomic_fcmpset_acq_##WIDTH(__volatile uint##WIDTH##_t *p, \
440 uint##WIDTH##_t *cmpval, uint##WIDTH##_t newval) \
444 retval = atomic_fcmpset_##WIDTH(p, cmpval, newval); \
449 static __inline int \
450 atomic_fcmpset_rel_##WIDTH(__volatile uint##WIDTH##_t *p, \
451 uint##WIDTH##_t *cmpval, uint##WIDTH##_t newval) \
454 return (atomic_fcmpset_##WIDTH(p, cmpval, newval)); \
458 * Atomically compare the value stored at *p with cmpval and if the
459 * two values are equal, update the value of *p with newval. Returns
460 * zero if the compare failed, nonzero otherwise.
462 ATOMIC_CMPSET_ACQ_REL(8);
463 ATOMIC_CMPSET_ACQ_REL(16);
464 ATOMIC_CMPSET_ACQ_REL(32);
465 ATOMIC_FCMPSET_ACQ_REL(8);
466 ATOMIC_FCMPSET_ACQ_REL(16);
467 ATOMIC_FCMPSET_ACQ_REL(32);
470 * Atomically add the value of v to the integer pointed to by p and return
471 * the previous value of *p.
473 static __inline uint32_t
474 atomic_fetchadd_32(__volatile uint32_t *p, uint32_t v)
476 uint32_t value, temp;
479 "1:\tll %0, %1\n\t" /* load old value */
480 "addu %2, %3, %0\n\t" /* calculate new value */
481 "sc %2, %1\n\t" /* attempt to store */
482 "beqz %2, 1b\n\t" /* spin if failed */
483 : "=&r" (value), "=m" (*p), "=&r" (temp)
484 : "r" (v), "m" (*p));
488 #if defined(__mips_n64) || defined(__mips_n32)
490 * Atomically compare the value stored at *p with cmpval and if the
491 * two values are equal, update the value of *p with newval. Returns
492 * zero if the compare failed, nonzero otherwise.
495 atomic_cmpset_64(__volatile uint64_t *p, uint64_t cmpval, uint64_t newval)
501 "lld %0, %4\n\t" /* load old value */
502 "bne %0, %2, 2f\n\t" /* compare */
503 "move %0, %3\n\t" /* value to store */
504 "scd %0, %1\n\t" /* attempt to store */
505 "beqz %0, 1b\n\t" /* if it failed, spin */
510 : "=&r" (ret), "=m" (*p)
511 : "r" (cmpval), "r" (newval), "m" (*p)
518 atomic_fcmpset_64(__volatile uint64_t *p, uint64_t *cmpval, uint64_t newval)
523 "lld %0, %1\n\t" /* load old value */
524 "bne %0, %4, 1f\n\t" /* compare */
525 "move %0, %3\n\t" /* value to store */
526 "scd %0, %1\n\t" /* attempt to store */
527 "j 2f\n\t" /* exit regardless of success */
528 "nop\n\t" /* avoid delay slot accident */
530 "sd %0, %2\n\t" /* save old value */
533 : "=&r" (ret), "+m" (*p), "=m" (*cmpval)
534 : "r" (newval), "r" (*cmpval)
541 * Atomically compare the value stored at *p with cmpval and if the
542 * two values are equal, update the value of *p with newval. Returns
543 * zero if the compare failed, nonzero otherwise.
545 ATOMIC_CMPSET_ACQ_REL(64);
546 ATOMIC_FCMPSET_ACQ_REL(64);
549 * Atomically add the value of v to the integer pointed to by p and return
550 * the previous value of *p.
552 static __inline uint64_t
553 atomic_fetchadd_64(__volatile uint64_t *p, uint64_t v)
555 uint64_t value, temp;
559 "lld %0, %1\n\t" /* load old value */
560 "daddu %2, %3, %0\n\t" /* calculate new value */
561 "scd %2, %1\n\t" /* attempt to store */
562 "beqz %2, 1b\n\t" /* spin if failed */
563 : "=&r" (value), "=m" (*p), "=&r" (temp)
564 : "r" (v), "m" (*p));
570 atomic_thread_fence_acq(void)
577 atomic_thread_fence_rel(void)
584 atomic_thread_fence_acq_rel(void)
591 atomic_thread_fence_seq_cst(void)
597 /* Operations on chars. */
598 #define atomic_set_char atomic_set_8
599 #define atomic_set_acq_char atomic_set_acq_8
600 #define atomic_set_rel_char atomic_set_rel_8
601 #define atomic_clear_char atomic_clear_8
602 #define atomic_clear_acq_char atomic_clear_acq_8
603 #define atomic_clear_rel_char atomic_clear_rel_8
604 #define atomic_add_char atomic_add_8
605 #define atomic_add_acq_char atomic_add_acq_8
606 #define atomic_add_rel_char atomic_add_rel_8
607 #define atomic_subtract_char atomic_subtract_8
608 #define atomic_subtract_acq_char atomic_subtract_acq_8
609 #define atomic_subtract_rel_char atomic_subtract_rel_8
610 #define atomic_cmpset_char atomic_cmpset_8
611 #define atomic_cmpset_acq_char atomic_cmpset_acq_8
612 #define atomic_cmpset_rel_char atomic_cmpset_rel_8
613 #define atomic_fcmpset_char atomic_fcmpset_8
614 #define atomic_fcmpset_acq_char atomic_fcmpset_acq_8
615 #define atomic_fcmpset_rel_char atomic_fcmpset_rel_8
617 /* Operations on shorts. */
618 #define atomic_set_short atomic_set_16
619 #define atomic_set_acq_short atomic_set_acq_16
620 #define atomic_set_rel_short atomic_set_rel_16
621 #define atomic_clear_short atomic_clear_16
622 #define atomic_clear_acq_short atomic_clear_acq_16
623 #define atomic_clear_rel_short atomic_clear_rel_16
624 #define atomic_add_short atomic_add_16
625 #define atomic_add_acq_short atomic_add_acq_16
626 #define atomic_add_rel_short atomic_add_rel_16
627 #define atomic_subtract_short atomic_subtract_16
628 #define atomic_subtract_acq_short atomic_subtract_acq_16
629 #define atomic_subtract_rel_short atomic_subtract_rel_16
630 #define atomic_cmpset_short atomic_cmpset_16
631 #define atomic_cmpset_acq_short atomic_cmpset_acq_16
632 #define atomic_cmpset_rel_short atomic_cmpset_rel_16
633 #define atomic_fcmpset_short atomic_fcmpset_16
634 #define atomic_fcmpset_acq_short atomic_fcmpset_acq_16
635 #define atomic_fcmpset_rel_short atomic_fcmpset_rel_16
637 /* Operations on ints. */
638 #define atomic_set_int atomic_set_32
639 #define atomic_set_acq_int atomic_set_acq_32
640 #define atomic_set_rel_int atomic_set_rel_32
641 #define atomic_clear_int atomic_clear_32
642 #define atomic_clear_acq_int atomic_clear_acq_32
643 #define atomic_clear_rel_int atomic_clear_rel_32
644 #define atomic_add_int atomic_add_32
645 #define atomic_add_acq_int atomic_add_acq_32
646 #define atomic_add_rel_int atomic_add_rel_32
647 #define atomic_subtract_int atomic_subtract_32
648 #define atomic_subtract_acq_int atomic_subtract_acq_32
649 #define atomic_subtract_rel_int atomic_subtract_rel_32
650 #define atomic_cmpset_int atomic_cmpset_32
651 #define atomic_cmpset_acq_int atomic_cmpset_acq_32
652 #define atomic_cmpset_rel_int atomic_cmpset_rel_32
653 #define atomic_fcmpset_int atomic_fcmpset_32
654 #define atomic_fcmpset_acq_int atomic_fcmpset_acq_32
655 #define atomic_fcmpset_rel_int atomic_fcmpset_rel_32
656 #define atomic_load_acq_int atomic_load_acq_32
657 #define atomic_store_rel_int atomic_store_rel_32
658 #define atomic_readandclear_int atomic_readandclear_32
659 #define atomic_readandset_int atomic_readandset_32
660 #define atomic_fetchadd_int atomic_fetchadd_32
663 * I think the following is right, even for n32. For n32 the pointers
664 * are still 32-bits, so we need to operate on them as 32-bit quantities,
665 * even though they are sign extended in operation. For longs, there's
666 * no question because they are always 32-bits.
669 /* Operations on longs. */
670 #define atomic_set_long atomic_set_64
671 #define atomic_set_acq_long atomic_set_acq_64
672 #define atomic_set_rel_long atomic_set_rel_64
673 #define atomic_clear_long atomic_clear_64
674 #define atomic_clear_acq_long atomic_clear_acq_64
675 #define atomic_clear_rel_long atomic_clear_rel_64
676 #define atomic_add_long atomic_add_64
677 #define atomic_add_acq_long atomic_add_acq_64
678 #define atomic_add_rel_long atomic_add_rel_64
679 #define atomic_subtract_long atomic_subtract_64
680 #define atomic_subtract_acq_long atomic_subtract_acq_64
681 #define atomic_subtract_rel_long atomic_subtract_rel_64
682 #define atomic_cmpset_long atomic_cmpset_64
683 #define atomic_cmpset_acq_long atomic_cmpset_acq_64
684 #define atomic_cmpset_rel_long atomic_cmpset_rel_64
685 #define atomic_fcmpset_long atomic_fcmpset_64
686 #define atomic_fcmpset_acq_long atomic_fcmpset_acq_64
687 #define atomic_fcmpset_rel_long atomic_fcmpset_rel_64
688 #define atomic_load_acq_long atomic_load_acq_64
689 #define atomic_store_rel_long atomic_store_rel_64
690 #define atomic_fetchadd_long atomic_fetchadd_64
691 #define atomic_readandclear_long atomic_readandclear_64
693 #else /* !__mips_n64 */
695 /* Operations on longs. */
696 #define atomic_set_long(p, v) \
697 atomic_set_32((volatile u_int *)(p), (u_int)(v))
698 #define atomic_set_acq_long(p, v) \
699 atomic_set_acq_32((volatile u_int *)(p), (u_int)(v))
700 #define atomic_set_rel_long(p, v) \
701 atomic_set_rel_32((volatile u_int *)(p), (u_int)(v))
702 #define atomic_clear_long(p, v) \
703 atomic_clear_32((volatile u_int *)(p), (u_int)(v))
704 #define atomic_clear_acq_long(p, v) \
705 atomic_clear_acq_32((volatile u_int *)(p), (u_int)(v))
706 #define atomic_clear_rel_long(p, v) \
707 atomic_clear_rel_32((volatile u_int *)(p), (u_int)(v))
708 #define atomic_add_long(p, v) \
709 atomic_add_32((volatile u_int *)(p), (u_int)(v))
710 #define atomic_add_acq_long(p, v) \
711 atomic_add_32((volatile u_int *)(p), (u_int)(v))
712 #define atomic_add_rel_long(p, v) \
713 atomic_add_32((volatile u_int *)(p), (u_int)(v))
714 #define atomic_subtract_long(p, v) \
715 atomic_subtract_32((volatile u_int *)(p), (u_int)(v))
716 #define atomic_subtract_acq_long(p, v) \
717 atomic_subtract_acq_32((volatile u_int *)(p), (u_int)(v))
718 #define atomic_subtract_rel_long(p, v) \
719 atomic_subtract_rel_32((volatile u_int *)(p), (u_int)(v))
720 #define atomic_cmpset_long(p, cmpval, newval) \
721 atomic_cmpset_32((volatile u_int *)(p), (u_int)(cmpval), \
723 #define atomic_cmpset_acq_long(p, cmpval, newval) \
724 atomic_cmpset_acq_32((volatile u_int *)(p), (u_int)(cmpval), \
726 #define atomic_cmpset_rel_long(p, cmpval, newval) \
727 atomic_cmpset_rel_32((volatile u_int *)(p), (u_int)(cmpval), \
729 #define atomic_fcmpset_long(p, cmpval, newval) \
730 atomic_fcmpset_32((volatile u_int *)(p), (u_int *)(cmpval), \
732 #define atomic_fcmpset_acq_long(p, cmpval, newval) \
733 atomic_fcmpset_acq_32((volatile u_int *)(p), (u_int *)(cmpval), \
735 #define atomic_fcmpset_rel_long(p, cmpval, newval) \
736 atomic_fcmpset_rel_32((volatile u_int *)(p), (u_int *)(cmpval), \
738 #define atomic_load_acq_long(p) \
739 (u_long)atomic_load_acq_32((volatile u_int *)(p))
740 #define atomic_store_rel_long(p, v) \
741 atomic_store_rel_32((volatile u_int *)(p), (u_int)(v))
742 #define atomic_fetchadd_long(p, v) \
743 atomic_fetchadd_32((volatile u_int *)(p), (u_int)(v))
744 #define atomic_readandclear_long(p) \
745 atomic_readandclear_32((volatile u_int *)(p))
747 #endif /* __mips_n64 */
749 /* Operations on pointers. */
750 #define atomic_set_ptr atomic_set_long
751 #define atomic_set_acq_ptr atomic_set_acq_long
752 #define atomic_set_rel_ptr atomic_set_rel_long
753 #define atomic_clear_ptr atomic_clear_long
754 #define atomic_clear_acq_ptr atomic_clear_acq_long
755 #define atomic_clear_rel_ptr atomic_clear_rel_long
756 #define atomic_add_ptr atomic_add_long
757 #define atomic_add_acq_ptr atomic_add_acq_long
758 #define atomic_add_rel_ptr atomic_add_rel_long
759 #define atomic_subtract_ptr atomic_subtract_long
760 #define atomic_subtract_acq_ptr atomic_subtract_acq_long
761 #define atomic_subtract_rel_ptr atomic_subtract_rel_long
762 #define atomic_cmpset_ptr atomic_cmpset_long
763 #define atomic_cmpset_acq_ptr atomic_cmpset_acq_long
764 #define atomic_cmpset_rel_ptr atomic_cmpset_rel_long
765 #define atomic_fcmpset_ptr atomic_fcmpset_long
766 #define atomic_fcmpset_acq_ptr atomic_fcmpset_acq_long
767 #define atomic_fcmpset_rel_ptr atomic_fcmpset_rel_long
768 #define atomic_load_acq_ptr atomic_load_acq_long
769 #define atomic_store_rel_ptr atomic_store_rel_long
770 #define atomic_readandclear_ptr atomic_readandclear_long
772 static __inline unsigned int
773 atomic_swap_int(volatile unsigned int *ptr, const unsigned int value)
779 while (!atomic_fcmpset_int(ptr, &retval, value))
784 static __inline uint32_t
785 atomic_swap_32(volatile uint32_t *ptr, const uint32_t value)
791 while (!atomic_fcmpset_32(ptr, &retval, value))
796 #if defined(__mips_n64) || defined(__mips_n32)
797 static __inline uint64_t
798 atomic_swap_64(volatile uint64_t *ptr, const uint64_t value)
804 while (!atomic_fcmpset_64(ptr, &retval, value))
811 static __inline unsigned long
812 atomic_swap_long(volatile unsigned long *ptr, const unsigned long value)
814 unsigned long retval;
818 while (!atomic_fcmpset_64((volatile uint64_t *)ptr,
819 (uint64_t *)&retval, value))
824 static __inline unsigned long
825 atomic_swap_long(volatile unsigned long *ptr, const unsigned long value)
827 unsigned long retval;
831 while (!atomic_fcmpset_32((volatile uint32_t *)ptr,
832 (uint32_t *)&retval, value))
837 #define atomic_swap_ptr(ptr, value) atomic_swap_long((unsigned long *)(ptr), value)
839 #include <sys/_atomic_subword.h>
841 #endif /* ! _MACHINE_ATOMIC_H_ */