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" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 20 | #include "collector/concurrent_copying.h" |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 21 | #include "heap.h" |
| 22 | #include "mirror/class-inl.h" |
| 23 | #include "mirror/object-inl.h" |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 24 | #include "mirror/reference-inl.h" |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | namespace gc { |
| 28 | |
Mathieu Chartier | a5a53ef | 2014-09-12 12:58:05 -0700 | [diff] [blame] | 29 | ReferenceQueue::ReferenceQueue(Mutex* lock) : lock_(lock), list_(nullptr) { |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 30 | } |
| 31 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 32 | void ReferenceQueue::AtomicEnqueueIfNotEnqueued(Thread* self, mirror::Reference* ref) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 33 | DCHECK(ref != nullptr); |
Mathieu Chartier | a5a53ef | 2014-09-12 12:58:05 -0700 | [diff] [blame] | 34 | MutexLock mu(self, *lock_); |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 35 | if (ref->IsUnprocessed()) { |
| 36 | EnqueueReference(ref); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 40 | void ReferenceQueue::EnqueueReference(mirror::Reference* ref) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 41 | DCHECK(ref != nullptr); |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 42 | CHECK(ref->IsUnprocessed()); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 43 | if (IsEmpty()) { |
| 44 | // 1 element cyclic queue, ie: Reference ref = ..; ref.pendingNext = ref; |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 45 | list_ = ref; |
| 46 | } else { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 47 | mirror::Reference* head = list_->GetPendingNext(); |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 48 | DCHECK(head != nullptr); |
Richard Uhler | 522d51b | 2016-01-22 14:18:57 -0800 | [diff] [blame] | 49 | ref->SetPendingNext(head); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 50 | } |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 51 | // Add the reference in the middle to preserve the cycle. |
Richard Uhler | 522d51b | 2016-01-22 14:18:57 -0800 | [diff] [blame] | 52 | list_->SetPendingNext(ref); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 55 | mirror::Reference* ReferenceQueue::DequeuePendingReference() { |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 56 | DCHECK(!IsEmpty()); |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 57 | mirror::Reference* ref = list_->GetPendingNext(); |
| 58 | DCHECK(ref != nullptr); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 59 | // Note: the following code is thread-safe because it is only called from ProcessReferences which |
| 60 | // is single threaded. |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 61 | if (list_ == ref) { |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 62 | list_ = nullptr; |
| 63 | } else { |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 64 | mirror::Reference* next = ref->GetPendingNext(); |
Richard Uhler | 522d51b | 2016-01-22 14:18:57 -0800 | [diff] [blame] | 65 | list_->SetPendingNext(next); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 66 | } |
Richard Uhler | 522d51b | 2016-01-22 14:18:57 -0800 | [diff] [blame] | 67 | ref->SetPendingNext(nullptr); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 68 | Heap* heap = Runtime::Current()->GetHeap(); |
| 69 | if (kUseBakerOrBrooksReadBarrier && heap->CurrentCollectorType() == kCollectorTypeCC && |
| 70 | heap->ConcurrentCopyingCollector()->IsActive()) { |
Hiroshi Yamauchi | 70c08d3 | 2015-09-10 16:01:30 -0700 | [diff] [blame] | 71 | // Change the gray ptr we left in ConcurrentCopying::ProcessMarkStackRef() to black or white. |
| 72 | // We check IsActive() above because we don't want to do this when the zygote compaction |
| 73 | // collector (SemiSpace) is running. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 74 | CHECK(ref != nullptr); |
Hiroshi Yamauchi | 70c08d3 | 2015-09-10 16:01:30 -0700 | [diff] [blame] | 75 | collector::ConcurrentCopying* concurrent_copying = heap->ConcurrentCopyingCollector(); |
| 76 | const bool is_moving = concurrent_copying->RegionSpace()->IsInToSpace(ref); |
| 77 | if (ref->GetReadBarrierPointer() == ReadBarrier::GrayPtr()) { |
| 78 | if (is_moving) { |
| 79 | ref->AtomicSetReadBarrierPointer(ReadBarrier::GrayPtr(), ReadBarrier::WhitePtr()); |
| 80 | CHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::WhitePtr()); |
| 81 | } else { |
| 82 | ref->AtomicSetReadBarrierPointer(ReadBarrier::GrayPtr(), ReadBarrier::BlackPtr()); |
| 83 | CHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::BlackPtr()); |
| 84 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 85 | } else { |
Hiroshi Yamauchi | 70c08d3 | 2015-09-10 16:01:30 -0700 | [diff] [blame] | 86 | // In ConcurrentCopying::ProcessMarkStackRef() we may leave a black or white Reference in the |
| 87 | // queue and find it here, which is OK. Check that the color makes sense depending on whether |
| 88 | // the Reference is moving or not and that the referent has been marked. |
| 89 | if (is_moving) { |
| 90 | CHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::WhitePtr()) |
| 91 | << "ref=" << ref << " rb_ptr=" << ref->GetReadBarrierPointer(); |
| 92 | } else { |
| 93 | CHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::BlackPtr()) |
| 94 | << "ref=" << ref << " rb_ptr=" << ref->GetReadBarrierPointer(); |
| 95 | } |
| 96 | mirror::Object* referent = ref->GetReferent<kWithoutReadBarrier>(); |
Hiroshi Yamauchi | d2bb5ba | 2015-09-14 15:10:50 -0700 | [diff] [blame] | 97 | // The referent could be null if it's cleared by a mutator (Reference.clear()). |
| 98 | if (referent != nullptr) { |
| 99 | CHECK(concurrent_copying->IsInToSpace(referent)) |
| 100 | << "ref=" << ref << " rb_ptr=" << ref->GetReadBarrierPointer() |
| 101 | << " referent=" << referent; |
| 102 | } |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 105 | return ref; |
| 106 | } |
| 107 | |
| 108 | void ReferenceQueue::Dump(std::ostream& os) const { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 109 | mirror::Reference* cur = list_; |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 110 | os << "Reference starting at list_=" << list_ << "\n"; |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 111 | if (cur == nullptr) { |
| 112 | return; |
| 113 | } |
| 114 | do { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 115 | mirror::Reference* pending_next = cur->GetPendingNext(); |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 116 | os << "Reference= " << cur << " PendingNext=" << pending_next; |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 117 | if (cur->IsFinalizerReferenceInstance()) { |
| 118 | os << " Zombie=" << cur->AsFinalizerReference()->GetZombie(); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 119 | } |
| 120 | os << "\n"; |
| 121 | cur = pending_next; |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 122 | } while (cur != list_); |
| 123 | } |
| 124 | |
| 125 | size_t ReferenceQueue::GetLength() const { |
| 126 | size_t count = 0; |
| 127 | mirror::Reference* cur = list_; |
| 128 | if (cur != nullptr) { |
| 129 | do { |
| 130 | ++count; |
| 131 | cur = cur->GetPendingNext(); |
| 132 | } while (cur != list_); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 133 | } |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 134 | return count; |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 137 | void ReferenceQueue::ClearWhiteReferences(ReferenceQueue* cleared_references, |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 138 | collector::GarbageCollector* collector) { |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 139 | while (!IsEmpty()) { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 140 | mirror::Reference* ref = DequeuePendingReference(); |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 141 | mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr(); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 142 | if (referent_addr->AsMirrorPtr() != nullptr && |
| 143 | !collector->IsMarkedHeapReference(referent_addr)) { |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 144 | // Referent is white, clear it. |
| 145 | if (Runtime::Current()->IsActiveTransaction()) { |
| 146 | ref->ClearReferent<true>(); |
| 147 | } else { |
| 148 | ref->ClearReferent<false>(); |
| 149 | } |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 150 | cleared_references->EnqueueReference(ref); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 155 | void ReferenceQueue::EnqueueFinalizerReferences(ReferenceQueue* cleared_references, |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 156 | collector::GarbageCollector* collector) { |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 157 | while (!IsEmpty()) { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 158 | mirror::FinalizerReference* ref = DequeuePendingReference()->AsFinalizerReference(); |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 159 | mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr(); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 160 | if (referent_addr->AsMirrorPtr() != nullptr && |
| 161 | !collector->IsMarkedHeapReference(referent_addr)) { |
| 162 | mirror::Object* forward_address = collector->MarkObject(referent_addr->AsMirrorPtr()); |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 163 | // Move the updated referent to the zombie field. |
| 164 | if (Runtime::Current()->IsActiveTransaction()) { |
| 165 | ref->SetZombie<true>(forward_address); |
| 166 | ref->ClearReferent<true>(); |
| 167 | } else { |
| 168 | ref->SetZombie<false>(forward_address); |
| 169 | ref->ClearReferent<false>(); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 170 | } |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 171 | cleared_references->EnqueueReference(ref); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 176 | void ReferenceQueue::ForwardSoftReferences(MarkObjectVisitor* visitor) { |
Fred Shih | 530e1b5 | 2014-06-09 15:19:54 -0700 | [diff] [blame] | 177 | if (UNLIKELY(IsEmpty())) { |
| 178 | return; |
| 179 | } |
| 180 | mirror::Reference* const head = list_; |
| 181 | mirror::Reference* ref = head; |
| 182 | do { |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 183 | mirror::HeapReference<mirror::Object>* referent_addr = ref->GetReferentReferenceAddr(); |
| 184 | if (referent_addr->AsMirrorPtr() != nullptr) { |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 185 | visitor->MarkHeapReference(referent_addr); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 186 | } |
Fred Shih | 530e1b5 | 2014-06-09 15:19:54 -0700 | [diff] [blame] | 187 | ref = ref->GetPendingNext(); |
| 188 | } while (LIKELY(ref != head)); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 191 | void ReferenceQueue::UpdateRoots(IsMarkedVisitor* visitor) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 192 | if (list_ != nullptr) { |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 193 | list_ = down_cast<mirror::Reference*>(visitor->IsMarked(list_)); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 197 | } // namespace gc |
| 198 | } // namespace art |