2 * atomic.h: Atomic operations
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
10 #ifndef _WAPI_ATOMIC_H_
11 #define _WAPI_ATOMIC_H_
15 #include "mono/io-layer/wapi.h"
17 #if defined(__i386__) || defined(__x86_64__)
18 #define WAPI_ATOMIC_ASM
21 * NB: The *Pointer() functions here assume that
22 * sizeof(pointer)==sizeof(gint32)
24 * NB2: These asm functions assume 486+ (some of the opcodes dont
25 * exist on 386). If this becomes an issue, we can get configure to
26 * fall back to the non-atomic C versions of these calls.
29 static inline gint32 InterlockedCompareExchange(volatile gint32 *dest,
30 gint32 exch, gint32 comp)
34 __asm__ __volatile__ ("lock; cmpxchgl %2, %0"
35 : "=m" (*dest), "=a" (old)
36 : "r" (exch), "m" (*dest), "a" (comp));
40 static inline gpointer InterlockedCompareExchangePointer(volatile gpointer *dest, gpointer exch, gpointer comp)
44 __asm__ __volatile__ ("lock; "
51 : "=m" (*dest), "=a" (old)
52 : "r" (exch), "m" (*dest), "a" (comp));
57 static inline gint32 InterlockedIncrement(volatile gint32 *val)
61 __asm__ __volatile__ ("lock; xaddl %0, %1"
62 : "=r" (tmp), "=m" (*val)
63 : "0" (1), "m" (*val));
68 static inline gint32 InterlockedDecrement(volatile gint32 *val)
72 __asm__ __volatile__ ("lock; xaddl %0, %1"
73 : "=r" (tmp), "=m" (*val)
74 : "0" (-1), "m" (*val));
81 * http://msdn.microsoft.com/library/en-us/dnmag00/html/win320700.asp?frame=true
82 * for the reasons for using cmpxchg and a loop here.
84 * That url is no longer valid, but it's still in the google cache at the
85 * moment: http://www.google.com/search?q=cache:http://msdn.microsoft.com/library/en-us/dnmag00/html/win320700.asp?frame=true
87 * For the time being, http://msdn.microsoft.com/msdnmag/issues/0700/Win32/
88 * might work. Bet it will change soon enough though.
90 static inline gint32 InterlockedExchange(volatile gint32 *val, gint32 new_val)
94 __asm__ __volatile__ ("1:; lock; cmpxchgl %2, %0; jne 1b"
95 : "=m" (*val), "=a" (ret)
96 : "r" (new_val), "m" (*val), "a" (*val));
101 static inline gpointer InterlockedExchangePointer(volatile gpointer *val,
106 __asm__ __volatile__ ("1:; lock; "
113 : "=m" (*val), "=a" (ret)
114 : "r" (new_val), "m" (*val), "a" (*val));
119 static inline gint32 InterlockedExchangeAdd(volatile gint32 *val, gint32 add)
123 __asm__ __volatile__ ("lock; xaddl %0, %1"
124 : "=r" (ret), "=m" (*val)
125 : "0" (add), "m" (*val));
130 #elif (defined(sparc) || defined (__sparc__)) && defined(__GNUC__)
131 #define WAPI_ATOMIC_ASM
134 static inline gint32 InterlockedCompareExchange(volatile gint32 *_dest, gint32 _exch, gint32 _comp)
136 register volatile gint32 *dest asm("g1") = _dest;
137 register gint32 comp asm("o4") = _comp;
138 register gint32 exch asm("o5") = _exch;
140 __asm__ __volatile__(
141 /* cas [%%g1], %%o4, %%o5 */
144 : "0" (exch), "r" (dest), "r" (comp)
151 static inline gpointer InterlockedCompareExchangePointer(volatile gpointer *_dest, gpointer _exch, gpointer _comp)
153 register volatile gpointer *dest asm("g1") = _dest;
154 register gpointer comp asm("o4") = _comp;
155 register gpointer exch asm("o5") = _exch;
157 __asm__ __volatile__(
159 /* casx [%%g1], %%o4, %%o5 */
162 /* cas [%%g1], %%o4, %%o5 */
166 : "0" (exch), "r" (dest), "r" (comp)
173 static inline gint32 InterlockedIncrement(volatile gint32 *_dest)
175 register volatile gint32 *dest asm("g1") = _dest;
176 register gint32 tmp asm("o4");
177 register gint32 ret asm("o5");
179 __asm__ __volatile__(
180 "1: ld [%%g1], %%o4\n\t"
181 " add %%o4, 1, %%o5\n\t"
182 /* cas [%%g1], %%o4, %%o5 */
183 " .word 0xdbe0500c\n\t"
184 " cmp %%o4, %%o5\n\t"
187 : "=&r" (tmp), "=&r" (ret)
195 static inline gint32 InterlockedDecrement(volatile gint32 *_dest)
197 register volatile gint32 *dest asm("g1") = _dest;
198 register gint32 tmp asm("o4");
199 register gint32 ret asm("o5");
201 __asm__ __volatile__(
202 "1: ld [%%g1], %%o4\n\t"
203 " sub %%o4, 1, %%o5\n\t"
204 /* cas [%%g1], %%o4, %%o5 */
205 " .word 0xdbe0500c\n\t"
206 " cmp %%o4, %%o5\n\t"
209 : "=&r" (tmp), "=&r" (ret)
217 static inline gint32 InterlockedExchange(volatile gint32 *_dest, gint32 exch)
219 register volatile gint32 *dest asm("g1") = _dest;
220 register gint32 tmp asm("o4");
221 register gint32 ret asm("o5");
223 __asm__ __volatile__(
224 "1: ld [%%g1], %%o4\n\t"
226 /* cas [%%g1], %%o4, %%o5 */
227 " .word 0xdbe0500c\n\t"
228 " cmp %%o4, %%o5\n\t"
231 : "=&r" (tmp), "=&r" (ret)
232 : "r" (dest), "r" (exch)
239 static inline gpointer InterlockedExchangePointer(volatile gpointer *_dest, gpointer exch)
241 register volatile gpointer *dest asm("g1") = _dest;
242 register gpointer tmp asm("o4");
243 register gpointer ret asm("o5");
245 __asm__ __volatile__(
247 "1: ldx [%%g1], %%o4\n\t"
249 "1: ld [%%g1], %%o4\n\t"
253 /* casx [%%g1], %%o4, %%o5 */
254 " .word 0xdbf0500c\n\t"
256 /* cas [%%g1], %%o4, %%o5 */
257 " .word 0xdbe0500c\n\t"
259 " cmp %%o4, %%o5\n\t"
262 : "=&r" (tmp), "=&r" (ret)
263 : "r" (dest), "r" (exch)
270 static inline gint32 InterlockedExchangeAdd(volatile gint32 *_dest, gint32 add)
272 register volatile gint32 *dest asm("g1") = _dest;
273 register gint32 tmp asm("o4");
274 register gint32 ret asm("o5");
276 __asm__ __volatile__(
277 "1: ld [%%g1], %%o4\n\t"
278 " add %%o4, %3, %%o5\n\t"
279 /* cas [%%g1], %%o4, %%o5 */
280 " .word 0xdbe0500c\n\t"
281 " cmp %%o4, %%o5\n\t"
283 " add %%o5, %3, %%o5"
284 : "=&r" (tmp), "=&r" (ret)
285 : "r" (dest), "r" (add)
293 #define WAPI_ATOMIC_ASM
296 InterlockedCompareExchange(volatile gint32 *dest,
297 gint32 exch, gint32 comp)
301 __asm__ __volatile__ ("\tLA\t1,%0\n"
308 : "+m" (*dest), "=r" (old)
309 : "r" (exch), "r" (comp)
315 static inline gpointer
316 InterlockedCompareExchangePointer(volatile gpointer *dest,
317 gpointer exch, gpointer comp)
321 __asm__ __volatile__ ("\tLA\t1,%0\n"
328 : "+m" (*dest), "=r" (old)
329 : "r" (exch), "r" (comp)
334 static inline gpointer
335 InterlockedCompareExchangePointer(volatile gpointer *dest,
341 __asm__ __volatile__ ("\tLA\t1,%0\n"
345 "\tCSG\t%1,%2,0(1)\n"
348 : "+m" (*dest), "=r" (old)
349 : "r" (exch), "r" (comp)
358 InterlockedIncrement(volatile gint32 *val)
362 __asm__ __volatile__ ("\tLA\t2,%1\n"
369 : "=r" (tmp), "+m" (*val)
376 InterlockedIncrement(volatile gint32 *val)
380 __asm__ __volatile__ ("\tLA\t2,%1\n"
387 : "=r" (tmp), "+m" (*val)
396 InterlockedDecrement(volatile gint32 *val)
400 __asm__ __volatile__ ("\tLA\t2,%1\n"
407 : "=r" (tmp), "+m" (*val)
414 InterlockedDecrement(volatile gint32 *val)
418 __asm__ __volatile__ ("\tLA\t2,%1\n"
425 : "=r" (tmp), "+m" (*val)
433 InterlockedExchange(volatile gint32 *val, gint32 new_val)
437 __asm__ __volatile__ ("\tLA\t1,%0\n"
441 : "+m" (*val), "=r" (ret)
449 static inline gpointer
450 InterlockedExchangePointer(volatile gpointer *val, gpointer new_val)
454 __asm__ __volatile__ ("\tLA\t1,%0\n"
458 : "+m" (*val), "=r" (ret)
465 static inline gpointer
466 InterlockedExchangePointer(volatile gpointer *val, gpointer new_val)
470 __asm__ __volatile__ ("\tLA\t1,%0\n"
472 "\tCSG\t%1,%2,0(1)\n"
474 : "+m" (*val), "=r" (ret)
484 InterlockedExchangeAdd(volatile gint32 *val, gint32 add)
488 __asm__ __volatile__ ("\tLA\t2,%1\n"
494 : "=r" (ret), "+m" (*val)
502 InterlockedExchangeAdd(volatile gint32 *val, gint32 add)
506 __asm__ __volatile__ ("\tLA\t2,%1\n"
512 : "=r" (ret), "+m" (*val)
520 #elif defined(__ppc__) || defined (__powerpc__)
521 #define WAPI_ATOMIC_ASM
523 static inline gint32 InterlockedIncrement(volatile gint32 *val)
525 gint32 result = 0, tmp;
527 __asm__ __volatile__ ("\n1:\n\t"
528 "lwarx %0, 0, %2\n\t"
530 "stwcx. %1, 0, %2\n\t"
532 : "=&b" (result), "=&b" (tmp): "r" (val): "cc", "memory");
536 static inline gint32 InterlockedDecrement(volatile gint32 *val)
538 gint32 result = 0, tmp;
540 __asm__ __volatile__ ("\n1:\n\t"
541 "lwarx %0, 0, %2\n\t"
542 "addi %1, %0, -1\n\t"
543 "stwcx. %1, 0, %2\n\t"
545 : "=&b" (result), "=&b" (tmp): "r" (val): "cc", "memory");
549 #define InterlockedCompareExchangePointer(dest,exch,comp) InterlockedCompareExchange((volatile gint32 *)(dest), (gint32)(exch), (gint32)(comp))
551 static inline gint32 InterlockedCompareExchange(volatile gint32 *dest,
552 gint32 exch, gint32 comp) {
555 __asm__ __volatile__ ("\n1:\n\t"
556 "lwarx %0, 0, %1\n\t"
559 "stwcx. %3, 0, %1\n\t"
563 : "b" (dest), "r" (comp), "r" (exch): "cc", "memory");
567 static inline gint32 InterlockedExchange(volatile gint32 *dest, gint32 exch)
571 __asm__ __volatile__ ("\n1:\n\t"
572 "lwarx %0, 0, %2\n\t"
573 "stwcx. %3, 0, %2\n\t"
575 : "=r" (tmp) : "0" (tmp), "b" (dest), "r" (exch): "cc", "memory");
578 #define InterlockedExchangePointer(dest,exch) InterlockedExchange((volatile gint32 *)(dest), (gint32)(exch))
580 static inline gint32 InterlockedExchangeAdd(volatile gint32 *dest, gint32 add)
583 __asm__ __volatile__ ("\n1:\n\t"
584 "lwarx %0, 0, %2\n\t"
586 "stwcx. %1, 0, %2\n\t"
588 : "=&r" (result), "=&r" (tmp)
589 : "r" (dest), "r" (add) : "cc", "memory");
593 #elif defined(__arm__)
594 #define WAPI_ATOMIC_ASM
596 static inline gint32 InterlockedCompareExchange(volatile gint32 *dest, gint32 exch, gint32 comp)
600 __asm__ __volatile__ ( "0:\n\t"
604 "swp %0, %3, [%2]\n\t"
606 "swpne %3, %0, [%2]\n\t"
609 : "=&r" (a), "=&r" (b)
610 : "r" (dest), "r" (exch), "r" (comp)
616 static inline gpointer InterlockedCompareExchangePointer(volatile gpointer *dest, gpointer exch, gpointer comp)
620 __asm__ __volatile__ ( "0:\n\t"
624 "swpeq %0, %3, [%2]\n\t"
626 "swpne %3, %0, [%2]\n\t"
629 : "=&r" (a), "=&r" (b)
630 : "r" (dest), "r" (exch), "r" (comp)
636 static inline gint32 InterlockedIncrement(volatile gint32 *dest)
640 __asm__ __volatile__ ( "0:\n\t"
643 "swp %2, %1, [%3]\n\t"
645 "swpne %1, %2, [%3]\n\t"
647 : "=&r" (a), "=&r" (b), "=&r" (c)
648 : "r" (dest), "r" (1)
654 static inline gint32 InterlockedDecrement(volatile gint32 *dest)
658 __asm__ __volatile__ ( "0:\n\t"
661 "swp %2, %1, [%3]\n\t"
663 "swpne %1, %2, [%3]\n\t"
665 : "=&r" (a), "=&r" (b), "=&r" (c)
666 : "r" (dest), "r" (-1)
672 static inline gint32 InterlockedExchange(volatile gint32 *dest, gint32 exch)
676 __asm__ __volatile__ ( "swp %0, %2, [%1]"
678 : "r" (dest), "r" (exch));
683 static inline gpointer InterlockedExchangePointer(volatile gpointer *dest, gpointer exch)
687 __asm__ __volatile__ ( "swp %0, %2, [%1]"
689 : "r" (dest), "r" (exch));
694 static inline gint32 InterlockedExchangeAdd(volatile gint32 *dest, gint32 add)
698 __asm__ __volatile__ ( "0:\n\t"
701 "swp %2, %1, [%3]\n\t"
703 "swpne %1, %2, [%3]\n\t"
705 : "=&r" (a), "=&r" (b), "=&r" (c)
706 : "r" (dest), "r" (add)
712 #elif defined(__ia64__)
713 #define WAPI_ATOMIC_ASM
715 #ifdef __INTEL_COMPILER
716 #include <ia64intrin.h>
719 static inline gint32 InterlockedCompareExchange(gint32 volatile *dest,
720 gint32 exch, gint32 comp)
724 #ifdef __INTEL_COMPILER
725 old = _InterlockedCompareExchange (dest, exch, comp);
727 asm volatile ("mov ar.ccv = %2 ;;\n\t"
728 "cmpxchg4.acq %0 = [%1], %3, ar.ccv\n\t"
729 : "=r" (old) : "r" (dest), "r" (comp), "r" (exch));
735 static inline gpointer InterlockedCompareExchangePointer(gpointer volatile *dest,
736 gpointer exch, gpointer comp)
740 #ifdef __INTEL_COMPILER
741 old = _InterlockedCompareExchangePointer (dest, exch, comp);
743 asm volatile ("mov ar.ccv = %2 ;;\n\t"
744 "cmpxchg8.acq %0 = [%1], %3, ar.ccv\n\t"
745 : "=r" (old) : "r" (dest), "r" (comp), "r" (exch));
751 static inline gint32 InterlockedIncrement(gint32 volatile *val)
753 #ifdef __INTEL_COMPILER
754 return _InterlockedIncrement (val);
760 } while (InterlockedCompareExchange (val, old + 1, old) != old);
766 static inline gint32 InterlockedDecrement(gint32 volatile *val)
768 #ifdef __INTEL_COMPILER
769 return _InterlockedDecrement (val);
775 } while (InterlockedCompareExchange (val, old - 1, old) != old);
781 static inline gint32 InterlockedExchange(gint32 volatile *dest, gint32 new_val)
783 #ifdef __INTEL_COMPILER
784 return _InterlockedExchange (dest, new_val);
790 } while (InterlockedCompareExchange (dest, new_val, res) != res);
796 static inline gpointer InterlockedExchangePointer(gpointer volatile *dest, gpointer new_val)
798 #ifdef __INTEL_COMPILER
799 return (gpointer)_InterlockedExchange64 ((gint64*)dest, (gint64)new_val);
805 } while (InterlockedCompareExchangePointer (dest, new_val, res) != res);
811 static inline gint32 InterlockedExchangeAdd(gint32 volatile *val, gint32 add)
815 #ifdef __INTEL_COMPILER
816 old = _InterlockedExchangeAdd (val, add);
820 } while (InterlockedCompareExchange (val, old + add, old) != old);
828 extern gint32 InterlockedCompareExchange(volatile gint32 *dest, gint32 exch, gint32 comp);
829 extern gpointer InterlockedCompareExchangePointer(volatile gpointer *dest, gpointer exch, gpointer comp);
830 extern gint32 InterlockedIncrement(volatile gint32 *dest);
831 extern gint32 InterlockedDecrement(volatile gint32 *dest);
832 extern gint32 InterlockedExchange(volatile gint32 *dest, gint32 exch);
833 extern gpointer InterlockedExchangePointer(volatile gpointer *dest, gpointer exch);
834 extern gint32 InterlockedExchangeAdd(volatile gint32 *dest, gint32 add);
836 #if defined(__hpux) && !defined(__GNUC__)
837 #define WAPI_ATOMIC_ASM
842 #endif /* _WAPI_ATOMIC_H_ */