blob: d36acbcdc4a168857a9dd38890ada412446bd6ce [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
Andreas Gampeaea05c12017-05-19 08:45:02 -070020#include "base/logging.h"
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070021#include "base/macros.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070022#include "base/mutex.h"
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070023#include "gc_root.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080024#include "jni.h"
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070025#include "mirror/object_reference.h"
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070026#include "offsets.h"
27#include "read_barrier_c.h"
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070028
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070029// This is a C++ (not C) header file, separate from read_barrier_c.h
30// which needs to be a C header file for asm_support.h.
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070031
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070032namespace art {
33namespace mirror {
34 class Object;
35 template<typename MirrorType> class HeapReference;
36} // namespace mirror
Mathieu Chartiere401d142015-04-22 13:56:20 -070037class ArtMethod;
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070038
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070039class ReadBarrier {
40 public:
Andreas Gampeaea05c12017-05-19 08:45:02 -070041 // Enable the to-space invariant checks. This is slow and happens very often. Do not enable in
42 // fast-debug environment.
43 DECLARE_RUNTIME_DEBUG_FLAG(kEnableToSpaceInvariantChecks);
44
45 // Enable the read barrier checks. This is slow and happens very often. Do not enable in
46 // fast-debug environment.
47 DECLARE_RUNTIME_DEBUG_FLAG(kEnableReadBarrierInvariantChecks);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080048
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -080049 // It's up to the implementation whether the given field gets updated whereas the return value
50 // must be an updated reference unless kAlwaysUpdateField is true.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080051 template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -080052 bool kAlwaysUpdateField = false>
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070053 ALWAYS_INLINE static MirrorType* Barrier(
54 mirror::Object* obj, MemberOffset offset, mirror::HeapReference<MirrorType>* ref_addr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070055 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -070056
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -070057 // It's up to the implementation whether the given root gets updated
58 // whereas the return value must be an updated reference.
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -080059 template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070060 ALWAYS_INLINE static MirrorType* BarrierForRoot(MirrorType** root,
61 GcRootSource* gc_root_source = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070062 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080063
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070064 // It's up to the implementation whether the given root gets updated
65 // whereas the return value must be an updated reference.
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -080066 template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070067 ALWAYS_INLINE static MirrorType* BarrierForRoot(mirror::CompressedReference<MirrorType>* root,
68 GcRootSource* gc_root_source = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070069 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070070
Nicolas Geoffray13056a12017-05-11 11:48:28 +000071 // Return the mirror Object if it is marked, or null if not.
72 template <typename MirrorType>
73 ALWAYS_INLINE static MirrorType* IsMarked(MirrorType* ref)
74 REQUIRES_SHARED(Locks::mutator_lock_);
75
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080076 static bool IsDuringStartup();
77
78 // Without the holder object.
79 static void AssertToSpaceInvariant(mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070080 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080081 AssertToSpaceInvariant(nullptr, MemberOffset(0), ref);
82 }
83 // With the holder object.
84 static void AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset,
85 mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070086 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070087 // With GcRootSource.
88 static void AssertToSpaceInvariant(GcRootSource* gc_root_source, mirror::Object* ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070089 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080090
Hiroshi Yamauchi2e5de782016-01-29 12:06:36 -080091 // ALWAYS_INLINE on this caused a performance regression b/26744236.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070092 static mirror::Object* Mark(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080093
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -070094 static constexpr uint32_t WhiteState() {
95 return white_state_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080096 }
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -070097 static constexpr uint32_t GrayState() {
98 return gray_state_;
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080099 }
100
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700101 // fake_address_dependency will be zero which should be bitwise-or'ed with the address of the
102 // subsequent load to prevent the reordering of the read barrier bit load and the subsequent
103 // object reference load (from one of `obj`'s fields).
104 // *fake_address_dependency will be set to 0.
105 ALWAYS_INLINE static bool IsGray(mirror::Object* obj, uintptr_t* fake_address_dependency)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700106 REQUIRES_SHARED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800107
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700108 // This uses a load-acquire to load the read barrier bit internally to prevent the reordering of
109 // the read barrier bit load and the subsequent load.
110 ALWAYS_INLINE static bool IsGray(mirror::Object* obj)
111 REQUIRES_SHARED(Locks::mutator_lock_);
112
113 static bool IsValidReadBarrierState(uint32_t rb_state) {
114 return rb_state == white_state_ || rb_state == gray_state_;
115 }
116
117 static constexpr uint32_t white_state_ = 0x0; // Not marked.
118 static constexpr uint32_t gray_state_ = 0x1; // Marked, but not marked through. On mark stack.
119 static constexpr uint32_t rb_state_mask_ = 0x1; // The low bits for white|gray.
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -0700120};
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700121
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -0700122} // namespace art
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700123
124#endif // ART_RUNTIME_READ_BARRIER_H_