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 | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 25 | #include "base/timing_logger.h" |
| 26 | #include "globals.h" |
| 27 | #include "gtest/gtest.h" |
| 28 | #include "jni.h" |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 29 | #include "object_callbacks.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 | |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 38 | namespace gc { |
| 39 | |
| 40 | class Heap; |
| 41 | |
| 42 | // Used to temporarily store java.lang.ref.Reference(s) during GC and prior to queueing on the |
| 43 | // appropriate java.lang.ref.ReferenceQueue. The linked list is maintained in the |
| 44 | // java.lang.ref.Reference objects. |
| 45 | class ReferenceQueue { |
| 46 | public: |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 47 | explicit ReferenceQueue(); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 48 | // Enqueue a reference if is not already enqueued. Thread safe to call from multiple threads |
| 49 | // since it uses a lock to avoid a race between checking for the references presence and adding |
| 50 | // it. |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 51 | void AtomicEnqueueIfNotEnqueued(Thread* self, mirror::Reference* ref) |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 52 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_); |
| 53 | // Enqueue a reference, unlike EnqueuePendingReference, enqueue reference checks that the |
| 54 | // reference IsEnqueueable. Not thread safe, used when mutators are paused to minimize lock |
| 55 | // overhead. |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 56 | void EnqueueReference(mirror::Reference* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 57 | void EnqueuePendingReference(mirror::Reference* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 58 | mirror::Reference* DequeuePendingReference() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 59 | // Enqueues finalizer references with white referents. White referents are blackened, moved to the |
| 60 | // zombie field, and the referent field is cleared. |
| 61 | void EnqueueFinalizerReferences(ReferenceQueue& cleared_references, |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 62 | IsMarkedCallback* is_marked_callback, |
| 63 | MarkObjectCallback* mark_object_callback, void* arg) |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 64 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 65 | // Walks the reference list marking any references subject to the reference clearing policy. |
| 66 | // References with a black referent are removed from the list. References with white referents |
| 67 | // biased toward saving are blackened and also removed from the list. |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 68 | void PreserveSomeSoftReferences(IsMarkedCallback* preserve_callback, void* arg) |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 69 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 70 | // Unlink the reference list clearing references objects with white referents. Cleared references |
| 71 | // registered to a reference queue are scheduled for appending by the heap worker thread. |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 72 | void ClearWhiteReferences(ReferenceQueue& cleared_references, |
| 73 | IsMarkedCallback* is_marked_callback, |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 74 | void* arg) |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 75 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 76 | void Dump(std::ostream& os) const |
| 77 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 78 | bool IsEmpty() const { |
| 79 | return list_ == nullptr; |
| 80 | } |
| 81 | void Clear() { |
| 82 | list_ = nullptr; |
| 83 | } |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 84 | mirror::Reference* GetList() { |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 85 | return list_; |
| 86 | } |
| 87 | |
| 88 | private: |
| 89 | // Lock, used for parallel GC reference enqueuing. It allows for multiple threads simultaneously |
| 90 | // calling AtomicEnqueueIfNotEnqueued. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 91 | Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 92 | // The actual reference list. Not a root since it will be nullptr when the GC is not running. |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 93 | mirror::Reference* list_; |
Mathieu Chartier | 39e3261 | 2013-11-12 16:28:05 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | } // namespace gc |
| 97 | } // namespace art |
| 98 | |
| 99 | #endif // ART_RUNTIME_GC_REFERENCE_QUEUE_H_ |