blob: 67dcc2d1a8d23ad84bd48d96cb29473b8cd5cc2d [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_);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070035 if (!ref->IsEnqueued()) {
Mathieu Chartier39e32612013-11-12 16:28:05 -080036 EnqueuePendingReference(ref);
37 }
38}
39
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070040void ReferenceQueue::EnqueueReference(mirror::Reference* ref) {
41 CHECK(ref->IsEnqueuable());
Mathieu Chartier39e32612013-11-12 16:28:05 -080042 EnqueuePendingReference(ref);
43}
44
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070045void ReferenceQueue::EnqueuePendingReference(mirror::Reference* ref) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070046 DCHECK(ref != nullptr);
Mathieu Chartier39e32612013-11-12 16:28:05 -080047 if (IsEmpty()) {
48 // 1 element cyclic queue, ie: Reference ref = ..; ref.pendingNext = ref;
Mathieu Chartier39e32612013-11-12 16:28:05 -080049 list_ = ref;
50 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070051 mirror::Reference* head = list_->GetPendingNext();
Richard Uhler522d51b2016-01-22 14:18:57 -080052 ref->SetPendingNext(head);
Mathieu Chartier39e32612013-11-12 16:28:05 -080053 }
Richard Uhler522d51b2016-01-22 14:18:57 -080054 list_->SetPendingNext(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -080055}
56
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070057mirror::Reference* ReferenceQueue::DequeuePendingReference() {
Mathieu Chartier39e32612013-11-12 16:28:05 -080058 DCHECK(!IsEmpty());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070059 mirror::Reference* head = list_->GetPendingNext();
Mathieu Chartier39e32612013-11-12 16:28:05 -080060 DCHECK(head != nullptr);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070061 mirror::Reference* ref;
Mathieu Chartier39e32612013-11-12 16:28:05 -080062 // Note: the following code is thread-safe because it is only called from ProcessReferences which
63 // is single threaded.
64 if (list_ == head) {
65 ref = list_;
66 list_ = nullptr;
67 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070068 mirror::Reference* next = head->GetPendingNext();
Richard Uhler522d51b2016-01-22 14:18:57 -080069 list_->SetPendingNext(next);
Mathieu Chartier39e32612013-11-12 16:28:05 -080070 ref = head;
71 }
Richard Uhler522d51b2016-01-22 14:18:57 -080072 ref->SetPendingNext(nullptr);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080073 Heap* heap = Runtime::Current()->GetHeap();
74 if (kUseBakerOrBrooksReadBarrier && heap->CurrentCollectorType() == kCollectorTypeCC &&
75 heap->ConcurrentCopyingCollector()->IsActive()) {
Hiroshi Yamauchi70c08d32015-09-10 16:01:30 -070076 // Change the gray ptr we left in ConcurrentCopying::ProcessMarkStackRef() to black or white.
77 // We check IsActive() above because we don't want to do this when the zygote compaction
78 // collector (SemiSpace) is running.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080079 CHECK(ref != nullptr);
Hiroshi Yamauchi70c08d32015-09-10 16:01:30 -070080 collector::ConcurrentCopying* concurrent_copying = heap->ConcurrentCopyingCollector();
81 const bool is_moving = concurrent_copying->RegionSpace()->IsInToSpace(ref);
82 if (ref->GetReadBarrierPointer() == ReadBarrier::GrayPtr()) {
83 if (is_moving) {
84 ref->AtomicSetReadBarrierPointer(ReadBarrier::GrayPtr(), ReadBarrier::WhitePtr());
85 CHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::WhitePtr());
86 } else {
87 ref->AtomicSetReadBarrierPointer(ReadBarrier::GrayPtr(), ReadBarrier::BlackPtr());
88 CHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::BlackPtr());
89 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080090 } else {
Hiroshi Yamauchi70c08d32015-09-10 16:01:30 -070091 // In ConcurrentCopying::ProcessMarkStackRef() we may leave a black or white Reference in the
92 // queue and find it here, which is OK. Check that the color makes sense depending on whether
93 // the Reference is moving or not and that the referent has been marked.
94 if (is_moving) {
95 CHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::WhitePtr())
96 << "ref=" << ref << " rb_ptr=" << ref->GetReadBarrierPointer();
97 } else {
98 CHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::BlackPtr())
99 << "ref=" << ref << " rb_ptr=" << ref->GetReadBarrierPointer();
100 }
101 mirror::Object* referent = ref->GetReferent<kWithoutReadBarrier>();
Hiroshi Yamauchid2bb5ba2015-09-14 15:10:50 -0700102 // The referent could be null if it's cleared by a mutator (Reference.clear()).
103 if (referent != nullptr) {
104 CHECK(concurrent_copying->IsInToSpace(referent))
105 << "ref=" << ref << " rb_ptr=" << ref->GetReadBarrierPointer()
106 << " referent=" << referent;
107 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800108 }
109 }
Mathieu Chartier39e32612013-11-12 16:28:05 -0800110 return ref;
111}
112
113void ReferenceQueue::Dump(std::ostream& os) const {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700114 mirror::Reference* cur = list_;
Mathieu Chartier39e32612013-11-12 16:28:05 -0800115 os << "Reference starting at list_=" << list_ << "\n";
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800116 if (cur == nullptr) {
117 return;
118 }
119 do {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700120 mirror::Reference* pending_next = cur->GetPendingNext();
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800121 os << "Reference= " << cur << " PendingNext=" << pending_next;
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700122 if (cur->IsFinalizerReferenceInstance()) {
123 os << " Zombie=" << cur->AsFinalizerReference()->GetZombie();
Mathieu Chartier39e32612013-11-12 16:28:05 -0800124 }
125 os << "\n";
126 cur = pending_next;
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800127 } while (cur != list_);
128}
129
130size_t ReferenceQueue::GetLength() const {
131 size_t count = 0;
132 mirror::Reference* cur = list_;
133 if (cur != nullptr) {
134 do {
135 ++count;
136 cur = cur->GetPendingNext();
137 } while (cur != list_);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800138 }
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800139 return count;
Mathieu Chartier39e32612013-11-12 16:28:05 -0800140}
141
Mathieu Chartier308351a2014-06-15 12:39:02 -0700142void ReferenceQueue::ClearWhiteReferences(ReferenceQueue* cleared_references,
Mathieu Chartier97509952015-07-13 14:35:43 -0700143 collector::GarbageCollector* collector) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800144 while (!IsEmpty()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700145 mirror::Reference* ref = DequeuePendingReference();
Mathieu Chartier308351a2014-06-15 12:39:02 -0700146 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
Mathieu Chartier97509952015-07-13 14:35:43 -0700147 if (referent_addr->AsMirrorPtr() != nullptr &&
148 !collector->IsMarkedHeapReference(referent_addr)) {
Mathieu Chartier308351a2014-06-15 12:39:02 -0700149 // Referent is white, clear it.
150 if (Runtime::Current()->IsActiveTransaction()) {
151 ref->ClearReferent<true>();
152 } else {
153 ref->ClearReferent<false>();
154 }
155 if (ref->IsEnqueuable()) {
156 cleared_references->EnqueuePendingReference(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800157 }
158 }
159 }
160}
161
Mathieu Chartier308351a2014-06-15 12:39:02 -0700162void ReferenceQueue::EnqueueFinalizerReferences(ReferenceQueue* cleared_references,
Mathieu Chartier97509952015-07-13 14:35:43 -0700163 collector::GarbageCollector* collector) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800164 while (!IsEmpty()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700165 mirror::FinalizerReference* ref = DequeuePendingReference()->AsFinalizerReference();
Mathieu Chartier308351a2014-06-15 12:39:02 -0700166 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
Mathieu Chartier97509952015-07-13 14:35:43 -0700167 if (referent_addr->AsMirrorPtr() != nullptr &&
168 !collector->IsMarkedHeapReference(referent_addr)) {
169 mirror::Object* forward_address = collector->MarkObject(referent_addr->AsMirrorPtr());
Mathieu Chartier308351a2014-06-15 12:39:02 -0700170 // If the referent is non-null the reference must queuable.
171 DCHECK(ref->IsEnqueuable());
172 // Move the updated referent to the zombie field.
173 if (Runtime::Current()->IsActiveTransaction()) {
174 ref->SetZombie<true>(forward_address);
175 ref->ClearReferent<true>();
176 } else {
177 ref->SetZombie<false>(forward_address);
178 ref->ClearReferent<false>();
Mathieu Chartier39e32612013-11-12 16:28:05 -0800179 }
Mathieu Chartier308351a2014-06-15 12:39:02 -0700180 cleared_references->EnqueueReference(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800181 }
182 }
183}
184
Mathieu Chartier97509952015-07-13 14:35:43 -0700185void ReferenceQueue::ForwardSoftReferences(MarkObjectVisitor* visitor) {
Fred Shih530e1b52014-06-09 15:19:54 -0700186 if (UNLIKELY(IsEmpty())) {
187 return;
188 }
189 mirror::Reference* const head = list_;
190 mirror::Reference* ref = head;
191 do {
Mathieu Chartier308351a2014-06-15 12:39:02 -0700192 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
193 if (referent_addr->AsMirrorPtr() != nullptr) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700194 visitor->MarkHeapReference(referent_addr);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800195 }
Fred Shih530e1b52014-06-09 15:19:54 -0700196 ref = ref->GetPendingNext();
197 } while (LIKELY(ref != head));
Mathieu Chartier39e32612013-11-12 16:28:05 -0800198}
199
Mathieu Chartier97509952015-07-13 14:35:43 -0700200void ReferenceQueue::UpdateRoots(IsMarkedVisitor* visitor) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700201 if (list_ != nullptr) {
Mathieu Chartier97509952015-07-13 14:35:43 -0700202 list_ = down_cast<mirror::Reference*>(visitor->IsMarked(list_));
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700203 }
204}
205
Mathieu Chartier39e32612013-11-12 16:28:05 -0800206} // namespace gc
207} // namespace art