blob: 8d392babf66495b004e8c27bd4f96eef43ecf394 [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 Chartier39e32612013-11-12 16:28:05 -080025#include "base/timing_logger.h"
26#include "globals.h"
27#include "gtest/gtest.h"
28#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
40class Heap;
41
42// Used to temporarily store java.lang.ref.Reference(s) during GC and prior to queueing on the
43// appropriate java.lang.ref.ReferenceQueue. The linked list is maintained in the
44// java.lang.ref.Reference objects.
45class ReferenceQueue {
46 public:
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070047 explicit ReferenceQueue();
Mathieu Chartier39e32612013-11-12 16:28:05 -080048 // Enqueue a reference if is not already enqueued. Thread safe to call from multiple threads
49 // since it uses a lock to avoid a race between checking for the references presence and adding
50 // it.
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070051 void AtomicEnqueueIfNotEnqueued(Thread* self, mirror::Reference* ref)
Mathieu Chartier39e32612013-11-12 16:28:05 -080052 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
53 // Enqueue a reference, unlike EnqueuePendingReference, enqueue reference checks that the
54 // reference IsEnqueueable. Not thread safe, used when mutators are paused to minimize lock
55 // overhead.
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070056 void EnqueueReference(mirror::Reference* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
57 void EnqueuePendingReference(mirror::Reference* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
58 mirror::Reference* DequeuePendingReference() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier39e32612013-11-12 16:28:05 -080059 // Enqueues finalizer references with white referents. White referents are blackened, moved to the
60 // zombie field, and the referent field is cleared.
61 void EnqueueFinalizerReferences(ReferenceQueue& cleared_references,
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080062 IsMarkedCallback is_marked_callback,
63 MarkObjectCallback recursive_mark_callback, void* arg)
Mathieu Chartier39e32612013-11-12 16:28:05 -080064 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
65 // Walks the reference list marking any references subject to the reference clearing policy.
66 // References with a black referent are removed from the list. References with white referents
67 // biased toward saving are blackened and also removed from the list.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080068 void PreserveSomeSoftReferences(IsMarkedCallback* preserve_callback, void* arg)
Mathieu Chartier39e32612013-11-12 16:28:05 -080069 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
70 // Unlink the reference list clearing references objects with white referents. Cleared references
71 // registered to a reference queue are scheduled for appending by the heap worker thread.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080072 void ClearWhiteReferences(ReferenceQueue& cleared_references, IsMarkedCallback is_marked_callback,
73 void* arg)
Mathieu Chartier39e32612013-11-12 16:28:05 -080074 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
75 void Dump(std::ostream& os) const
76 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
77 bool IsEmpty() const {
78 return list_ == nullptr;
79 }
80 void Clear() {
81 list_ = nullptr;
82 }
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070083 mirror::Reference* GetList() {
Mathieu Chartier39e32612013-11-12 16:28:05 -080084 return list_;
85 }
86
87 private:
88 // Lock, used for parallel GC reference enqueuing. It allows for multiple threads simultaneously
89 // calling AtomicEnqueueIfNotEnqueued.
Ian Rogersef7d42f2014-01-06 12:55:46 -080090 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartier39e32612013-11-12 16:28:05 -080091 // The actual reference list. Not a root since it will be nullptr when the GC is not running.
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070092 mirror::Reference* list_;
Mathieu Chartier39e32612013-11-12 16:28:05 -080093};
94
95} // namespace gc
96} // namespace art
97
98#endif // ART_RUNTIME_GC_REFERENCE_QUEUE_H_