blob: f4efe3c823bcb57d4de5fb548e9acf5fb1e21b67 [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";
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080094 if (cur == nullptr) {
95 return;
96 }
97 do {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070098 mirror::Reference* pending_next = cur->GetPendingNext();
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080099 os << "Reference= " << cur << " PendingNext=" << pending_next;
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700100 if (cur->IsFinalizerReferenceInstance()) {
101 os << " Zombie=" << cur->AsFinalizerReference()->GetZombie();
Mathieu Chartier39e32612013-11-12 16:28:05 -0800102 }
103 os << "\n";
104 cur = pending_next;
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800105 } while (cur != list_);
106}
107
108size_t ReferenceQueue::GetLength() const {
109 size_t count = 0;
110 mirror::Reference* cur = list_;
111 if (cur != nullptr) {
112 do {
113 ++count;
114 cur = cur->GetPendingNext();
115 } while (cur != list_);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800116 }
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800117 return count;
Mathieu Chartier39e32612013-11-12 16:28:05 -0800118}
119
Mathieu Chartier308351a2014-06-15 12:39:02 -0700120void ReferenceQueue::ClearWhiteReferences(ReferenceQueue* cleared_references,
121 IsHeapReferenceMarkedCallback* preserve_callback,
Mathieu Chartier39e32612013-11-12 16:28:05 -0800122 void* arg) {
123 while (!IsEmpty()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700124 mirror::Reference* ref = DequeuePendingReference();
Mathieu Chartier308351a2014-06-15 12:39:02 -0700125 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
126 if (referent_addr->AsMirrorPtr() != nullptr && !preserve_callback(referent_addr, arg)) {
127 // Referent is white, clear it.
128 if (Runtime::Current()->IsActiveTransaction()) {
129 ref->ClearReferent<true>();
130 } else {
131 ref->ClearReferent<false>();
132 }
133 if (ref->IsEnqueuable()) {
134 cleared_references->EnqueuePendingReference(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800135 }
136 }
137 }
138}
139
Mathieu Chartier308351a2014-06-15 12:39:02 -0700140void ReferenceQueue::EnqueueFinalizerReferences(ReferenceQueue* cleared_references,
141 IsHeapReferenceMarkedCallback* is_marked_callback,
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700142 MarkObjectCallback* mark_object_callback,
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800143 void* arg) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800144 while (!IsEmpty()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700145 mirror::FinalizerReference* ref = DequeuePendingReference()->AsFinalizerReference();
Mathieu Chartier308351a2014-06-15 12:39:02 -0700146 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
147 if (referent_addr->AsMirrorPtr() != nullptr && !is_marked_callback(referent_addr, arg)) {
148 mirror::Object* forward_address = mark_object_callback(referent_addr->AsMirrorPtr(), arg);
149 // If the referent is non-null the reference must queuable.
150 DCHECK(ref->IsEnqueuable());
151 // Move the updated referent to the zombie field.
152 if (Runtime::Current()->IsActiveTransaction()) {
153 ref->SetZombie<true>(forward_address);
154 ref->ClearReferent<true>();
155 } else {
156 ref->SetZombie<false>(forward_address);
157 ref->ClearReferent<false>();
Mathieu Chartier39e32612013-11-12 16:28:05 -0800158 }
Mathieu Chartier308351a2014-06-15 12:39:02 -0700159 cleared_references->EnqueueReference(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800160 }
161 }
162}
163
Mathieu Chartier308351a2014-06-15 12:39:02 -0700164void ReferenceQueue::ForwardSoftReferences(IsHeapReferenceMarkedCallback* preserve_callback,
165 void* arg) {
Fred Shih530e1b52014-06-09 15:19:54 -0700166 if (UNLIKELY(IsEmpty())) {
167 return;
168 }
169 mirror::Reference* const head = list_;
170 mirror::Reference* ref = head;
171 do {
Mathieu Chartier308351a2014-06-15 12:39:02 -0700172 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
173 if (referent_addr->AsMirrorPtr() != nullptr) {
174 UNUSED(preserve_callback(referent_addr, arg));
Mathieu Chartier39e32612013-11-12 16:28:05 -0800175 }
Fred Shih530e1b52014-06-09 15:19:54 -0700176 ref = ref->GetPendingNext();
177 } while (LIKELY(ref != head));
Mathieu Chartier39e32612013-11-12 16:28:05 -0800178}
179
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700180void ReferenceQueue::UpdateRoots(IsMarkedCallback* callback, void* arg) {
181 if (list_ != nullptr) {
182 list_ = down_cast<mirror::Reference*>(callback(list_, arg));
183 }
184}
185
Mathieu Chartier39e32612013-11-12 16:28:05 -0800186} // namespace gc
187} // namespace art