Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #ifndef ART_RUNTIME_READ_BARRIER_INL_H_ |
| 18 | #define ART_RUNTIME_READ_BARRIER_INL_H_ |
| 19 | |
| 20 | #include "read_barrier.h" |
| 21 | |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 22 | #include "gc/collector/concurrent_copying-inl.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 23 | #include "gc/heap.h" |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 24 | #include "mirror/object_reference.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 25 | #include "mirror/reference.h" |
| 26 | #include "runtime.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 27 | #include "utils.h" |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
Hiroshi Yamauchi | cc78f3f | 2015-12-11 15:51:04 -0800 | [diff] [blame] | 31 | template <typename MirrorType, ReadBarrierOption kReadBarrierOption, bool kAlwaysUpdateField> |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 32 | inline MirrorType* ReadBarrier::Barrier( |
| 33 | mirror::Object* obj, MemberOffset offset, mirror::HeapReference<MirrorType>* ref_addr) { |
Igor Murashkin | c449e8b | 2015-06-10 15:56:42 -0700 | [diff] [blame] | 34 | constexpr bool with_read_barrier = kReadBarrierOption == kWithReadBarrier; |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 35 | if (kUseReadBarrier && with_read_barrier) { |
| 36 | if (kIsDebugBuild) { |
| 37 | Thread* const self = Thread::Current(); |
| 38 | if (self != nullptr) { |
| 39 | CHECK_EQ(self->GetDebugDisallowReadBarrierCount(), 0u); |
Hiroshi Yamauchi | cc78f3f | 2015-12-11 15:51:04 -0800 | [diff] [blame] | 40 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 41 | } |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 42 | if (kUseBakerReadBarrier) { |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 43 | // fake_address_dependency (must be zero) is used to create artificial data dependency from |
| 44 | // the is_gray load to the ref field (ptr) load to avoid needing a load-load barrier between |
| 45 | // the two. |
| 46 | uintptr_t fake_address_dependency; |
| 47 | bool is_gray = IsGray(obj, &fake_address_dependency); |
| 48 | if (kEnableReadBarrierInvariantChecks) { |
| 49 | CHECK_EQ(fake_address_dependency, 0U) << obj << " rb_state=" << obj->GetReadBarrierState(); |
| 50 | } |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 51 | ref_addr = reinterpret_cast<mirror::HeapReference<MirrorType>*>( |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 52 | fake_address_dependency | reinterpret_cast<uintptr_t>(ref_addr)); |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 53 | MirrorType* ref = ref_addr->AsMirrorPtr(); |
| 54 | MirrorType* old_ref = ref; |
| 55 | if (is_gray) { |
| 56 | // Slow-path. |
| 57 | ref = reinterpret_cast<MirrorType*>(Mark(ref)); |
| 58 | // If kAlwaysUpdateField is true, update the field atomically. This may fail if mutator |
Roland Levillain | a1aa3b1 | 2016-10-26 13:03:38 +0100 | [diff] [blame] | 59 | // updates before us, but it's OK. |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 60 | if (kAlwaysUpdateField && ref != old_ref) { |
| 61 | obj->CasFieldStrongRelaxedObjectWithoutWriteBarrier<false, false>( |
| 62 | offset, old_ref, ref); |
| 63 | } |
Hiroshi Yamauchi | fa75518 | 2015-09-30 20:12:11 -0700 | [diff] [blame] | 64 | } |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 65 | AssertToSpaceInvariant(obj, offset, ref); |
| 66 | return ref; |
| 67 | } else if (kUseBrooksReadBarrier) { |
| 68 | // To be implemented. |
| 69 | return ref_addr->AsMirrorPtr(); |
| 70 | } else if (kUseTableLookupReadBarrier) { |
| 71 | MirrorType* ref = ref_addr->AsMirrorPtr(); |
| 72 | MirrorType* old_ref = ref; |
| 73 | // The heap or the collector can be null at startup. TODO: avoid the need for this null check. |
| 74 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 75 | if (heap != nullptr && heap->GetReadBarrierTable()->IsSet(old_ref)) { |
| 76 | ref = reinterpret_cast<MirrorType*>(Mark(old_ref)); |
| 77 | // Update the field atomically. This may fail if mutator updates before us, but it's ok. |
| 78 | if (ref != old_ref) { |
| 79 | obj->CasFieldStrongRelaxedObjectWithoutWriteBarrier<false, false>( |
| 80 | offset, old_ref, ref); |
| 81 | } |
| 82 | } |
| 83 | AssertToSpaceInvariant(obj, offset, ref); |
| 84 | return ref; |
| 85 | } else { |
| 86 | LOG(FATAL) << "Unexpected read barrier type"; |
| 87 | UNREACHABLE(); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 88 | } |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 89 | } else { |
| 90 | // No read barrier. |
| 91 | return ref_addr->AsMirrorPtr(); |
| 92 | } |
| 93 | } |
| 94 | |
Hiroshi Yamauchi | cc78f3f | 2015-12-11 15:51:04 -0800 | [diff] [blame] | 95 | template <typename MirrorType, ReadBarrierOption kReadBarrierOption> |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 96 | inline MirrorType* ReadBarrier::BarrierForRoot(MirrorType** root, |
| 97 | GcRootSource* gc_root_source) { |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 98 | MirrorType* ref = *root; |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 99 | const bool with_read_barrier = kReadBarrierOption == kWithReadBarrier; |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 100 | if (kUseReadBarrier && with_read_barrier) { |
| 101 | if (kIsDebugBuild) { |
| 102 | Thread* const self = Thread::Current(); |
| 103 | if (self != nullptr) { |
| 104 | CHECK_EQ(self->GetDebugDisallowReadBarrierCount(), 0u); |
Hiroshi Yamauchi | fa75518 | 2015-09-30 20:12:11 -0700 | [diff] [blame] | 105 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 106 | } |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 107 | if (kUseBakerReadBarrier) { |
| 108 | // TODO: separate the read barrier code from the collector code more. |
| 109 | Thread* self = Thread::Current(); |
| 110 | if (self != nullptr && self->GetIsGcMarking()) { |
| 111 | ref = reinterpret_cast<MirrorType*>(Mark(ref)); |
| 112 | } |
| 113 | AssertToSpaceInvariant(gc_root_source, ref); |
| 114 | return ref; |
| 115 | } else if (kUseBrooksReadBarrier) { |
| 116 | // To be implemented. |
| 117 | return ref; |
| 118 | } else if (kUseTableLookupReadBarrier) { |
| 119 | Thread* self = Thread::Current(); |
| 120 | if (self != nullptr && |
| 121 | self->GetIsGcMarking() && |
| 122 | Runtime::Current()->GetHeap()->GetReadBarrierTable()->IsSet(ref)) { |
| 123 | MirrorType* old_ref = ref; |
| 124 | ref = reinterpret_cast<MirrorType*>(Mark(old_ref)); |
| 125 | // Update the field atomically. This may fail if mutator updates before us, but it's ok. |
| 126 | if (ref != old_ref) { |
| 127 | Atomic<mirror::Object*>* atomic_root = reinterpret_cast<Atomic<mirror::Object*>*>(root); |
| 128 | atomic_root->CompareExchangeStrongRelaxed(old_ref, ref); |
| 129 | } |
| 130 | } |
| 131 | AssertToSpaceInvariant(gc_root_source, ref); |
| 132 | return ref; |
| 133 | } else { |
| 134 | LOG(FATAL) << "Unexpected read barrier type"; |
| 135 | UNREACHABLE(); |
| 136 | } |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 137 | } else { |
| 138 | return ref; |
| 139 | } |
| 140 | } |
| 141 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 142 | // TODO: Reduce copy paste |
Hiroshi Yamauchi | cc78f3f | 2015-12-11 15:51:04 -0800 | [diff] [blame] | 143 | template <typename MirrorType, ReadBarrierOption kReadBarrierOption> |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 144 | inline MirrorType* ReadBarrier::BarrierForRoot(mirror::CompressedReference<MirrorType>* root, |
| 145 | GcRootSource* gc_root_source) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 146 | MirrorType* ref = root->AsMirrorPtr(); |
| 147 | const bool with_read_barrier = kReadBarrierOption == kWithReadBarrier; |
| 148 | if (with_read_barrier && kUseBakerReadBarrier) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 149 | // TODO: separate the read barrier code from the collector code more. |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 150 | Thread* self = Thread::Current(); |
| 151 | if (self != nullptr && self->GetIsGcMarking()) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 152 | ref = reinterpret_cast<MirrorType*>(Mark(ref)); |
| 153 | } |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 154 | AssertToSpaceInvariant(gc_root_source, ref); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 155 | return ref; |
| 156 | } else if (with_read_barrier && kUseBrooksReadBarrier) { |
| 157 | // To be implemented. |
| 158 | return ref; |
| 159 | } else if (with_read_barrier && kUseTableLookupReadBarrier) { |
Hiroshi Yamauchi | fa75518 | 2015-09-30 20:12:11 -0700 | [diff] [blame] | 160 | Thread* self = Thread::Current(); |
| 161 | if (self != nullptr && |
| 162 | self->GetIsGcMarking() && |
| 163 | Runtime::Current()->GetHeap()->GetReadBarrierTable()->IsSet(ref)) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 164 | auto old_ref = mirror::CompressedReference<MirrorType>::FromMirrorPtr(ref); |
| 165 | ref = reinterpret_cast<MirrorType*>(Mark(ref)); |
| 166 | auto new_ref = mirror::CompressedReference<MirrorType>::FromMirrorPtr(ref); |
| 167 | // Update the field atomically. This may fail if mutator updates before us, but it's ok. |
Hiroshi Yamauchi | fa75518 | 2015-09-30 20:12:11 -0700 | [diff] [blame] | 168 | if (new_ref.AsMirrorPtr() != old_ref.AsMirrorPtr()) { |
| 169 | auto* atomic_root = |
| 170 | reinterpret_cast<Atomic<mirror::CompressedReference<MirrorType>>*>(root); |
Hiroshi Yamauchi | fed3e2f | 2015-10-20 11:11:56 -0700 | [diff] [blame] | 171 | atomic_root->CompareExchangeStrongRelaxed(old_ref, new_ref); |
Hiroshi Yamauchi | fa75518 | 2015-09-30 20:12:11 -0700 | [diff] [blame] | 172 | } |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 173 | } |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 174 | AssertToSpaceInvariant(gc_root_source, ref); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 175 | return ref; |
| 176 | } else { |
| 177 | return ref; |
| 178 | } |
| 179 | } |
| 180 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 181 | inline bool ReadBarrier::IsDuringStartup() { |
| 182 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 183 | if (heap == nullptr) { |
| 184 | // During startup, the heap can be null. |
| 185 | return true; |
| 186 | } |
| 187 | if (heap->CurrentCollectorType() != gc::kCollectorTypeCC) { |
| 188 | // CC isn't running. |
| 189 | return true; |
| 190 | } |
| 191 | gc::collector::ConcurrentCopying* collector = heap->ConcurrentCopyingCollector(); |
| 192 | if (collector == nullptr) { |
| 193 | // During startup, the collector can be null. |
| 194 | return true; |
| 195 | } |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | inline void ReadBarrier::AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset, |
| 200 | mirror::Object* ref) { |
Andreas Gampe | e3ce787 | 2017-02-22 13:36:21 -0800 | [diff] [blame] | 201 | if (kEnableToSpaceInvariantChecks) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 202 | if (ref == nullptr || IsDuringStartup()) { |
| 203 | return; |
| 204 | } |
| 205 | Runtime::Current()->GetHeap()->ConcurrentCopyingCollector()-> |
| 206 | AssertToSpaceInvariant(obj, offset, ref); |
| 207 | } |
| 208 | } |
| 209 | |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 210 | inline void ReadBarrier::AssertToSpaceInvariant(GcRootSource* gc_root_source, |
| 211 | mirror::Object* ref) { |
Andreas Gampe | e3ce787 | 2017-02-22 13:36:21 -0800 | [diff] [blame] | 212 | if (kEnableToSpaceInvariantChecks) { |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 213 | if (ref == nullptr || IsDuringStartup()) { |
| 214 | return; |
| 215 | } |
| 216 | Runtime::Current()->GetHeap()->ConcurrentCopyingCollector()-> |
| 217 | AssertToSpaceInvariant(gc_root_source, ref); |
| 218 | } |
| 219 | } |
| 220 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 221 | inline mirror::Object* ReadBarrier::Mark(mirror::Object* obj) { |
Mathieu Chartier | 56fe258 | 2016-07-14 13:30:03 -0700 | [diff] [blame] | 222 | return Runtime::Current()->GetHeap()->ConcurrentCopyingCollector()->MarkFromReadBarrier(obj); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 225 | inline bool ReadBarrier::IsGray(mirror::Object* obj, uintptr_t* fake_address_dependency) { |
| 226 | return obj->GetReadBarrierState(fake_address_dependency) == gray_state_; |
| 227 | } |
| 228 | |
| 229 | inline bool ReadBarrier::IsGray(mirror::Object* obj) { |
| 230 | // Use a load-acquire to load the read barrier bit to avoid reordering with the subsequent load. |
| 231 | // GetReadBarrierStateAcquire() has load-acquire semantics. |
| 232 | return obj->GetReadBarrierStateAcquire() == gray_state_; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 235 | } // namespace art |
| 236 | |
| 237 | #endif // ART_RUNTIME_READ_BARRIER_INL_H_ |