blob: 284d13cd4be434e12a3a9cddca5f34448462a66e [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 Chartiera5a53ef2014-09-12 12:58:05 -070031class FinalizerReference;
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070032class Object;
33class Reference;
34} // namespace mirror
35
36namespace gc {
37
38class Heap;
39
40// Used to process java.lang.References concurrently or paused.
41class ReferenceProcessor {
42 public:
43 explicit ReferenceProcessor();
Mathieu Chartier308351a2014-06-15 12:39:02 -070044 static bool PreserveSoftReferenceCallback(mirror::HeapReference<mirror::Object>* obj, void* arg)
45 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070046 void ProcessReferences(bool concurrent, TimingLogger* timings, bool clear_soft_references,
Mathieu Chartier308351a2014-06-15 12:39:02 -070047 IsHeapReferenceMarkedCallback* is_marked_callback,
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070048 MarkObjectCallback* mark_object_callback,
49 ProcessMarkStackCallback* process_mark_stack_callback, void* arg)
50 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
51 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070052 LOCKS_EXCLUDED(Locks::reference_processor_lock_);
Fred Shih4ee7a662014-07-11 09:59:27 -070053 // The slow path bool is contained in the reference class object, can only be set once
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070054 // Only allow setting this with mutators suspended so that we can avoid using a lock in the
55 // GetReferent fast path as an optimization.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080056 void EnableSlowPath() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -070057 void BroadcastForSlowPath(Thread* self);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070058 // Decode the referent, may block if references are being processed.
59 mirror::Object* GetReferent(Thread* self, mirror::Reference* reference)
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070060 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(Locks::reference_processor_lock_);
Mathieu Chartier308351a2014-06-15 12:39:02 -070061 void EnqueueClearedReferences(Thread* self) LOCKS_EXCLUDED(Locks::mutator_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070062 void DelayReferenceReferent(mirror::Class* klass, mirror::Reference* ref,
Mathieu Chartier308351a2014-06-15 12:39:02 -070063 IsHeapReferenceMarkedCallback* is_marked_callback, void* arg)
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070064 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier52e4b432014-06-10 11:22:31 -070065 void UpdateRoots(IsMarkedCallback* callback, void* arg)
Fred Shih4ee7a662014-07-11 09:59:27 -070066 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_);
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070067 // Make a circular list with reference if it is not enqueued. Uses the finalizer queue lock.
68 bool MakeCircularListIfUnenqueued(mirror::FinalizerReference* reference)
69 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
70 LOCKS_EXCLUDED(Locks::reference_processor_lock_,
71 Locks::reference_queue_finalizer_references_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070072
73 private:
74 class ProcessReferencesArgs {
75 public:
Mathieu Chartier308351a2014-06-15 12:39:02 -070076 ProcessReferencesArgs(IsHeapReferenceMarkedCallback* is_marked_callback,
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070077 MarkObjectCallback* mark_callback, void* arg)
78 : is_marked_callback_(is_marked_callback), mark_callback_(mark_callback), arg_(arg) {
79 }
80
81 // The is marked callback is null when the args aren't set up.
Mathieu Chartier308351a2014-06-15 12:39:02 -070082 IsHeapReferenceMarkedCallback* is_marked_callback_;
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070083 MarkObjectCallback* mark_callback_;
84 void* arg_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -070085
86 private:
87 DISALLOW_IMPLICIT_CONSTRUCTORS(ProcessReferencesArgs);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070088 };
Fred Shih4ee7a662014-07-11 09:59:27 -070089 bool SlowPathEnabled() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070090 // Called by ProcessReferences.
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070091 void DisableSlowPath(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(Locks::reference_processor_lock_)
Fred Shih4ee7a662014-07-11 09:59:27 -070092 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070093 // If we are preserving references it means that some dead objects may become live, we use start
94 // and stop preserving to block mutators using GetReferrent from getting access to these
95 // referents.
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070096 void StartPreservingReferences(Thread* self) LOCKS_EXCLUDED(Locks::reference_processor_lock_);
97 void StopPreservingReferences(Thread* self) LOCKS_EXCLUDED(Locks::reference_processor_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -070098 // Process args, used by the GetReferent to return referents which are already marked.
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070099 ProcessReferencesArgs process_references_args_ GUARDED_BY(Locks::reference_processor_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700100 // Boolean for whether or not we are preserving references (either soft references or finalizers).
101 // If this is true, then we cannot return a referent (see comment in GetReferent).
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -0700102 bool preserving_references_ GUARDED_BY(Locks::reference_processor_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700103 // Condition that people wait on if they attempt to get the referent of a reference while
104 // processing is in progress.
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -0700105 ConditionVariable condition_ GUARDED_BY(Locks::reference_processor_lock_);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700106 // Reference queues used by the GC.
107 ReferenceQueue soft_reference_queue_;
108 ReferenceQueue weak_reference_queue_;
109 ReferenceQueue finalizer_reference_queue_;
110 ReferenceQueue phantom_reference_queue_;
111 ReferenceQueue cleared_references_;
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700112
113 DISALLOW_COPY_AND_ASSIGN(ReferenceProcessor);
Mathieu Chartier78f7b4c2014-05-06 10:57:27 -0700114};
115
116} // namespace gc
117} // namespace art
118
119#endif // ART_RUNTIME_GC_REFERENCE_PROCESSOR_H_