blob: cf65d2093b4ccd824a0c831548704656deeb8614 [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
45 Object* GetReferent() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070046 return GetFieldObjectVolatile<Object>(ReferentOffset());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070047 }
48 template<bool kTransactionActive>
49 void SetReferent(Object* referent) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070050 SetFieldObjectVolatile<kTransactionActive>(ReferentOffset(), referent);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070051 }
52 template<bool kTransactionActive>
53 void ClearReferent() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070054 SetFieldObjectVolatile<kTransactionActive>(ReferentOffset(), nullptr);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070055 }
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 Rogersb0fa5dc2014-04-28 16:47:08 -070061 return GetFieldObject<Reference>(PendingNextOffset());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070062 }
63 template<bool kTransactionActive>
64 void SetPendingNext(Reference* pending_next) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070065 SetFieldObject<kTransactionActive>(PendingNextOffset(), pending_next);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070066 }
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
88class 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 Rogersb0fa5dc2014-04-28 16:47:08 -070096 return SetFieldObjectVolatile<kTransactionActive>(ZombieOffset(), zombie);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070097 }
98 Object* GetZombie() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070099 return GetFieldObjectVolatile<Object>(ZombieOffset());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700100 }
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_