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 //      - constant reference field known to point to an un-reclaimable immune space object, or
40 //      - constant reference field for comparison involving a non-moving space reference, or
41 //      - constant reference field for comparison with null, or
42 //      - constant reference fields in a chain leading to one or more of the above purposes;
43 //        the entire chain needs to be read without read barrier.
44 //    The term "constant" refers to fields set to their final value between allocating
45 //    the holder and the next opportunity for the holder to be moved by the GC, i.e.
46 //    before the first suspend point after the allocation, or seen by another thread.
47 //    This includes several fields in the Class object, such as the primitive type or
48 //    component type but not the superclass.
49 //
50 // References read without a read barrier must not remain live at the next suspend point,
51 // with the exception of references to un-reclaimable immune space objects.
52 //
53 // For un-reclaimable immune space objects, we rely on graying dirty objects in the FlipCallback
54 // pause (we try to gray them just before flipping thread roots but the FlipCallback has to re-scan
55 // for newly dirtied objects) and clean objects conceptually become black at that point
56 // (marking them through is a no-op as all reference fields must also point to immune spaces),
57 // so mutator threads can never miss a read barrier as they never see white immune space object.
58 enum ReadBarrierOption {
59   kWithReadBarrier,     // Perform a read barrier.
60   kWithoutReadBarrier,  // Don't perform a read barrier.
61 };
62 
63 }  // namespace art
64 
65 #endif  // ART_RUNTIME_READ_BARRIER_OPTION_H_
66