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 | #ifndef ART_RUNTIME_GC_REFERENCE_QUEUE_H_ |
| 18 | #define ART_RUNTIME_GC_REFERENCE_QUEUE_H_ |
| 19 | |
| 20 | #include <iosfwd> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 24 | #include "atomic.h" |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 25 | #include "base/mutex.h" |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 26 | #include "base/timing_logger.h" |
| 27 | #include "globals.h" |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 28 | #include "jni.h" |
Mathieu Chartier | 5d3f73a | 2016-10-14 14:28:47 -0700 | [diff] [blame] | 29 | #include "obj_ptr.h" |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 30 | #include "offsets.h" |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 31 | #include "thread_pool.h" |
| 32 | |
| 33 | namespace art { |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 34 | namespace mirror { |
| 35 | class Reference; |
| 36 | } // namespace mirror |
| 37 | |
Andreas Gampe | 5d08fcc | 2017-06-05 17:56:46 -0700 | [diff] [blame] | 38 | class IsMarkedVisitor; |
| 39 | class MarkObjectVisitor; |
| 40 | |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 41 | namespace gc { |
| 42 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 43 | namespace collector { |
| 44 | class GarbageCollector; |
| 45 | } // namespace collector |
| 46 | |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 47 | class Heap; |
| 48 | |
| 49 | // Used to temporarily store java.lang.ref.Reference(s) during GC and prior to queueing on the |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 50 | // appropriate java.lang.ref.ReferenceQueue. The linked list is maintained as an unordered, |
| 51 | // circular, and singly-linked list using the pendingNext fields of the java.lang.ref.Reference |
| 52 | // objects. |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 53 | class ReferenceQueue { |
| 54 | public: |
Mathieu Chartier | a5a53ef | 2014-09-12 12:58:05 -0700 | [diff] [blame] | 55 | explicit ReferenceQueue(Mutex* lock); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 56 | |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 57 | // Enqueue a reference if it is unprocessed. Thread safe to call from multiple |
| 58 | // threads since it uses a lock to avoid a race between checking for the references presence and |
| 59 | // adding it. |
Mathieu Chartier | 5d3f73a | 2016-10-14 14:28:47 -0700 | [diff] [blame] | 60 | void AtomicEnqueueIfNotEnqueued(Thread* self, ObjPtr<mirror::Reference> ref) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 61 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*lock_); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 62 | |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 63 | // Enqueue a reference. The reference must be unprocessed. |
| 64 | // Not thread safe, used when mutators are paused to minimize lock overhead. |
Mathieu Chartier | 5d3f73a | 2016-10-14 14:28:47 -0700 | [diff] [blame] | 65 | void EnqueueReference(ObjPtr<mirror::Reference> ref) REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 66 | |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 67 | // Dequeue a reference from the queue and return that dequeued reference. |
Hiroshi Yamauchi | 057d977 | 2017-02-17 15:33:23 -0800 | [diff] [blame] | 68 | // Call DisableReadBarrierForReference for the reference that's returned from this function. |
Mathieu Chartier | 5d3f73a | 2016-10-14 14:28:47 -0700 | [diff] [blame] | 69 | ObjPtr<mirror::Reference> DequeuePendingReference() REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 70 | |
Hiroshi Yamauchi | 057d977 | 2017-02-17 15:33:23 -0800 | [diff] [blame] | 71 | // If applicable, disable the read barrier for the reference after its referent is handled (see |
| 72 | // ConcurrentCopying::ProcessMarkStackRef.) This must be called for a reference that's dequeued |
| 73 | // from pending queue (DequeuePendingReference). |
| 74 | void DisableReadBarrierForReference(ObjPtr<mirror::Reference> ref) |
| 75 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 76 | |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 77 | // Enqueues finalizer references with white referents. White referents are blackened, moved to |
| 78 | // the zombie field, and the referent field is cleared. |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 79 | void EnqueueFinalizerReferences(ReferenceQueue* cleared_references, |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 80 | collector::GarbageCollector* collector) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 81 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 82 | |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 83 | // Walks the reference list marking any references subject to the reference clearing policy. |
| 84 | // References with a black referent are removed from the list. References with white referents |
| 85 | // biased toward saving are blackened and also removed from the list. |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 86 | void ForwardSoftReferences(MarkObjectVisitor* visitor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 87 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 88 | |
Mathieu Chartier | 9e2094f | 2014-12-11 18:43:48 -0800 | [diff] [blame] | 89 | // Unlink the reference list clearing references objects with white referents. Cleared references |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 90 | // registered to a reference queue are scheduled for appending by the heap worker thread. |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 91 | void ClearWhiteReferences(ReferenceQueue* cleared_references, |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 92 | collector::GarbageCollector* collector) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 93 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 94 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 95 | void Dump(std::ostream& os) const REQUIRES_SHARED(Locks::mutator_lock_); |
| 96 | size_t GetLength() const REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 97 | |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 98 | bool IsEmpty() const { |
| 99 | return list_ == nullptr; |
| 100 | } |
| 101 | void Clear() { |
| 102 | list_ = nullptr; |
| 103 | } |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 104 | mirror::Reference* GetList() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 105 | return list_; |
| 106 | } |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 107 | |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 108 | // Visits list_, currently only used for the mark compact GC. |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 109 | void UpdateRoots(IsMarkedVisitor* visitor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 110 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 111 | |
| 112 | private: |
| 113 | // Lock, used for parallel GC reference enqueuing. It allows for multiple threads simultaneously |
| 114 | // calling AtomicEnqueueIfNotEnqueued. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 115 | Mutex* const lock_; |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 116 | // The actual reference list. Only a root for the mark compact GC since it will be null for other |
Mathieu Chartier | 5d3f73a | 2016-10-14 14:28:47 -0700 | [diff] [blame] | 117 | // GC types. Not an ObjPtr since it is accessed from multiple threads. |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 118 | mirror::Reference* list_; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 119 | |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 120 | DISALLOW_IMPLICIT_CONSTRUCTORS(ReferenceQueue); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | } // namespace gc |
| 124 | } // namespace art |
| 125 | |
| 126 | #endif // ART_RUNTIME_GC_REFERENCE_QUEUE_H_ |