blob: 54de5cc572a919c06991a55a8b86775f44065a02 [file] [log] [blame]
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -07001/*
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
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080020#include "base/locks.h"
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070021#include "jni.h"
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070022#include "reference_queue.h"
Andreas Gampe5a0430d2019-01-04 14:33:57 -080023#include "runtime_globals.h"
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070024
25namespace art {
26
Andreas Gampe5d08fcc2017-06-05 17:56:46 -070027class IsMarkedVisitor;
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070028class TimingLogger;
29
30namespace mirror {
Mathieu Chartier97509952015-07-13 14:35:43 -070031class Class;
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070032class FinalizerReference;
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070033class Object;
34class Reference;
35} // namespace mirror
36
37namespace gc {
38
Mathieu Chartier97509952015-07-13 14:35:43 -070039namespace collector {
40class GarbageCollector;
41} // namespace collector
42
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070043class Heap;
44
Roland Levillain5e8d5f02016-10-18 18:03:43 +010045// Used to process java.lang.ref.Reference instances concurrently or paused.
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070046class ReferenceProcessor {
47 public:
Igor Murashkin2ffb7032017-11-08 13:35:21 -080048 ReferenceProcessor();
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070049 void ProcessReferences(bool concurrent,
50 TimingLogger* timings,
51 bool clear_soft_references,
Mathieu Chartier97509952015-07-13 14:35:43 -070052 gc::collector::GarbageCollector* collector)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070053 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070054 REQUIRES(Locks::heap_bitmap_lock_)
55 REQUIRES(!Locks::reference_processor_lock_);
Fred Shih4ee7a662014-07-11 09:59:27 -070056 // The slow path bool is contained in the reference class object, can only be set once
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070057 // 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 Gampebdf7f1c2016-08-30 16:38:47 -070059 void EnableSlowPath() REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -070060 void BroadcastForSlowPath(Thread* self);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070061 // Decode the referent, may block if references are being processed.
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070062 ObjPtr<mirror::Object> GetReferent(Thread* self, ObjPtr<mirror::Reference> reference)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070063 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::reference_processor_lock_);
Alex Lighte3020882019-05-13 16:35:02 -070064 // 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_);
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070067 void DelayReferenceReferent(ObjPtr<mirror::Class> klass,
68 ObjPtr<mirror::Reference> ref,
Mathieu Chartier97509952015-07-13 14:35:43 -070069 collector::GarbageCollector* collector)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070070 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -070071 void UpdateRoots(IsMarkedVisitor* visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070072 REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070073 // Make a circular list with reference if it is not enqueued. Uses the finalizer queue lock.
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070074 bool MakeCircularListIfUnenqueued(ObjPtr<mirror::FinalizerReference> reference)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070075 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070076 REQUIRES(!Locks::reference_processor_lock_,
77 !Locks::reference_queue_finalizer_references_lock_);
Mathieu Chartierc9a70282016-12-13 14:44:33 -080078 void ClearReferent(ObjPtr<mirror::Reference> ref)
79 REQUIRES_SHARED(Locks::mutator_lock_)
80 REQUIRES(!Locks::reference_processor_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070081
82 private:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070083 bool SlowPathEnabled() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070084 // Called by ProcessReferences.
Mathieu Chartier90443472015-07-16 20:32:27 -070085 void DisableSlowPath(Thread* self) REQUIRES(Locks::reference_processor_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070086 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070087 // 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.
Mathieu Chartier90443472015-07-16 20:32:27 -070090 void StartPreservingReferences(Thread* self) REQUIRES(!Locks::reference_processor_lock_);
91 void StopPreservingReferences(Thread* self) REQUIRES(!Locks::reference_processor_lock_);
Mathieu Chartierc9a70282016-12-13 14:44:33 -080092 // Wait until reference processing is done.
93 void WaitUntilDoneProcessingReferences(Thread* self)
94 REQUIRES_SHARED(Locks::mutator_lock_)
95 REQUIRES(Locks::reference_processor_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -070096 // 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_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070099 // 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).
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -0700101 bool preserving_references_ GUARDED_BY(Locks::reference_processor_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700102 // Condition that people wait on if they attempt to get the referent of a reference while
103 // processing is in progress.
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -0700104 ConditionVariable condition_ GUARDED_BY(Locks::reference_processor_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700105 // 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_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700111
112 DISALLOW_COPY_AND_ASSIGN(ReferenceProcessor);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700113};
114
115} // namespace gc
116} // namespace art
117
118#endif // ART_RUNTIME_GC_REFERENCE_PROCESSOR_H_