blob: e12a95f332a7e6a3594d6ec4b406c23382135aa2 [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"
29#include "locks.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 {
35namespace gc {
36
37class Heap;
38
39// Used to temporarily store java.lang.ref.Reference(s) during GC and prior to queueing on the
40// appropriate java.lang.ref.ReferenceQueue. The linked list is maintained in the
41// java.lang.ref.Reference objects.
42class ReferenceQueue {
43 public:
44 explicit ReferenceQueue(Heap* heap);
45 // Enqueue a reference if is not already enqueued. Thread safe to call from multiple threads
46 // since it uses a lock to avoid a race between checking for the references presence and adding
47 // it.
48 void AtomicEnqueueIfNotEnqueued(Thread* self, mirror::Object* ref)
49 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
50 // Enqueue a reference, unlike EnqueuePendingReference, enqueue reference checks that the
51 // reference IsEnqueueable. Not thread safe, used when mutators are paused to minimize lock
52 // overhead.
53 void EnqueueReference(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
54 void EnqueuePendingReference(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
55 mirror::Object* DequeuePendingReference() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
56 // Enqueues finalizer references with white referents. White referents are blackened, moved to the
57 // zombie field, and the referent field is cleared.
58 void EnqueueFinalizerReferences(ReferenceQueue& cleared_references,
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080059 IsMarkedCallback is_marked_callback,
60 MarkObjectCallback recursive_mark_callback, void* arg)
Mathieu Chartier39e32612013-11-12 16:28:05 -080061 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
62 // Walks the reference list marking any references subject to the reference clearing policy.
63 // References with a black referent are removed from the list. References with white referents
64 // biased toward saving are blackened and also removed from the list.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080065 void PreserveSomeSoftReferences(IsMarkedCallback* preserve_callback, void* arg)
Mathieu Chartier39e32612013-11-12 16:28:05 -080066 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
67 // Unlink the reference list clearing references objects with white referents. Cleared references
68 // registered to a reference queue are scheduled for appending by the heap worker thread.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080069 void ClearWhiteReferences(ReferenceQueue& cleared_references, IsMarkedCallback is_marked_callback,
70 void* arg)
Mathieu Chartier39e32612013-11-12 16:28:05 -080071 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
72 void Dump(std::ostream& os) const
73 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
74 bool IsEmpty() const {
75 return list_ == nullptr;
76 }
77 void Clear() {
78 list_ = nullptr;
79 }
80 mirror::Object* GetList() {
81 return list_;
82 }
83
84 private:
85 // Lock, used for parallel GC reference enqueuing. It allows for multiple threads simultaneously
86 // calling AtomicEnqueueIfNotEnqueued.
Ian Rogersef7d42f2014-01-06 12:55:46 -080087 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartier39e32612013-11-12 16:28:05 -080088 // The heap contains the reference offsets.
89 Heap* const heap_;
90 // The actual reference list. Not a root since it will be nullptr when the GC is not running.
91 mirror::Object* list_;
92};
93
94} // namespace gc
95} // namespace art
96
97#endif // ART_RUNTIME_GC_REFERENCE_QUEUE_H_