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 | |
| 20 | #include <iosfwd> |
| 21 | #include <stdint.h> |
| 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 | * |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 38 | * |33|22|222222221111|1111110000000000| |
| 39 | * |10|98|765432109876|5432109876543210| |
| 40 | * |00|rb| 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 | * |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 44 | * |33|22|2222222211111111110000000000| |
| 45 | * |10|98|7654321098765432109876543210| |
| 46 | * |01|rb| 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 | * |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 50 | * |33|22|2222222211111111110000000000| |
| 51 | * |10|98|7654321098765432109876543210| |
| 52 | * |10|rb| HashCode | |
| 53 | * |
| 54 | * When the lock word is in fowarding address state and its bits are formatted as follows: |
| 55 | * |
| 56 | * |33|22|2222222211111111110000000000| |
| 57 | * |10|98|7654321098765432109876543210| |
| 58 | * |11| ForwardingAddress | |
| 59 | * |
| 60 | * The rb bits store the read barrier state. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 61 | */ |
| 62 | class LockWord { |
| 63 | public: |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 64 | enum SizeShiftsAndMasks { // private marker to avoid generate-operator-out.py from processing. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 65 | // Number of bits to encode the state, currently just fat or thin/unlocked or hash code. |
| 66 | kStateSize = 2, |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 67 | kReadBarrierStateSize = 2, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 68 | // Number of bits to encode the thin lock owner. |
| 69 | kThinLockOwnerSize = 16, |
| 70 | // Remaining bits are the recursive lock count. |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 71 | kThinLockCountSize = 32 - kThinLockOwnerSize - kStateSize - kReadBarrierStateSize, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 72 | // Thin lock bits. Owner in lowest bits. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 73 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 74 | kThinLockOwnerShift = 0, |
| 75 | kThinLockOwnerMask = (1 << kThinLockOwnerSize) - 1, |
nikolay serdjuk | d8481cc | 2014-07-28 17:40:16 +0700 | [diff] [blame] | 76 | kThinLockMaxOwner = kThinLockOwnerMask, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 77 | // Count in higher bits. |
| 78 | kThinLockCountShift = kThinLockOwnerSize + kThinLockOwnerShift, |
Dmitry Petrochenko | 8d82de5 | 2014-07-28 17:40:16 +0700 | [diff] [blame] | 79 | kThinLockCountMask = (1 << kThinLockCountSize) - 1, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 80 | kThinLockMaxCount = kThinLockCountMask, |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 81 | kThinLockCountOne = 1 << kThinLockCountShift, // == 65536 (0x10000) |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 82 | |
| 83 | // State in the highest bits. |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 84 | kStateShift = kReadBarrierStateSize + kThinLockCountSize + kThinLockCountShift, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 85 | kStateMask = (1 << kStateSize) - 1, |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 86 | kStateMaskShifted = kStateMask << kStateShift, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 87 | kStateThinOrUnlocked = 0, |
| 88 | kStateFat = 1, |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 89 | kStateHash = 2, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 90 | kStateForwardingAddress = 3, |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 91 | kReadBarrierStateShift = kThinLockCountSize + kThinLockCountShift, |
| 92 | kReadBarrierStateMask = (1 << kReadBarrierStateSize) - 1, |
| 93 | kReadBarrierStateMaskShifted = kReadBarrierStateMask << kReadBarrierStateShift, |
| 94 | kReadBarrierStateMaskShiftedToggled = ~kReadBarrierStateMaskShifted, |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 95 | |
| 96 | // When the state is kHashCode, the non-state bits hold the hashcode. |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 97 | // Note Object.hashCode() has the hash code layout hardcoded. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 98 | kHashShift = 0, |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 99 | kHashSize = 32 - kStateSize - kReadBarrierStateSize, |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 100 | kHashMask = (1 << kHashSize) - 1, |
nikolay serdjuk | d8481cc | 2014-07-28 17:40:16 +0700 | [diff] [blame] | 101 | kMaxHash = kHashMask, |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 102 | |
| 103 | kMonitorIdShift = kHashShift, |
| 104 | kMonitorIdSize = kHashSize, |
| 105 | kMonitorIdMask = kHashMask, |
| 106 | kMonitorIdAlignmentShift = 32 - kMonitorIdSize, |
| 107 | kMonitorIdAlignment = 1 << kMonitorIdAlignmentShift, |
nikolay serdjuk | d8481cc | 2014-07-28 17:40:16 +0700 | [diff] [blame] | 108 | kMaxMonitorId = kMaxHash |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 111 | static LockWord FromThinLockId(uint32_t thread_id, uint32_t count, uint32_t rb_state) { |
nikolay serdjuk | d8481cc | 2014-07-28 17:40:16 +0700 | [diff] [blame] | 112 | CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockMaxOwner)); |
| 113 | CHECK_LE(count, static_cast<uint32_t>(kThinLockMaxCount)); |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 114 | DCHECK_EQ(rb_state & ~kReadBarrierStateMask, 0U); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 115 | return LockWord((thread_id << kThinLockOwnerShift) | (count << kThinLockCountShift) | |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 116 | (rb_state << kReadBarrierStateShift) | |
| 117 | (kStateThinOrUnlocked << kStateShift)); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 120 | static LockWord FromForwardingAddress(size_t target) { |
| 121 | DCHECK(IsAligned < 1 << kStateSize>(target)); |
| 122 | return LockWord((target >> kStateSize) | (kStateForwardingAddress << kStateShift)); |
| 123 | } |
| 124 | |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 125 | static LockWord FromHashCode(uint32_t hash_code, uint32_t rb_state) { |
nikolay serdjuk | d8481cc | 2014-07-28 17:40:16 +0700 | [diff] [blame] | 126 | CHECK_LE(hash_code, static_cast<uint32_t>(kMaxHash)); |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 127 | DCHECK_EQ(rb_state & ~kReadBarrierStateMask, 0U); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 128 | return LockWord((hash_code << kHashShift) | |
| 129 | (rb_state << kReadBarrierStateShift) | |
| 130 | (kStateHash << kStateShift)); |
| 131 | } |
| 132 | |
| 133 | static LockWord FromDefault(uint32_t rb_state) { |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 134 | DCHECK_EQ(rb_state & ~kReadBarrierStateMask, 0U); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 135 | return LockWord(rb_state << kReadBarrierStateShift); |
| 136 | } |
| 137 | |
| 138 | static bool IsDefault(LockWord lw) { |
| 139 | return LockWord().GetValue() == lw.GetValue(); |
| 140 | } |
| 141 | |
| 142 | static LockWord Default() { |
| 143 | return LockWord(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | enum LockState { |
| 147 | kUnlocked, // No lock owners. |
| 148 | kThinLocked, // Single uncontended owner. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 149 | kFatLocked, // See associated monitor. |
| 150 | kHashCode, // Lock word contains an identity hash. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 151 | kForwardingAddress, // Lock word contains the forwarding address of an object. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | LockState GetState() const { |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 155 | CheckReadBarrierState(); |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 156 | if ((!kUseReadBarrier && UNLIKELY(value_ == 0)) || |
| 157 | (kUseReadBarrier && UNLIKELY((value_ & kReadBarrierStateMaskShiftedToggled) == 0))) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 158 | return kUnlocked; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 159 | } else { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 160 | uint32_t internal_state = (value_ >> kStateShift) & kStateMask; |
| 161 | switch (internal_state) { |
| 162 | case kStateThinOrUnlocked: |
| 163 | return kThinLocked; |
| 164 | case kStateHash: |
| 165 | return kHashCode; |
| 166 | case kStateForwardingAddress: |
| 167 | return kForwardingAddress; |
| 168 | default: |
| 169 | DCHECK_EQ(internal_state, static_cast<uint32_t>(kStateFat)); |
| 170 | return kFatLocked; |
| 171 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 175 | uint32_t ReadBarrierState() const { |
| 176 | return (value_ >> kReadBarrierStateShift) & kReadBarrierStateMask; |
| 177 | } |
| 178 | |
Hiroshi Yamauchi | 60f63f5 | 2015-04-23 16:12:40 -0700 | [diff] [blame] | 179 | void SetReadBarrierState(uint32_t rb_state) { |
| 180 | DCHECK_EQ(rb_state & ~kReadBarrierStateMask, 0U); |
| 181 | DCHECK_NE(static_cast<uint32_t>(GetState()), static_cast<uint32_t>(kForwardingAddress)); |
| 182 | // Clear and or the bits. |
| 183 | value_ &= ~(kReadBarrierStateMask << kReadBarrierStateShift); |
| 184 | value_ |= (rb_state & kReadBarrierStateMask) << kReadBarrierStateShift; |
| 185 | } |
| 186 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 187 | // Return the owner thin lock thread id. |
| 188 | uint32_t ThinLockOwner() const; |
| 189 | |
| 190 | // Return the number of times a lock value has been locked. |
| 191 | uint32_t ThinLockCount() const; |
| 192 | |
| 193 | // Return the Monitor encoded in a fat lock. |
| 194 | Monitor* FatLockMonitor() const; |
| 195 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 196 | // Return the forwarding address stored in the monitor. |
| 197 | size_t ForwardingAddress() const; |
| 198 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 199 | // Constructor a lock word for inflation to use a Monitor. |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 200 | explicit LockWord(Monitor* mon, uint32_t rb_state); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 201 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 202 | // 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] | 203 | int32_t GetHashCode() const; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 204 | |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 205 | template <bool kIncludeReadBarrierState> |
| 206 | static bool Equal(LockWord lw1, LockWord lw2) { |
| 207 | if (kIncludeReadBarrierState) { |
| 208 | return lw1.GetValue() == lw2.GetValue(); |
| 209 | } |
| 210 | return lw1.GetValueWithoutReadBarrierState() == lw2.GetValueWithoutReadBarrierState(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 213 | private: |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 214 | // Default constructor with no lock ownership. |
| 215 | LockWord(); |
| 216 | |
| 217 | explicit LockWord(uint32_t val) : value_(val) { |
| 218 | CheckReadBarrierState(); |
| 219 | } |
| 220 | |
| 221 | // Disallow this in favor of explicit Equal() with the |
| 222 | // kIncludeReadBarrierState param to make clients be aware of the |
| 223 | // read barrier state. |
| 224 | bool operator==(const LockWord& rhs) = delete; |
| 225 | |
| 226 | void CheckReadBarrierState() const { |
| 227 | if (kIsDebugBuild && ((value_ >> kStateShift) & kStateMask) != kStateForwardingAddress) { |
| 228 | uint32_t rb_state = ReadBarrierState(); |
| 229 | if (!kUseReadBarrier) { |
| 230 | DCHECK_EQ(rb_state, 0U); |
| 231 | } else { |
| 232 | DCHECK(rb_state == ReadBarrier::white_ptr_ || |
| 233 | rb_state == ReadBarrier::gray_ptr_ || |
| 234 | rb_state == ReadBarrier::black_ptr_) << rb_state; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // Note GetValue() includes the read barrier bits and comparing (==) |
| 240 | // GetValue() between two lock words to compare the lock states may |
| 241 | // not work. Prefer Equal() or GetValueWithoutReadBarrierState(). |
| 242 | uint32_t GetValue() const { |
| 243 | CheckReadBarrierState(); |
| 244 | return value_; |
| 245 | } |
| 246 | |
| 247 | uint32_t GetValueWithoutReadBarrierState() const { |
| 248 | CheckReadBarrierState(); |
| 249 | return value_ & ~(kReadBarrierStateMask << kReadBarrierStateShift); |
| 250 | } |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 251 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 252 | // Only Object should be converting LockWords to/from uints. |
| 253 | friend class mirror::Object; |
| 254 | |
| 255 | // The encoded value holding all the state. |
| 256 | uint32_t value_; |
| 257 | }; |
| 258 | std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code); |
| 259 | |
| 260 | } // namespace art |
| 261 | |
| 262 | |
| 263 | #endif // ART_RUNTIME_LOCK_WORD_H_ |