blob: 3f3069edc2cae9790f6a8c8f78f1ddfc01df1509 [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"
30#include "offsets.h"
31#include "root_visitor.h"
32#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,
59 RootVisitor is_marked_callback,
60 RootVisitor recursive_mark_callback, void* arg)
61 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.
65 void PreserveSomeSoftReferences(RootVisitor preserve_callback, void* arg)
66 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.
69 void ClearWhiteReferences(ReferenceQueue& cleared_references, RootVisitor visitor, void* arg)
70 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
71 void Dump(std::ostream& os) const
72 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
73 bool IsEmpty() const {
74 return list_ == nullptr;
75 }
76 void Clear() {
77 list_ = nullptr;
78 }
79 mirror::Object* GetList() {
80 return list_;
81 }
82
83 private:
84 // Lock, used for parallel GC reference enqueuing. It allows for multiple threads simultaneously
85 // calling AtomicEnqueueIfNotEnqueued.
Ian Rogersef7d42f2014-01-06 12:55:46 -080086 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartier39e32612013-11-12 16:28:05 -080087 // The heap contains the reference offsets.
88 Heap* const heap_;
89 // The actual reference list. Not a root since it will be nullptr when the GC is not running.
90 mirror::Object* list_;
91};
92
93} // namespace gc
94} // namespace art
95
96#endif // ART_RUNTIME_GC_REFERENCE_QUEUE_H_