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