blob: 672303a51b7d8eb82baee923f42ad23758e04749 [file] [log] [blame]
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -07001/*
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
David Sehrc431b9d2018-03-02 12:01:51 -080022#include "base/utils.h"
Andreas Gamped4901292017-05-30 18:41:34 -070023#include "gc/accounting/read_barrier_table.h"
Hiroshi Yamauchi723e6ce2015-10-28 20:59:47 -070024#include "gc/collector/concurrent_copying-inl.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080025#include "gc/heap.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070026#include "mirror/object-readbarrier-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include "mirror/object_reference.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080028#include "mirror/reference.h"
29#include "runtime.h"
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070030
31namespace art {
32
Mathieu Chartierd08f66f2017-04-13 11:47:53 -070033// Disabled for performance reasons.
34static constexpr bool kCheckDebugDisallowReadBarrierCount = false;
35
Hans Boehmcc55e1d2017-07-27 15:28:07 -070036template <typename MirrorType, bool kIsVolatile, ReadBarrierOption kReadBarrierOption,
37 bool kAlwaysUpdateField>
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070038inline MirrorType* ReadBarrier::Barrier(
39 mirror::Object* obj, MemberOffset offset, mirror::HeapReference<MirrorType>* ref_addr) {
Igor Murashkinc449e8b2015-06-10 15:56:42 -070040 constexpr bool with_read_barrier = kReadBarrierOption == kWithReadBarrier;
Mathieu Chartierdfe02f62016-02-01 20:15:11 -080041 if (kUseReadBarrier && with_read_barrier) {
Mathieu Chartierd08f66f2017-04-13 11:47:53 -070042 if (kCheckDebugDisallowReadBarrierCount) {
Mathieu Chartierdfe02f62016-02-01 20:15:11 -080043 Thread* const self = Thread::Current();
44 if (self != nullptr) {
45 CHECK_EQ(self->GetDebugDisallowReadBarrierCount(), 0u);
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -080046 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080047 }
Mathieu Chartierdfe02f62016-02-01 20:15:11 -080048 if (kUseBakerReadBarrier) {
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -070049 // fake_address_dependency (must be zero) is used to create artificial data dependency from
50 // the is_gray load to the ref field (ptr) load to avoid needing a load-load barrier between
51 // the two.
52 uintptr_t fake_address_dependency;
53 bool is_gray = IsGray(obj, &fake_address_dependency);
54 if (kEnableReadBarrierInvariantChecks) {
55 CHECK_EQ(fake_address_dependency, 0U) << obj << " rb_state=" << obj->GetReadBarrierState();
56 }
Mathieu Chartierdfe02f62016-02-01 20:15:11 -080057 ref_addr = reinterpret_cast<mirror::HeapReference<MirrorType>*>(
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -070058 fake_address_dependency | reinterpret_cast<uintptr_t>(ref_addr));
Hans Boehmcc55e1d2017-07-27 15:28:07 -070059 MirrorType* ref = ref_addr->template AsMirrorPtr<kIsVolatile>();
Mathieu Chartierdfe02f62016-02-01 20:15:11 -080060 MirrorType* old_ref = ref;
61 if (is_gray) {
62 // Slow-path.
63 ref = reinterpret_cast<MirrorType*>(Mark(ref));
64 // If kAlwaysUpdateField is true, update the field atomically. This may fail if mutator
Roland Levillaina1aa3b12016-10-26 13:03:38 +010065 // updates before us, but it's OK.
Mathieu Chartierdfe02f62016-02-01 20:15:11 -080066 if (kAlwaysUpdateField && ref != old_ref) {
Mathieu Chartiera9746b92018-06-22 10:25:40 -070067 obj->CasFieldObjectWithoutWriteBarrier<false, false>(offset,
68 old_ref,
69 ref,
70 CASMode::kStrong,
71 std::memory_order_release);
Mathieu Chartierdfe02f62016-02-01 20:15:11 -080072 }
Hiroshi Yamauchifa755182015-09-30 20:12:11 -070073 }
Mathieu Chartierdfe02f62016-02-01 20:15:11 -080074 AssertToSpaceInvariant(obj, offset, ref);
75 return ref;
76 } else if (kUseBrooksReadBarrier) {
77 // To be implemented.
Hans Boehmcc55e1d2017-07-27 15:28:07 -070078 return ref_addr->template AsMirrorPtr<kIsVolatile>();
Mathieu Chartierdfe02f62016-02-01 20:15:11 -080079 } else if (kUseTableLookupReadBarrier) {
Hans Boehmcc55e1d2017-07-27 15:28:07 -070080 MirrorType* ref = ref_addr->template AsMirrorPtr<kIsVolatile>();
Mathieu Chartierdfe02f62016-02-01 20:15:11 -080081 MirrorType* old_ref = ref;
82 // The heap or the collector can be null at startup. TODO: avoid the need for this null check.
83 gc::Heap* heap = Runtime::Current()->GetHeap();
84 if (heap != nullptr && heap->GetReadBarrierTable()->IsSet(old_ref)) {
85 ref = reinterpret_cast<MirrorType*>(Mark(old_ref));
86 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
87 if (ref != old_ref) {
Mathieu Chartiera9746b92018-06-22 10:25:40 -070088 obj->CasFieldObjectWithoutWriteBarrier<false, false>(offset,
89 old_ref,
90 ref,
91 CASMode::kStrong,
92 std::memory_order_release);
Mathieu Chartierdfe02f62016-02-01 20:15:11 -080093 }
94 }
95 AssertToSpaceInvariant(obj, offset, ref);
96 return ref;
97 } else {
98 LOG(FATAL) << "Unexpected read barrier type";
99 UNREACHABLE();
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800100 }
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -0700101 } else {
102 // No read barrier.
Hans Boehmcc55e1d2017-07-27 15:28:07 -0700103 return ref_addr->template AsMirrorPtr<kIsVolatile>();
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -0700104 }
105}
106
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -0800107template <typename MirrorType, ReadBarrierOption kReadBarrierOption>
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700108inline MirrorType* ReadBarrier::BarrierForRoot(MirrorType** root,
109 GcRootSource* gc_root_source) {
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -0700110 MirrorType* ref = *root;
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -0700111 const bool with_read_barrier = kReadBarrierOption == kWithReadBarrier;
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800112 if (kUseReadBarrier && with_read_barrier) {
113 if (kIsDebugBuild) {
114 Thread* const self = Thread::Current();
115 if (self != nullptr) {
116 CHECK_EQ(self->GetDebugDisallowReadBarrierCount(), 0u);
Hiroshi Yamauchifa755182015-09-30 20:12:11 -0700117 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800118 }
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800119 if (kUseBakerReadBarrier) {
120 // TODO: separate the read barrier code from the collector code more.
121 Thread* self = Thread::Current();
122 if (self != nullptr && self->GetIsGcMarking()) {
123 ref = reinterpret_cast<MirrorType*>(Mark(ref));
124 }
125 AssertToSpaceInvariant(gc_root_source, ref);
126 return ref;
127 } else if (kUseBrooksReadBarrier) {
128 // To be implemented.
129 return ref;
130 } else if (kUseTableLookupReadBarrier) {
131 Thread* self = Thread::Current();
132 if (self != nullptr &&
133 self->GetIsGcMarking() &&
134 Runtime::Current()->GetHeap()->GetReadBarrierTable()->IsSet(ref)) {
135 MirrorType* old_ref = ref;
136 ref = reinterpret_cast<MirrorType*>(Mark(old_ref));
137 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
138 if (ref != old_ref) {
Orion Hodson88591fe2018-03-06 13:35:43 +0000139 Atomic<MirrorType*>* atomic_root = reinterpret_cast<Atomic<MirrorType*>*>(root);
Orion Hodson4557b382018-01-03 11:47:54 +0000140 atomic_root->CompareAndSetStrongRelaxed(old_ref, ref);
Mathieu Chartierdfe02f62016-02-01 20:15:11 -0800141 }
142 }
143 AssertToSpaceInvariant(gc_root_source, ref);
144 return ref;
145 } else {
146 LOG(FATAL) << "Unexpected read barrier type";
147 UNREACHABLE();
148 }
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -0700149 } else {
150 return ref;
151 }
152}
153
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700154// TODO: Reduce copy paste
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -0800155template <typename MirrorType, ReadBarrierOption kReadBarrierOption>
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700156inline MirrorType* ReadBarrier::BarrierForRoot(mirror::CompressedReference<MirrorType>* root,
157 GcRootSource* gc_root_source) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700158 MirrorType* ref = root->AsMirrorPtr();
159 const bool with_read_barrier = kReadBarrierOption == kWithReadBarrier;
160 if (with_read_barrier && kUseBakerReadBarrier) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700161 // TODO: separate the read barrier code from the collector code more.
Hiroshi Yamauchi00370822015-08-18 14:47:25 -0700162 Thread* self = Thread::Current();
163 if (self != nullptr && self->GetIsGcMarking()) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700164 ref = reinterpret_cast<MirrorType*>(Mark(ref));
165 }
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700166 AssertToSpaceInvariant(gc_root_source, ref);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700167 return ref;
168 } else if (with_read_barrier && kUseBrooksReadBarrier) {
169 // To be implemented.
170 return ref;
171 } else if (with_read_barrier && kUseTableLookupReadBarrier) {
Hiroshi Yamauchifa755182015-09-30 20:12:11 -0700172 Thread* self = Thread::Current();
173 if (self != nullptr &&
174 self->GetIsGcMarking() &&
175 Runtime::Current()->GetHeap()->GetReadBarrierTable()->IsSet(ref)) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700176 auto old_ref = mirror::CompressedReference<MirrorType>::FromMirrorPtr(ref);
177 ref = reinterpret_cast<MirrorType*>(Mark(ref));
178 auto new_ref = mirror::CompressedReference<MirrorType>::FromMirrorPtr(ref);
179 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
Hiroshi Yamauchifa755182015-09-30 20:12:11 -0700180 if (new_ref.AsMirrorPtr() != old_ref.AsMirrorPtr()) {
181 auto* atomic_root =
182 reinterpret_cast<Atomic<mirror::CompressedReference<MirrorType>>*>(root);
Orion Hodson4557b382018-01-03 11:47:54 +0000183 atomic_root->CompareAndSetStrongRelaxed(old_ref, new_ref);
Hiroshi Yamauchifa755182015-09-30 20:12:11 -0700184 }
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700185 }
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700186 AssertToSpaceInvariant(gc_root_source, ref);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700187 return ref;
188 } else {
189 return ref;
190 }
191}
192
Nicolas Geoffray13056a12017-05-11 11:48:28 +0000193template <typename MirrorType>
194inline MirrorType* ReadBarrier::IsMarked(MirrorType* ref) {
195 // Only read-barrier configurations can have mutators run while
196 // the GC is marking.
197 if (!kUseReadBarrier) {
198 return ref;
199 }
200 // IsMarked does not handle null, so handle it here.
201 if (ref == nullptr) {
202 return nullptr;
203 }
204 // IsMarked should only be called when the GC is marking.
205 if (!Thread::Current()->GetIsGcMarking()) {
206 return ref;
207 }
208
209 return reinterpret_cast<MirrorType*>(
210 Runtime::Current()->GetHeap()->ConcurrentCopyingCollector()->IsMarked(ref));
211}
212
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800213inline bool ReadBarrier::IsDuringStartup() {
214 gc::Heap* heap = Runtime::Current()->GetHeap();
215 if (heap == nullptr) {
216 // During startup, the heap can be null.
217 return true;
218 }
219 if (heap->CurrentCollectorType() != gc::kCollectorTypeCC) {
220 // CC isn't running.
221 return true;
222 }
223 gc::collector::ConcurrentCopying* collector = heap->ConcurrentCopyingCollector();
224 if (collector == nullptr) {
225 // During startup, the collector can be null.
226 return true;
227 }
228 return false;
229}
230
231inline void ReadBarrier::AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset,
232 mirror::Object* ref) {
Andreas Gampee3ce7872017-02-22 13:36:21 -0800233 if (kEnableToSpaceInvariantChecks) {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800234 if (ref == nullptr || IsDuringStartup()) {
235 return;
236 }
237 Runtime::Current()->GetHeap()->ConcurrentCopyingCollector()->
238 AssertToSpaceInvariant(obj, offset, ref);
239 }
240}
241
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700242inline void ReadBarrier::AssertToSpaceInvariant(GcRootSource* gc_root_source,
243 mirror::Object* ref) {
Andreas Gampee3ce7872017-02-22 13:36:21 -0800244 if (kEnableToSpaceInvariantChecks) {
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700245 if (ref == nullptr || IsDuringStartup()) {
246 return;
247 }
248 Runtime::Current()->GetHeap()->ConcurrentCopyingCollector()->
249 AssertToSpaceInvariant(gc_root_source, ref);
250 }
251}
252
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800253inline mirror::Object* ReadBarrier::Mark(mirror::Object* obj) {
Mathieu Chartier56fe2582016-07-14 13:30:03 -0700254 return Runtime::Current()->GetHeap()->ConcurrentCopyingCollector()->MarkFromReadBarrier(obj);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800255}
256
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700257inline bool ReadBarrier::IsGray(mirror::Object* obj, uintptr_t* fake_address_dependency) {
Roland Levillain14e5a292018-06-28 12:00:56 +0100258 return obj->GetReadBarrierState(fake_address_dependency) == kGrayState;
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700259}
260
261inline bool ReadBarrier::IsGray(mirror::Object* obj) {
262 // Use a load-acquire to load the read barrier bit to avoid reordering with the subsequent load.
263 // GetReadBarrierStateAcquire() has load-acquire semantics.
Roland Levillain14e5a292018-06-28 12:00:56 +0100264 return obj->GetReadBarrierStateAcquire() == kGrayState;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800265}
266
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -0700267} // namespace art
268
269#endif // ART_RUNTIME_READ_BARRIER_INL_H_