blob: fd5dcf9de685c975ed9d03a2e35acea63e4e1ccc [file] [log] [blame]
Mathieu Chartier39e32612013-11-12 16:28:05 -08001/*
2 * Copyright (C) 2013 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 "reference_queue.h"
18
19#include "accounting/card_table-inl.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080020#include "collector/concurrent_copying.h"
Mathieu Chartier39e32612013-11-12 16:28:05 -080021#include "heap.h"
22#include "mirror/class-inl.h"
23#include "mirror/object-inl.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070024#include "mirror/reference-inl.h"
Mathieu Chartier39e32612013-11-12 16:28:05 -080025
26namespace art {
27namespace gc {
28
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070029ReferenceQueue::ReferenceQueue(Mutex* lock) : lock_(lock), list_(nullptr) {
Mathieu Chartier39e32612013-11-12 16:28:05 -080030}
31
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070032void ReferenceQueue::AtomicEnqueueIfNotEnqueued(Thread* self, ObjPtr<mirror::Reference> ref) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070033 DCHECK(ref != nullptr);
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070034 MutexLock mu(self, *lock_);
Richard Uhlerc4695df2016-01-15 14:08:05 -080035 if (ref->IsUnprocessed()) {
36 EnqueueReference(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -080037 }
38}
39
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070040void ReferenceQueue::EnqueueReference(ObjPtr<mirror::Reference> ref) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070041 DCHECK(ref != nullptr);
Richard Uhlerc4695df2016-01-15 14:08:05 -080042 CHECK(ref->IsUnprocessed());
Mathieu Chartier39e32612013-11-12 16:28:05 -080043 if (IsEmpty()) {
44 // 1 element cyclic queue, ie: Reference ref = ..; ref.pendingNext = ref;
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070045 list_ = ref.Ptr();
Mathieu Chartier39e32612013-11-12 16:28:05 -080046 } else {
Mathieu Chartier5ffa0782016-07-27 10:45:47 -070047 // The list is owned by the GC, everything that has been inserted must already be at least
48 // gray.
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070049 ObjPtr<mirror::Reference> head = list_->GetPendingNext<kWithoutReadBarrier>();
Richard Uhlerc4695df2016-01-15 14:08:05 -080050 DCHECK(head != nullptr);
Richard Uhler522d51b2016-01-22 14:18:57 -080051 ref->SetPendingNext(head);
Mathieu Chartier39e32612013-11-12 16:28:05 -080052 }
Richard Uhlerc4695df2016-01-15 14:08:05 -080053 // Add the reference in the middle to preserve the cycle.
Richard Uhler522d51b2016-01-22 14:18:57 -080054 list_->SetPendingNext(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -080055}
56
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070057ObjPtr<mirror::Reference> ReferenceQueue::DequeuePendingReference() {
Mathieu Chartier39e32612013-11-12 16:28:05 -080058 DCHECK(!IsEmpty());
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070059 ObjPtr<mirror::Reference> ref = list_->GetPendingNext<kWithoutReadBarrier>();
Richard Uhlerc4695df2016-01-15 14:08:05 -080060 DCHECK(ref != nullptr);
Mathieu Chartier39e32612013-11-12 16:28:05 -080061 // Note: the following code is thread-safe because it is only called from ProcessReferences which
62 // is single threaded.
Richard Uhlerc4695df2016-01-15 14:08:05 -080063 if (list_ == ref) {
Mathieu Chartier39e32612013-11-12 16:28:05 -080064 list_ = nullptr;
65 } else {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070066 ObjPtr<mirror::Reference> next = ref->GetPendingNext<kWithoutReadBarrier>();
Richard Uhler522d51b2016-01-22 14:18:57 -080067 list_->SetPendingNext(next);
Mathieu Chartier39e32612013-11-12 16:28:05 -080068 }
Richard Uhler522d51b2016-01-22 14:18:57 -080069 ref->SetPendingNext(nullptr);
Hiroshi Yamauchi057d9772017-02-17 15:33:23 -080070 return ref;
71}
72
73// This must be called whenever DequeuePendingReference is called.
74void ReferenceQueue::DisableReadBarrierForReference(ObjPtr<mirror::Reference> ref) {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080075 Heap* heap = Runtime::Current()->GetHeap();
76 if (kUseBakerOrBrooksReadBarrier && heap->CurrentCollectorType() == kCollectorTypeCC &&
77 heap->ConcurrentCopyingCollector()->IsActive()) {
Hiroshi Yamauchi8e674652015-12-22 11:09:18 -080078 // Change the gray ptr we left in ConcurrentCopying::ProcessMarkStackRef() to white.
Hiroshi Yamauchi70c08d32015-09-10 16:01:30 -070079 // We check IsActive() above because we don't want to do this when the zygote compaction
80 // collector (SemiSpace) is running.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080081 CHECK(ref != nullptr);
Hiroshi Yamauchi70c08d32015-09-10 16:01:30 -070082 collector::ConcurrentCopying* concurrent_copying = heap->ConcurrentCopyingCollector();
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -070083 uint32_t rb_state = ref->GetReadBarrierState();
84 if (rb_state == ReadBarrier::GrayState()) {
85 ref->AtomicSetReadBarrierState(ReadBarrier::GrayState(), ReadBarrier::WhiteState());
86 CHECK_EQ(ref->GetReadBarrierState(), ReadBarrier::WhiteState());
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080087 } else {
Hiroshi Yamauchi8e674652015-12-22 11:09:18 -080088 // In ConcurrentCopying::ProcessMarkStackRef() we may leave a white reference in the queue and
89 // find it here, which is OK.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -070090 CHECK_EQ(rb_state, ReadBarrier::WhiteState()) << "ref=" << ref << " rb_state=" << rb_state;
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070091 ObjPtr<mirror::Object> referent = ref->GetReferent<kWithoutReadBarrier>();
Hiroshi Yamauchid2bb5ba2015-09-14 15:10:50 -070092 // The referent could be null if it's cleared by a mutator (Reference.clear()).
93 if (referent != nullptr) {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070094 CHECK(concurrent_copying->IsInToSpace(referent.Ptr()))
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -070095 << "ref=" << ref << " rb_state=" << ref->GetReadBarrierState()
Hiroshi Yamauchid2bb5ba2015-09-14 15:10:50 -070096 << " referent=" << referent;
97 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080098 }
99 }
Mathieu Chartier39e32612013-11-12 16:28:05 -0800100}
101
102void ReferenceQueue::Dump(std::ostream& os) const {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -0700103 ObjPtr<mirror::Reference> cur = list_;
Mathieu Chartier39e32612013-11-12 16:28:05 -0800104 os << "Reference starting at list_=" << list_ << "\n";
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800105 if (cur == nullptr) {
106 return;
107 }
108 do {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -0700109 ObjPtr<mirror::Reference> pending_next = cur->GetPendingNext();
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800110 os << "Reference= " << cur << " PendingNext=" << pending_next;
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700111 if (cur->IsFinalizerReferenceInstance()) {
112 os << " Zombie=" << cur->AsFinalizerReference()->GetZombie();
Mathieu Chartier39e32612013-11-12 16:28:05 -0800113 }
114 os << "\n";
115 cur = pending_next;
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800116 } while (cur != list_);
117}
118
119size_t ReferenceQueue::GetLength() const {
120 size_t count = 0;
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -0700121 ObjPtr<mirror::Reference> cur = list_;
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800122 if (cur != nullptr) {
123 do {
124 ++count;
125 cur = cur->GetPendingNext();
126 } while (cur != list_);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800127 }
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800128 return count;
Mathieu Chartier39e32612013-11-12 16:28:05 -0800129}
130
Mathieu Chartier308351a2014-06-15 12:39:02 -0700131void ReferenceQueue::ClearWhiteReferences(ReferenceQueue* cleared_references,
Mathieu Chartier97509952015-07-13 14:35:43 -0700132 collector::GarbageCollector* collector) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800133 while (!IsEmpty()) {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -0700134 ObjPtr<mirror::Reference> ref = DequeuePendingReference();
Mathieu Chartier308351a2014-06-15 12:39:02 -0700135 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
Hiroshi Yamauchi65f5f242016-12-19 11:44:47 -0800136 // do_atomic_update is false because this happens during the reference processing phase where
137 // Reference.clear() would block.
138 if (!collector->IsNullOrMarkedHeapReference(referent_addr, /*do_atomic_update*/false)) {
Mathieu Chartier308351a2014-06-15 12:39:02 -0700139 // Referent is white, clear it.
140 if (Runtime::Current()->IsActiveTransaction()) {
141 ref->ClearReferent<true>();
142 } else {
143 ref->ClearReferent<false>();
144 }
Richard Uhlerc4695df2016-01-15 14:08:05 -0800145 cleared_references->EnqueueReference(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800146 }
Hiroshi Yamauchi057d9772017-02-17 15:33:23 -0800147 // Delay disabling the read barrier until here so that the ClearReferent call above in
148 // transaction mode will trigger the read barrier.
149 DisableReadBarrierForReference(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800150 }
151}
152
Mathieu Chartier308351a2014-06-15 12:39:02 -0700153void ReferenceQueue::EnqueueFinalizerReferences(ReferenceQueue* cleared_references,
Mathieu Chartier97509952015-07-13 14:35:43 -0700154 collector::GarbageCollector* collector) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800155 while (!IsEmpty()) {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -0700156 ObjPtr<mirror::FinalizerReference> ref = DequeuePendingReference()->AsFinalizerReference();
Mathieu Chartier308351a2014-06-15 12:39:02 -0700157 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
Hiroshi Yamauchi65f5f242016-12-19 11:44:47 -0800158 // do_atomic_update is false because this happens during the reference processing phase where
159 // Reference.clear() would block.
160 if (!collector->IsNullOrMarkedHeapReference(referent_addr, /*do_atomic_update*/false)) {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -0700161 ObjPtr<mirror::Object> forward_address = collector->MarkObject(referent_addr->AsMirrorPtr());
Mathieu Chartier308351a2014-06-15 12:39:02 -0700162 // Move the updated referent to the zombie field.
163 if (Runtime::Current()->IsActiveTransaction()) {
164 ref->SetZombie<true>(forward_address);
165 ref->ClearReferent<true>();
166 } else {
167 ref->SetZombie<false>(forward_address);
168 ref->ClearReferent<false>();
Mathieu Chartier39e32612013-11-12 16:28:05 -0800169 }
Mathieu Chartier308351a2014-06-15 12:39:02 -0700170 cleared_references->EnqueueReference(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800171 }
Hiroshi Yamauchi057d9772017-02-17 15:33:23 -0800172 // Delay disabling the read barrier until here so that the ClearReferent call above in
173 // transaction mode will trigger the read barrier.
174 DisableReadBarrierForReference(ref->AsReference());
Mathieu Chartier39e32612013-11-12 16:28:05 -0800175 }
176}
177
Mathieu Chartier97509952015-07-13 14:35:43 -0700178void ReferenceQueue::ForwardSoftReferences(MarkObjectVisitor* visitor) {
Fred Shih530e1b52014-06-09 15:19:54 -0700179 if (UNLIKELY(IsEmpty())) {
180 return;
181 }
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -0700182 ObjPtr<mirror::Reference> const head = list_;
183 ObjPtr<mirror::Reference> ref = head;
Fred Shih530e1b52014-06-09 15:19:54 -0700184 do {
Mathieu Chartier308351a2014-06-15 12:39:02 -0700185 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
186 if (referent_addr->AsMirrorPtr() != nullptr) {
Hiroshi Yamauchi057d9772017-02-17 15:33:23 -0800187 // do_atomic_update is false because mutators can't access the referent due to the weak ref
188 // access blocking.
189 visitor->MarkHeapReference(referent_addr, /*do_atomic_update*/ false);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800190 }
Fred Shih530e1b52014-06-09 15:19:54 -0700191 ref = ref->GetPendingNext();
192 } while (LIKELY(ref != head));
Mathieu Chartier39e32612013-11-12 16:28:05 -0800193}
194
Mathieu Chartier97509952015-07-13 14:35:43 -0700195void ReferenceQueue::UpdateRoots(IsMarkedVisitor* visitor) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700196 if (list_ != nullptr) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700197 list_ = down_cast<mirror::Reference*>(visitor->IsMarked(list_));
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700198 }
199}
200
Mathieu Chartier39e32612013-11-12 16:28:05 -0800201} // namespace gc
202} // namespace art