blob: d9dfedb4647af7a6a6c1dea7d51f5468bb9a1d95 [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
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 {
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
45// Used to process java.lang.References concurrently or paused.
46class ReferenceProcessor {
47 public:
48 explicit ReferenceProcessor();
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070049 void ProcessReferences(bool concurrent, TimingLogger* timings, bool clear_soft_references,
Mathieu Chartier97509952015-07-13 14:35:43 -070050 gc::collector::GarbageCollector* collector)
Mathieu Chartier90443472015-07-16 20:32:27 -070051 SHARED_REQUIRES(Locks::mutator_lock_)
52 REQUIRES(Locks::heap_bitmap_lock_)
53 REQUIRES(!Locks::reference_processor_lock_);
Fred Shih4ee7a662014-07-11 09:59:27 -070054 // The slow path bool is contained in the reference class object, can only be set once
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070055 // Only allow setting this with mutators suspended so that we can avoid using a lock in the
56 // GetReferent fast path as an optimization.
Mathieu Chartier90443472015-07-16 20:32:27 -070057 void EnableSlowPath() SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -070058 void BroadcastForSlowPath(Thread* self);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070059 // Decode the referent, may block if references are being processed.
60 mirror::Object* GetReferent(Thread* self, mirror::Reference* reference)
Mathieu Chartier90443472015-07-16 20:32:27 -070061 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Locks::reference_processor_lock_);
62 void EnqueueClearedReferences(Thread* self) REQUIRES(!Locks::mutator_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070063 void DelayReferenceReferent(mirror::Class* klass, mirror::Reference* ref,
Mathieu Chartier97509952015-07-13 14:35:43 -070064 collector::GarbageCollector* collector)
Mathieu Chartier90443472015-07-16 20:32:27 -070065 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -070066 void UpdateRoots(IsMarkedVisitor* visitor)
Mathieu Chartier90443472015-07-16 20:32:27 -070067 SHARED_REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070068 // Make a circular list with reference if it is not enqueued. Uses the finalizer queue lock.
69 bool MakeCircularListIfUnenqueued(mirror::FinalizerReference* reference)
Mathieu Chartier90443472015-07-16 20:32:27 -070070 SHARED_REQUIRES(Locks::mutator_lock_)
71 REQUIRES(!Locks::reference_processor_lock_,
72 !Locks::reference_queue_finalizer_references_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070073
74 private:
Mathieu Chartier90443472015-07-16 20:32:27 -070075 bool SlowPathEnabled() SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070076 // Called by ProcessReferences.
Mathieu Chartier90443472015-07-16 20:32:27 -070077 void DisableSlowPath(Thread* self) REQUIRES(Locks::reference_processor_lock_)
78 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070079 // If we are preserving references it means that some dead objects may become live, we use start
80 // and stop preserving to block mutators using GetReferrent from getting access to these
81 // referents.
Mathieu Chartier90443472015-07-16 20:32:27 -070082 void StartPreservingReferences(Thread* self) REQUIRES(!Locks::reference_processor_lock_);
83 void StopPreservingReferences(Thread* self) REQUIRES(!Locks::reference_processor_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -070084 // Collector which is clearing references, used by the GetReferent to return referents which are
85 // already marked.
86 collector::GarbageCollector* collector_ GUARDED_BY(Locks::reference_processor_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070087 // Boolean for whether or not we are preserving references (either soft references or finalizers).
88 // If this is true, then we cannot return a referent (see comment in GetReferent).
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070089 bool preserving_references_ GUARDED_BY(Locks::reference_processor_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070090 // Condition that people wait on if they attempt to get the referent of a reference while
91 // processing is in progress.
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070092 ConditionVariable condition_ GUARDED_BY(Locks::reference_processor_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070093 // Reference queues used by the GC.
94 ReferenceQueue soft_reference_queue_;
95 ReferenceQueue weak_reference_queue_;
96 ReferenceQueue finalizer_reference_queue_;
97 ReferenceQueue phantom_reference_queue_;
98 ReferenceQueue cleared_references_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -070099
100 DISALLOW_COPY_AND_ASSIGN(ReferenceProcessor);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700101};
102
103} // namespace gc
104} // namespace art
105
106#endif // ART_RUNTIME_GC_REFERENCE_PROCESSOR_H_