Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_ATOMIC_H_ |
| 18 | #define ART_RUNTIME_ATOMIC_H_ |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 21 | #include <vector> |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 22 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 23 | #include "base/macros.h" |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 27 | class Mutex; |
| 28 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 29 | // NOTE: Two "quasiatomic" operations on the exact same memory address |
| 30 | // are guaranteed to operate atomically with respect to each other, |
| 31 | // but no guarantees are made about quasiatomic operations mixed with |
| 32 | // non-quasiatomic operations on the same address, nor about |
| 33 | // quasiatomic operations that are performed on partially-overlapping |
| 34 | // memory. |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 35 | class QuasiAtomic { |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 36 | #if !defined(__arm__) && !defined(__i386__) |
| 37 | static constexpr bool kNeedSwapMutexes = true; |
| 38 | #else |
| 39 | static constexpr bool kNeedSwapMutexes = false; |
| 40 | #endif |
| 41 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 42 | public: |
| 43 | static void Startup(); |
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 | static void Shutdown(); |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 46 | |
Ian Rogers | 9adbff5 | 2013-01-23 18:19:03 -0800 | [diff] [blame] | 47 | // Reads the 64-bit value at "addr" without tearing. |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 48 | static int64_t Read64(volatile const int64_t* addr) { |
| 49 | if (!kNeedSwapMutexes) { |
| 50 | return *addr; |
| 51 | } else { |
| 52 | return SwapMutexRead64(addr); |
| 53 | } |
| 54 | } |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 55 | |
Ian Rogers | 9adbff5 | 2013-01-23 18:19:03 -0800 | [diff] [blame] | 56 | // Writes to the 64-bit value at "addr" without tearing. |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 57 | static void Write64(volatile int64_t* addr, int64_t val) { |
| 58 | if (!kNeedSwapMutexes) { |
| 59 | *addr = val; |
| 60 | } else { |
| 61 | SwapMutexWrite64(addr, val); |
| 62 | } |
| 63 | } |
Ian Rogers | 9adbff5 | 2013-01-23 18:19:03 -0800 | [diff] [blame] | 64 | |
| 65 | // Atomically compare the value at "addr" to "old_value", if equal replace it with "new_value" |
| 66 | // and return true. Otherwise, don't swap, and return false. |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 67 | static bool Cas64(int64_t old_value, int64_t new_value, volatile int64_t* addr) { |
| 68 | if (!kNeedSwapMutexes) { |
| 69 | return __sync_bool_compare_and_swap(addr, old_value, new_value); |
| 70 | } else { |
| 71 | return SwapMutexCas64(old_value, new_value, addr); |
| 72 | } |
| 73 | } |
Ian Rogers | 9adbff5 | 2013-01-23 18:19:03 -0800 | [diff] [blame] | 74 | |
| 75 | // Does the architecture provide reasonable atomic long operations or do we fall back on mutexes? |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 76 | static bool LongAtomicsUseMutexes() { |
| 77 | return !kNeedSwapMutexes; |
| 78 | } |
| 79 | |
| 80 | static void MembarLoadStore() { |
| 81 | #if defined(__arm__) |
| 82 | __asm__ __volatile__("dmb ish" : : : "memory"); |
| 83 | #elif defined(__i386__) |
| 84 | __asm__ __volatile__("" : : : "memory"); |
| 85 | #elif defined(__mips__) |
| 86 | __asm__ __volatile__("sync" : : : "memory"); |
| 87 | #else |
| 88 | #error Unexpected architecture |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | static void MembarLoadLoad() { |
| 93 | #if defined(__arm__) |
| 94 | __asm__ __volatile__("dmb ish" : : : "memory"); |
| 95 | #elif defined(__i386__) |
| 96 | __asm__ __volatile__("" : : : "memory"); |
| 97 | #elif defined(__mips__) |
| 98 | __asm__ __volatile__("sync" : : : "memory"); |
| 99 | #else |
| 100 | #error Unexpected architecture |
| 101 | #endif |
| 102 | } |
| 103 | |
| 104 | static void MembarStoreStore() { |
| 105 | #if defined(__arm__) |
| 106 | __asm__ __volatile__("dmb ishst" : : : "memory"); |
| 107 | #elif defined(__i386__) |
| 108 | __asm__ __volatile__("" : : : "memory"); |
| 109 | #elif defined(__mips__) |
| 110 | __asm__ __volatile__("sync" : : : "memory"); |
| 111 | #else |
| 112 | #error Unexpected architecture |
| 113 | #endif |
| 114 | } |
| 115 | |
| 116 | static void MembarStoreLoad() { |
| 117 | #if defined(__arm__) |
| 118 | __asm__ __volatile__("dmb ish" : : : "memory"); |
| 119 | #elif defined(__i386__) |
| 120 | __asm__ __volatile__("mfence" : : : "memory"); |
| 121 | #elif defined(__mips__) |
| 122 | __asm__ __volatile__("sync" : : : "memory"); |
| 123 | #else |
| 124 | #error Unexpected architecture |
| 125 | #endif |
| 126 | } |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 127 | |
| 128 | private: |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 129 | static Mutex* GetSwapMutex(const volatile int64_t* addr); |
| 130 | static int64_t SwapMutexRead64(volatile const int64_t* addr); |
| 131 | static void SwapMutexWrite64(volatile int64_t* addr, int64_t val); |
| 132 | static bool SwapMutexCas64(int64_t old_value, int64_t new_value, volatile int64_t* addr); |
| 133 | |
| 134 | // We stripe across a bunch of different mutexes to reduce contention. |
| 135 | static constexpr size_t kSwapMutexCount = 32; |
| 136 | static std::vector<Mutex*>* gSwapMutexes; |
| 137 | |
Elliott Hughes | 7c6169d | 2012-05-02 16:11:48 -0700 | [diff] [blame] | 138 | DISALLOW_COPY_AND_ASSIGN(QuasiAtomic); |
| 139 | }; |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 140 | |
| 141 | } // namespace art |
| 142 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 143 | #endif // ART_RUNTIME_ATOMIC_H_ |