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__)
131 #define WAPI_ATOMIC_ASM
134 #define BEGIN_SPIN(tmp,lock) \
135 __asm__ __volatile__("1: ldstub [%1],%0\n\t" \
143 #define END_SPIN(lock) \
144 __asm__ __volatile__("stb %%g0, [%0]" \
149 static inline void begin_spin(volatile unsigned char *lock)
151 asm("1: ldstub [%i0], %l0");
156 #define BEGIN_SPIN(tmp,lock) begin_spin(&lock);
157 #define END_SPIN(lock) ((lock) = 0);
160 extern volatile unsigned char _wapi_sparc_lock;
162 static inline gint32 InterlockedCompareExchange(volatile gint32 *dest, gint32 exch, gint32 comp)
167 BEGIN_SPIN(tmp,_wapi_sparc_lock)
174 END_SPIN(_wapi_sparc_lock)
179 static inline gpointer InterlockedCompareExchangePointer(volatile gpointer *dest, gpointer exch, gpointer comp)
184 BEGIN_SPIN(tmp,_wapi_sparc_lock)
191 END_SPIN(_wapi_sparc_lock)
196 static inline gint32 InterlockedIncrement(volatile gint32 *dest)
201 BEGIN_SPIN(tmp,_wapi_sparc_lock)
206 END_SPIN(_wapi_sparc_lock)
211 static inline gint32 InterlockedDecrement(volatile gint32 *dest)
216 BEGIN_SPIN(tmp,_wapi_sparc_lock)
221 END_SPIN(_wapi_sparc_lock)
226 static inline gint32 InterlockedExchange(volatile gint32 *dest, gint32 exch)
231 BEGIN_SPIN(tmp,_wapi_sparc_lock)
236 END_SPIN(_wapi_sparc_lock)
241 static inline gpointer InterlockedExchangePointer(volatile gpointer *dest, gpointer exch)
246 BEGIN_SPIN(tmp,_wapi_sparc_lock)
251 END_SPIN(_wapi_sparc_lock)
256 static inline gint32 InterlockedExchangeAdd(volatile gint32 *dest, gint32 add)
261 BEGIN_SPIN(tmp,_wapi_sparc_lock)
266 END_SPIN(_wapi_sparc_lock)
273 #define WAPI_ATOMIC_ASM
276 InterlockedCompareExchange(volatile gint32 *dest,
277 gint32 exch, gint32 comp)
281 __asm__ __volatile__ ("\tL\t%1,%0\n"
284 : "=m" (*dest), "=r" (old)
285 : "r" (exch), "r" (comp)
290 #define InterlockedCompareExchangePointer InterlockedCompareExchange
293 InterlockedIncrement(volatile gint32 *val)
297 __asm__ __volatile__ ("\tLA\t2,%1\n"
301 "0:\tCS\t%0,1,0(2)\n"
303 : "=r" (tmp), "+m" (*val)
310 InterlockedDecrement(volatile gint32 *val)
314 __asm__ __volatile__ ("\tLA\t2,%1\n"
318 "0:\tCS\t%0,1,0(2)\n"
320 : "=r" (tmp), "+m" (*val)
328 InterlockedExchange(volatile gint32 *val, gint32 new_val)
332 __asm__ __volatile__ ("\tLA\t1,%1\n"
336 : "+m" (*val), "=r" (ret)
343 #define InterlockedExchangePointer InterlockedExchange
346 InterlockedExchangeAdd(volatile gint32 *val, gint32 add)
350 __asm__ __volatile__ ("\tL\t%0,%1\n"
354 "0:\tCS\t%0,1,0(2)\n"
356 : "=r" (ret), "+m" (*val)
363 #elif defined(__ppc__) || defined (__powerpc__)
364 #define WAPI_ATOMIC_ASM
366 static inline gint32 InterlockedIncrement(volatile gint32 *val)
370 __asm__ __volatile__ ("\n1:\n\t"
371 "lwarx %0, 0, %2\n\t"
373 "stwcx. %1, 0, %2\n\t"
375 : "=&b" (tmp): "r" (tmp), "r" (val): "cc", "memory");
379 static inline gint32 InterlockedDecrement(volatile gint32 *val)
383 __asm__ __volatile__ ("\n1:\n\t"
384 "lwarx %0, 0, %2\n\t"
385 "addi %1, %0, -1\n\t"
386 "stwcx. %1, 0, %2\n\t"
388 : "=&b" (tmp) : "r" (tmp), "r" (val): "cc", "memory");
392 #define InterlockedCompareExchangePointer InterlockedCompareExchange
394 static inline gint32 InterlockedCompareExchange(volatile gint32 *dest,
395 gint32 exch, gint32 comp) {
398 __asm__ __volatile__ ("\n1:\n\t"
399 "lwarx %0, 0, %1\n\t"
402 "stwcx. %4, 0, %1\n\t"
406 : "r" (dest), "0" (tmp) ,"r" (comp), "r" (exch): "cc", "memory");
410 static inline gint32 InterlockedExchange(volatile gint32 *dest, gint32 exch)
414 __asm__ __volatile__ ("\n1:\n\t"
415 "lwarx %0, 0, %1\n\t"
416 "stwcx. %2, 0, %1\n\t"
418 : "=r" (tmp) : "r" (dest), "r" (exch): "cc", "memory");
421 #define InterlockedExchangePointer InterlockedExchange
423 static inline gint32 InterlockedExchangeAdd(volatile gint32 *dest, gint32 add)
427 __asm__ __volatile__ ("\n1:\n\t"
428 "lwarx %0, 0, %2\n\t"
430 "stwcx. %1, 0, %2\n\t"
432 : "=r" (tmp), "=r" (add)
433 : "r" (dest), "0" (tmp), "1" (add) : "cc", "memory");
437 #elif defined(__arm__)
438 #define WAPI_ATOMIC_ASM
440 static inline gint32 InterlockedCompareExchange(volatile gint32 *dest, gint32 exch, gint32 comp)
444 __asm__ __volatile__ ( "0:\n\t"
448 "swp %0, %3, [%2]\n\t"
450 "swpne %3, %0, [%2]\n\t"
453 : "=&r" (a), "=&r" (b)
454 : "r" (dest), "r" (exch), "r" (comp)
460 static inline gpointer InterlockedCompareExchangePointer(volatile gpointer *dest, gpointer exch, gpointer comp)
464 __asm__ __volatile__ ( "0:\n\t"
468 "swpeq %0, %3, [%2]\n\t"
470 "swpne %3, %0, [%2]\n\t"
473 : "=&r" (a), "=&r" (b)
474 : "r" (dest), "r" (exch), "r" (comp)
480 static inline gint32 InterlockedIncrement(volatile gint32 *dest)
484 __asm__ __volatile__ ( "0:\n\t"
487 "swp %2, %1, [%3]\n\t"
489 "swpne %1, %2, [%3]\n\t"
491 : "=&r" (a), "=&r" (b), "=&r" (c)
492 : "r" (dest), "r" (1)
498 static inline gint32 InterlockedDecrement(volatile gint32 *dest)
502 __asm__ __volatile__ ( "0:\n\t"
505 "swp %2, %1, [%3]\n\t"
507 "swpne %1, %2, [%3]\n\t"
509 : "=&r" (a), "=&r" (b), "=&r" (c)
510 : "r" (dest), "r" (-1)
516 static inline gint32 InterlockedExchange(volatile gint32 *dest, gint32 exch)
520 __asm__ __volatile__ ( "swp %0, %2, [%1]"
522 : "r" (dest), "r" (exch));
527 static inline gpointer InterlockedExchangePointer(volatile gpointer *dest, gpointer exch)
531 __asm__ __volatile__ ( "swp %0, %2, [%1]"
533 : "r" (dest), "r" (exch));
538 static inline gint32 InterlockedExchangeAdd(volatile gint32 *dest, gint32 add)
542 __asm__ __volatile__ ( "0:\n\t"
545 "swp %2, %1, [%3]\n\t"
547 "swpne %1, %2, [%3]\n\t"
549 : "=&r" (a), "=&r" (b), "=&r" (c)
550 : "r" (dest), "r" (add)
558 extern gint32 InterlockedCompareExchange(volatile gint32 *dest, gint32 exch, gint32 comp);
559 extern gpointer InterlockedCompareExchangePointer(volatile gpointer *dest, gpointer exch, gpointer comp);
560 extern gint32 InterlockedIncrement(volatile gint32 *dest);
561 extern gint32 InterlockedDecrement(volatile gint32 *dest);
562 extern gint32 InterlockedExchange(volatile gint32 *dest, gint32 exch);
563 extern gpointer InterlockedExchangePointer(volatile gpointer *dest, gpointer exch);
564 extern gint32 InterlockedExchangeAdd(volatile gint32 *dest, gint32 add);
566 #if defined(__hpux) && !defined(__GNUC__)
567 #define WAPI_ATOMIC_ASM
572 #endif /* _WAPI_ATOMIC_H_ */