blob: 0b6e75909778e09c65bab745413869dabf28152a [file] [log] [blame]
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -07001/*
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
22namespace art {
23
24struct ReferenceOffsets;
25struct FinalizerReferenceOffsets;
26
27namespace mirror {
28
29// C++ mirror of java.lang.ref.Reference
30class 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 Yamauchibfff21a2014-05-09 12:21:15 -070045 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070046 Object* GetReferent() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchibfff21a2014-05-09 12:21:15 -070047 return GetFieldObjectVolatile<Object, kDefaultVerifyFlags, kReadBarrierOption>(
48 ReferentOffset());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070049 }
50 template<bool kTransactionActive>
51 void SetReferent(Object* referent) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070052 SetFieldObjectVolatile<kTransactionActive>(ReferentOffset(), referent);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070053 }
54 template<bool kTransactionActive>
55 void ClearReferent() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070056 SetFieldObjectVolatile<kTransactionActive>(ReferentOffset(), nullptr);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070057 }
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 Rogersb0fa5dc2014-04-28 16:47:08 -070063 return GetFieldObject<Reference>(PendingNextOffset());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070064 }
65 template<bool kTransactionActive>
66 void SetPendingNext(Reference* pending_next) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070067 SetFieldObject<kTransactionActive>(PendingNextOffset(), pending_next);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070068 }
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
90class 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 Rogersb0fa5dc2014-04-28 16:47:08 -070098 return SetFieldObjectVolatile<kTransactionActive>(ZombieOffset(), zombie);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070099 }
100 Object* GetZombie() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700101 return GetFieldObjectVolatile<Object>(ZombieOffset());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700102 }
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_