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