Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 1 | /* |
| 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 Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 23 | #include "mirror/reference-inl.h" |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | namespace gc { |
| 27 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 28 | ReferenceQueue::ReferenceQueue() |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 29 | : lock_("reference queue lock"), |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 30 | list_(nullptr) { |
| 31 | } |
| 32 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 33 | void ReferenceQueue::AtomicEnqueueIfNotEnqueued(Thread* self, mirror::Reference* ref) { |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 34 | DCHECK(ref != NULL); |
| 35 | MutexLock mu(self, lock_); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 36 | if (!ref->IsEnqueued()) { |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 37 | EnqueuePendingReference(ref); |
| 38 | } |
| 39 | } |
| 40 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 41 | void ReferenceQueue::EnqueueReference(mirror::Reference* ref) { |
| 42 | CHECK(ref->IsEnqueuable()); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 43 | EnqueuePendingReference(ref); |
| 44 | } |
| 45 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 46 | void ReferenceQueue::EnqueuePendingReference(mirror::Reference* ref) { |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 47 | DCHECK(ref != NULL); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 48 | if (IsEmpty()) { |
| 49 | // 1 element cyclic queue, ie: Reference ref = ..; ref.pendingNext = ref; |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 50 | list_ = ref; |
| 51 | } else { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 52 | mirror::Reference* head = list_->GetPendingNext(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 53 | if (Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 54 | ref->SetPendingNext<true>(head); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 55 | } else { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 56 | ref->SetPendingNext<false>(head); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 57 | } |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 58 | } |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 59 | if (Runtime::Current()->IsActiveTransaction()) { |
| 60 | list_->SetPendingNext<true>(ref); |
| 61 | } else { |
| 62 | list_->SetPendingNext<false>(ref); |
| 63 | } |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 66 | mirror::Reference* ReferenceQueue::DequeuePendingReference() { |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 67 | DCHECK(!IsEmpty()); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 68 | mirror::Reference* head = list_->GetPendingNext(); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 69 | DCHECK(head != nullptr); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 70 | mirror::Reference* ref; |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 71 | // 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 Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 77 | mirror::Reference* next = head->GetPendingNext(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 78 | if (Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 79 | list_->SetPendingNext<true>(next); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 80 | } else { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 81 | list_->SetPendingNext<false>(next); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 82 | } |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 83 | ref = head; |
| 84 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 85 | if (Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 86 | ref->SetPendingNext<true>(nullptr); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 87 | } else { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 88 | ref->SetPendingNext<false>(nullptr); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 89 | } |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 90 | return ref; |
| 91 | } |
| 92 | |
| 93 | void ReferenceQueue::Dump(std::ostream& os) const { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 94 | mirror::Reference* cur = list_; |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 95 | os << "Reference starting at list_=" << list_ << "\n"; |
| 96 | while (cur != nullptr) { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 97 | mirror::Reference* pending_next = cur->GetPendingNext(); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 98 | os << "PendingNext=" << pending_next; |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 99 | if (cur->IsFinalizerReferenceInstance()) { |
| 100 | os << " Zombie=" << cur->AsFinalizerReference()->GetZombie(); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 101 | } |
| 102 | os << "\n"; |
| 103 | cur = pending_next; |
| 104 | } |
| 105 | } |
| 106 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 107 | void ReferenceQueue::ClearWhiteReferences(ReferenceQueue& cleared_references, |
| 108 | IsMarkedCallback* preserve_callback, |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 109 | void* arg) { |
| 110 | while (!IsEmpty()) { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 111 | mirror::Reference* ref = DequeuePendingReference(); |
| 112 | mirror::Object* referent = ref->GetReferent(); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 113 | if (referent != nullptr) { |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 114 | mirror::Object* forward_address = preserve_callback(referent, arg); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 115 | if (forward_address == nullptr) { |
| 116 | // Referent is white, clear it. |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 117 | if (Runtime::Current()->IsActiveTransaction()) { |
| 118 | ref->ClearReferent<true>(); |
| 119 | } else { |
| 120 | ref->ClearReferent<false>(); |
| 121 | } |
| 122 | if (ref->IsEnqueuable()) { |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 123 | cleared_references.EnqueuePendingReference(ref); |
| 124 | } |
| 125 | } else if (referent != forward_address) { |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 126 | // Object moved, need to updated the referent. |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 127 | ref->SetReferent<false>(forward_address); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void ReferenceQueue::EnqueueFinalizerReferences(ReferenceQueue& cleared_references, |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame^] | 134 | IsMarkedCallback* is_marked_callback, |
| 135 | MarkObjectCallback* mark_object_callback, |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 136 | void* arg) { |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 137 | while (!IsEmpty()) { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 138 | mirror::FinalizerReference* ref = DequeuePendingReference()->AsFinalizerReference(); |
| 139 | mirror::Object* referent = ref->GetReferent(); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 140 | 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 Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame^] | 144 | forward_address = mark_object_callback(referent, arg); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 145 | // If the referent is non-null the reference must queuable. |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 146 | DCHECK(ref->IsEnqueuable()); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 147 | // Move the updated referent to the zombie field. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 148 | if (Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 149 | ref->SetZombie<true>(forward_address); |
| 150 | ref->ClearReferent<true>(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 151 | } else { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 152 | ref->SetZombie<false>(forward_address); |
| 153 | ref->ClearReferent<false>(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 154 | } |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 155 | cleared_references.EnqueueReference(ref); |
| 156 | } else if (referent != forward_address) { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 157 | ref->SetReferent<false>(forward_address); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame^] | 163 | void ReferenceQueue::PreserveSomeSoftReferences(IsMarkedCallback* preserve_callback, void* arg) { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 164 | ReferenceQueue cleared; |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 165 | while (!IsEmpty()) { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 166 | mirror::Reference* ref = DequeuePendingReference(); |
| 167 | mirror::Object* referent = ref->GetReferent(); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 168 | 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 Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 173 | } else if (forward_address != referent) { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 174 | ref->SetReferent<false>(forward_address); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | } |
| 178 | list_ = cleared.GetList(); |
| 179 | } |
| 180 | |
| 181 | } // namespace gc |
| 182 | } // namespace art |
| 183 | |