Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "atomic.h" |
| 18 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 19 | #include <pthread.h> |
| 20 | |
| 21 | #include "mutex.h" |
| 22 | #include "stl_util.h" |
| 23 | #include "stringprintf.h" |
| 24 | |
| 25 | #if defined(__APPLE__) |
| 26 | #include <libkern/OSAtomic.h> |
| 27 | #endif |
| 28 | #if defined(__arm__) |
| 29 | #include <machine/cpu-features.h> |
| 30 | #endif |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 34 | #if defined(HAVE_MACOSX_IPC) |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 35 | #define NEED_MAC_QUASI_ATOMICS 1 |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 36 | |
| 37 | #elif defined(__i386__) || defined(__x86_64__) |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 38 | #define NEED_PTHREADS_QUASI_ATOMICS 1 |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 40 | #elif defined(__mips__) |
| 41 | #define NEED_PTHREADS_QUASI_ATOMICS 1 |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 42 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 43 | #elif defined(__arm__) |
| 44 | |
| 45 | #if defined(__ARM_HAVE_LDREXD) |
| 46 | #define NEED_ARM_LDREXD_QUASI_ATOMICS 1 |
| 47 | #else |
| 48 | #define NEED_PTHREADS_QUASI_ATOMICS 1 |
| 49 | #endif |
| 50 | |
| 51 | #elif defined(__sh__) |
| 52 | #define NEED_PTHREADS_QUASI_ATOMICS 1 |
| 53 | |
| 54 | #else |
| 55 | #error "QuasiAtomic unsupported on this platform" |
| 56 | #endif |
| 57 | |
| 58 | // ***************************************************************************** |
| 59 | |
| 60 | #if NEED_ARM_LDREXD_QUASI_ATOMICS |
| 61 | |
Elliott Hughes | 557e027 | 2011-09-29 10:52:22 -0700 | [diff] [blame] | 62 | static inline int64_t QuasiAtomicSwap64Impl(int64_t new_value, volatile int64_t* addr) { |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 63 | int64_t prev; |
| 64 | int status; |
| 65 | do { |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 66 | __asm__ __volatile__("@ QuasiAtomic::Swap64\n" |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 67 | "ldrexd %0, %H0, [%3]\n" |
| 68 | "strexd %1, %4, %H4, [%3]" |
| 69 | : "=&r" (prev), "=&r" (status), "+m"(*addr) |
| 70 | : "r" (addr), "r" (new_value) |
| 71 | : "cc"); |
| 72 | } while (__builtin_expect(status != 0, 0)); |
| 73 | return prev; |
| 74 | } |
| 75 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 76 | int64_t QuasiAtomic::Swap64(int64_t new_value, volatile int64_t* addr) { |
Elliott Hughes | 557e027 | 2011-09-29 10:52:22 -0700 | [diff] [blame] | 77 | return QuasiAtomicSwap64Impl(new_value, addr); |
| 78 | } |
| 79 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 80 | int64_t QuasiAtomic::Swap64Sync(int64_t new_value, volatile int64_t* addr) { |
Elliott Hughes | 557e027 | 2011-09-29 10:52:22 -0700 | [diff] [blame] | 81 | ANDROID_MEMBAR_STORE(); |
| 82 | int64_t old_value = QuasiAtomicSwap64Impl(new_value, addr); |
| 83 | ANDROID_MEMBAR_FULL(); |
| 84 | return old_value; |
| 85 | } |
| 86 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 87 | int64_t QuasiAtomic::Read64(volatile const int64_t* addr) { |
| 88 | int64_t value; |
| 89 | __asm__ __volatile__("@ QuasiAtomic::Read64\n" |
| 90 | "ldrexd %0, %H0, [%1]" |
| 91 | : "=&r" (value) |
| 92 | : "r" (addr)); |
| 93 | return value; |
| 94 | } |
| 95 | |
| 96 | int QuasiAtomic::Cas64(int64_t old_value, int64_t new_value, volatile int64_t* addr) { |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 97 | int64_t prev; |
| 98 | int status; |
| 99 | do { |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 100 | __asm__ __volatile__("@ QuasiAtomic::Cas64\n" |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 101 | "ldrexd %0, %H0, [%3]\n" |
| 102 | "mov %1, #0\n" |
| 103 | "teq %0, %4\n" |
| 104 | "teqeq %H0, %H4\n" |
| 105 | "strexdeq %1, %5, %H5, [%3]" |
| 106 | : "=&r" (prev), "=&r" (status), "+m"(*addr) |
| 107 | : "r" (addr), "Ir" (old_value), "r" (new_value) |
| 108 | : "cc"); |
| 109 | } while (__builtin_expect(status != 0, 0)); |
| 110 | return prev != old_value; |
| 111 | } |
| 112 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 113 | #endif |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 114 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 115 | // ***************************************************************************** |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 116 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 117 | #if NEED_MAC_QUASI_ATOMICS |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 118 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 119 | static inline int64_t QuasiAtomicSwap64Impl(int64_t value, volatile int64_t* addr) { |
| 120 | int64_t old_value; |
| 121 | do { |
| 122 | old_value = *addr; |
| 123 | } while (QuasiAtomic::Cas64(old_value, value, addr)); |
Elliott Hughes | 557e027 | 2011-09-29 10:52:22 -0700 | [diff] [blame] | 124 | return old_value; |
| 125 | } |
| 126 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 127 | int64_t QuasiAtomic::Swap64(int64_t value, volatile int64_t* addr) { |
| 128 | return QuasiAtomicSwap64Impl(value, addr); |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 131 | int64_t QuasiAtomic::Swap64Sync(int64_t value, volatile int64_t* addr) { |
| 132 | ANDROID_MEMBAR_STORE(); |
| 133 | int64_t old_value = QuasiAtomicSwap64Impl(value, addr); |
| 134 | // TUNING: barriers can be avoided on some architectures. |
| 135 | ANDROID_MEMBAR_FULL(); |
| 136 | return old_value; |
| 137 | } |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 138 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 139 | int64_t QuasiAtomic::Read64(volatile const int64_t* addr) { |
| 140 | return OSAtomicAdd64Barrier(0, const_cast<volatile int64_t*>(addr)); |
| 141 | } |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 142 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 143 | int QuasiAtomic::Cas64(int64_t old_value, int64_t new_value, volatile int64_t* addr) { |
| 144 | return OSAtomicCompareAndSwap64Barrier(old_value, new_value, const_cast<int64_t*>(addr)) == 0; |
| 145 | } |
| 146 | |
| 147 | #endif |
| 148 | |
| 149 | // ***************************************************************************** |
| 150 | |
| 151 | #if NEED_PTHREADS_QUASI_ATOMICS |
| 152 | |
| 153 | // In the absence of a better implementation, we implement the 64-bit atomic |
| 154 | // operations through mutex locking. |
| 155 | |
| 156 | // We stripe across a bunch of different mutexes to reduce contention. |
| 157 | static const size_t kSwapLockCount = 32; |
| 158 | static std::vector<Mutex*>* gSwapLocks; |
| 159 | |
| 160 | void QuasiAtomic::Startup() { |
| 161 | gSwapLocks = new std::vector<Mutex*>; |
| 162 | for (size_t i = 0; i < kSwapLockCount; ++i) { |
| 163 | gSwapLocks->push_back(new Mutex(StringPrintf("QuasiAtomic stripe %d", i).c_str())); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void QuasiAtomic::Shutdown() { |
| 168 | STLDeleteElements(gSwapLocks); |
| 169 | delete gSwapLocks; |
| 170 | } |
| 171 | |
| 172 | static inline Mutex& GetSwapLock(const volatile int64_t* addr) { |
| 173 | return *(*gSwapLocks)[((unsigned)(void*)(addr) >> 3U) % kSwapLockCount]; |
| 174 | } |
| 175 | |
| 176 | int64_t QuasiAtomic::Swap64(int64_t value, volatile int64_t* addr) { |
| 177 | MutexLock mu(GetSwapLock(addr)); |
| 178 | int64_t old_value = *addr; |
| 179 | *addr = value; |
| 180 | return old_value; |
| 181 | } |
| 182 | |
| 183 | int64_t QuasiAtomic::Swap64Sync(int64_t value, volatile int64_t* addr) { |
| 184 | // Same as QuasiAtomicSwap64 - mutex handles barrier. |
| 185 | return QuasiAtomic::Swap64(value, addr); |
| 186 | } |
| 187 | |
| 188 | int QuasiAtomic::Cas64(int64_t old_value, int64_t new_value, volatile int64_t* addr) { |
| 189 | MutexLock mu(GetSwapLock(addr)); |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 190 | if (*addr == old_value) { |
| 191 | *addr = new_value; |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 192 | return 0; |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 193 | } |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 194 | return 1; |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 197 | int64_t QuasiAtomic::Read64(volatile const int64_t* addr) { |
| 198 | MutexLock mu(GetSwapLock(addr)); |
| 199 | return *addr; |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 202 | #else |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 203 | |
| 204 | // The other implementations don't need any special setup. |
| 205 | void QuasiAtomic::Startup() {} |
| 206 | void QuasiAtomic::Shutdown() {} |
| 207 | |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 208 | #endif |
| 209 | |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 210 | } // namespace art |