1 /* 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_OPTION_H_ 18 #define ART_RUNTIME_READ_BARRIER_OPTION_H_ 19 namespace art { 20 21 // Options for performing a read barrier or not. 22 // 23 // Besides disabled GC and GC's internal usage, there are a few cases where the read 24 // barrier is unnecessary and can be avoided to reduce code size and improve performance. 25 // In the following cases, the result of the operation or chain of operations shall be the 26 // same whether we go through the from-space or to-space: 27 // 28 // 1. We're reading a reference known to point to an un-reclaimable immune space object. 29 // (For example boot image class and string references, read by compiled code from 30 // .data.bimg.rel.ro . Similarly, such references constructed using position independent 31 // code in the compiled boot image code do not need a read barrier.) 32 // 2. We're reading the reference for comparison involving a non-moving space reference. 33 // (Whether the non-moving space reference is the one we're reading or the one we shall 34 // compare it with, the result is the same with and without read barrier.) 35 // 3. We're reading the reference for comparison with null. 36 // (Similar to 2 above, given that null is "non-moving".) 37 // 4. We're reading a reference to a holder from which we shall read 38 // - constant primitive field, or 39 // - mutable primitive field for testing an invariant, or 40 // - constant reference field known to point to an un-reclaimable immune space object, or 41 // - constant reference field for comparison involving a non-moving space reference, or 42 // - constant reference field for comparison with null, or 43 // - constant reference fields in a chain leading to one or more of the above purposes; 44 // the entire chain needs to be read without read barrier. 45 // The terms "constant" and "invariant" refer to values stored in holder fields before the 46 // holder reference is stored in the location for which we want to avoid the read barrier. 47 // Since the stored holder reference points to an object with the initialized constant or 48 // invariant, when we start a new GC and that holder instance becomes a from-space object 49 // both the from-space and to-space versions shall hold the same constant or invariant. 50 // 51 // While correct inter-thread memory visibility needs to be ensured for these constants and 52 // invariants, it needs to be equally ensured for non-moving GC types, so read barriers or 53 // their avoidance do not place any additional constraints on inter-thread synchronization. 54 // 55 // References read without a read barrier must not remain live at the next suspend point, 56 // with the exception of references to un-reclaimable immune space objects. 57 // 58 // For un-reclaimable immune space objects, we rely on graying dirty objects in the FlipCallback 59 // pause (we try to gray them just before flipping thread roots but the FlipCallback has to re-scan 60 // for newly dirtied objects) and clean objects conceptually become black at that point 61 // (marking them through is a no-op as all reference fields must also point to immune spaces), 62 // so mutator threads can never miss a read barrier as they never see white immune space object. 63 // 64 // Examples: 65 // 66 // The j.l.Class contains many constant fields and invariants: 67 // - primitive type is constant (primitive classes are pre-initialized in the boot image, 68 // or created in early single-threaded stage when running without boot image; non-primitive 69 // classes keep the value 0 from the Class object allocation), 70 // - element type is constant (initialized during array class object allocation, null otherwise), 71 // - access flags are mutable but the proxy class bit is an invariant set during class creation, 72 // - once the class is resolved, the class status is still mutable but it shall remain resolved, 73 // being a resolved is an invariant from that point on, 74 // - once a class becomes erroneous, the class status shall be constant (and unresolved 75 // erroneous class shall not become resolved). 76 // This allows reading a chain of element type references for any number of array dimensions 77 // without read barrier to find the (non-array) element class and check whether it's primitive, 78 // or proxy class. When creating an array class, the element type is already either resolved or 79 // unresolved erroneous and neither shall change, so we can also check these invariants (but not 80 // resolved erroneous because that is not an invariant from the creation of the array class). 81 // 82 // The superclass becomes constant during the ClassStatus::kIdx stage, so it's safe to treat it 83 // as constant when reading from locations that can reference only resolved classes. 84 enum ReadBarrierOption { 85 kWithReadBarrier, // Perform a read barrier. 86 kWithoutReadBarrier, // Don't perform a read barrier. 87 }; 88 89 } // namespace art 90 91 #endif // ART_RUNTIME_READ_BARRIER_OPTION_H_ 92