blob: daae401b465ce769f5b0399a200bfe3c64ccba27 [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
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080022#include "gc/collector/concurrent_copying.h"
23#include "gc/heap.h"
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070024#include "mirror/object_reference.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080025#include "mirror/reference.h"
26#include "runtime.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010027#include "utils.h"
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070028
29namespace art {
30
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080031template <typename MirrorType, ReadBarrierOption kReadBarrierOption, bool kMaybeDuringStartup>
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070032inline MirrorType* ReadBarrier::Barrier(
33 mirror::Object* obj, MemberOffset offset, mirror::HeapReference<MirrorType>* ref_addr) {
Igor Murashkinc449e8b2015-06-10 15:56:42 -070034 constexpr bool with_read_barrier = kReadBarrierOption == kWithReadBarrier;
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -070035 if (with_read_barrier && kUseBakerReadBarrier) {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080036 // The higher bits of the rb ptr, rb_ptr_high_bits (must be zero)
37 // is used to create artificial data dependency from the is_gray
38 // load to the ref field (ptr) load to avoid needing a load-load
39 // barrier between the two.
40 uintptr_t rb_ptr_high_bits;
41 bool is_gray = HasGrayReadBarrierPointer(obj, &rb_ptr_high_bits);
42 ref_addr = reinterpret_cast<mirror::HeapReference<MirrorType>*>(
43 rb_ptr_high_bits | reinterpret_cast<uintptr_t>(ref_addr));
44 MirrorType* ref = ref_addr->AsMirrorPtr();
45 if (is_gray) {
46 // Slow-path.
47 ref = reinterpret_cast<MirrorType*>(Mark(ref));
48 }
49 if (kEnableReadBarrierInvariantChecks) {
50 CHECK_EQ(rb_ptr_high_bits, 0U) << obj << " rb_ptr=" << obj->GetReadBarrierPointer();
51 }
52 AssertToSpaceInvariant(obj, offset, ref);
53 return ref;
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -070054 } else if (with_read_barrier && kUseBrooksReadBarrier) {
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070055 // To be implemented.
56 return ref_addr->AsMirrorPtr();
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080057 } else if (with_read_barrier && kUseTableLookupReadBarrier) {
58 MirrorType* ref = ref_addr->AsMirrorPtr();
59 MirrorType* old_ref = ref;
60 // The heap or the collector can be null at startup. TODO: avoid the need for this null check.
61 gc::Heap* heap = Runtime::Current()->GetHeap();
62 if (heap != nullptr && heap->GetReadBarrierTable()->IsSet(old_ref)) {
63 ref = reinterpret_cast<MirrorType*>(Mark(old_ref));
64 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
65 obj->CasFieldStrongSequentiallyConsistentObjectWithoutWriteBarrier<false, false>(
66 offset, old_ref, ref);
67 }
68 AssertToSpaceInvariant(obj, offset, ref);
69 return ref;
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070070 } else {
71 // No read barrier.
72 return ref_addr->AsMirrorPtr();
73 }
74}
75
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080076template <typename MirrorType, ReadBarrierOption kReadBarrierOption, bool kMaybeDuringStartup>
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070077inline MirrorType* ReadBarrier::BarrierForRoot(MirrorType** root,
78 GcRootSource* gc_root_source) {
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070079 MirrorType* ref = *root;
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -070080 const bool with_read_barrier = kReadBarrierOption == kWithReadBarrier;
81 if (with_read_barrier && kUseBakerReadBarrier) {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080082 // TODO: separate the read barrier code from the collector code more.
Hiroshi Yamauchi00370822015-08-18 14:47:25 -070083 Thread* self = Thread::Current();
84 if (self != nullptr && self->GetIsGcMarking()) {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080085 ref = reinterpret_cast<MirrorType*>(Mark(ref));
86 }
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070087 AssertToSpaceInvariant(gc_root_source, ref);
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -070088 return ref;
89 } else if (with_read_barrier && kUseBrooksReadBarrier) {
90 // To be implemented.
91 return ref;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080092 } else if (with_read_barrier && kUseTableLookupReadBarrier) {
93 if (kMaybeDuringStartup && IsDuringStartup()) {
94 // During startup, the heap may not be initialized yet. Just
95 // return the given ref.
96 return ref;
97 }
98 if (Runtime::Current()->GetHeap()->GetReadBarrierTable()->IsSet(ref)) {
99 MirrorType* old_ref = ref;
100 ref = reinterpret_cast<MirrorType*>(Mark(old_ref));
101 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
102 Atomic<mirror::Object*>* atomic_root = reinterpret_cast<Atomic<mirror::Object*>*>(root);
103 atomic_root->CompareExchangeStrongSequentiallyConsistent(old_ref, ref);
104 }
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700105 AssertToSpaceInvariant(gc_root_source, ref);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800106 return ref;
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -0700107 } else {
108 return ref;
109 }
110}
111
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700112// TODO: Reduce copy paste
113template <typename MirrorType, ReadBarrierOption kReadBarrierOption, bool kMaybeDuringStartup>
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700114inline MirrorType* ReadBarrier::BarrierForRoot(mirror::CompressedReference<MirrorType>* root,
115 GcRootSource* gc_root_source) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700116 MirrorType* ref = root->AsMirrorPtr();
117 const bool with_read_barrier = kReadBarrierOption == kWithReadBarrier;
118 if (with_read_barrier && kUseBakerReadBarrier) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700119 // TODO: separate the read barrier code from the collector code more.
Hiroshi Yamauchi00370822015-08-18 14:47:25 -0700120 Thread* self = Thread::Current();
121 if (self != nullptr && self->GetIsGcMarking()) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700122 ref = reinterpret_cast<MirrorType*>(Mark(ref));
123 }
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700124 AssertToSpaceInvariant(gc_root_source, ref);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700125 return ref;
126 } else if (with_read_barrier && kUseBrooksReadBarrier) {
127 // To be implemented.
128 return ref;
129 } else if (with_read_barrier && kUseTableLookupReadBarrier) {
130 if (kMaybeDuringStartup && IsDuringStartup()) {
131 // During startup, the heap may not be initialized yet. Just
132 // return the given ref.
133 return ref;
134 }
135 if (Runtime::Current()->GetHeap()->GetReadBarrierTable()->IsSet(ref)) {
136 auto old_ref = mirror::CompressedReference<MirrorType>::FromMirrorPtr(ref);
137 ref = reinterpret_cast<MirrorType*>(Mark(ref));
138 auto new_ref = mirror::CompressedReference<MirrorType>::FromMirrorPtr(ref);
139 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
140 auto* atomic_root =
141 reinterpret_cast<Atomic<mirror::CompressedReference<MirrorType>>*>(root);
142 atomic_root->CompareExchangeStrongSequentiallyConsistent(old_ref, new_ref);
143 }
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700144 AssertToSpaceInvariant(gc_root_source, ref);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700145 return ref;
146 } else {
147 return ref;
148 }
149}
150
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800151inline bool ReadBarrier::IsDuringStartup() {
152 gc::Heap* heap = Runtime::Current()->GetHeap();
153 if (heap == nullptr) {
154 // During startup, the heap can be null.
155 return true;
156 }
157 if (heap->CurrentCollectorType() != gc::kCollectorTypeCC) {
158 // CC isn't running.
159 return true;
160 }
161 gc::collector::ConcurrentCopying* collector = heap->ConcurrentCopyingCollector();
162 if (collector == nullptr) {
163 // During startup, the collector can be null.
164 return true;
165 }
166 return false;
167}
168
169inline void ReadBarrier::AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset,
170 mirror::Object* ref) {
171 if (kEnableToSpaceInvariantChecks || kIsDebugBuild) {
172 if (ref == nullptr || IsDuringStartup()) {
173 return;
174 }
175 Runtime::Current()->GetHeap()->ConcurrentCopyingCollector()->
176 AssertToSpaceInvariant(obj, offset, ref);
177 }
178}
179
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700180inline void ReadBarrier::AssertToSpaceInvariant(GcRootSource* gc_root_source,
181 mirror::Object* ref) {
182 if (kEnableToSpaceInvariantChecks || kIsDebugBuild) {
183 if (ref == nullptr || IsDuringStartup()) {
184 return;
185 }
186 Runtime::Current()->GetHeap()->ConcurrentCopyingCollector()->
187 AssertToSpaceInvariant(gc_root_source, ref);
188 }
189}
190
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800191inline mirror::Object* ReadBarrier::Mark(mirror::Object* obj) {
192 return Runtime::Current()->GetHeap()->ConcurrentCopyingCollector()->Mark(obj);
193}
194
195inline bool ReadBarrier::HasGrayReadBarrierPointer(mirror::Object* obj,
196 uintptr_t* out_rb_ptr_high_bits) {
197 mirror::Object* rb_ptr = obj->GetReadBarrierPointer();
198 uintptr_t rb_ptr_bits = reinterpret_cast<uintptr_t>(rb_ptr);
199 uintptr_t rb_ptr_low_bits = rb_ptr_bits & rb_ptr_mask_;
200 if (kEnableReadBarrierInvariantChecks) {
201 CHECK(rb_ptr_low_bits == white_ptr_ || rb_ptr_low_bits == gray_ptr_ ||
202 rb_ptr_low_bits == black_ptr_)
203 << "obj=" << obj << " rb_ptr=" << rb_ptr << " " << PrettyTypeOf(obj);
204 }
205 bool is_gray = rb_ptr_low_bits == gray_ptr_;
206 // The high bits are supposed to be zero. We check this on the caller side.
207 *out_rb_ptr_high_bits = rb_ptr_bits & ~rb_ptr_mask_;
208 return is_gray;
209}
210
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -0700211} // namespace art
212
213#endif // ART_RUNTIME_READ_BARRIER_INL_H_