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