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 | |
| 20 | #include "object.h" |
| 21 | |
| 22 | namespace art { |
| 23 | |
| 24 | struct ReferenceOffsets; |
| 25 | struct FinalizerReferenceOffsets; |
| 26 | |
| 27 | namespace mirror { |
| 28 | |
| 29 | // C++ mirror of java.lang.ref.Reference |
| 30 | class MANAGED Reference : public Object { |
| 31 | public: |
| 32 | static MemberOffset PendingNextOffset() { |
| 33 | return OFFSET_OF_OBJECT_MEMBER(Reference, pending_next_); |
| 34 | } |
| 35 | static MemberOffset QueueOffset() { |
| 36 | return OFFSET_OF_OBJECT_MEMBER(Reference, queue_); |
| 37 | } |
| 38 | static MemberOffset QueueNextOffset() { |
| 39 | return OFFSET_OF_OBJECT_MEMBER(Reference, queue_next_); |
| 40 | } |
| 41 | static MemberOffset ReferentOffset() { |
| 42 | return OFFSET_OF_OBJECT_MEMBER(Reference, referent_); |
| 43 | } |
| 44 | |
| 45 | Object* GetReferent() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 46 | return GetFieldObjectVolatile<Object>(ReferentOffset()); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 47 | } |
| 48 | template<bool kTransactionActive> |
| 49 | void SetReferent(Object* referent) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 50 | SetFieldObjectVolatile<kTransactionActive>(ReferentOffset(), referent); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 51 | } |
| 52 | template<bool kTransactionActive> |
| 53 | void ClearReferent() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 54 | SetFieldObjectVolatile<kTransactionActive>(ReferentOffset(), nullptr); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // Volatile read/write is not necessary since the java pending next is only accessed from |
| 58 | // the java threads for cleared references. Once these cleared references have a null referent, |
| 59 | // we never end up reading their pending next from the GC again. |
| 60 | Reference* GetPendingNext() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 61 | return GetFieldObject<Reference>(PendingNextOffset()); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 62 | } |
| 63 | template<bool kTransactionActive> |
| 64 | void SetPendingNext(Reference* pending_next) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 65 | SetFieldObject<kTransactionActive>(PendingNextOffset(), pending_next); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | bool IsEnqueued() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 69 | // Since the references are stored as cyclic lists it means that once enqueued, the pending |
| 70 | // next is always non-null. |
| 71 | return GetPendingNext() != nullptr; |
| 72 | } |
| 73 | |
| 74 | bool IsEnqueuable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 75 | |
| 76 | private: |
| 77 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
| 78 | HeapReference<Reference> pending_next_; // Note this is Java volatile: |
| 79 | HeapReference<Object> queue_; // Note this is Java volatile: |
| 80 | HeapReference<Reference> queue_next_; // Note this is Java volatile: |
| 81 | HeapReference<Object> referent_; // Note this is Java volatile: |
| 82 | |
| 83 | friend struct art::ReferenceOffsets; // for verifying offset information |
| 84 | DISALLOW_IMPLICIT_CONSTRUCTORS(Reference); |
| 85 | }; |
| 86 | |
| 87 | // C++ mirror of java.lang.ref.FinalizerReference |
| 88 | class MANAGED FinalizerReference : public Reference { |
| 89 | public: |
| 90 | static MemberOffset ZombieOffset() { |
| 91 | return OFFSET_OF_OBJECT_MEMBER(FinalizerReference, zombie_); |
| 92 | } |
| 93 | |
| 94 | template<bool kTransactionActive> |
| 95 | void SetZombie(Object* zombie) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 96 | return SetFieldObjectVolatile<kTransactionActive>(ZombieOffset(), zombie); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 97 | } |
| 98 | Object* GetZombie() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 99 | return GetFieldObjectVolatile<Object>(ZombieOffset()); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | private: |
| 103 | HeapReference<FinalizerReference> next_; |
| 104 | HeapReference<FinalizerReference> prev_; |
| 105 | HeapReference<Object> zombie_; |
| 106 | |
| 107 | friend struct art::FinalizerReferenceOffsets; // for verifying offset information |
| 108 | DISALLOW_IMPLICIT_CONSTRUCTORS(FinalizerReference); |
| 109 | }; |
| 110 | |
| 111 | } // namespace mirror |
| 112 | } // namespace art |
| 113 | |
| 114 | #endif // ART_RUNTIME_MIRROR_REFERENCE_H_ |