Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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_LOCK_WORD_H_ |
| 18 | #define ART_RUNTIME_LOCK_WORD_H_ |
| 19 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 20 | #include <cstdint> |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 21 | #include <iosfwd> |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 22 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 23 | #include <android-base/logging.h> |
| 24 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 25 | #include "base/bit_utils.h" |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 26 | #include "read_barrier.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | namespace mirror { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 30 | class Object; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 31 | } // namespace mirror |
| 32 | |
| 33 | class Monitor; |
| 34 | |
Daniel Colascione | b16949a | 2018-04-15 11:21:02 -0700 | [diff] [blame] | 35 | /* The lock value itself as stored in mirror::Object::monitor_. The two most significant bits |
| 36 | * encode the state. The four possible states are fat locked, thin/unlocked, hash code, and |
| 37 | * forwarding address. |
Roland Levillain | 2ae376f | 2018-01-30 11:35:11 +0000 | [diff] [blame] | 38 | * |
| 39 | * When the lock word is in the "thin" state and its bits are formatted as follows: |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 40 | * |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 41 | * |33|2|2|222222221111|1111110000000000| |
| 42 | * |10|9|8|765432109876|5432109876543210| |
| 43 | * |00|m|r| lock count |thread id owner | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 44 | * |
Hans Boehm | 65c18a2 | 2020-01-03 23:37:13 +0000 | [diff] [blame] | 45 | * The lock count is zero, but the owner is nonzero for a simply held lock. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 46 | * When the lock word is in the "fat" state and its bits are formatted as follows: |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 47 | * |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 48 | * |33|2|2|2222222211111111110000000000| |
| 49 | * |10|9|8|7654321098765432109876543210| |
| 50 | * |01|m|r| MonitorId | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 51 | * |
| 52 | * When the lock word is in hash state and its bits are formatted as follows: |
| 53 | * |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 54 | * |33|2|2|2222222211111111110000000000| |
| 55 | * |10|9|8|7654321098765432109876543210| |
| 56 | * |10|m|r| HashCode | |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 57 | * |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 58 | * When the lock word is in forwarding address state and its bits are formatted as follows: |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 59 | * |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 60 | * |33|2|22222222211111111110000000000| |
| 61 | * |10|9|87654321098765432109876543210| |
| 62 | * |11|0| ForwardingAddress | |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 63 | * |
Roland Levillain | ba650a4 | 2017-03-06 13:52:32 +0000 | [diff] [blame] | 64 | * The `r` bit stores the read barrier state. |
Roland Levillain | 2ae376f | 2018-01-30 11:35:11 +0000 | [diff] [blame] | 65 | * The `m` bit stores the mark bit state. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 66 | */ |
| 67 | class LockWord { |
| 68 | public: |
Mathieu Chartier | 1cf194f | 2016-11-01 20:13:24 -0700 | [diff] [blame] | 69 | enum SizeShiftsAndMasks : uint32_t { // private marker to avoid generate-operator-out.py from processing. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 70 | // Number of bits to encode the state, currently just fat or thin/unlocked or hash code. |
| 71 | kStateSize = 2, |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 72 | kReadBarrierStateSize = 1, |
| 73 | kMarkBitStateSize = 1, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 74 | // Number of bits to encode the thin lock owner. |
| 75 | kThinLockOwnerSize = 16, |
Hans Boehm | 65c18a2 | 2020-01-03 23:37:13 +0000 | [diff] [blame] | 76 | // Remaining bits are the recursive lock count. Zero means it is locked exactly once |
| 77 | // and not recursively. |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 78 | kThinLockCountSize = 32 - kThinLockOwnerSize - kStateSize - kReadBarrierStateSize - |
| 79 | kMarkBitStateSize, |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 80 | |
Vladimir Marko | c8b1d5e | 2018-05-15 16:07:12 +0100 | [diff] [blame] | 81 | // Thin lock bits. Owner in lowest bits. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 82 | kThinLockOwnerShift = 0, |
| 83 | kThinLockOwnerMask = (1 << kThinLockOwnerSize) - 1, |
Vladimir Marko | c8b1d5e | 2018-05-15 16:07:12 +0100 | [diff] [blame] | 84 | kThinLockOwnerMaskShifted = kThinLockOwnerMask << kThinLockOwnerShift, |
nikolay serdjuk | d8481cc | 2014-07-28 17:40:16 +0700 | [diff] [blame] | 85 | kThinLockMaxOwner = kThinLockOwnerMask, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 86 | // Count in higher bits. |
| 87 | kThinLockCountShift = kThinLockOwnerSize + kThinLockOwnerShift, |
Dmitry Petrochenko | 8d82de5 | 2014-07-28 17:40:16 +0700 | [diff] [blame] | 88 | kThinLockCountMask = (1 << kThinLockCountSize) - 1, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 89 | kThinLockMaxCount = kThinLockCountMask, |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 90 | kThinLockCountOne = 1 << kThinLockCountShift, // == 65536 (0x10000) |
Vladimir Marko | c8b1d5e | 2018-05-15 16:07:12 +0100 | [diff] [blame] | 91 | kThinLockCountMaskShifted = kThinLockCountMask << kThinLockCountShift, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 92 | |
| 93 | // State in the highest bits. |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 94 | kStateShift = kReadBarrierStateSize + kThinLockCountSize + kThinLockCountShift + |
| 95 | kMarkBitStateSize, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 96 | kStateMask = (1 << kStateSize) - 1, |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 97 | kStateMaskShifted = kStateMask << kStateShift, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 98 | kStateThinOrUnlocked = 0, |
| 99 | kStateFat = 1, |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 100 | kStateHash = 2, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 101 | kStateForwardingAddress = 3, |
Mathieu Chartier | 1cf194f | 2016-11-01 20:13:24 -0700 | [diff] [blame] | 102 | kStateForwardingAddressShifted = kStateForwardingAddress << kStateShift, |
| 103 | kStateForwardingAddressOverflow = (1 + kStateMask - kStateForwardingAddress) << kStateShift, |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 104 | |
| 105 | // Read barrier bit. |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 106 | kReadBarrierStateShift = kThinLockCountSize + kThinLockCountShift, |
| 107 | kReadBarrierStateMask = (1 << kReadBarrierStateSize) - 1, |
| 108 | kReadBarrierStateMaskShifted = kReadBarrierStateMask << kReadBarrierStateShift, |
| 109 | kReadBarrierStateMaskShiftedToggled = ~kReadBarrierStateMaskShifted, |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 110 | |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 111 | // Mark bit. |
| 112 | kMarkBitStateShift = kReadBarrierStateSize + kReadBarrierStateShift, |
| 113 | kMarkBitStateMask = (1 << kMarkBitStateSize) - 1, |
| 114 | kMarkBitStateMaskShifted = kMarkBitStateMask << kMarkBitStateShift, |
| 115 | kMarkBitStateMaskShiftedToggled = ~kMarkBitStateMaskShifted, |
| 116 | |
| 117 | // GC state is mark bit and read barrier state. |
| 118 | kGCStateSize = kReadBarrierStateSize + kMarkBitStateSize, |
| 119 | kGCStateShift = kReadBarrierStateShift, |
| 120 | kGCStateMaskShifted = kReadBarrierStateMaskShifted | kMarkBitStateMaskShifted, |
| 121 | kGCStateMaskShiftedToggled = ~kGCStateMaskShifted, |
| 122 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 123 | // When the state is kHashCode, the non-state bits hold the hashcode. |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 124 | // Note Object.hashCode() has the hash code layout hardcoded. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 125 | kHashShift = 0, |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 126 | kHashSize = 32 - kStateSize - kReadBarrierStateSize - kMarkBitStateSize, |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 127 | kHashMask = (1 << kHashSize) - 1, |
nikolay serdjuk | d8481cc | 2014-07-28 17:40:16 +0700 | [diff] [blame] | 128 | kMaxHash = kHashMask, |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 129 | |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 130 | // Forwarding address shift. |
| 131 | kForwardingAddressShift = kObjectAlignmentShift, |
| 132 | |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 133 | kMonitorIdShift = kHashShift, |
| 134 | kMonitorIdSize = kHashSize, |
| 135 | kMonitorIdMask = kHashMask, |
| 136 | kMonitorIdAlignmentShift = 32 - kMonitorIdSize, |
| 137 | kMonitorIdAlignment = 1 << kMonitorIdAlignmentShift, |
nikolay serdjuk | d8481cc | 2014-07-28 17:40:16 +0700 | [diff] [blame] | 138 | kMaxMonitorId = kMaxHash |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 141 | static LockWord FromThinLockId(uint32_t thread_id, uint32_t count, uint32_t gc_state) { |
nikolay serdjuk | d8481cc | 2014-07-28 17:40:16 +0700 | [diff] [blame] | 142 | CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockMaxOwner)); |
| 143 | CHECK_LE(count, static_cast<uint32_t>(kThinLockMaxCount)); |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 144 | // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U); |
| 145 | return LockWord((thread_id << kThinLockOwnerShift) | |
| 146 | (count << kThinLockCountShift) | |
| 147 | (gc_state << kGCStateShift) | |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 148 | (kStateThinOrUnlocked << kStateShift)); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 151 | static LockWord FromForwardingAddress(size_t target) { |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 152 | DCHECK_ALIGNED(target, (1 << kStateSize)); |
Mathieu Chartier | 1cf194f | 2016-11-01 20:13:24 -0700 | [diff] [blame] | 153 | return LockWord((target >> kForwardingAddressShift) | kStateForwardingAddressShifted); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 156 | static LockWord FromHashCode(uint32_t hash_code, uint32_t gc_state) { |
nikolay serdjuk | d8481cc | 2014-07-28 17:40:16 +0700 | [diff] [blame] | 157 | CHECK_LE(hash_code, static_cast<uint32_t>(kMaxHash)); |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 158 | // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 159 | return LockWord((hash_code << kHashShift) | |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 160 | (gc_state << kGCStateShift) | |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 161 | (kStateHash << kStateShift)); |
| 162 | } |
| 163 | |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 164 | static LockWord FromDefault(uint32_t gc_state) { |
| 165 | return LockWord(gc_state << kGCStateShift); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static bool IsDefault(LockWord lw) { |
| 169 | return LockWord().GetValue() == lw.GetValue(); |
| 170 | } |
| 171 | |
| 172 | static LockWord Default() { |
| 173 | return LockWord(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | enum LockState { |
| 177 | kUnlocked, // No lock owners. |
| 178 | kThinLocked, // Single uncontended owner. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 179 | kFatLocked, // See associated monitor. |
| 180 | kHashCode, // Lock word contains an identity hash. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 181 | kForwardingAddress, // Lock word contains the forwarding address of an object. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | LockState GetState() const { |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 185 | CheckReadBarrierState(); |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 186 | if ((!kUseReadBarrier && UNLIKELY(value_ == 0)) || |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 187 | (kUseReadBarrier && UNLIKELY((value_ & kGCStateMaskShiftedToggled) == 0))) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 188 | return kUnlocked; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 189 | } else { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 190 | uint32_t internal_state = (value_ >> kStateShift) & kStateMask; |
| 191 | switch (internal_state) { |
| 192 | case kStateThinOrUnlocked: |
| 193 | return kThinLocked; |
| 194 | case kStateHash: |
| 195 | return kHashCode; |
| 196 | case kStateForwardingAddress: |
| 197 | return kForwardingAddress; |
| 198 | default: |
| 199 | DCHECK_EQ(internal_state, static_cast<uint32_t>(kStateFat)); |
| 200 | return kFatLocked; |
| 201 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 205 | uint32_t ReadBarrierState() const { |
| 206 | return (value_ >> kReadBarrierStateShift) & kReadBarrierStateMask; |
| 207 | } |
| 208 | |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 209 | uint32_t GCState() const { |
| 210 | return (value_ & kGCStateMaskShifted) >> kGCStateShift; |
| 211 | } |
| 212 | |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 213 | void SetReadBarrierState(uint32_t rb_state) { |
| 214 | DCHECK_EQ(rb_state & ~kReadBarrierStateMask, 0U); |
Roland Levillain | 14e5a29 | 2018-06-28 12:00:56 +0100 | [diff] [blame] | 215 | DCHECK(rb_state == ReadBarrier::NonGrayState() || |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 216 | rb_state == ReadBarrier::GrayState()) << rb_state; |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 217 | DCHECK_NE(static_cast<uint32_t>(GetState()), static_cast<uint32_t>(kForwardingAddress)); |
| 218 | // Clear and or the bits. |
| 219 | value_ &= ~(kReadBarrierStateMask << kReadBarrierStateShift); |
| 220 | value_ |= (rb_state & kReadBarrierStateMask) << kReadBarrierStateShift; |
| 221 | } |
| 222 | |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 223 | |
| 224 | uint32_t MarkBitState() const { |
| 225 | return (value_ >> kMarkBitStateShift) & kMarkBitStateMask; |
| 226 | } |
| 227 | |
| 228 | void SetMarkBitState(uint32_t mark_bit) { |
| 229 | DCHECK_EQ(mark_bit & ~kMarkBitStateMask, 0U); |
| 230 | DCHECK_NE(static_cast<uint32_t>(GetState()), static_cast<uint32_t>(kForwardingAddress)); |
| 231 | // Clear and or the bits. |
| 232 | value_ &= kMarkBitStateMaskShiftedToggled; |
| 233 | value_ |= mark_bit << kMarkBitStateShift; |
| 234 | } |
| 235 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 236 | // Return the owner thin lock thread id. |
| 237 | uint32_t ThinLockOwner() const; |
| 238 | |
Hans Boehm | 65c18a2 | 2020-01-03 23:37:13 +0000 | [diff] [blame] | 239 | // Return the number of times a lock value has been re-locked. Only valid in thin-locked state. |
| 240 | // If the lock is held only once the return value is zero. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 241 | uint32_t ThinLockCount() const; |
| 242 | |
| 243 | // Return the Monitor encoded in a fat lock. |
| 244 | Monitor* FatLockMonitor() const; |
| 245 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 246 | // Return the forwarding address stored in the monitor. |
| 247 | size_t ForwardingAddress() const; |
| 248 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 249 | // Constructor a lock word for inflation to use a Monitor. |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 250 | LockWord(Monitor* mon, uint32_t gc_state); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 251 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 252 | // Return the hash code stored in the lock word, must be kHashCode state. |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 253 | int32_t GetHashCode() const; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 254 | |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 255 | template <bool kIncludeReadBarrierState> |
| 256 | static bool Equal(LockWord lw1, LockWord lw2) { |
| 257 | if (kIncludeReadBarrierState) { |
| 258 | return lw1.GetValue() == lw2.GetValue(); |
| 259 | } |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 260 | return lw1.GetValueWithoutGCState() == lw2.GetValueWithoutGCState(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 263 | void Dump(std::ostream& os) { |
| 264 | os << "LockWord:" << std::hex << value_; |
| 265 | } |
| 266 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 267 | private: |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 268 | // Default constructor with no lock ownership. |
| 269 | LockWord(); |
| 270 | |
| 271 | explicit LockWord(uint32_t val) : value_(val) { |
Mathieu Chartier | 1cf194f | 2016-11-01 20:13:24 -0700 | [diff] [blame] | 272 | // Make sure adding the overflow causes an overflow. |
| 273 | constexpr uint64_t overflow = static_cast<uint64_t>(kStateForwardingAddressShifted) + |
| 274 | static_cast<uint64_t>(kStateForwardingAddressOverflow); |
| 275 | constexpr bool is_larger = overflow > static_cast<uint64_t>(0xFFFFFFFF); |
| 276 | static_assert(is_larger, "should have overflowed"); |
Mathieu Chartier | 6f198e3 | 2016-11-03 11:15:04 -0700 | [diff] [blame] | 277 | static_assert( |
| 278 | (~kStateForwardingAddress & kStateMask) == 0, |
| 279 | "READ_BARRIER_MARK_REG relies on the forwarding address state being only one bits"); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 280 | CheckReadBarrierState(); |
| 281 | } |
| 282 | |
| 283 | // Disallow this in favor of explicit Equal() with the |
| 284 | // kIncludeReadBarrierState param to make clients be aware of the |
| 285 | // read barrier state. |
| 286 | bool operator==(const LockWord& rhs) = delete; |
| 287 | |
| 288 | void CheckReadBarrierState() const { |
| 289 | if (kIsDebugBuild && ((value_ >> kStateShift) & kStateMask) != kStateForwardingAddress) { |
| 290 | uint32_t rb_state = ReadBarrierState(); |
| 291 | if (!kUseReadBarrier) { |
| 292 | DCHECK_EQ(rb_state, 0U); |
| 293 | } else { |
Roland Levillain | 14e5a29 | 2018-06-28 12:00:56 +0100 | [diff] [blame] | 294 | DCHECK(rb_state == ReadBarrier::NonGrayState() || |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 295 | rb_state == ReadBarrier::GrayState()) << rb_state; |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // Note GetValue() includes the read barrier bits and comparing (==) |
| 301 | // GetValue() between two lock words to compare the lock states may |
| 302 | // not work. Prefer Equal() or GetValueWithoutReadBarrierState(). |
| 303 | uint32_t GetValue() const { |
| 304 | CheckReadBarrierState(); |
| 305 | return value_; |
| 306 | } |
| 307 | |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 308 | uint32_t GetValueWithoutGCState() const { |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 309 | CheckReadBarrierState(); |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 310 | return value_ & kGCStateMaskShiftedToggled; |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 311 | } |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 312 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 313 | // Only Object should be converting LockWords to/from uints. |
| 314 | friend class mirror::Object; |
| 315 | |
| 316 | // The encoded value holding all the state. |
| 317 | uint32_t value_; |
| 318 | }; |
Vladimir Marko | 9974e3c | 2020-06-10 16:27:06 +0100 | [diff] [blame] | 319 | std::ostream& operator<<(std::ostream& os, LockWord::LockState code); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 320 | |
| 321 | } // namespace art |
| 322 | |
| 323 | |
| 324 | #endif // ART_RUNTIME_LOCK_WORD_H_ |