blob: 03ab9a1a73af510cbe5d76b6ca7afd1eb0d1bd69 [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 Chartier8fa2dad2014-03-13 12:22:56 -070032void ReferenceQueue::AtomicEnqueueIfNotEnqueued(Thread* self, 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 Chartier8fa2dad2014-03-13 12:22:56 -070040void ReferenceQueue::EnqueueReference(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 Chartier39e32612013-11-12 16:28:05 -080045 list_ = ref;
46 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070047 mirror::Reference* head = list_->GetPendingNext();
Richard Uhlerc4695df2016-01-15 14:08:05 -080048 DCHECK(head != nullptr);
Richard Uhler522d51b2016-01-22 14:18:57 -080049 ref->SetPendingNext(head);
Mathieu Chartier39e32612013-11-12 16:28:05 -080050 }
Richard Uhlerc4695df2016-01-15 14:08:05 -080051 // Add the reference in the middle to preserve the cycle.
Richard Uhler522d51b2016-01-22 14:18:57 -080052 list_->SetPendingNext(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -080053}
54
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070055mirror::Reference* ReferenceQueue::DequeuePendingReference() {
Mathieu Chartier39e32612013-11-12 16:28:05 -080056 DCHECK(!IsEmpty());
Richard Uhlerc4695df2016-01-15 14:08:05 -080057 mirror::Reference* ref = list_->GetPendingNext();
58 DCHECK(ref != nullptr);
Mathieu Chartier39e32612013-11-12 16:28:05 -080059 // Note: the following code is thread-safe because it is only called from ProcessReferences which
60 // is single threaded.
Richard Uhlerc4695df2016-01-15 14:08:05 -080061 if (list_ == ref) {
Mathieu Chartier39e32612013-11-12 16:28:05 -080062 list_ = nullptr;
63 } else {
Richard Uhlerc4695df2016-01-15 14:08:05 -080064 mirror::Reference* next = ref->GetPendingNext();
Richard Uhler522d51b2016-01-22 14:18:57 -080065 list_->SetPendingNext(next);
Mathieu Chartier39e32612013-11-12 16:28:05 -080066 }
Richard Uhler522d51b2016-01-22 14:18:57 -080067 ref->SetPendingNext(nullptr);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080068 Heap* heap = Runtime::Current()->GetHeap();
69 if (kUseBakerOrBrooksReadBarrier && heap->CurrentCollectorType() == kCollectorTypeCC &&
70 heap->ConcurrentCopyingCollector()->IsActive()) {
Hiroshi Yamauchi70c08d32015-09-10 16:01:30 -070071 // Change the gray ptr we left in ConcurrentCopying::ProcessMarkStackRef() to black or white.
72 // We check IsActive() above because we don't want to do this when the zygote compaction
73 // collector (SemiSpace) is running.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080074 CHECK(ref != nullptr);
Hiroshi Yamauchi70c08d32015-09-10 16:01:30 -070075 collector::ConcurrentCopying* concurrent_copying = heap->ConcurrentCopyingCollector();
76 const bool is_moving = concurrent_copying->RegionSpace()->IsInToSpace(ref);
77 if (ref->GetReadBarrierPointer() == ReadBarrier::GrayPtr()) {
78 if (is_moving) {
79 ref->AtomicSetReadBarrierPointer(ReadBarrier::GrayPtr(), ReadBarrier::WhitePtr());
80 CHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::WhitePtr());
81 } else {
82 ref->AtomicSetReadBarrierPointer(ReadBarrier::GrayPtr(), ReadBarrier::BlackPtr());
83 CHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::BlackPtr());
84 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080085 } else {
Hiroshi Yamauchi70c08d32015-09-10 16:01:30 -070086 // In ConcurrentCopying::ProcessMarkStackRef() we may leave a black or white Reference in the
87 // queue and find it here, which is OK. Check that the color makes sense depending on whether
88 // the Reference is moving or not and that the referent has been marked.
89 if (is_moving) {
90 CHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::WhitePtr())
91 << "ref=" << ref << " rb_ptr=" << ref->GetReadBarrierPointer();
92 } else {
93 CHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::BlackPtr())
94 << "ref=" << ref << " rb_ptr=" << ref->GetReadBarrierPointer();
95 }
96 mirror::Object* referent = ref->GetReferent<kWithoutReadBarrier>();
Hiroshi Yamauchid2bb5ba2015-09-14 15:10:50 -070097 // The referent could be null if it's cleared by a mutator (Reference.clear()).
98 if (referent != nullptr) {
99 CHECK(concurrent_copying->IsInToSpace(referent))
100 << "ref=" << ref << " rb_ptr=" << ref->GetReadBarrierPointer()
101 << " referent=" << referent;
102 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800103 }
104 }
Mathieu Chartier39e32612013-11-12 16:28:05 -0800105 return ref;
106}
107
108void ReferenceQueue::Dump(std::ostream& os) const {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700109 mirror::Reference* cur = list_;
Mathieu Chartier39e32612013-11-12 16:28:05 -0800110 os << "Reference starting at list_=" << list_ << "\n";
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800111 if (cur == nullptr) {
112 return;
113 }
114 do {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700115 mirror::Reference* pending_next = cur->GetPendingNext();
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800116 os << "Reference= " << cur << " PendingNext=" << pending_next;
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700117 if (cur->IsFinalizerReferenceInstance()) {
118 os << " Zombie=" << cur->AsFinalizerReference()->GetZombie();
Mathieu Chartier39e32612013-11-12 16:28:05 -0800119 }
120 os << "\n";
121 cur = pending_next;
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800122 } while (cur != list_);
123}
124
125size_t ReferenceQueue::GetLength() const {
126 size_t count = 0;
127 mirror::Reference* cur = list_;
128 if (cur != nullptr) {
129 do {
130 ++count;
131 cur = cur->GetPendingNext();
132 } while (cur != list_);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800133 }
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800134 return count;
Mathieu Chartier39e32612013-11-12 16:28:05 -0800135}
136
Mathieu Chartier308351a2014-06-15 12:39:02 -0700137void ReferenceQueue::ClearWhiteReferences(ReferenceQueue* cleared_references,
Mathieu Chartier97509952015-07-13 14:35:43 -0700138 collector::GarbageCollector* collector) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800139 while (!IsEmpty()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700140 mirror::Reference* ref = DequeuePendingReference();
Mathieu Chartier308351a2014-06-15 12:39:02 -0700141 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
Mathieu Chartier97509952015-07-13 14:35:43 -0700142 if (referent_addr->AsMirrorPtr() != nullptr &&
143 !collector->IsMarkedHeapReference(referent_addr)) {
Mathieu Chartier308351a2014-06-15 12:39:02 -0700144 // Referent is white, clear it.
145 if (Runtime::Current()->IsActiveTransaction()) {
146 ref->ClearReferent<true>();
147 } else {
148 ref->ClearReferent<false>();
149 }
Richard Uhlerc4695df2016-01-15 14:08:05 -0800150 cleared_references->EnqueueReference(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800151 }
152 }
153}
154
Mathieu Chartier308351a2014-06-15 12:39:02 -0700155void ReferenceQueue::EnqueueFinalizerReferences(ReferenceQueue* cleared_references,
Mathieu Chartier97509952015-07-13 14:35:43 -0700156 collector::GarbageCollector* collector) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800157 while (!IsEmpty()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700158 mirror::FinalizerReference* ref = DequeuePendingReference()->AsFinalizerReference();
Mathieu Chartier308351a2014-06-15 12:39:02 -0700159 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
Mathieu Chartier97509952015-07-13 14:35:43 -0700160 if (referent_addr->AsMirrorPtr() != nullptr &&
161 !collector->IsMarkedHeapReference(referent_addr)) {
162 mirror::Object* forward_address = collector->MarkObject(referent_addr->AsMirrorPtr());
Mathieu Chartier308351a2014-06-15 12:39:02 -0700163 // Move the updated referent to the zombie field.
164 if (Runtime::Current()->IsActiveTransaction()) {
165 ref->SetZombie<true>(forward_address);
166 ref->ClearReferent<true>();
167 } else {
168 ref->SetZombie<false>(forward_address);
169 ref->ClearReferent<false>();
Mathieu Chartier39e32612013-11-12 16:28:05 -0800170 }
Mathieu Chartier308351a2014-06-15 12:39:02 -0700171 cleared_references->EnqueueReference(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800172 }
173 }
174}
175
Mathieu Chartier97509952015-07-13 14:35:43 -0700176void ReferenceQueue::ForwardSoftReferences(MarkObjectVisitor* visitor) {
Fred Shih530e1b52014-06-09 15:19:54 -0700177 if (UNLIKELY(IsEmpty())) {
178 return;
179 }
180 mirror::Reference* const head = list_;
181 mirror::Reference* ref = head;
182 do {
Mathieu Chartier308351a2014-06-15 12:39:02 -0700183 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
184 if (referent_addr->AsMirrorPtr() != nullptr) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700185 visitor->MarkHeapReference(referent_addr);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800186 }
Fred Shih530e1b52014-06-09 15:19:54 -0700187 ref = ref->GetPendingNext();
188 } while (LIKELY(ref != head));
Mathieu Chartier39e32612013-11-12 16:28:05 -0800189}
190
Mathieu Chartier97509952015-07-13 14:35:43 -0700191void ReferenceQueue::UpdateRoots(IsMarkedVisitor* visitor) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700192 if (list_ != nullptr) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700193 list_ = down_cast<mirror::Reference*>(visitor->IsMarked(list_));
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700194 }
195}
196
Mathieu Chartier39e32612013-11-12 16:28:05 -0800197} // namespace gc
198} // namespace art