blob: b5ec1e53411266dcd9756500d85f0b8d8db11d94 [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 Chartier5d3f73a2016-10-14 14:28:47 -070029#include "obj_ptr.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080030#include "object_callbacks.h"
Mathieu Chartier39e32612013-11-12 16:28:05 -080031#include "offsets.h"
Mathieu Chartier39e32612013-11-12 16:28:05 -080032#include "thread_pool.h"
33
34namespace art {
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070035namespace mirror {
36class Reference;
37} // namespace mirror
38
Mathieu Chartier39e32612013-11-12 16:28:05 -080039namespace gc {
40
Mathieu Chartier97509952015-07-13 14:35:43 -070041namespace collector {
42class GarbageCollector;
43} // namespace collector
44
Mathieu Chartier39e32612013-11-12 16:28:05 -080045class Heap;
46
47// Used to temporarily store java.lang.ref.Reference(s) during GC and prior to queueing on the
Richard Uhlerc4695df2016-01-15 14:08:05 -080048// appropriate java.lang.ref.ReferenceQueue. The linked list is maintained as an unordered,
49// circular, and singly-linked list using the pendingNext fields of the java.lang.ref.Reference
50// objects.
Mathieu Chartier39e32612013-11-12 16:28:05 -080051class ReferenceQueue {
52 public:
Mathieu Chartiera5a53ef2014-09-12 12:58:05 -070053 explicit ReferenceQueue(Mutex* lock);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070054
Richard Uhlerc4695df2016-01-15 14:08:05 -080055 // Enqueue a reference if it is unprocessed. Thread safe to call from multiple
56 // threads since it uses a lock to avoid a race between checking for the references presence and
57 // adding it.
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070058 void AtomicEnqueueIfNotEnqueued(Thread* self, ObjPtr<mirror::Reference> ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070059 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!*lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070060
Richard Uhlerc4695df2016-01-15 14:08:05 -080061 // Enqueue a reference. The reference must be unprocessed.
62 // Not thread safe, used when mutators are paused to minimize lock overhead.
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070063 void EnqueueReference(ObjPtr<mirror::Reference> ref) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070064
Richard Uhlerc4695df2016-01-15 14:08:05 -080065 // Dequeue a reference from the queue and return that dequeued reference.
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070066 ObjPtr<mirror::Reference> DequeuePendingReference() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070067
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080068 // Enqueues finalizer references with white referents. White referents are blackened, moved to
69 // the zombie field, and the referent field is cleared.
Mathieu Chartier308351a2014-06-15 12:39:02 -070070 void EnqueueFinalizerReferences(ReferenceQueue* cleared_references,
Mathieu Chartier97509952015-07-13 14:35:43 -070071 collector::GarbageCollector* collector)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070072 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070073
Mathieu Chartier39e32612013-11-12 16:28:05 -080074 // Walks the reference list marking any references subject to the reference clearing policy.
75 // References with a black referent are removed from the list. References with white referents
76 // biased toward saving are blackened and also removed from the list.
Mathieu Chartier97509952015-07-13 14:35:43 -070077 void ForwardSoftReferences(MarkObjectVisitor* visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070078 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070079
Mathieu Chartier9e2094f2014-12-11 18:43:48 -080080 // Unlink the reference list clearing references objects with white referents. Cleared references
Mathieu Chartier39e32612013-11-12 16:28:05 -080081 // registered to a reference queue are scheduled for appending by the heap worker thread.
Mathieu Chartier308351a2014-06-15 12:39:02 -070082 void ClearWhiteReferences(ReferenceQueue* cleared_references,
Mathieu Chartier97509952015-07-13 14:35:43 -070083 collector::GarbageCollector* collector)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070084 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070085
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070086 void Dump(std::ostream& os) const REQUIRES_SHARED(Locks::mutator_lock_);
87 size_t GetLength() const REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers6f3dbba2014-10-14 17:41:57 -070088
Mathieu Chartier39e32612013-11-12 16:28:05 -080089 bool IsEmpty() const {
90 return list_ == nullptr;
91 }
92 void Clear() {
93 list_ = nullptr;
94 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070095 mirror::Reference* GetList() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier39e32612013-11-12 16:28:05 -080096 return list_;
97 }
Ian Rogers6f3dbba2014-10-14 17:41:57 -070098
Mathieu Chartier52e4b432014-06-10 11:22:31 -070099 // Visits list_, currently only used for the mark compact GC.
Mathieu Chartier97509952015-07-13 14:35:43 -0700100 void UpdateRoots(IsMarkedVisitor* visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700101 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800102
103 private:
104 // Lock, used for parallel GC reference enqueuing. It allows for multiple threads simultaneously
105 // calling AtomicEnqueueIfNotEnqueued.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700106 Mutex* const lock_;
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700107 // 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 -0700108 // GC types. Not an ObjPtr since it is accessed from multiple threads.
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700109 mirror::Reference* list_;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700110
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700111 DISALLOW_IMPLICIT_CONSTRUCTORS(ReferenceQueue);
Mathieu Chartier39e32612013-11-12 16:28:05 -0800112};
113
114} // namespace gc
115} // namespace art
116
117#endif // ART_RUNTIME_GC_REFERENCE_QUEUE_H_