blob: 06243c40a2e66c2aa4e5cde5b97a8d9e8c200a6b [file] [log] [blame]
Mathieu Chartier39e32612013-11-12 16:28:05 -08001/*
2 * Copyright (C) 2013 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_QUEUE_H_
18#define ART_RUNTIME_GC_REFERENCE_QUEUE_H_
19
20#include <iosfwd>
21#include <string>
22#include <vector>
23
David Sehrc431b9d2018-03-02 12:01:51 -080024#include "base/atomic.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080025#include "base/locks.h"
Mathieu Chartier39e32612013-11-12 16:28:05 -080026#include "base/timing_logger.h"
Mathieu Chartier39e32612013-11-12 16:28:05 -080027#include "jni.h"
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070028#include "obj_ptr.h"
Mathieu Chartier39e32612013-11-12 16:28:05 -080029#include "offsets.h"
Andreas Gampe5a0430d2019-01-04 14:33:57 -080030#include "runtime_globals.h"
Mathieu Chartier39e32612013-11-12 16:28:05 -080031#include "thread_pool.h"
32
33namespace art {
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080034
35class Mutex;
36
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070037namespace mirror {
38class Reference;
39} // namespace mirror
40
Andreas Gampe5d08fcc2017-06-05 17:56:46 -070041class IsMarkedVisitor;
42class MarkObjectVisitor;
43
Mathieu Chartier39e32612013-11-12 16:28:05 -080044namespace gc {
45
Mathieu Chartier97509952015-07-13 14:35:43 -070046namespace collector {
47class GarbageCollector;
48} // namespace collector
49
Mathieu Chartier39e32612013-11-12 16:28:05 -080050class Heap;
51
Hans Boehmd7b41612021-06-17 18:31:14 -070052struct FinalizerStats {
53 FinalizerStats(size_t num_refs, size_t num_enqueued)
54 : num_refs_(num_refs), num_enqueued_(num_enqueued) {}
55 const uint32_t num_refs_;
56 const uint32_t num_enqueued_;
57};
58
Mathieu Chartier39e32612013-11-12 16:28:05 -080059// Used to temporarily store java.lang.ref.Reference(s) during GC and prior to queueing on the
Richard Uhlerc4695df2016-01-15 14:08:05 -080060// appropriate java.lang.ref.ReferenceQueue. The linked list is maintained as an unordered,
61// circular, and singly-linked list using the pendingNext fields of the java.lang.ref.Reference
62// objects.
Mathieu Chartier39e32612013-11-12 16:28:05 -080063class ReferenceQueue {
64 public:
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070065 explicit ReferenceQueue(Mutex* lock);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070066
Richard Uhlerc4695df2016-01-15 14:08:05 -080067 // Enqueue a reference if it is unprocessed. Thread safe to call from multiple
68 // threads since it uses a lock to avoid a race between checking for the references presence and
69 // adding it.
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070070 void AtomicEnqueueIfNotEnqueued(Thread* self, ObjPtr<mirror::Reference> ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070071 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070072
Richard Uhlerc4695df2016-01-15 14:08:05 -080073 // Enqueue a reference. The reference must be unprocessed.
74 // Not thread safe, used when mutators are paused to minimize lock overhead.
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070075 void EnqueueReference(ObjPtr<mirror::Reference> ref) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070076
Richard Uhlerc4695df2016-01-15 14:08:05 -080077 // Dequeue a reference from the queue and return that dequeued reference.
Hiroshi Yamauchi057d9772017-02-17 15:33:23 -080078 // Call DisableReadBarrierForReference for the reference that's returned from this function.
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070079 ObjPtr<mirror::Reference> DequeuePendingReference() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070080
Hiroshi Yamauchi057d9772017-02-17 15:33:23 -080081 // If applicable, disable the read barrier for the reference after its referent is handled (see
82 // ConcurrentCopying::ProcessMarkStackRef.) This must be called for a reference that's dequeued
83 // from pending queue (DequeuePendingReference).
84 void DisableReadBarrierForReference(ObjPtr<mirror::Reference> ref)
85 REQUIRES_SHARED(Locks::mutator_lock_);
86
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080087 // Enqueues finalizer references with white referents. White referents are blackened, moved to
88 // the zombie field, and the referent field is cleared.
Hans Boehmd7b41612021-06-17 18:31:14 -070089 FinalizerStats EnqueueFinalizerReferences(ReferenceQueue* cleared_references,
Mathieu Chartier97509952015-07-13 14:35:43 -070090 collector::GarbageCollector* collector)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070091 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070092
Mathieu Chartier39e32612013-11-12 16:28:05 -080093 // Walks the reference list marking any references subject to the reference clearing policy.
94 // References with a black referent are removed from the list. References with white referents
95 // biased toward saving are blackened and also removed from the list.
Hans Boehmd7b41612021-06-17 18:31:14 -070096 // Returns the number of non-null soft references.
97 uint32_t ForwardSoftReferences(MarkObjectVisitor* visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070098 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070099
Mathieu Chartier9e2094f2014-12-11 18:43:48 -0800100 // Unlink the reference list clearing references objects with white referents. Cleared references
Mathieu Chartier39e32612013-11-12 16:28:05 -0800101 // registered to a reference queue are scheduled for appending by the heap worker thread.
Mathieu Chartier308351a2014-06-15 12:39:02 -0700102 void ClearWhiteReferences(ReferenceQueue* cleared_references,
Mathieu Chartier97509952015-07-13 14:35:43 -0700103 collector::GarbageCollector* collector)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700104 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700105
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700106 void Dump(std::ostream& os) const REQUIRES_SHARED(Locks::mutator_lock_);
107 size_t GetLength() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700108
Mathieu Chartier39e32612013-11-12 16:28:05 -0800109 bool IsEmpty() const {
110 return list_ == nullptr;
111 }
112 void Clear() {
113 list_ = nullptr;
114 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700115 mirror::Reference* GetList() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier39e32612013-11-12 16:28:05 -0800116 return list_;
117 }
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700118
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700119 // Visits list_, currently only used for the mark compact GC.
Mathieu Chartier97509952015-07-13 14:35:43 -0700120 void UpdateRoots(IsMarkedVisitor* visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700121 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800122
123 private:
124 // Lock, used for parallel GC reference enqueuing. It allows for multiple threads simultaneously
125 // calling AtomicEnqueueIfNotEnqueued.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700126 Mutex* const lock_;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700127 // The actual reference list. Only a root for the mark compact GC since it will be null for other
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -0700128 // GC types. Not an ObjPtr since it is accessed from multiple threads.
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700129 mirror::Reference* list_;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700130
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700131 DISALLOW_IMPLICIT_CONSTRUCTORS(ReferenceQueue);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800132};
133
134} // namespace gc
135} // namespace art
136
137#endif // ART_RUNTIME_GC_REFERENCE_QUEUE_H_