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 Class;
32class FinalizerReference;
33class Object;
34class Reference;
35}  // namespace mirror
36
37namespace gc {
38
39namespace collector {
40class GarbageCollector;
41}  // namespace collector
42
43class Heap;
44
45// Used to process java.lang.References concurrently or paused.
46class ReferenceProcessor {
47 public:
48  explicit ReferenceProcessor();
49  void ProcessReferences(bool concurrent, TimingLogger* timings, bool clear_soft_references,
50                         gc::collector::GarbageCollector* collector)
51      SHARED_REQUIRES(Locks::mutator_lock_)
52      REQUIRES(Locks::heap_bitmap_lock_)
53      REQUIRES(!Locks::reference_processor_lock_);
54  // The slow path bool is contained in the reference class object, can only be set once
55  // Only allow setting this with mutators suspended so that we can avoid using a lock in the
56  // GetReferent fast path as an optimization.
57  void EnableSlowPath() SHARED_REQUIRES(Locks::mutator_lock_);
58  void BroadcastForSlowPath(Thread* self);
59  // Decode the referent, may block if references are being processed.
60  mirror::Object* GetReferent(Thread* self, mirror::Reference* reference)
61      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!Locks::reference_processor_lock_);
62  void EnqueueClearedReferences(Thread* self) REQUIRES(!Locks::mutator_lock_);
63  void DelayReferenceReferent(mirror::Class* klass, mirror::Reference* ref,
64                              collector::GarbageCollector* collector)
65      SHARED_REQUIRES(Locks::mutator_lock_);
66  void UpdateRoots(IsMarkedVisitor* visitor)
67      SHARED_REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
68  // Make a circular list with reference if it is not enqueued. Uses the finalizer queue lock.
69  bool MakeCircularListIfUnenqueued(mirror::FinalizerReference* reference)
70      SHARED_REQUIRES(Locks::mutator_lock_)
71      REQUIRES(!Locks::reference_processor_lock_,
72               !Locks::reference_queue_finalizer_references_lock_);
73
74 private:
75  bool SlowPathEnabled() SHARED_REQUIRES(Locks::mutator_lock_);
76  // Called by ProcessReferences.
77  void DisableSlowPath(Thread* self) REQUIRES(Locks::reference_processor_lock_)
78      SHARED_REQUIRES(Locks::mutator_lock_);
79  // 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.
82  void StartPreservingReferences(Thread* self) REQUIRES(!Locks::reference_processor_lock_);
83  void StopPreservingReferences(Thread* self) REQUIRES(!Locks::reference_processor_lock_);
84  // 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_);
87  // 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).
89  bool preserving_references_ GUARDED_BY(Locks::reference_processor_lock_);
90  // Condition that people wait on if they attempt to get the referent of a reference while
91  // processing is in progress.
92  ConditionVariable condition_ GUARDED_BY(Locks::reference_processor_lock_);
93  // 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_;
99
100  DISALLOW_COPY_AND_ASSIGN(ReferenceProcessor);
101};
102
103}  // namespace gc
104}  // namespace art
105
106#endif  // ART_RUNTIME_GC_REFERENCE_PROCESSOR_H_
107