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