00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "SGAtomic.hxx"
00022
00023 #if defined(SGATOMIC_USE_GCC4_BUILTINS) && defined (__i386__)
00024
00025
00026
00027
00028
00029
00030
00031
00032 extern "C" {
00033
00034 unsigned __sync_sub_and_fetch_4(volatile void *ptr, unsigned value)
00035 {
00036 register volatile unsigned* mem = reinterpret_cast<volatile unsigned*>(ptr);
00037 register unsigned result;
00038 __asm__ __volatile__("lock; xadd{l} {%0,%1|%1,%0}"
00039 : "=r" (result), "=m" (*mem)
00040 : "0" (-value), "m" (*mem)
00041 : "memory");
00042 return result - value;
00043 }
00044
00045 unsigned __sync_add_and_fetch_4(volatile void *ptr, unsigned value)
00046 {
00047 register volatile unsigned* mem = reinterpret_cast<volatile unsigned*>(ptr);
00048 register unsigned result;
00049 __asm__ __volatile__("lock; xadd{l} {%0,%1|%1,%0}"
00050 : "=r" (result), "=m" (*mem)
00051 : "0" (value), "m" (*mem)
00052 : "memory");
00053 return result + value;
00054 }
00055
00056 unsigned __sync_bool_compare_and_swap_4(volatile void *ptr,
00057 unsigned oldValue, unsigned newValue)
00058 {
00059 register volatile unsigned* mem = reinterpret_cast<volatile unsigned*>(ptr);
00060 unsigned before;
00061 __asm__ __volatile__("lock; cmpxchg{l} {%1,%2|%1,%2}"
00062 : "=a"(before)
00063 : "q"(newValue), "m"(*mem), "0"(oldValue)
00064 : "memory");
00065 return before == oldValue;
00066 }
00067
00068 void __sync_synchronize()
00069 {
00070 __asm__ __volatile__("": : : "memory");
00071 }
00072
00073 }
00074
00075 #endif