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/mutex.h" 21 #include "globals.h" 22 #include "jni.h" 23 #include "object_callbacks.h" 24 #include "reference_queue.h" 25 26 namespace art { 27 28 class TimingLogger; 29 30 namespace mirror { 31 class FinalizerReference; 32 class Object; 33 class Reference; 34 } // namespace mirror 35 36 namespace gc { 37 38 class Heap; 39 40 // Used to process java.lang.References concurrently or paused. 41 class ReferenceProcessor { 42 public: 43 explicit ReferenceProcessor(); 44 static bool PreserveSoftReferenceCallback(mirror::HeapReference<mirror::Object>* obj, void* arg) 45 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 46 void ProcessReferences(bool concurrent, TimingLogger* timings, bool clear_soft_references, 47 IsHeapReferenceMarkedCallback* is_marked_callback, 48 MarkObjectCallback* mark_object_callback, 49 ProcessMarkStackCallback* process_mark_stack_callback, void* arg) 50 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 51 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) 52 LOCKS_EXCLUDED(Locks::reference_processor_lock_); 53 // The slow path bool is contained in the reference class object, can only be set once 54 // Only allow setting this with mutators suspended so that we can avoid using a lock in the 55 // GetReferent fast path as an optimization. 56 void EnableSlowPath() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 57 // Decode the referent, may block if references are being processed. 58 mirror::Object* GetReferent(Thread* self, mirror::Reference* reference) 59 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(Locks::reference_processor_lock_); 60 void EnqueueClearedReferences(Thread* self) LOCKS_EXCLUDED(Locks::mutator_lock_); 61 void DelayReferenceReferent(mirror::Class* klass, mirror::Reference* ref, 62 IsHeapReferenceMarkedCallback* is_marked_callback, void* arg) 63 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 64 void UpdateRoots(IsMarkedCallback* callback, void* arg) 65 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_); 66 // Make a circular list with reference if it is not enqueued. Uses the finalizer queue lock. 67 bool MakeCircularListIfUnenqueued(mirror::FinalizerReference* reference) 68 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 69 LOCKS_EXCLUDED(Locks::reference_processor_lock_, 70 Locks::reference_queue_finalizer_references_lock_); 71 72 private: 73 class ProcessReferencesArgs { 74 public: ProcessReferencesArgs(IsHeapReferenceMarkedCallback * is_marked_callback,MarkObjectCallback * mark_callback,void * arg)75 ProcessReferencesArgs(IsHeapReferenceMarkedCallback* is_marked_callback, 76 MarkObjectCallback* mark_callback, void* arg) 77 : is_marked_callback_(is_marked_callback), mark_callback_(mark_callback), arg_(arg) { 78 } 79 80 // The is marked callback is null when the args aren't set up. 81 IsHeapReferenceMarkedCallback* is_marked_callback_; 82 MarkObjectCallback* mark_callback_; 83 void* arg_; 84 85 private: 86 DISALLOW_IMPLICIT_CONSTRUCTORS(ProcessReferencesArgs); 87 }; 88 bool SlowPathEnabled() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 89 // Called by ProcessReferences. 90 void DisableSlowPath(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(Locks::reference_processor_lock_) 91 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 92 // If we are preserving references it means that some dead objects may become live, we use start 93 // and stop preserving to block mutators using GetReferrent from getting access to these 94 // referents. 95 void StartPreservingReferences(Thread* self) LOCKS_EXCLUDED(Locks::reference_processor_lock_); 96 void StopPreservingReferences(Thread* self) LOCKS_EXCLUDED(Locks::reference_processor_lock_); 97 // Process args, used by the GetReferent to return referents which are already marked. 98 ProcessReferencesArgs process_references_args_ GUARDED_BY(Locks::reference_processor_lock_); 99 // Boolean for whether or not we are preserving references (either soft references or finalizers). 100 // If this is true, then we cannot return a referent (see comment in GetReferent). 101 bool preserving_references_ GUARDED_BY(Locks::reference_processor_lock_); 102 // Condition that people wait on if they attempt to get the referent of a reference while 103 // processing is in progress. 104 ConditionVariable condition_ GUARDED_BY(Locks::reference_processor_lock_); 105 // Reference queues used by the GC. 106 ReferenceQueue soft_reference_queue_; 107 ReferenceQueue weak_reference_queue_; 108 ReferenceQueue finalizer_reference_queue_; 109 ReferenceQueue phantom_reference_queue_; 110 ReferenceQueue cleared_references_; 111 112 DISALLOW_COPY_AND_ASSIGN(ReferenceProcessor); 113 }; 114 115 } // namespace gc 116 } // namespace art 117 118 #endif // ART_RUNTIME_GC_REFERENCE_PROCESSOR_H_ 119