blob: c3931e8bdc273e84a1a9ca98eb9ad196e08ca086 [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 Chartier308351a2014-06-15 12:39:02 -070029 : lock_("reference queue 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);
34 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 }
Mathieu Chartier39e32612013-11-12 16:28:05 -080089 return ref;
90}
91
92void ReferenceQueue::Dump(std::ostream& os) const {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070093 mirror::Reference* cur = list_;
Mathieu Chartier39e32612013-11-12 16:28:05 -080094 os << "Reference starting at list_=" << list_ << "\n";
95 while (cur != nullptr) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070096 mirror::Reference* pending_next = cur->GetPendingNext();
Mathieu Chartier39e32612013-11-12 16:28:05 -080097 os << "PendingNext=" << pending_next;
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070098 if (cur->IsFinalizerReferenceInstance()) {
99 os << " Zombie=" << cur->AsFinalizerReference()->GetZombie();
Mathieu Chartier39e32612013-11-12 16:28:05 -0800100 }
101 os << "\n";
102 cur = pending_next;
103 }
104}
105
Mathieu Chartier308351a2014-06-15 12:39:02 -0700106void ReferenceQueue::ClearWhiteReferences(ReferenceQueue* cleared_references,
107 IsHeapReferenceMarkedCallback* preserve_callback,
Mathieu Chartier39e32612013-11-12 16:28:05 -0800108 void* arg) {
109 while (!IsEmpty()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700110 mirror::Reference* ref = DequeuePendingReference();
Mathieu Chartier308351a2014-06-15 12:39:02 -0700111 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
112 if (referent_addr->AsMirrorPtr() != nullptr && !preserve_callback(referent_addr, arg)) {
113 // Referent is white, clear it.
114 if (Runtime::Current()->IsActiveTransaction()) {
115 ref->ClearReferent<true>();
116 } else {
117 ref->ClearReferent<false>();
118 }
119 if (ref->IsEnqueuable()) {
120 cleared_references->EnqueuePendingReference(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800121 }
122 }
123 }
124}
125
Mathieu Chartier308351a2014-06-15 12:39:02 -0700126void ReferenceQueue::EnqueueFinalizerReferences(ReferenceQueue* cleared_references,
127 IsHeapReferenceMarkedCallback* is_marked_callback,
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700128 MarkObjectCallback* mark_object_callback,
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800129 void* arg) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800130 while (!IsEmpty()) {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700131 mirror::FinalizerReference* ref = DequeuePendingReference()->AsFinalizerReference();
Mathieu Chartier308351a2014-06-15 12:39:02 -0700132 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
133 if (referent_addr->AsMirrorPtr() != nullptr && !is_marked_callback(referent_addr, arg)) {
134 mirror::Object* forward_address = mark_object_callback(referent_addr->AsMirrorPtr(), arg);
135 // If the referent is non-null the reference must queuable.
136 DCHECK(ref->IsEnqueuable());
137 // Move the updated referent to the zombie field.
138 if (Runtime::Current()->IsActiveTransaction()) {
139 ref->SetZombie<true>(forward_address);
140 ref->ClearReferent<true>();
141 } else {
142 ref->SetZombie<false>(forward_address);
143 ref->ClearReferent<false>();
Mathieu Chartier39e32612013-11-12 16:28:05 -0800144 }
Mathieu Chartier308351a2014-06-15 12:39:02 -0700145 cleared_references->EnqueueReference(ref);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800146 }
147 }
148}
149
Mathieu Chartier308351a2014-06-15 12:39:02 -0700150void ReferenceQueue::ForwardSoftReferences(IsHeapReferenceMarkedCallback* preserve_callback,
151 void* arg) {
Fred Shih530e1b52014-06-09 15:19:54 -0700152 if (UNLIKELY(IsEmpty())) {
153 return;
154 }
155 mirror::Reference* const head = list_;
156 mirror::Reference* ref = head;
157 do {
Mathieu Chartier308351a2014-06-15 12:39:02 -0700158 mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr();
159 if (referent_addr->AsMirrorPtr() != nullptr) {
160 UNUSED(preserve_callback(referent_addr, arg));
Mathieu Chartier39e32612013-11-12 16:28:05 -0800161 }
Fred Shih530e1b52014-06-09 15:19:54 -0700162 ref = ref->GetPendingNext();
163 } while (LIKELY(ref != head));
Mathieu Chartier39e32612013-11-12 16:28:05 -0800164}
165
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700166void ReferenceQueue::UpdateRoots(IsMarkedCallback* callback, void* arg) {
167 if (list_ != nullptr) {
168 list_ = down_cast<mirror::Reference*>(callback(list_, arg));
169 }
170}
171
Mathieu Chartier39e32612013-11-12 16:28:05 -0800172} // namespace gc
173} // namespace art