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