• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   void ProcessReferences(bool concurrent,
50                          TimingLogger* timings,
51                          bool clear_soft_references,
52                          gc::collector::GarbageCollector* collector)
53       REQUIRES_SHARED(Locks::mutator_lock_)
54       REQUIRES(Locks::heap_bitmap_lock_)
55       REQUIRES(!Locks::reference_processor_lock_);
56   // The slow path bool is contained in the reference class object, can only be set once
57   // Only allow setting this with mutators suspended so that we can avoid using a lock in the
58   // GetReferent fast path as an optimization.
59   void EnableSlowPath() REQUIRES_SHARED(Locks::mutator_lock_);
60   void BroadcastForSlowPath(Thread* self);
61   // Decode the referent, may block if references are being processed.
62   ObjPtr<mirror::Object> GetReferent(Thread* self, ObjPtr<mirror::Reference> reference)
63       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::reference_processor_lock_);
64   // Collects the cleared references and returns a task, to be executed after FinishGC, that will
65   // enqueue all of them.
66   SelfDeletingTask* CollectClearedReferences(Thread* self) REQUIRES(!Locks::mutator_lock_);
67   void DelayReferenceReferent(ObjPtr<mirror::Class> klass,
68                               ObjPtr<mirror::Reference> ref,
69                               collector::GarbageCollector* collector)
70       REQUIRES_SHARED(Locks::mutator_lock_);
71   void UpdateRoots(IsMarkedVisitor* visitor)
72       REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
73   // Make a circular list with reference if it is not enqueued. Uses the finalizer queue lock.
74   bool MakeCircularListIfUnenqueued(ObjPtr<mirror::FinalizerReference> reference)
75       REQUIRES_SHARED(Locks::mutator_lock_)
76       REQUIRES(!Locks::reference_processor_lock_,
77                !Locks::reference_queue_finalizer_references_lock_);
78   void ClearReferent(ObjPtr<mirror::Reference> ref)
79       REQUIRES_SHARED(Locks::mutator_lock_)
80       REQUIRES(!Locks::reference_processor_lock_);
81 
82  private:
83   bool SlowPathEnabled() REQUIRES_SHARED(Locks::mutator_lock_);
84   // Called by ProcessReferences.
85   void DisableSlowPath(Thread* self) REQUIRES(Locks::reference_processor_lock_)
86       REQUIRES_SHARED(Locks::mutator_lock_);
87   // If we are preserving references it means that some dead objects may become live, we use start
88   // and stop preserving to block mutators using GetReferrent from getting access to these
89   // referents.
90   void StartPreservingReferences(Thread* self) REQUIRES(!Locks::reference_processor_lock_);
91   void StopPreservingReferences(Thread* self) REQUIRES(!Locks::reference_processor_lock_);
92   // Wait until reference processing is done.
93   void WaitUntilDoneProcessingReferences(Thread* self)
94       REQUIRES_SHARED(Locks::mutator_lock_)
95       REQUIRES(Locks::reference_processor_lock_);
96   // Collector which is clearing references, used by the GetReferent to return referents which are
97   // already marked.
98   collector::GarbageCollector* collector_ 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