blob: e89beb6d41bc19c926b7db9b0a0ecf1f099f3110 [file] [log] [blame]
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001/*
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 Gampe8cf9cb32017-07-19 09:28:38 -070020#include <cstdint>
Ian Rogersd9c4fc92013-10-01 19:45:43 -070021#include <iosfwd>
Ian Rogersd9c4fc92013-10-01 19:45:43 -070022
Andreas Gampe57943812017-12-06 21:39:13 -080023#include <android-base/logging.h>
24
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "base/bit_utils.h"
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080026#include "read_barrier.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070027
28namespace art {
29namespace mirror {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080030class Object;
Ian Rogersd9c4fc92013-10-01 19:45:43 -070031} // namespace mirror
32
33class Monitor;
34
Mathieu Chartierad2541a2013-10-25 10:05:23 -070035/* The lock value itself as stored in mirror::Object::monitor_. The two most significant bits of
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080036 * the state. The four possible states are fat locked, thin/unlocked, hash code, and forwarding
Roland Levillain2ae376f2018-01-30 11:35:11 +000037 * address.
38 *
39 * When the lock word is in the "thin" state and its bits are formatted as follows:
Ian Rogersd9c4fc92013-10-01 19:45:43 -070040 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070041 * |33|2|2|222222221111|1111110000000000|
42 * |10|9|8|765432109876|5432109876543210|
43 * |00|m|r| lock count |thread id owner |
Ian Rogersd9c4fc92013-10-01 19:45:43 -070044 *
Mathieu Chartierad2541a2013-10-25 10:05:23 -070045 * When the lock word is in the "fat" state and its bits are formatted as follows:
Ian Rogersd9c4fc92013-10-01 19:45:43 -070046 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070047 * |33|2|2|2222222211111111110000000000|
48 * |10|9|8|7654321098765432109876543210|
49 * |01|m|r| MonitorId |
Mathieu Chartierad2541a2013-10-25 10:05:23 -070050 *
51 * When the lock word is in hash state and its bits are formatted as follows:
52 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070053 * |33|2|2|2222222211111111110000000000|
54 * |10|9|8|7654321098765432109876543210|
55 * |10|m|r| HashCode |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080056 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070057 * When the lock word is in forwarding address state and its bits are formatted as follows:
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080058 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070059 * |33|2|22222222211111111110000000000|
60 * |10|9|87654321098765432109876543210|
61 * |11|0| ForwardingAddress |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080062 *
Roland Levillainba650a42017-03-06 13:52:32 +000063 * The `r` bit stores the read barrier state.
Roland Levillain2ae376f2018-01-30 11:35:11 +000064 * The `m` bit stores the mark bit state.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070065 */
66class LockWord {
67 public:
Mathieu Chartier1cf194f2016-11-01 20:13:24 -070068 enum SizeShiftsAndMasks : uint32_t { // private marker to avoid generate-operator-out.py from processing.
Mathieu Chartierad2541a2013-10-25 10:05:23 -070069 // Number of bits to encode the state, currently just fat or thin/unlocked or hash code.
70 kStateSize = 2,
Mathieu Chartier36a270a2016-07-28 18:08:51 -070071 kReadBarrierStateSize = 1,
72 kMarkBitStateSize = 1,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070073 // Number of bits to encode the thin lock owner.
74 kThinLockOwnerSize = 16,
75 // Remaining bits are the recursive lock count.
Mathieu Chartier36a270a2016-07-28 18:08:51 -070076 kThinLockCountSize = 32 - kThinLockOwnerSize - kStateSize - kReadBarrierStateSize -
77 kMarkBitStateSize,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070078 // Thin lock bits. Owner in lowest bits.
Mathieu Chartierad2541a2013-10-25 10:05:23 -070079
Ian Rogersd9c4fc92013-10-01 19:45:43 -070080 kThinLockOwnerShift = 0,
81 kThinLockOwnerMask = (1 << kThinLockOwnerSize) - 1,
nikolay serdjukd8481cc2014-07-28 17:40:16 +070082 kThinLockMaxOwner = kThinLockOwnerMask,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070083 // Count in higher bits.
84 kThinLockCountShift = kThinLockOwnerSize + kThinLockOwnerShift,
Dmitry Petrochenko8d82de52014-07-28 17:40:16 +070085 kThinLockCountMask = (1 << kThinLockCountSize) - 1,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070086 kThinLockMaxCount = kThinLockCountMask,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080087 kThinLockCountOne = 1 << kThinLockCountShift, // == 65536 (0x10000)
Ian Rogersd9c4fc92013-10-01 19:45:43 -070088
89 // State in the highest bits.
Mathieu Chartier36a270a2016-07-28 18:08:51 -070090 kStateShift = kReadBarrierStateSize + kThinLockCountSize + kThinLockCountShift +
91 kMarkBitStateSize,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070092 kStateMask = (1 << kStateSize) - 1,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080093 kStateMaskShifted = kStateMask << kStateShift,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070094 kStateThinOrUnlocked = 0,
95 kStateFat = 1,
Mathieu Chartierad2541a2013-10-25 10:05:23 -070096 kStateHash = 2,
Mathieu Chartier590fee92013-09-13 13:46:47 -070097 kStateForwardingAddress = 3,
Mathieu Chartier1cf194f2016-11-01 20:13:24 -070098 kStateForwardingAddressShifted = kStateForwardingAddress << kStateShift,
99 kStateForwardingAddressOverflow = (1 + kStateMask - kStateForwardingAddress) << kStateShift,
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700100
101 // Read barrier bit.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800102 kReadBarrierStateShift = kThinLockCountSize + kThinLockCountShift,
103 kReadBarrierStateMask = (1 << kReadBarrierStateSize) - 1,
104 kReadBarrierStateMaskShifted = kReadBarrierStateMask << kReadBarrierStateShift,
105 kReadBarrierStateMaskShiftedToggled = ~kReadBarrierStateMaskShifted,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700106
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700107 // Mark bit.
108 kMarkBitStateShift = kReadBarrierStateSize + kReadBarrierStateShift,
109 kMarkBitStateMask = (1 << kMarkBitStateSize) - 1,
110 kMarkBitStateMaskShifted = kMarkBitStateMask << kMarkBitStateShift,
111 kMarkBitStateMaskShiftedToggled = ~kMarkBitStateMaskShifted,
112
113 // GC state is mark bit and read barrier state.
114 kGCStateSize = kReadBarrierStateSize + kMarkBitStateSize,
115 kGCStateShift = kReadBarrierStateShift,
116 kGCStateMaskShifted = kReadBarrierStateMaskShifted | kMarkBitStateMaskShifted,
117 kGCStateMaskShiftedToggled = ~kGCStateMaskShifted,
118
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700119 // When the state is kHashCode, the non-state bits hold the hashcode.
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700120 // Note Object.hashCode() has the hash code layout hardcoded.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700121 kHashShift = 0,
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700122 kHashSize = 32 - kStateSize - kReadBarrierStateSize - kMarkBitStateSize,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700123 kHashMask = (1 << kHashSize) - 1,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700124 kMaxHash = kHashMask,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800125
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700126 // Forwarding address shift.
127 kForwardingAddressShift = kObjectAlignmentShift,
128
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800129 kMonitorIdShift = kHashShift,
130 kMonitorIdSize = kHashSize,
131 kMonitorIdMask = kHashMask,
132 kMonitorIdAlignmentShift = 32 - kMonitorIdSize,
133 kMonitorIdAlignment = 1 << kMonitorIdAlignmentShift,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700134 kMaxMonitorId = kMaxHash
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700135 };
136
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700137 static LockWord FromThinLockId(uint32_t thread_id, uint32_t count, uint32_t gc_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700138 CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockMaxOwner));
139 CHECK_LE(count, static_cast<uint32_t>(kThinLockMaxCount));
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700140 // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U);
141 return LockWord((thread_id << kThinLockOwnerShift) |
142 (count << kThinLockCountShift) |
143 (gc_state << kGCStateShift) |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800144 (kStateThinOrUnlocked << kStateShift));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700145 }
146
Mathieu Chartier590fee92013-09-13 13:46:47 -0700147 static LockWord FromForwardingAddress(size_t target) {
Roland Levillain14d90572015-07-16 10:52:26 +0100148 DCHECK_ALIGNED(target, (1 << kStateSize));
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700149 return LockWord((target >> kForwardingAddressShift) | kStateForwardingAddressShifted);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700150 }
151
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700152 static LockWord FromHashCode(uint32_t hash_code, uint32_t gc_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700153 CHECK_LE(hash_code, static_cast<uint32_t>(kMaxHash));
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700154 // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800155 return LockWord((hash_code << kHashShift) |
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700156 (gc_state << kGCStateShift) |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800157 (kStateHash << kStateShift));
158 }
159
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700160 static LockWord FromDefault(uint32_t gc_state) {
161 return LockWord(gc_state << kGCStateShift);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800162 }
163
164 static bool IsDefault(LockWord lw) {
165 return LockWord().GetValue() == lw.GetValue();
166 }
167
168 static LockWord Default() {
169 return LockWord();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700170 }
171
172 enum LockState {
173 kUnlocked, // No lock owners.
174 kThinLocked, // Single uncontended owner.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700175 kFatLocked, // See associated monitor.
176 kHashCode, // Lock word contains an identity hash.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700177 kForwardingAddress, // Lock word contains the forwarding address of an object.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700178 };
179
180 LockState GetState() const {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800181 CheckReadBarrierState();
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700182 if ((!kUseReadBarrier && UNLIKELY(value_ == 0)) ||
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700183 (kUseReadBarrier && UNLIKELY((value_ & kGCStateMaskShiftedToggled) == 0))) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700184 return kUnlocked;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700185 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700186 uint32_t internal_state = (value_ >> kStateShift) & kStateMask;
187 switch (internal_state) {
188 case kStateThinOrUnlocked:
189 return kThinLocked;
190 case kStateHash:
191 return kHashCode;
192 case kStateForwardingAddress:
193 return kForwardingAddress;
194 default:
195 DCHECK_EQ(internal_state, static_cast<uint32_t>(kStateFat));
196 return kFatLocked;
197 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700198 }
199 }
200
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800201 uint32_t ReadBarrierState() const {
202 return (value_ >> kReadBarrierStateShift) & kReadBarrierStateMask;
203 }
204
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700205 uint32_t GCState() const {
206 return (value_ & kGCStateMaskShifted) >> kGCStateShift;
207 }
208
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700209 void SetReadBarrierState(uint32_t rb_state) {
210 DCHECK_EQ(rb_state & ~kReadBarrierStateMask, 0U);
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700211 DCHECK(rb_state == ReadBarrier::WhiteState() ||
212 rb_state == ReadBarrier::GrayState()) << rb_state;
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700213 DCHECK_NE(static_cast<uint32_t>(GetState()), static_cast<uint32_t>(kForwardingAddress));
214 // Clear and or the bits.
215 value_ &= ~(kReadBarrierStateMask << kReadBarrierStateShift);
216 value_ |= (rb_state & kReadBarrierStateMask) << kReadBarrierStateShift;
217 }
218
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700219
220 uint32_t MarkBitState() const {
221 return (value_ >> kMarkBitStateShift) & kMarkBitStateMask;
222 }
223
224 void SetMarkBitState(uint32_t mark_bit) {
225 DCHECK_EQ(mark_bit & ~kMarkBitStateMask, 0U);
226 DCHECK_NE(static_cast<uint32_t>(GetState()), static_cast<uint32_t>(kForwardingAddress));
227 // Clear and or the bits.
228 value_ &= kMarkBitStateMaskShiftedToggled;
229 value_ |= mark_bit << kMarkBitStateShift;
230 }
231
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700232 // Return the owner thin lock thread id.
233 uint32_t ThinLockOwner() const;
234
235 // Return the number of times a lock value has been locked.
236 uint32_t ThinLockCount() const;
237
238 // Return the Monitor encoded in a fat lock.
239 Monitor* FatLockMonitor() const;
240
Mathieu Chartier590fee92013-09-13 13:46:47 -0700241 // Return the forwarding address stored in the monitor.
242 size_t ForwardingAddress() const;
243
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700244 // Constructor a lock word for inflation to use a Monitor.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700245 LockWord(Monitor* mon, uint32_t gc_state);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700246
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700247 // Return the hash code stored in the lock word, must be kHashCode state.
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700248 int32_t GetHashCode() const;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700249
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800250 template <bool kIncludeReadBarrierState>
251 static bool Equal(LockWord lw1, LockWord lw2) {
252 if (kIncludeReadBarrierState) {
253 return lw1.GetValue() == lw2.GetValue();
254 }
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700255 return lw1.GetValueWithoutGCState() == lw2.GetValueWithoutGCState();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700256 }
257
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700258 void Dump(std::ostream& os) {
259 os << "LockWord:" << std::hex << value_;
260 }
261
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700262 private:
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800263 // Default constructor with no lock ownership.
264 LockWord();
265
266 explicit LockWord(uint32_t val) : value_(val) {
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700267 // Make sure adding the overflow causes an overflow.
268 constexpr uint64_t overflow = static_cast<uint64_t>(kStateForwardingAddressShifted) +
269 static_cast<uint64_t>(kStateForwardingAddressOverflow);
270 constexpr bool is_larger = overflow > static_cast<uint64_t>(0xFFFFFFFF);
271 static_assert(is_larger, "should have overflowed");
Mathieu Chartier6f198e32016-11-03 11:15:04 -0700272 static_assert(
273 (~kStateForwardingAddress & kStateMask) == 0,
274 "READ_BARRIER_MARK_REG relies on the forwarding address state being only one bits");
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800275 CheckReadBarrierState();
276 }
277
278 // Disallow this in favor of explicit Equal() with the
279 // kIncludeReadBarrierState param to make clients be aware of the
280 // read barrier state.
281 bool operator==(const LockWord& rhs) = delete;
282
283 void CheckReadBarrierState() const {
284 if (kIsDebugBuild && ((value_ >> kStateShift) & kStateMask) != kStateForwardingAddress) {
285 uint32_t rb_state = ReadBarrierState();
286 if (!kUseReadBarrier) {
287 DCHECK_EQ(rb_state, 0U);
288 } else {
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700289 DCHECK(rb_state == ReadBarrier::WhiteState() ||
290 rb_state == ReadBarrier::GrayState()) << rb_state;
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800291 }
292 }
293 }
294
295 // Note GetValue() includes the read barrier bits and comparing (==)
296 // GetValue() between two lock words to compare the lock states may
297 // not work. Prefer Equal() or GetValueWithoutReadBarrierState().
298 uint32_t GetValue() const {
299 CheckReadBarrierState();
300 return value_;
301 }
302
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700303 uint32_t GetValueWithoutGCState() const {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800304 CheckReadBarrierState();
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700305 return value_ & kGCStateMaskShiftedToggled;
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800306 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700307
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700308 // Only Object should be converting LockWords to/from uints.
309 friend class mirror::Object;
310
311 // The encoded value holding all the state.
312 uint32_t value_;
313};
314std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code);
315
316} // namespace art
317
318
319#endif // ART_RUNTIME_LOCK_WORD_H_