blob: cbc26977fb474cc0a723850e21e3e8a98c00229b [file] [log] [blame]
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -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_READ_BARRIER_H_
18#define ART_RUNTIME_READ_BARRIER_H_
19
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070020#include "base/mutex.h"
21#include "base/macros.h"
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070022#include "gc_root.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080023#include "jni.h"
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070024#include "mirror/object_reference.h"
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070025#include "offsets.h"
26#include "read_barrier_c.h"
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070027
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070028// This is a C++ (not C) header file, separate from read_barrier_c.h
29// which needs to be a C header file for asm_support.h.
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070030
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070031namespace art {
32namespace mirror {
33 class Object;
34 template<typename MirrorType> class HeapReference;
35} // namespace mirror
Mathieu Chartiere401d142015-04-22 13:56:20 -070036class ArtMethod;
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070037
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070038class ReadBarrier {
39 public:
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080040 // Enable the to-space invariant checks.
Hiroshi Yamauchidaf61a12016-06-10 14:27:38 -070041 static constexpr bool kEnableToSpaceInvariantChecks = kIsDebugBuild;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080042 // Enable the read barrier checks.
Hiroshi Yamauchidaf61a12016-06-10 14:27:38 -070043 static constexpr bool kEnableReadBarrierInvariantChecks = kIsDebugBuild;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080044
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -080045 // It's up to the implementation whether the given field gets updated whereas the return value
46 // must be an updated reference unless kAlwaysUpdateField is true.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080047 template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -080048 bool kAlwaysUpdateField = false>
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070049 ALWAYS_INLINE static MirrorType* Barrier(
50 mirror::Object* obj, MemberOffset offset, mirror::HeapReference<MirrorType>* ref_addr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070051 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -070052
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -070053 // It's up to the implementation whether the given root gets updated
54 // whereas the return value must be an updated reference.
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -080055 template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070056 ALWAYS_INLINE static MirrorType* BarrierForRoot(MirrorType** root,
57 GcRootSource* gc_root_source = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070058 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080059
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070060 // It's up to the implementation whether the given root gets updated
61 // whereas the return value must be an updated reference.
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -080062 template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070063 ALWAYS_INLINE static MirrorType* BarrierForRoot(mirror::CompressedReference<MirrorType>* root,
64 GcRootSource* gc_root_source = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070065 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070066
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080067 static bool IsDuringStartup();
68
69 // Without the holder object.
70 static void AssertToSpaceInvariant(mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070071 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080072 AssertToSpaceInvariant(nullptr, MemberOffset(0), ref);
73 }
74 // With the holder object.
75 static void AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset,
76 mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070077 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070078 // With GcRootSource.
79 static void AssertToSpaceInvariant(GcRootSource* gc_root_source, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070080 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080081
Hiroshi Yamauchi2e5de782016-01-29 12:06:36 -080082 // ALWAYS_INLINE on this caused a performance regression b/26744236.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070083 static mirror::Object* Mark(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080084
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -070085 static constexpr uint32_t WhiteState() {
86 return white_state_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080087 }
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -070088 static constexpr uint32_t GrayState() {
89 return gray_state_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080090 }
91
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -070092 // fake_address_dependency will be zero which should be bitwise-or'ed with the address of the
93 // subsequent load to prevent the reordering of the read barrier bit load and the subsequent
94 // object reference load (from one of `obj`'s fields).
95 // *fake_address_dependency will be set to 0.
96 ALWAYS_INLINE static bool IsGray(mirror::Object* obj, uintptr_t* fake_address_dependency)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070097 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080098
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -070099 // This uses a load-acquire to load the read barrier bit internally to prevent the reordering of
100 // the read barrier bit load and the subsequent load.
101 ALWAYS_INLINE static bool IsGray(mirror::Object* obj)
102 REQUIRES_SHARED(Locks::mutator_lock_);
103
104 static bool IsValidReadBarrierState(uint32_t rb_state) {
105 return rb_state == white_state_ || rb_state == gray_state_;
106 }
107
108 static constexpr uint32_t white_state_ = 0x0; // Not marked.
109 static constexpr uint32_t gray_state_ = 0x1; // Marked, but not marked through. On mark stack.
110 static constexpr uint32_t rb_state_mask_ = 0x1; // The low bits for white|gray.
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -0700111};
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700112
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -0700113} // namespace art
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700114
115#endif // ART_RUNTIME_READ_BARRIER_H_