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