blob: 7be0704d0158c805802a7c68c6909254a85a4259 [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 Chartier39e32612013-11-12 16:28:05 -080033 DCHECK(ref != NULL);
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 Chartier39e32612013-11-12 16:28:05 -080046 DCHECK(ref != NULL);
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();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010052 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070053 ref->SetPendingNext<true>(head);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010054 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070055 ref->SetPendingNext<false>(head);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010056 }
Mathieu Chartier39e32612013-11-12 16:28:05 -080057 }
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070058 if (Runtime::Current()->IsActiveTransaction()) {
59 list_->SetPendingNext<true>(ref);
60 } else {
61 list_->SetPendingNext<false>(ref);
62 }
Mathieu Chartier39e32612013-11-12 16:28:05 -080063}
64
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070065mirror::Reference* ReferenceQueue::DequeuePendingReference() {
Mathieu Chartier39e32612013-11-12 16:28:05 -080066 DCHECK(!IsEmpty());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070067 mirror::Reference* head = list_->GetPendingNext();
Mathieu Chartier39e32612013-11-12 16:28:05 -080068 DCHECK(head != nullptr);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070069 mirror::Reference* ref;
Mathieu Chartier39e32612013-11-12 16:28:05 -080070 // Note: the following code is thread-safe because it is only called from ProcessReferences which
71 // is single threaded.
72 if (list_ == head) {
73 ref = list_;
74 list_ = nullptr;
75 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070076 mirror::Reference* next = head->GetPendingNext();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010077 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070078 list_->SetPendingNext<true>(next);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010079 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070080 list_->SetPendingNext<false>(next);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010081 }
Mathieu Chartier39e32612013-11-12 16:28:05 -080082 ref = head;
83 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010084 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070085 ref->SetPendingNext<true>(nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010086 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070087 ref->SetPendingNext<false>(nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010088 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080089 Heap* heap = Runtime::Current()->GetHeap();
90 if (kUseBakerOrBrooksReadBarrier && heap->CurrentCollectorType() == kCollectorTypeCC &&
91 heap->ConcurrentCopyingCollector()->IsActive()) {
92 // Clear the gray ptr we left in ConcurrentCopying::ProcessMarkStack().
93 // We don't want to do this when the zygote compaction collector (SemiSpace) is running.
94 CHECK(ref != nullptr);
95 CHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::GrayPtr())
96 << "ref=" << ref << " rb_ptr=" << ref->GetReadBarrierPointer();
97 if (heap->ConcurrentCopyingCollector()->RegionSpace()->IsInToSpace(ref)) {
98 // Moving objects.
99 ref->SetReadBarrierPointer(ReadBarrier::WhitePtr());
100 CHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::WhitePtr());
101 } else {
102 // Non-moving objects.
103 ref->SetReadBarrierPointer(ReadBarrier::BlackPtr());
104 CHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::BlackPtr());
105 }
106 }
Mathieu Chartier39e32612013-11-12 16:28:05 -0800107 return ref;
108}
109
110void ReferenceQueue::Dump(std::ostream& os) const {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700111 mirror::Reference* cur = list_;
Mathieu Chartier39e32612013-11-12 16:28:05 -0800112 os << "Reference starting at list_=" << list_ << "\n";
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800113 if (cur == nullptr) {
114 return;
115 }
116 do {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700117 mirror::Reference* pending_next = cur->GetPendingNext();
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800118 os << "Reference= " << cur << " PendingNext=" << pending_next;
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700119 if (cur->IsFinalizerReferenceInstance()) {
120 os << " Zombie=" << cur->AsFinalizerReference()->GetZombie();
Mathieu Chartier39e32612013-11-12 16:28:05 -0800121 }
122 os << "\n";
123 cur = pending_next;
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800124 } while (cur != list_);
125}
126
127size_t ReferenceQueue::GetLength() const {
128 size_t count = 0;
129 mirror::Reference* cur = list_;
130 if (cur != nullptr) {
131 do {
132 ++count;
133 cur = cur->GetPendingNext();
134 } while (cur != list_);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800135 }
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800136 return count;
Mathieu Chartier39e32612013-11-12 16:28:05 -0800137}
138
Mathieu Chartier308351a2014-06-15 12:39:02 -0700139void ReferenceQueue::ClearWhiteReferences(ReferenceQueue* cleared_references,
140 IsHeapReferenceMarkedCallback* preserve_callback,
Mathieu Chartier39e32612013-11-12 16:28:05 -0800141 void* arg) {
142 while (!IsEmpty()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700143 mirror::Reference* ref = DequeuePendingReference();
Mathieu Chartier308351a2014-06-15 12:39:02 -0700144 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
145 if (referent_addr->AsMirrorPtr() != nullptr && !preserve_callback(referent_addr, arg)) {
146 // Referent is white, clear it.
147 if (Runtime::Current()->IsActiveTransaction()) {
148 ref->ClearReferent<true>();
149 } else {
150 ref->ClearReferent<false>();
151 }
152 if (ref->IsEnqueuable()) {
153 cleared_references->EnqueuePendingReference(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800154 }
155 }
156 }
157}
158
Mathieu Chartier308351a2014-06-15 12:39:02 -0700159void ReferenceQueue::EnqueueFinalizerReferences(ReferenceQueue* cleared_references,
160 IsHeapReferenceMarkedCallback* is_marked_callback,
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700161 MarkObjectCallback* mark_object_callback,
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800162 void* arg) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800163 while (!IsEmpty()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700164 mirror::FinalizerReference* ref = DequeuePendingReference()->AsFinalizerReference();
Mathieu Chartier308351a2014-06-15 12:39:02 -0700165 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
166 if (referent_addr->AsMirrorPtr() != nullptr && !is_marked_callback(referent_addr, arg)) {
167 mirror::Object* forward_address = mark_object_callback(referent_addr->AsMirrorPtr(), arg);
168 // If the referent is non-null the reference must queuable.
169 DCHECK(ref->IsEnqueuable());
170 // Move the updated referent to the zombie field.
171 if (Runtime::Current()->IsActiveTransaction()) {
172 ref->SetZombie<true>(forward_address);
173 ref->ClearReferent<true>();
174 } else {
175 ref->SetZombie<false>(forward_address);
176 ref->ClearReferent<false>();
Mathieu Chartier39e32612013-11-12 16:28:05 -0800177 }
Mathieu Chartier308351a2014-06-15 12:39:02 -0700178 cleared_references->EnqueueReference(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800179 }
180 }
181}
182
Mathieu Chartier308351a2014-06-15 12:39:02 -0700183void ReferenceQueue::ForwardSoftReferences(IsHeapReferenceMarkedCallback* preserve_callback,
184 void* arg) {
Fred Shih530e1b52014-06-09 15:19:54 -0700185 if (UNLIKELY(IsEmpty())) {
186 return;
187 }
188 mirror::Reference* const head = list_;
189 mirror::Reference* ref = head;
190 do {
Mathieu Chartier308351a2014-06-15 12:39:02 -0700191 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
192 if (referent_addr->AsMirrorPtr() != nullptr) {
193 UNUSED(preserve_callback(referent_addr, arg));
Mathieu Chartier39e32612013-11-12 16:28:05 -0800194 }
Fred Shih530e1b52014-06-09 15:19:54 -0700195 ref = ref->GetPendingNext();
196 } while (LIKELY(ref != head));
Mathieu Chartier39e32612013-11-12 16:28:05 -0800197}
198
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700199void ReferenceQueue::UpdateRoots(IsMarkedCallback* callback, void* arg) {
200 if (list_ != nullptr) {
201 list_ = down_cast<mirror::Reference*>(callback(list_, arg));
202 }
203}
204
Mathieu Chartier39e32612013-11-12 16:28:05 -0800205} // namespace gc
206} // namespace art