Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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_MIRROR_REFERENCE_H_ |
| 18 | #define ART_RUNTIME_MIRROR_REFERENCE_H_ |
| 19 | |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 20 | #include "class.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 21 | #include "gc_root.h" |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 22 | #include "object.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 23 | #include "object_callbacks.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 24 | #include "read_barrier_option.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 25 | #include "thread.h" |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 29 | namespace gc { |
| 30 | |
| 31 | class ReferenceProcessor; |
| 32 | class ReferenceQueue; |
| 33 | |
| 34 | } // namespace gc |
| 35 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 36 | struct ReferenceOffsets; |
| 37 | struct FinalizerReferenceOffsets; |
| 38 | |
| 39 | namespace mirror { |
| 40 | |
| 41 | // C++ mirror of java.lang.ref.Reference |
| 42 | class MANAGED Reference : public Object { |
| 43 | public: |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 44 | // Size of java.lang.ref.Reference.class. |
| 45 | static uint32_t ClassSize(); |
| 46 | |
| 47 | // Size of an instance of java.lang.ref.Reference. |
| 48 | static constexpr uint32_t InstanceSize() { |
| 49 | return sizeof(Reference); |
| 50 | } |
| 51 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 52 | static MemberOffset PendingNextOffset() { |
| 53 | return OFFSET_OF_OBJECT_MEMBER(Reference, pending_next_); |
| 54 | } |
| 55 | static MemberOffset QueueOffset() { |
| 56 | return OFFSET_OF_OBJECT_MEMBER(Reference, queue_); |
| 57 | } |
| 58 | static MemberOffset QueueNextOffset() { |
| 59 | return OFFSET_OF_OBJECT_MEMBER(Reference, queue_next_); |
| 60 | } |
| 61 | static MemberOffset ReferentOffset() { |
| 62 | return OFFSET_OF_OBJECT_MEMBER(Reference, referent_); |
| 63 | } |
Hiroshi Yamauchi | bfff21a | 2014-05-09 12:21:15 -0700 | [diff] [blame] | 64 | template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier> |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 65 | Object* GetReferent() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | bfff21a | 2014-05-09 12:21:15 -0700 | [diff] [blame] | 66 | return GetFieldObjectVolatile<Object, kDefaultVerifyFlags, kReadBarrierOption>( |
| 67 | ReferentOffset()); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 68 | } |
| 69 | template<bool kTransactionActive> |
| 70 | void SetReferent(Object* referent) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 71 | SetFieldObjectVolatile<kTransactionActive>(ReferentOffset(), referent); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 72 | } |
| 73 | template<bool kTransactionActive> |
| 74 | void ClearReferent() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 75 | SetFieldObjectVolatile<kTransactionActive>(ReferentOffset(), nullptr); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 76 | } |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 77 | // Volatile read/write is not necessary since the java pending next is only accessed from |
| 78 | // the java threads for cleared references. Once these cleared references have a null referent, |
| 79 | // we never end up reading their pending next from the GC again. |
| 80 | Reference* GetPendingNext() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 81 | return GetFieldObject<Reference>(PendingNextOffset()); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 82 | } |
| 83 | template<bool kTransactionActive> |
| 84 | void SetPendingNext(Reference* pending_next) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 85 | SetFieldObject<kTransactionActive>(PendingNextOffset(), pending_next); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | bool IsEnqueued() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 89 | // Since the references are stored as cyclic lists it means that once enqueued, the pending |
| 90 | // next is always non-null. |
| 91 | return GetPendingNext() != nullptr; |
| 92 | } |
| 93 | |
| 94 | bool IsEnqueuable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 95 | |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 96 | template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier> |
| 97 | static Class* GetJavaLangRefReference() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 98 | DCHECK(!java_lang_ref_Reference_.IsNull()); |
| 99 | return java_lang_ref_Reference_.Read<kReadBarrierOption>(); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 100 | } |
| 101 | static void SetClass(Class* klass); |
| 102 | static void ResetClass(void); |
| 103 | static void VisitRoots(RootCallback* callback, void* arg); |
| 104 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 105 | private: |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 106 | // Note: This avoids a read barrier, it should only be used by the GC. |
| 107 | HeapReference<Object>* GetReferentReferenceAddr() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 108 | return GetFieldObjectReferenceAddr<kDefaultVerifyFlags>(ReferentOffset()); |
| 109 | } |
| 110 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 111 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
| 112 | HeapReference<Reference> pending_next_; // Note this is Java volatile: |
| 113 | HeapReference<Object> queue_; // Note this is Java volatile: |
| 114 | HeapReference<Reference> queue_next_; // Note this is Java volatile: |
| 115 | HeapReference<Object> referent_; // Note this is Java volatile: |
| 116 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 117 | static GcRoot<Class> java_lang_ref_Reference_; |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 118 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 119 | friend struct art::ReferenceOffsets; // for verifying offset information |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 120 | friend class gc::ReferenceProcessor; |
| 121 | friend class gc::ReferenceQueue; |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 122 | DISALLOW_IMPLICIT_CONSTRUCTORS(Reference); |
| 123 | }; |
| 124 | |
| 125 | // C++ mirror of java.lang.ref.FinalizerReference |
| 126 | class MANAGED FinalizerReference : public Reference { |
| 127 | public: |
| 128 | static MemberOffset ZombieOffset() { |
| 129 | return OFFSET_OF_OBJECT_MEMBER(FinalizerReference, zombie_); |
| 130 | } |
| 131 | |
| 132 | template<bool kTransactionActive> |
| 133 | void SetZombie(Object* zombie) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 134 | return SetFieldObjectVolatile<kTransactionActive>(ZombieOffset(), zombie); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 135 | } |
| 136 | Object* GetZombie() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 137 | return GetFieldObjectVolatile<Object>(ZombieOffset()); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | private: |
| 141 | HeapReference<FinalizerReference> next_; |
| 142 | HeapReference<FinalizerReference> prev_; |
| 143 | HeapReference<Object> zombie_; |
| 144 | |
| 145 | friend struct art::FinalizerReferenceOffsets; // for verifying offset information |
| 146 | DISALLOW_IMPLICIT_CONSTRUCTORS(FinalizerReference); |
| 147 | }; |
| 148 | |
| 149 | } // namespace mirror |
| 150 | } // namespace art |
| 151 | |
| 152 | #endif // ART_RUNTIME_MIRROR_REFERENCE_H_ |