blob: 4003524e5ea3d3d9a0f07cef69ad8bd2cdacc67b [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 Chartiera5a53ef2014-09-12 12:58:05 -070028ReferenceQueue::ReferenceQueue(Mutex* lock) : lock_(lock), list_(nullptr) {
Mathieu Chartier39e32612013-11-12 16:28:05 -080029}
30
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070031void ReferenceQueue::AtomicEnqueueIfNotEnqueued(Thread* self, mirror::Reference* ref) {
Mathieu Chartier39e32612013-11-12 16:28:05 -080032 DCHECK(ref != NULL);
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070033 MutexLock mu(self, *lock_);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070034 if (!ref->IsEnqueued()) {
Mathieu Chartier39e32612013-11-12 16:28:05 -080035 EnqueuePendingReference(ref);
36 }
37}
38
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070039void ReferenceQueue::EnqueueReference(mirror::Reference* ref) {
40 CHECK(ref->IsEnqueuable());
Mathieu Chartier39e32612013-11-12 16:28:05 -080041 EnqueuePendingReference(ref);
42}
43
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070044void ReferenceQueue::EnqueuePendingReference(mirror::Reference* ref) {
Mathieu Chartier39e32612013-11-12 16:28:05 -080045 DCHECK(ref != NULL);
Mathieu Chartier39e32612013-11-12 16:28:05 -080046 if (IsEmpty()) {
47 // 1 element cyclic queue, ie: Reference ref = ..; ref.pendingNext = ref;
Mathieu Chartier39e32612013-11-12 16:28:05 -080048 list_ = ref;
49 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070050 mirror::Reference* head = list_->GetPendingNext();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010051 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070052 ref->SetPendingNext<true>(head);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010053 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070054 ref->SetPendingNext<false>(head);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010055 }
Mathieu Chartier39e32612013-11-12 16:28:05 -080056 }
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070057 if (Runtime::Current()->IsActiveTransaction()) {
58 list_->SetPendingNext<true>(ref);
59 } else {
60 list_->SetPendingNext<false>(ref);
61 }
Mathieu Chartier39e32612013-11-12 16:28:05 -080062}
63
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070064mirror::Reference* ReferenceQueue::DequeuePendingReference() {
Mathieu Chartier39e32612013-11-12 16:28:05 -080065 DCHECK(!IsEmpty());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070066 mirror::Reference* head = list_->GetPendingNext();
Mathieu Chartier39e32612013-11-12 16:28:05 -080067 DCHECK(head != nullptr);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070068 mirror::Reference* ref;
Mathieu Chartier39e32612013-11-12 16:28:05 -080069 // Note: the following code is thread-safe because it is only called from ProcessReferences which
70 // is single threaded.
71 if (list_ == head) {
72 ref = list_;
73 list_ = nullptr;
74 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070075 mirror::Reference* next = head->GetPendingNext();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010076 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070077 list_->SetPendingNext<true>(next);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010078 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070079 list_->SetPendingNext<false>(next);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010080 }
Mathieu Chartier39e32612013-11-12 16:28:05 -080081 ref = head;
82 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010083 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070084 ref->SetPendingNext<true>(nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010085 } else {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070086 ref->SetPendingNext<false>(nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010087 }
Mathieu Chartier39e32612013-11-12 16:28:05 -080088 return ref;
89}
90
91void ReferenceQueue::Dump(std::ostream& os) const {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070092 mirror::Reference* cur = list_;
Mathieu Chartier39e32612013-11-12 16:28:05 -080093 os << "Reference starting at list_=" << list_ << "\n";
94 while (cur != nullptr) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070095 mirror::Reference* pending_next = cur->GetPendingNext();
Mathieu Chartier39e32612013-11-12 16:28:05 -080096 os << "PendingNext=" << pending_next;
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070097 if (cur->IsFinalizerReferenceInstance()) {
98 os << " Zombie=" << cur->AsFinalizerReference()->GetZombie();
Mathieu Chartier39e32612013-11-12 16:28:05 -080099 }
100 os << "\n";
101 cur = pending_next;
102 }
103}
104
Mathieu Chartier308351a2014-06-15 12:39:02 -0700105void ReferenceQueue::ClearWhiteReferences(ReferenceQueue* cleared_references,
106 IsHeapReferenceMarkedCallback* preserve_callback,
Mathieu Chartier39e32612013-11-12 16:28:05 -0800107 void* arg) {
108 while (!IsEmpty()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700109 mirror::Reference* ref = DequeuePendingReference();
Mathieu Chartier308351a2014-06-15 12:39:02 -0700110 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
111 if (referent_addr->AsMirrorPtr() != nullptr && !preserve_callback(referent_addr, arg)) {
112 // Referent is white, clear it.
113 if (Runtime::Current()->IsActiveTransaction()) {
114 ref->ClearReferent<true>();
115 } else {
116 ref->ClearReferent<false>();
117 }
118 if (ref->IsEnqueuable()) {
119 cleared_references->EnqueuePendingReference(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800120 }
121 }
122 }
123}
124
Mathieu Chartier308351a2014-06-15 12:39:02 -0700125void ReferenceQueue::EnqueueFinalizerReferences(ReferenceQueue* cleared_references,
126 IsHeapReferenceMarkedCallback* is_marked_callback,
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700127 MarkObjectCallback* mark_object_callback,
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800128 void* arg) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800129 while (!IsEmpty()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700130 mirror::FinalizerReference* ref = DequeuePendingReference()->AsFinalizerReference();
Mathieu Chartier308351a2014-06-15 12:39:02 -0700131 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
132 if (referent_addr->AsMirrorPtr() != nullptr && !is_marked_callback(referent_addr, arg)) {
133 mirror::Object* forward_address = mark_object_callback(referent_addr->AsMirrorPtr(), arg);
134 // If the referent is non-null the reference must queuable.
135 DCHECK(ref->IsEnqueuable());
136 // Move the updated referent to the zombie field.
137 if (Runtime::Current()->IsActiveTransaction()) {
138 ref->SetZombie<true>(forward_address);
139 ref->ClearReferent<true>();
140 } else {
141 ref->SetZombie<false>(forward_address);
142 ref->ClearReferent<false>();
Mathieu Chartier39e32612013-11-12 16:28:05 -0800143 }
Mathieu Chartier308351a2014-06-15 12:39:02 -0700144 cleared_references->EnqueueReference(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800145 }
146 }
147}
148
Mathieu Chartier308351a2014-06-15 12:39:02 -0700149void ReferenceQueue::ForwardSoftReferences(IsHeapReferenceMarkedCallback* preserve_callback,
150 void* arg) {
Fred Shih530e1b52014-06-09 15:19:54 -0700151 if (UNLIKELY(IsEmpty())) {
152 return;
153 }
154 mirror::Reference* const head = list_;
155 mirror::Reference* ref = head;
156 do {
Mathieu Chartier308351a2014-06-15 12:39:02 -0700157 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
158 if (referent_addr->AsMirrorPtr() != nullptr) {
159 UNUSED(preserve_callback(referent_addr, arg));
Mathieu Chartier39e32612013-11-12 16:28:05 -0800160 }
Fred Shih530e1b52014-06-09 15:19:54 -0700161 ref = ref->GetPendingNext();
162 } while (LIKELY(ref != head));
Mathieu Chartier39e32612013-11-12 16:28:05 -0800163}
164
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700165void ReferenceQueue::UpdateRoots(IsMarkedCallback* callback, void* arg) {
166 if (list_ != nullptr) {
167 list_ = down_cast<mirror::Reference*>(callback(list_, arg));
168 }
169}
170
Mathieu Chartier39e32612013-11-12 16:28:05 -0800171} // namespace gc
172} // namespace art