Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 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" |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 23 | #include "reference_queue.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
Andreas Gampe | 5d08fcc | 2017-06-05 17:56:46 -0700 | [diff] [blame] | 27 | class IsMarkedVisitor; |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 28 | class TimingLogger; |
| 29 | |
| 30 | namespace mirror { |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 31 | class Class; |
Mathieu Chartier | a5a53ef | 2014-09-12 12:58:05 -0700 | [diff] [blame] | 32 | class FinalizerReference; |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 33 | class Object; |
| 34 | class Reference; |
| 35 | } // namespace mirror |
| 36 | |
| 37 | namespace gc { |
| 38 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 39 | namespace collector { |
| 40 | class GarbageCollector; |
| 41 | } // namespace collector |
| 42 | |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 43 | class Heap; |
| 44 | |
Roland Levillain | 5e8d5f0 | 2016-10-18 18:03:43 +0100 | [diff] [blame] | 45 | // Used to process java.lang.ref.Reference instances concurrently or paused. |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 46 | class ReferenceProcessor { |
| 47 | public: |
| 48 | explicit ReferenceProcessor(); |
Mathieu Chartier | 5d3f73a | 2016-10-14 14:28:47 -0700 | [diff] [blame] | 49 | void ProcessReferences(bool concurrent, |
| 50 | TimingLogger* timings, |
| 51 | bool clear_soft_references, |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 52 | gc::collector::GarbageCollector* collector) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 53 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 54 | REQUIRES(Locks::heap_bitmap_lock_) |
| 55 | REQUIRES(!Locks::reference_processor_lock_); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 56 | // The slow path bool is contained in the reference class object, can only be set once |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 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. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 59 | void EnableSlowPath() REQUIRES_SHARED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 60 | void BroadcastForSlowPath(Thread* self); |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 61 | // Decode the referent, may block if references are being processed. |
Mathieu Chartier | 5d3f73a | 2016-10-14 14:28:47 -0700 | [diff] [blame] | 62 | ObjPtr<mirror::Object> GetReferent(Thread* self, ObjPtr<mirror::Reference> reference) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 63 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::reference_processor_lock_); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 64 | void EnqueueClearedReferences(Thread* self) REQUIRES(!Locks::mutator_lock_); |
Mathieu Chartier | 5d3f73a | 2016-10-14 14:28:47 -0700 | [diff] [blame] | 65 | void DelayReferenceReferent(ObjPtr<mirror::Class> klass, |
| 66 | ObjPtr<mirror::Reference> ref, |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 67 | collector::GarbageCollector* collector) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 68 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 69 | void UpdateRoots(IsMarkedVisitor* visitor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 70 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_); |
Mathieu Chartier | a5a53ef | 2014-09-12 12:58:05 -0700 | [diff] [blame] | 71 | // Make a circular list with reference if it is not enqueued. Uses the finalizer queue lock. |
Mathieu Chartier | 5d3f73a | 2016-10-14 14:28:47 -0700 | [diff] [blame] | 72 | bool MakeCircularListIfUnenqueued(ObjPtr<mirror::FinalizerReference> reference) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 73 | REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 74 | REQUIRES(!Locks::reference_processor_lock_, |
| 75 | !Locks::reference_queue_finalizer_references_lock_); |
Mathieu Chartier | c9a7028 | 2016-12-13 14:44:33 -0800 | [diff] [blame] | 76 | void ClearReferent(ObjPtr<mirror::Reference> ref) |
| 77 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 78 | REQUIRES(!Locks::reference_processor_lock_); |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 79 | |
| 80 | private: |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 81 | bool SlowPathEnabled() REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 82 | // Called by ProcessReferences. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 83 | void DisableSlowPath(Thread* self) REQUIRES(Locks::reference_processor_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 84 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 85 | // If we are preserving references it means that some dead objects may become live, we use start |
| 86 | // and stop preserving to block mutators using GetReferrent from getting access to these |
| 87 | // referents. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 88 | void StartPreservingReferences(Thread* self) REQUIRES(!Locks::reference_processor_lock_); |
| 89 | void StopPreservingReferences(Thread* self) REQUIRES(!Locks::reference_processor_lock_); |
Mathieu Chartier | c9a7028 | 2016-12-13 14:44:33 -0800 | [diff] [blame] | 90 | // Wait until reference processing is done. |
| 91 | void WaitUntilDoneProcessingReferences(Thread* self) |
| 92 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 93 | REQUIRES(Locks::reference_processor_lock_); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 94 | // Collector which is clearing references, used by the GetReferent to return referents which are |
| 95 | // already marked. |
| 96 | collector::GarbageCollector* collector_ GUARDED_BY(Locks::reference_processor_lock_); |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 97 | // Boolean for whether or not we are preserving references (either soft references or finalizers). |
| 98 | // If this is true, then we cannot return a referent (see comment in GetReferent). |
Mathieu Chartier | a5a53ef | 2014-09-12 12:58:05 -0700 | [diff] [blame] | 99 | bool preserving_references_ GUARDED_BY(Locks::reference_processor_lock_); |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 100 | // Condition that people wait on if they attempt to get the referent of a reference while |
| 101 | // processing is in progress. |
Mathieu Chartier | a5a53ef | 2014-09-12 12:58:05 -0700 | [diff] [blame] | 102 | ConditionVariable condition_ GUARDED_BY(Locks::reference_processor_lock_); |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 103 | // Reference queues used by the GC. |
| 104 | ReferenceQueue soft_reference_queue_; |
| 105 | ReferenceQueue weak_reference_queue_; |
| 106 | ReferenceQueue finalizer_reference_queue_; |
| 107 | ReferenceQueue phantom_reference_queue_; |
| 108 | ReferenceQueue cleared_references_; |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 109 | |
| 110 | DISALLOW_COPY_AND_ASSIGN(ReferenceProcessor); |
Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | } // namespace gc |
| 114 | } // namespace art |
| 115 | |
| 116 | #endif // ART_RUNTIME_GC_REFERENCE_PROCESSOR_H_ |