xref: /aosp_15_r20/art/runtime/read_barrier_option.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_READ_BARRIER_OPTION_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_READ_BARRIER_OPTION_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker // Options for performing a read barrier or not.
25*795d594fSAndroid Build Coastguard Worker //
26*795d594fSAndroid Build Coastguard Worker // Besides disabled GC and GC's internal usage, there are a few cases where the read
27*795d594fSAndroid Build Coastguard Worker // barrier is unnecessary and can be avoided to reduce code size and improve performance.
28*795d594fSAndroid Build Coastguard Worker // In the following cases, the result of the operation or chain of operations shall be the
29*795d594fSAndroid Build Coastguard Worker // same whether we go through the from-space or to-space:
30*795d594fSAndroid Build Coastguard Worker //
31*795d594fSAndroid Build Coastguard Worker // 1. We're reading a reference known to point to an un-reclaimable immune space object.
32*795d594fSAndroid Build Coastguard Worker //    (For example boot image class and string references, read by compiled code from
33*795d594fSAndroid Build Coastguard Worker //    .data.img.rel.ro . Similarly, such references constructed using position independent
34*795d594fSAndroid Build Coastguard Worker //    code in the compiled boot image code do not need a read barrier.)
35*795d594fSAndroid Build Coastguard Worker // 2. We're reading the reference for comparison involving a non-moving space reference.
36*795d594fSAndroid Build Coastguard Worker //    (Whether the non-moving space reference is the one we're reading or the one we shall
37*795d594fSAndroid Build Coastguard Worker //    compare it with, the result is the same with and without read barrier.)
38*795d594fSAndroid Build Coastguard Worker // 3. We're reading the reference for comparison with null.
39*795d594fSAndroid Build Coastguard Worker //    (Similar to 2 above, given that null is "non-moving".)
40*795d594fSAndroid Build Coastguard Worker // 4. We're reading a reference to a holder from which we shall read
41*795d594fSAndroid Build Coastguard Worker //      - constant primitive field, or
42*795d594fSAndroid Build Coastguard Worker //      - mutable primitive field for testing an invariant, or
43*795d594fSAndroid Build Coastguard Worker //      - constant reference field known to point to an un-reclaimable immune space object, or
44*795d594fSAndroid Build Coastguard Worker //      - constant reference field for comparison involving a non-moving space reference, or
45*795d594fSAndroid Build Coastguard Worker //      - constant reference field for comparison with null, or
46*795d594fSAndroid Build Coastguard Worker //      - constant reference fields in a chain leading to one or more of the above purposes;
47*795d594fSAndroid Build Coastguard Worker //        the entire chain needs to be read without read barrier.
48*795d594fSAndroid Build Coastguard Worker // The terms "constant" and "invariant" refer to values stored in holder fields before the
49*795d594fSAndroid Build Coastguard Worker // holder reference is stored in the location for which we want to avoid the read barrier.
50*795d594fSAndroid Build Coastguard Worker // Since the stored holder reference points to an object with the initialized constant or
51*795d594fSAndroid Build Coastguard Worker // invariant, when we start a new GC and that holder instance becomes a from-space object
52*795d594fSAndroid Build Coastguard Worker // both the from-space and to-space versions shall hold the same constant or invariant.
53*795d594fSAndroid Build Coastguard Worker //
54*795d594fSAndroid Build Coastguard Worker // While correct inter-thread memory visibility needs to be ensured for these constants and
55*795d594fSAndroid Build Coastguard Worker // invariants, it needs to be equally ensured for non-moving GC types, so read barriers or
56*795d594fSAndroid Build Coastguard Worker // their avoidance do not place any additional constraints on inter-thread synchronization.
57*795d594fSAndroid Build Coastguard Worker //
58*795d594fSAndroid Build Coastguard Worker // References read without a read barrier must not remain live at the next suspend point,
59*795d594fSAndroid Build Coastguard Worker // with the exception of references to un-reclaimable immune space objects.
60*795d594fSAndroid Build Coastguard Worker //
61*795d594fSAndroid Build Coastguard Worker // For un-reclaimable immune space objects, we rely on graying dirty objects in the FlipCallback
62*795d594fSAndroid Build Coastguard Worker // pause (we try to gray them just before flipping thread roots but the FlipCallback has to re-scan
63*795d594fSAndroid Build Coastguard Worker // for newly dirtied objects) and clean objects conceptually become black at that point
64*795d594fSAndroid Build Coastguard Worker // (marking them through is a no-op as all reference fields must also point to immune spaces),
65*795d594fSAndroid Build Coastguard Worker // so mutator threads can never miss a read barrier as they never see white immune space object.
66*795d594fSAndroid Build Coastguard Worker //
67*795d594fSAndroid Build Coastguard Worker // Examples:
68*795d594fSAndroid Build Coastguard Worker //
69*795d594fSAndroid Build Coastguard Worker // The j.l.Class contains many constant fields and invariants:
70*795d594fSAndroid Build Coastguard Worker //   - primitive type is constant (primitive classes are pre-initialized in the boot image,
71*795d594fSAndroid Build Coastguard Worker //     or created in early single-threaded stage when running without boot image; non-primitive
72*795d594fSAndroid Build Coastguard Worker //     classes keep the value 0 from the Class object allocation),
73*795d594fSAndroid Build Coastguard Worker //   - element type is constant (initialized during array class object allocation, null otherwise),
74*795d594fSAndroid Build Coastguard Worker //   - access flags are mutable but the proxy class bit is an invariant set during class creation,
75*795d594fSAndroid Build Coastguard Worker //   - once the class is resolved, the class status is still mutable but it shall remain resolved,
76*795d594fSAndroid Build Coastguard Worker //     being a resolved is an invariant from that point on,
77*795d594fSAndroid Build Coastguard Worker //   - once a class becomes erroneous, the class status shall be constant (and unresolved
78*795d594fSAndroid Build Coastguard Worker //     erroneous class shall not become resolved).
79*795d594fSAndroid Build Coastguard Worker // This allows reading a chain of element type references for any number of array dimensions
80*795d594fSAndroid Build Coastguard Worker // without read barrier to find the (non-array) element class and check whether it's primitive,
81*795d594fSAndroid Build Coastguard Worker // or proxy class. When creating an array class, the element type is already either resolved or
82*795d594fSAndroid Build Coastguard Worker // unresolved erroneous and neither shall change, so we can also check these invariants (but not
83*795d594fSAndroid Build Coastguard Worker // resolved erroneous because that is not an invariant from the creation of the array class).
84*795d594fSAndroid Build Coastguard Worker //
85*795d594fSAndroid Build Coastguard Worker // The superclass becomes constant during the ClassStatus::kIdx stage, so it's safe to treat it
86*795d594fSAndroid Build Coastguard Worker // as constant when reading from locations that can reference only resolved classes.
87*795d594fSAndroid Build Coastguard Worker enum EXPORT ReadBarrierOption {
88*795d594fSAndroid Build Coastguard Worker   kWithReadBarrier,       // Perform a read barrier.
89*795d594fSAndroid Build Coastguard Worker   kWithoutReadBarrier,    // Don't perform a read barrier.
90*795d594fSAndroid Build Coastguard Worker   kWithFromSpaceBarrier,  // Get the from-space address for the given to-space address. Used by CMC
91*795d594fSAndroid Build Coastguard Worker };
92*795d594fSAndroid Build Coastguard Worker 
93*795d594fSAndroid Build Coastguard Worker }  // namespace art
94*795d594fSAndroid Build Coastguard Worker 
95*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_READ_BARRIER_OPTION_H_
96