| 55 | | #if defined(__APPLE__) |
|---|
| 56 | | |
|---|
| 57 | | #if defined(__ppc__) |
|---|
| 58 | | |
|---|
| 59 | | static inline int CAS(register uint32_t value, register uint32_t newvalue, register volatile void* addr) |
|---|
| 60 | | { |
|---|
| 61 | | register int result; |
|---|
| 62 | | asm volatile ( |
|---|
| 63 | | "# CAS \n" |
|---|
| 64 | | " lwarx r0, 0, %1 \n" // creates a reservation on addr |
|---|
| 65 | | " cmpw r0, %2 \n" // test value at addr |
|---|
| 66 | | " bne- 1f \n" |
|---|
| 67 | | " sync \n" // synchronize instructions |
|---|
| 68 | | " stwcx. %3, 0, %1 \n" // if the reservation is not altered |
|---|
| 69 | | // stores the new value at addr |
|---|
| 70 | | " bne- 1f \n" |
|---|
| 71 | | " li %0, 1 \n" |
|---|
| 72 | | " b 2f \n" |
|---|
| 73 | | "1: \n" |
|---|
| 74 | | " li %0, 0 \n" |
|---|
| 75 | | "2: \n" |
|---|
| 76 | | : "=r" (result) |
|---|
| 77 | | : "r" (addr), "r" (value), "r" (newvalue) |
|---|
| 78 | | : "r0" |
|---|
| 79 | | ); |
|---|
| 80 | | return result; |
|---|
| 81 | | } |
|---|
| 82 | | |
|---|
| 83 | | #endif |
|---|
| 84 | | |
|---|
| 85 | | #if defined(__i386__) || defined(__x86_64__) |
|---|
| 86 | | |
|---|
| 87 | | #ifdef __SMP__ |
|---|
| 88 | | # define LOCK "lock ; " |
|---|
| 89 | | #else |
|---|
| 90 | | # define LOCK "" |
|---|
| 91 | | #endif |
|---|
| 92 | | |
|---|
| 93 | | static inline char CAS(volatile uint32_t value, uint32_t newvalue, volatile void* addr) |
|---|
| 94 | | { |
|---|
| 95 | | register char ret; |
|---|
| 96 | | __asm__ __volatile__ ( |
|---|
| 97 | | "# CAS \n\t" |
|---|
| 98 | | LOCK "cmpxchg %2, (%1) \n\t" |
|---|
| 99 | | "sete %0 \n\t" |
|---|
| 100 | | : "=a" (ret) |
|---|
| 101 | | : "c" (addr), "d" (newvalue), "a" (value) |
|---|
| 102 | | ); |
|---|
| 103 | | return ret; |
|---|
| 104 | | } |
|---|
| 105 | | |
|---|
| 106 | | #endif |
|---|
| 107 | | |
|---|
| 108 | | #endif |
|---|
| 109 | | |
|---|
| 110 | | #ifdef __linux__ |
|---|
| 111 | | |
|---|
| 112 | | #ifdef __PPC__ |
|---|
| 113 | | |
|---|
| 114 | | static inline int CAS(register uint32_t value, register uint32_t newvalue, register volatile void* addr) |
|---|
| 115 | | { |
|---|
| 116 | | register int result; |
|---|
| 117 | | register uint32_t tmp; |
|---|
| 118 | | asm volatile ( |
|---|
| 119 | | "# CAS \n" |
|---|
| 120 | | " lwarx %4, 0, %1 \n" // creates a reservation on addr |
|---|
| 121 | | " cmpw %4, %2 \n" // test value at addr |
|---|
| 122 | | " bne- 1f \n" |
|---|
| 123 | | " sync \n" // synchronize instructions |
|---|
| 124 | | " stwcx. %3, 0, %1 \n" // if the reservation is not altered |
|---|
| 125 | | // stores the new value at addr |
|---|
| 126 | | " bne- 1f \n" |
|---|
| 127 | | " li %0, 1 \n" |
|---|
| 128 | | " b 2f \n" |
|---|
| 129 | | "1: \n" |
|---|
| 130 | | " li %0, 0 \n" |
|---|
| 131 | | "2: \n" |
|---|
| 132 | | : "=r" (result) |
|---|
| 133 | | : "r" (addr), "r" (value), "r" (newvalue), "r" (tmp) |
|---|
| 134 | | ); |
|---|
| 135 | | return result; |
|---|
| 136 | | } |
|---|
| 137 | | |
|---|
| 138 | | #endif |
|---|
| 139 | | |
|---|
| 140 | | #if defined(__i386__) || defined(__x86_64__) |
|---|
| 141 | | |
|---|
| 142 | | #ifdef __SMP__ |
|---|
| 143 | | # define LOCK "lock ; " |
|---|
| 144 | | #else |
|---|
| 145 | | # define LOCK "" |
|---|
| 146 | | #endif |
|---|
| 147 | | |
|---|
| 148 | | static inline char CAS(volatile uint32_t value, uint32_t newvalue, volatile void* addr) |
|---|
| 149 | | { |
|---|
| 150 | | register char ret; |
|---|
| 151 | | __asm__ __volatile__ ( |
|---|
| 152 | | "# CAS \n\t" |
|---|
| 153 | | LOCK "cmpxchg %2, (%1) \n\t" |
|---|
| 154 | | "sete %0 \n\t" |
|---|
| 155 | | : "=a" (ret) |
|---|
| 156 | | : "c" (addr), "d" (newvalue), "a" (value) |
|---|
| 157 | | ); |
|---|
| 158 | | return ret; |
|---|
| 159 | | } |
|---|
| 160 | | |
|---|
| 161 | | #endif |
|---|
| 162 | | |
|---|
| 163 | | #if !defined(__i386__) && !defined(__x86_64__) && !defined(__PPC__) |
|---|
| 164 | | #warning using builtin gcc (version >4.1) atomic |
|---|