2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ART_RUNTIME_LOCK_WORD_H_
18 #define ART_RUNTIME_LOCK_WORD_H_
23 #include "base/bit_utils.h"
24 #include "base/logging.h"
25 #include "read_barrier.h"
34 /* The lock value itself as stored in mirror::Object::monitor_. The two most significant bits of
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:
38 * |33|2|2|222222221111|1111110000000000|
39 * |10|9|8|765432109876|5432109876543210|
40 * |00|m|r| lock count |thread id owner |
42 * When the lock word is in the "fat" state and its bits are formatted as follows:
44 * |33|2|2|2222222211111111110000000000|
45 * |10|9|8|7654321098765432109876543210|
46 * |01|m|r| MonitorId |
48 * When the lock word is in hash state and its bits are formatted as follows:
50 * |33|2|2|2222222211111111110000000000|
51 * |10|9|8|7654321098765432109876543210|
54 * When the lock word is in forwarding address state and its bits are formatted as follows:
56 * |33|2|22222222211111111110000000000|
57 * |10|9|87654321098765432109876543210|
58 * |11|0| ForwardingAddress |
60 * The `r` bit stores the read barrier state.
61 * The `m` bit stores the mark state.
65 enum SizeShiftsAndMasks : uint32_t { // private marker to avoid generate-operator-out.py from processing.
66 // Number of bits to encode the state, currently just fat or thin/unlocked or hash code.
68 kReadBarrierStateSize = 1,
69 kMarkBitStateSize = 1,
70 // Number of bits to encode the thin lock owner.
71 kThinLockOwnerSize = 16,
72 // Remaining bits are the recursive lock count.
73 kThinLockCountSize = 32 - kThinLockOwnerSize - kStateSize - kReadBarrierStateSize -
75 // Thin lock bits. Owner in lowest bits.
77 kThinLockOwnerShift = 0,
78 kThinLockOwnerMask = (1 << kThinLockOwnerSize) - 1,
79 kThinLockMaxOwner = kThinLockOwnerMask,
80 // Count in higher bits.
81 kThinLockCountShift = kThinLockOwnerSize + kThinLockOwnerShift,
82 kThinLockCountMask = (1 << kThinLockCountSize) - 1,
83 kThinLockMaxCount = kThinLockCountMask,
84 kThinLockCountOne = 1 << kThinLockCountShift, // == 65536 (0x10000)
86 // State in the highest bits.
87 kStateShift = kReadBarrierStateSize + kThinLockCountSize + kThinLockCountShift +
89 kStateMask = (1 << kStateSize) - 1,
90 kStateMaskShifted = kStateMask << kStateShift,
91 kStateThinOrUnlocked = 0,
94 kStateForwardingAddress = 3,
95 kStateForwardingAddressShifted = kStateForwardingAddress << kStateShift,
96 kStateForwardingAddressOverflow = (1 + kStateMask - kStateForwardingAddress) << kStateShift,
99 kReadBarrierStateShift = kThinLockCountSize + kThinLockCountShift,
100 kReadBarrierStateMask = (1 << kReadBarrierStateSize) - 1,
101 kReadBarrierStateMaskShifted = kReadBarrierStateMask << kReadBarrierStateShift,
102 kReadBarrierStateMaskShiftedToggled = ~kReadBarrierStateMaskShifted,
105 kMarkBitStateShift = kReadBarrierStateSize + kReadBarrierStateShift,
106 kMarkBitStateMask = (1 << kMarkBitStateSize) - 1,
107 kMarkBitStateMaskShifted = kMarkBitStateMask << kMarkBitStateShift,
108 kMarkBitStateMaskShiftedToggled = ~kMarkBitStateMaskShifted,
110 // GC state is mark bit and read barrier state.
111 kGCStateSize = kReadBarrierStateSize + kMarkBitStateSize,
112 kGCStateShift = kReadBarrierStateShift,
113 kGCStateMaskShifted = kReadBarrierStateMaskShifted | kMarkBitStateMaskShifted,
114 kGCStateMaskShiftedToggled = ~kGCStateMaskShifted,
116 // When the state is kHashCode, the non-state bits hold the hashcode.
117 // Note Object.hashCode() has the hash code layout hardcoded.
119 kHashSize = 32 - kStateSize - kReadBarrierStateSize - kMarkBitStateSize,
120 kHashMask = (1 << kHashSize) - 1,
121 kMaxHash = kHashMask,
123 // Forwarding address shift.
124 kForwardingAddressShift = kObjectAlignmentShift,
126 kMonitorIdShift = kHashShift,
127 kMonitorIdSize = kHashSize,
128 kMonitorIdMask = kHashMask,
129 kMonitorIdAlignmentShift = 32 - kMonitorIdSize,
130 kMonitorIdAlignment = 1 << kMonitorIdAlignmentShift,
131 kMaxMonitorId = kMaxHash
134 static LockWord FromThinLockId(uint32_t thread_id, uint32_t count, uint32_t gc_state) {
135 CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockMaxOwner));
136 CHECK_LE(count, static_cast<uint32_t>(kThinLockMaxCount));
137 // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U);
138 return LockWord((thread_id << kThinLockOwnerShift) |
139 (count << kThinLockCountShift) |
140 (gc_state << kGCStateShift) |
141 (kStateThinOrUnlocked << kStateShift));
144 static LockWord FromForwardingAddress(size_t target) {
145 DCHECK_ALIGNED(target, (1 << kStateSize));
146 return LockWord((target >> kForwardingAddressShift) | kStateForwardingAddressShifted);
149 static LockWord FromHashCode(uint32_t hash_code, uint32_t gc_state) {
150 CHECK_LE(hash_code, static_cast<uint32_t>(kMaxHash));
151 // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U);
152 return LockWord((hash_code << kHashShift) |
153 (gc_state << kGCStateShift) |
154 (kStateHash << kStateShift));
157 static LockWord FromDefault(uint32_t gc_state) {
158 return LockWord(gc_state << kGCStateShift);
161 static bool IsDefault(LockWord lw) {
162 return LockWord().GetValue() == lw.GetValue();
165 static LockWord Default() {
170 kUnlocked, // No lock owners.
171 kThinLocked, // Single uncontended owner.
172 kFatLocked, // See associated monitor.
173 kHashCode, // Lock word contains an identity hash.
174 kForwardingAddress, // Lock word contains the forwarding address of an object.
177 LockState GetState() const {
178 CheckReadBarrierState();
179 if ((!kUseReadBarrier && UNLIKELY(value_ == 0)) ||
180 (kUseReadBarrier && UNLIKELY((value_ & kGCStateMaskShiftedToggled) == 0))) {
183 uint32_t internal_state = (value_ >> kStateShift) & kStateMask;
184 switch (internal_state) {
185 case kStateThinOrUnlocked:
189 case kStateForwardingAddress:
190 return kForwardingAddress;
192 DCHECK_EQ(internal_state, static_cast<uint32_t>(kStateFat));
198 uint32_t ReadBarrierState() const {
199 return (value_ >> kReadBarrierStateShift) & kReadBarrierStateMask;
202 uint32_t GCState() const {
203 return (value_ & kGCStateMaskShifted) >> kGCStateShift;
206 void SetReadBarrierState(uint32_t rb_state) {
207 DCHECK_EQ(rb_state & ~kReadBarrierStateMask, 0U);
208 DCHECK(rb_state == ReadBarrier::WhiteState() ||
209 rb_state == ReadBarrier::GrayState()) << rb_state;
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;
217 uint32_t MarkBitState() const {
218 return (value_ >> kMarkBitStateShift) & kMarkBitStateMask;
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;
229 // Return the owner thin lock thread id.
230 uint32_t ThinLockOwner() const;
232 // Return the number of times a lock value has been locked.
233 uint32_t ThinLockCount() const;
235 // Return the Monitor encoded in a fat lock.
236 Monitor* FatLockMonitor() const;
238 // Return the forwarding address stored in the monitor.
239 size_t ForwardingAddress() const;
241 // Constructor a lock word for inflation to use a Monitor.
242 LockWord(Monitor* mon, uint32_t gc_state);
244 // Return the hash code stored in the lock word, must be kHashCode state.
245 int32_t GetHashCode() const;
247 template <bool kIncludeReadBarrierState>
248 static bool Equal(LockWord lw1, LockWord lw2) {
249 if (kIncludeReadBarrierState) {
250 return lw1.GetValue() == lw2.GetValue();
252 return lw1.GetValueWithoutGCState() == lw2.GetValueWithoutGCState();
255 void Dump(std::ostream& os) {
256 os << "LockWord:" << std::hex << value_;
260 // Default constructor with no lock ownership.
263 explicit LockWord(uint32_t val) : value_(val) {
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");
270 (~kStateForwardingAddress & kStateMask) == 0,
271 "READ_BARRIER_MARK_REG relies on the forwarding address state being only one bits");
272 CheckReadBarrierState();
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;
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);
286 DCHECK(rb_state == ReadBarrier::WhiteState() ||
287 rb_state == ReadBarrier::GrayState()) << rb_state;
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();
300 uint32_t GetValueWithoutGCState() const {
301 CheckReadBarrierState();
302 return value_ & kGCStateMaskShiftedToggled;
305 // Only Object should be converting LockWords to/from uints.
306 friend class mirror::Object;
308 // The encoded value holding all the state.
311 std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code);
316 #endif // ART_RUNTIME_LOCK_WORD_H_