blob: aabac9774200e5a1c331d50f2748882ddc0dbbf9 [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
Ian Rogersef7d42f2014-01-06 12:55:46 -080024#include "atomic.h"
Mathieu Chartier90443472015-07-16 20:32:27 -070025#include "base/mutex.h"
Mathieu Chartier39e32612013-11-12 16:28:05 -080026#include "base/timing_logger.h"
27#include "globals.h"
Mathieu Chartier39e32612013-11-12 16:28:05 -080028#include "jni.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080029#include "object_callbacks.h"
Mathieu Chartier39e32612013-11-12 16:28:05 -080030#include "offsets.h"
Mathieu Chartier39e32612013-11-12 16:28:05 -080031#include "thread_pool.h"
32
33namespace art {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070034namespace mirror {
35class Reference;
36} // namespace mirror
37
Mathieu Chartier39e32612013-11-12 16:28:05 -080038namespace gc {
39
Mathieu Chartier97509952015-07-13 14:35:43 -070040namespace collector {
41class GarbageCollector;
42} // namespace collector
43
Mathieu Chartier39e32612013-11-12 16:28:05 -080044class Heap;
45
46// Used to temporarily store java.lang.ref.Reference(s) during GC and prior to queueing on the
47// appropriate java.lang.ref.ReferenceQueue. The linked list is maintained in the
48// java.lang.ref.Reference objects.
49class ReferenceQueue {
50 public:
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070051 explicit ReferenceQueue(Mutex* lock);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070052
Mathieu Chartier39e32612013-11-12 16:28:05 -080053 // Enqueue a reference if is not already enqueued. Thread safe to call from multiple threads
54 // since it uses a lock to avoid a race between checking for the references presence and adding
55 // it.
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070056 void AtomicEnqueueIfNotEnqueued(Thread* self, mirror::Reference* ref)
Mathieu Chartier90443472015-07-16 20:32:27 -070057 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!*lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070058
Mathieu Chartier39e32612013-11-12 16:28:05 -080059 // Enqueue a reference, unlike EnqueuePendingReference, enqueue reference checks that the
60 // reference IsEnqueueable. Not thread safe, used when mutators are paused to minimize lock
61 // overhead.
Mathieu Chartier90443472015-07-16 20:32:27 -070062 void EnqueueReference(mirror::Reference* ref) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070063
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080064 // Enqueue a reference without checking that it is enqueable.
Mathieu Chartier90443472015-07-16 20:32:27 -070065 void EnqueuePendingReference(mirror::Reference* ref) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070066
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080067 // Dequeue the first reference (returns list_).
Mathieu Chartier90443472015-07-16 20:32:27 -070068 mirror::Reference* DequeuePendingReference() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070069
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080070 // Enqueues finalizer references with white referents. White referents are blackened, moved to
71 // the zombie field, and the referent field is cleared.
Mathieu Chartier308351a2014-06-15 12:39:02 -070072 void EnqueueFinalizerReferences(ReferenceQueue* cleared_references,
Mathieu Chartier97509952015-07-13 14:35:43 -070073 collector::GarbageCollector* collector)
Mathieu Chartier90443472015-07-16 20:32:27 -070074 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070075
Mathieu Chartier39e32612013-11-12 16:28:05 -080076 // Walks the reference list marking any references subject to the reference clearing policy.
77 // References with a black referent are removed from the list. References with white referents
78 // biased toward saving are blackened and also removed from the list.
Mathieu Chartier97509952015-07-13 14:35:43 -070079 void ForwardSoftReferences(MarkObjectVisitor* visitor)
Mathieu Chartier90443472015-07-16 20:32:27 -070080 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070081
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080082 // Unlink the reference list clearing references objects with white referents. Cleared references
Mathieu Chartier39e32612013-11-12 16:28:05 -080083 // registered to a reference queue are scheduled for appending by the heap worker thread.
Mathieu Chartier308351a2014-06-15 12:39:02 -070084 void ClearWhiteReferences(ReferenceQueue* cleared_references,
Mathieu Chartier97509952015-07-13 14:35:43 -070085 collector::GarbageCollector* collector)
Mathieu Chartier90443472015-07-16 20:32:27 -070086 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070087
Mathieu Chartier90443472015-07-16 20:32:27 -070088 void Dump(std::ostream& os) const SHARED_REQUIRES(Locks::mutator_lock_);
89 size_t GetLength() const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070090
Mathieu Chartier39e32612013-11-12 16:28:05 -080091 bool IsEmpty() const {
92 return list_ == nullptr;
93 }
94 void Clear() {
95 list_ = nullptr;
96 }
Mathieu Chartier90443472015-07-16 20:32:27 -070097 mirror::Reference* GetList() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier39e32612013-11-12 16:28:05 -080098 return list_;
99 }
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700100
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700101 // Visits list_, currently only used for the mark compact GC.
Mathieu Chartier97509952015-07-13 14:35:43 -0700102 void UpdateRoots(IsMarkedVisitor* visitor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700103 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800104
105 private:
106 // Lock, used for parallel GC reference enqueuing. It allows for multiple threads simultaneously
107 // calling AtomicEnqueueIfNotEnqueued.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700108 Mutex* const lock_;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700109 // The actual reference list. Only a root for the mark compact GC since it will be null for other
110 // GC types.
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700111 mirror::Reference* list_;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700112
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700113 DISALLOW_IMPLICIT_CONSTRUCTORS(ReferenceQueue);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800114};
115
116} // namespace gc
117} // namespace art
118
119#endif // ART_RUNTIME_GC_REFERENCE_QUEUE_H_