1 /* 2 * Copyright (C) 2014 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_PROCESSOR_H_ 18 #define ART_RUNTIME_GC_REFERENCE_PROCESSOR_H_ 19 20 #include "base/macros.h" 21 #include "base/locks.h" 22 #include "jni.h" 23 #include "reference_queue.h" 24 #include "runtime_globals.h" 25 26 namespace art HIDDEN { 27 28 class IsMarkedVisitor; 29 class TimingLogger; 30 31 namespace mirror { 32 class Class; 33 class FinalizerReference; 34 class Object; 35 class Reference; 36 } // namespace mirror 37 38 namespace gc { 39 40 namespace collector { 41 class GarbageCollector; 42 } // namespace collector 43 44 class Heap; 45 46 // Used to process java.lang.ref.Reference instances concurrently or paused. 47 class ReferenceProcessor { 48 public: 49 ReferenceProcessor(); 50 51 // Initialize for a reference processing pass. Called before suspending weak 52 // access. 53 void Setup(Thread* self, 54 collector::GarbageCollector* collector, 55 bool concurrent, 56 bool clear_soft_references) 57 REQUIRES(!Locks::reference_processor_lock_); 58 // Enqueue all types of java.lang.ref.References, and mark through finalizers. 59 // Assumes there is no concurrent mutator-driven marking, i.e. all potentially 60 // mutator-accessible objects should be marked before this. 61 void ProcessReferences(Thread* self, TimingLogger* timings) 62 REQUIRES_SHARED(Locks::mutator_lock_) 63 REQUIRES(Locks::heap_bitmap_lock_) 64 REQUIRES(!Locks::reference_processor_lock_); 65 66 // The slow path bool is contained in the reference class object, can only be set once 67 // Only allow setting this with mutators suspended so that we can avoid using a lock in the 68 // GetReferent fast path as an optimization. 69 void EnableSlowPath() REQUIRES_SHARED(Locks::mutator_lock_); 70 void BroadcastForSlowPath(Thread* self); 71 // Decode the referent, may block if references are being processed. In the normal 72 // no-read-barrier or Baker-read-barrier cases, we assume reference is not a PhantomReference. 73 ObjPtr<mirror::Object> GetReferent(Thread* self, ObjPtr<mirror::Reference> reference) 74 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::reference_processor_lock_); 75 // Collects the cleared references and returns a task, to be executed after FinishGC, that will 76 // enqueue all of them. 77 SelfDeletingTask* CollectClearedReferences(Thread* self) REQUIRES(!Locks::mutator_lock_); 78 void DelayReferenceReferent(ObjPtr<mirror::Class> klass, 79 ObjPtr<mirror::Reference> ref, 80 collector::GarbageCollector* collector) 81 REQUIRES_SHARED(Locks::mutator_lock_); 82 void UpdateRoots(IsMarkedVisitor* visitor) 83 REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_); 84 // Make a circular list with reference if it is not enqueued. Uses the finalizer queue lock. 85 bool MakeCircularListIfUnenqueued(ObjPtr<mirror::FinalizerReference> reference) 86 REQUIRES_SHARED(Locks::mutator_lock_) 87 REQUIRES(!Locks::reference_processor_lock_, 88 !Locks::reference_queue_finalizer_references_lock_); 89 void ClearReferent(ObjPtr<mirror::Reference> ref) 90 REQUIRES_SHARED(Locks::mutator_lock_) 91 REQUIRES(!Locks::reference_processor_lock_); 92 uint32_t ForwardSoftReferences(TimingLogger* timings) 93 REQUIRES_SHARED(Locks::mutator_lock_); 94 95 private: 96 bool SlowPathEnabled() REQUIRES_SHARED(Locks::mutator_lock_); 97 // Called by ProcessReferences. 98 void DisableSlowPath(Thread* self) REQUIRES(Locks::reference_processor_lock_) 99 REQUIRES_SHARED(Locks::mutator_lock_); 100 // Wait until reference processing is done. 101 void WaitUntilDoneProcessingReferences(Thread* self) 102 REQUIRES_SHARED(Locks::mutator_lock_) 103 REQUIRES(Locks::reference_processor_lock_); 104 // Collector which is clearing references, used by the GetReferent to return referents which are 105 // already marked. Only updated by thread currently running GC. 106 // Guarded by reference_processor_lock_ when not read by collector. Only the collector changes 107 // it. 108 collector::GarbageCollector* collector_; 109 // Reference processor state. Only valid while weak reference processing is suspended. 110 // Used by GetReferent and friends to return early. 111 enum class RpState : uint8_t { kStarting, kInitMarkingDone, kInitClearingDone }; 112 RpState rp_state_ GUARDED_BY(Locks::reference_processor_lock_); 113 bool concurrent_; // Running concurrently with mutator? Only used by GC thread. 114 bool clear_soft_references_; // Only used by GC thread. 115 116 // Condition that people wait on if they attempt to get the referent of a reference while 117 // processing is in progress. Broadcast when an empty checkpoint is requested, but not for other 118 // checkpoints or thread suspensions. See mutator_gc_coord.md. 119 ConditionVariable condition_ GUARDED_BY(Locks::reference_processor_lock_); 120 // Reference queues used by the GC. 121 ReferenceQueue soft_reference_queue_; 122 ReferenceQueue weak_reference_queue_; 123 ReferenceQueue finalizer_reference_queue_; 124 ReferenceQueue phantom_reference_queue_; 125 ReferenceQueue cleared_references_; 126 127 DISALLOW_COPY_AND_ASSIGN(ReferenceProcessor); 128 }; 129 130 } // namespace gc 131 } // namespace art 132 133 #endif // ART_RUNTIME_GC_REFERENCE_PROCESSOR_H_ 134