reference_queue.h revision 78f7b4c04ab6e8b5581921bc95b67a9beee1c246
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 24#include "atomic.h" 25#include "base/timing_logger.h" 26#include "globals.h" 27#include "gtest/gtest.h" 28#include "jni.h" 29#include "object_callbacks.h" 30#include "offsets.h" 31#include "thread_pool.h" 32 33namespace art { 34namespace mirror { 35class Reference; 36} // namespace mirror 37 38namespace gc { 39 40class 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. 45class ReferenceQueue { 46 public: 47 explicit ReferenceQueue(); 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. 51 void AtomicEnqueueIfNotEnqueued(Thread* self, mirror::Reference* ref) 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. 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_); 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, 62 IsMarkedCallback* is_marked_callback, 63 MarkObjectCallback* mark_object_callback, void* arg) 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. 68 void PreserveSomeSoftReferences(IsMarkedCallback* preserve_callback, void* arg) 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. 72 void ClearWhiteReferences(ReferenceQueue& cleared_references, 73 IsMarkedCallback* is_marked_callback, 74 void* arg) 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 } 84 mirror::Reference* GetList() { 85 return list_; 86 } 87 88 private: 89 // Lock, used for parallel GC reference enqueuing. It allows for multiple threads simultaneously 90 // calling AtomicEnqueueIfNotEnqueued. 91 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 92 // The actual reference list. Not a root since it will be nullptr when the GC is not running. 93 mirror::Reference* list_; 94}; 95 96} // namespace gc 97} // namespace art 98 99#endif // ART_RUNTIME_GC_REFERENCE_QUEUE_H_ 100