blob: d2bd9a4797c2a1ee45f89b3207dcbff247dce29c [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"
20#include "heap.h"
21#include "mirror/class-inl.h"
22#include "mirror/object-inl.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070023#include "mirror/reference-inl.h"
Mathieu Chartier39e32612013-11-12 16:28:05 -080024
25namespace art {
26namespace gc {
27
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070028ReferenceQueue::ReferenceQueue()
Mathieu Chartier39e32612013-11-12 16:28:05 -080029 : lock_("reference queue lock"),
Mathieu Chartier39e32612013-11-12 16:28:05 -080030 list_(nullptr) {
31}
32
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070033void ReferenceQueue::AtomicEnqueueIfNotEnqueued(Thread* self, mirror::Reference* ref) {
Mathieu Chartier39e32612013-11-12 16:28:05 -080034 DCHECK(ref != NULL);
35 MutexLock mu(self, lock_);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070036 if (!ref->IsEnqueued()) {
Mathieu Chartier39e32612013-11-12 16:28:05 -080037 EnqueuePendingReference(ref);
38 }
39}
40
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070041void ReferenceQueue::EnqueueReference(mirror::Reference* ref) {
42 CHECK(ref->IsEnqueuable());
Mathieu Chartier39e32612013-11-12 16:28:05 -080043 EnqueuePendingReference(ref);
44}
45
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070046void ReferenceQueue::EnqueuePendingReference(mirror::Reference* ref) {
Mathieu Chartier39e32612013-11-12 16:28:05 -080047 DCHECK(ref != NULL);
Mathieu Chartier39e32612013-11-12 16:28:05 -080048 if (IsEmpty()) {
49 // 1 element cyclic queue, ie: Reference ref = ..; ref.pendingNext = ref;
Mathieu Chartier39e32612013-11-12 16:28:05 -080050 list_ = ref;
51 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070052 mirror::Reference* head = list_->GetPendingNext();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010053 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070054 ref->SetPendingNext<true>(head);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010055 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070056 ref->SetPendingNext<false>(head);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010057 }
Mathieu Chartier39e32612013-11-12 16:28:05 -080058 }
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070059 if (Runtime::Current()->IsActiveTransaction()) {
60 list_->SetPendingNext<true>(ref);
61 } else {
62 list_->SetPendingNext<false>(ref);
63 }
Mathieu Chartier39e32612013-11-12 16:28:05 -080064}
65
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070066mirror::Reference* ReferenceQueue::DequeuePendingReference() {
Mathieu Chartier39e32612013-11-12 16:28:05 -080067 DCHECK(!IsEmpty());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070068 mirror::Reference* head = list_->GetPendingNext();
Mathieu Chartier39e32612013-11-12 16:28:05 -080069 DCHECK(head != nullptr);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070070 mirror::Reference* ref;
Mathieu Chartier39e32612013-11-12 16:28:05 -080071 // Note: the following code is thread-safe because it is only called from ProcessReferences which
72 // is single threaded.
73 if (list_ == head) {
74 ref = list_;
75 list_ = nullptr;
76 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070077 mirror::Reference* next = head->GetPendingNext();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010078 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070079 list_->SetPendingNext<true>(next);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010080 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070081 list_->SetPendingNext<false>(next);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010082 }
Mathieu Chartier39e32612013-11-12 16:28:05 -080083 ref = head;
84 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010085 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070086 ref->SetPendingNext<true>(nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010087 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070088 ref->SetPendingNext<false>(nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010089 }
Mathieu Chartier39e32612013-11-12 16:28:05 -080090 return ref;
91}
92
93void ReferenceQueue::Dump(std::ostream& os) const {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070094 mirror::Reference* cur = list_;
Mathieu Chartier39e32612013-11-12 16:28:05 -080095 os << "Reference starting at list_=" << list_ << "\n";
96 while (cur != nullptr) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070097 mirror::Reference* pending_next = cur->GetPendingNext();
Mathieu Chartier39e32612013-11-12 16:28:05 -080098 os << "PendingNext=" << pending_next;
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070099 if (cur->IsFinalizerReferenceInstance()) {
100 os << " Zombie=" << cur->AsFinalizerReference()->GetZombie();
Mathieu Chartier39e32612013-11-12 16:28:05 -0800101 }
102 os << "\n";
103 cur = pending_next;
104 }
105}
106
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800107void ReferenceQueue::ClearWhiteReferences(ReferenceQueue& cleared_references,
108 IsMarkedCallback* preserve_callback,
Mathieu Chartier39e32612013-11-12 16:28:05 -0800109 void* arg) {
110 while (!IsEmpty()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700111 mirror::Reference* ref = DequeuePendingReference();
112 mirror::Object* referent = ref->GetReferent();
Mathieu Chartier39e32612013-11-12 16:28:05 -0800113 if (referent != nullptr) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800114 mirror::Object* forward_address = preserve_callback(referent, arg);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800115 if (forward_address == nullptr) {
116 // Referent is white, clear it.
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700117 if (Runtime::Current()->IsActiveTransaction()) {
118 ref->ClearReferent<true>();
119 } else {
120 ref->ClearReferent<false>();
121 }
122 if (ref->IsEnqueuable()) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800123 cleared_references.EnqueuePendingReference(ref);
124 }
125 } else if (referent != forward_address) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800126 // Object moved, need to updated the referent.
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700127 ref->SetReferent<false>(forward_address);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800128 }
129 }
130 }
131}
132
133void ReferenceQueue::EnqueueFinalizerReferences(ReferenceQueue& cleared_references,
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700134 IsMarkedCallback* is_marked_callback,
135 MarkObjectCallback* mark_object_callback,
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800136 void* arg) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800137 while (!IsEmpty()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700138 mirror::FinalizerReference* ref = DequeuePendingReference()->AsFinalizerReference();
139 mirror::Object* referent = ref->GetReferent();
Mathieu Chartier39e32612013-11-12 16:28:05 -0800140 if (referent != nullptr) {
141 mirror::Object* forward_address = is_marked_callback(referent, arg);
142 // If the referent isn't marked, mark it and update the
143 if (forward_address == nullptr) {
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700144 forward_address = mark_object_callback(referent, arg);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800145 // If the referent is non-null the reference must queuable.
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700146 DCHECK(ref->IsEnqueuable());
Mathieu Chartier39e32612013-11-12 16:28:05 -0800147 // Move the updated referent to the zombie field.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100148 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700149 ref->SetZombie<true>(forward_address);
150 ref->ClearReferent<true>();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100151 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700152 ref->SetZombie<false>(forward_address);
153 ref->ClearReferent<false>();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100154 }
Mathieu Chartier39e32612013-11-12 16:28:05 -0800155 cleared_references.EnqueueReference(ref);
156 } else if (referent != forward_address) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700157 ref->SetReferent<false>(forward_address);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800158 }
159 }
160 }
161}
162
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700163void ReferenceQueue::PreserveSomeSoftReferences(IsMarkedCallback* preserve_callback, void* arg) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700164 ReferenceQueue cleared;
Mathieu Chartier39e32612013-11-12 16:28:05 -0800165 while (!IsEmpty()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700166 mirror::Reference* ref = DequeuePendingReference();
167 mirror::Object* referent = ref->GetReferent();
Mathieu Chartier39e32612013-11-12 16:28:05 -0800168 if (referent != nullptr) {
169 mirror::Object* forward_address = preserve_callback(referent, arg);
170 if (forward_address == nullptr) {
171 // Either the reference isn't marked or we don't wish to preserve it.
172 cleared.EnqueuePendingReference(ref);
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800173 } else if (forward_address != referent) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700174 ref->SetReferent<false>(forward_address);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800175 }
176 }
177 }
178 list_ = cleared.GetList();
179}
180
181} // namespace gc
182} // namespace art
183