blob: 30559a0062e9bb013d57401e9a71839bc5fa4c89 [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
Daniel Colascioneb16949a2018-04-15 11:21:02 -070035/* 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 Levillain2ae376f2018-01-30 11:35:11 +000038 *
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 *
Hans Boehm65c18a22020-01-03 23:37:13 +000045 * The lock count is zero, but the owner is nonzero for a simply held lock.
Mathieu Chartierad2541a2013-10-25 10:05:23 -070046 * When the lock word is in the "fat" state and its bits are formatted as follows:
Ian Rogersd9c4fc92013-10-01 19:45:43 -070047 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070048 * |33|2|2|2222222211111111110000000000|
49 * |10|9|8|7654321098765432109876543210|
50 * |01|m|r| MonitorId |
Mathieu Chartierad2541a2013-10-25 10:05:23 -070051 *
52 * When the lock word is in hash state and its bits are formatted as follows:
53 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070054 * |33|2|2|2222222211111111110000000000|
55 * |10|9|8|7654321098765432109876543210|
56 * |10|m|r| HashCode |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080057 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070058 * When the lock word is in forwarding address state and its bits are formatted as follows:
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080059 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070060 * |33|2|22222222211111111110000000000|
61 * |10|9|87654321098765432109876543210|
62 * |11|0| ForwardingAddress |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080063 *
Roland Levillainba650a42017-03-06 13:52:32 +000064 * The `r` bit stores the read barrier state.
Roland Levillain2ae376f2018-01-30 11:35:11 +000065 * The `m` bit stores the mark bit state.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070066 */
67class LockWord {
68 public:
Mathieu Chartier1cf194f2016-11-01 20:13:24 -070069 enum SizeShiftsAndMasks : uint32_t { // private marker to avoid generate-operator-out.py from processing.
Mathieu Chartierad2541a2013-10-25 10:05:23 -070070 // Number of bits to encode the state, currently just fat or thin/unlocked or hash code.
71 kStateSize = 2,
Mathieu Chartier36a270a2016-07-28 18:08:51 -070072 kReadBarrierStateSize = 1,
73 kMarkBitStateSize = 1,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070074 // Number of bits to encode the thin lock owner.
75 kThinLockOwnerSize = 16,
Hans Boehm65c18a22020-01-03 23:37:13 +000076 // Remaining bits are the recursive lock count. Zero means it is locked exactly once
77 // and not recursively.
Mathieu Chartier36a270a2016-07-28 18:08:51 -070078 kThinLockCountSize = 32 - kThinLockOwnerSize - kStateSize - kReadBarrierStateSize -
79 kMarkBitStateSize,
Mathieu Chartierad2541a2013-10-25 10:05:23 -070080
Vladimir Markoc8b1d5e2018-05-15 16:07:12 +010081 // Thin lock bits. Owner in lowest bits.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070082 kThinLockOwnerShift = 0,
83 kThinLockOwnerMask = (1 << kThinLockOwnerSize) - 1,
Vladimir Markoc8b1d5e2018-05-15 16:07:12 +010084 kThinLockOwnerMaskShifted = kThinLockOwnerMask << kThinLockOwnerShift,
nikolay serdjukd8481cc2014-07-28 17:40:16 +070085 kThinLockMaxOwner = kThinLockOwnerMask,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070086 // Count in higher bits.
87 kThinLockCountShift = kThinLockOwnerSize + kThinLockOwnerShift,
Dmitry Petrochenko8d82de52014-07-28 17:40:16 +070088 kThinLockCountMask = (1 << kThinLockCountSize) - 1,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070089 kThinLockMaxCount = kThinLockCountMask,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080090 kThinLockCountOne = 1 << kThinLockCountShift, // == 65536 (0x10000)
Vladimir Markoc8b1d5e2018-05-15 16:07:12 +010091 kThinLockCountMaskShifted = kThinLockCountMask << kThinLockCountShift,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070092
93 // State in the highest bits.
Mathieu Chartier36a270a2016-07-28 18:08:51 -070094 kStateShift = kReadBarrierStateSize + kThinLockCountSize + kThinLockCountShift +
95 kMarkBitStateSize,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070096 kStateMask = (1 << kStateSize) - 1,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080097 kStateMaskShifted = kStateMask << kStateShift,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070098 kStateThinOrUnlocked = 0,
99 kStateFat = 1,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700100 kStateHash = 2,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700101 kStateForwardingAddress = 3,
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700102 kStateForwardingAddressShifted = kStateForwardingAddress << kStateShift,
103 kStateForwardingAddressOverflow = (1 + kStateMask - kStateForwardingAddress) << kStateShift,
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700104
105 // Read barrier bit.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800106 kReadBarrierStateShift = kThinLockCountSize + kThinLockCountShift,
107 kReadBarrierStateMask = (1 << kReadBarrierStateSize) - 1,
108 kReadBarrierStateMaskShifted = kReadBarrierStateMask << kReadBarrierStateShift,
109 kReadBarrierStateMaskShiftedToggled = ~kReadBarrierStateMaskShifted,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700110
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700111 // Mark bit.
112 kMarkBitStateShift = kReadBarrierStateSize + kReadBarrierStateShift,
113 kMarkBitStateMask = (1 << kMarkBitStateSize) - 1,
114 kMarkBitStateMaskShifted = kMarkBitStateMask << kMarkBitStateShift,
115 kMarkBitStateMaskShiftedToggled = ~kMarkBitStateMaskShifted,
116
117 // GC state is mark bit and read barrier state.
118 kGCStateSize = kReadBarrierStateSize + kMarkBitStateSize,
119 kGCStateShift = kReadBarrierStateShift,
120 kGCStateMaskShifted = kReadBarrierStateMaskShifted | kMarkBitStateMaskShifted,
121 kGCStateMaskShiftedToggled = ~kGCStateMaskShifted,
122
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700123 // When the state is kHashCode, the non-state bits hold the hashcode.
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700124 // Note Object.hashCode() has the hash code layout hardcoded.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700125 kHashShift = 0,
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700126 kHashSize = 32 - kStateSize - kReadBarrierStateSize - kMarkBitStateSize,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700127 kHashMask = (1 << kHashSize) - 1,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700128 kMaxHash = kHashMask,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800129
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700130 // Forwarding address shift.
131 kForwardingAddressShift = kObjectAlignmentShift,
132
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800133 kMonitorIdShift = kHashShift,
134 kMonitorIdSize = kHashSize,
135 kMonitorIdMask = kHashMask,
136 kMonitorIdAlignmentShift = 32 - kMonitorIdSize,
137 kMonitorIdAlignment = 1 << kMonitorIdAlignmentShift,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700138 kMaxMonitorId = kMaxHash
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700139 };
140
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700141 static LockWord FromThinLockId(uint32_t thread_id, uint32_t count, uint32_t gc_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700142 CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockMaxOwner));
143 CHECK_LE(count, static_cast<uint32_t>(kThinLockMaxCount));
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700144 // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U);
145 return LockWord((thread_id << kThinLockOwnerShift) |
146 (count << kThinLockCountShift) |
147 (gc_state << kGCStateShift) |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800148 (kStateThinOrUnlocked << kStateShift));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700149 }
150
Mathieu Chartier590fee92013-09-13 13:46:47 -0700151 static LockWord FromForwardingAddress(size_t target) {
Roland Levillain14d90572015-07-16 10:52:26 +0100152 DCHECK_ALIGNED(target, (1 << kStateSize));
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700153 return LockWord((target >> kForwardingAddressShift) | kStateForwardingAddressShifted);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700154 }
155
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700156 static LockWord FromHashCode(uint32_t hash_code, uint32_t gc_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700157 CHECK_LE(hash_code, static_cast<uint32_t>(kMaxHash));
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700158 // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800159 return LockWord((hash_code << kHashShift) |
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700160 (gc_state << kGCStateShift) |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800161 (kStateHash << kStateShift));
162 }
163
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700164 static LockWord FromDefault(uint32_t gc_state) {
165 return LockWord(gc_state << kGCStateShift);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800166 }
167
168 static bool IsDefault(LockWord lw) {
169 return LockWord().GetValue() == lw.GetValue();
170 }
171
172 static LockWord Default() {
173 return LockWord();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700174 }
175
176 enum LockState {
177 kUnlocked, // No lock owners.
178 kThinLocked, // Single uncontended owner.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700179 kFatLocked, // See associated monitor.
180 kHashCode, // Lock word contains an identity hash.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700181 kForwardingAddress, // Lock word contains the forwarding address of an object.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700182 };
183
184 LockState GetState() const {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800185 CheckReadBarrierState();
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700186 if ((!kUseReadBarrier && UNLIKELY(value_ == 0)) ||
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700187 (kUseReadBarrier && UNLIKELY((value_ & kGCStateMaskShiftedToggled) == 0))) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700188 return kUnlocked;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700189 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700190 uint32_t internal_state = (value_ >> kStateShift) & kStateMask;
191 switch (internal_state) {
192 case kStateThinOrUnlocked:
193 return kThinLocked;
194 case kStateHash:
195 return kHashCode;
196 case kStateForwardingAddress:
197 return kForwardingAddress;
198 default:
199 DCHECK_EQ(internal_state, static_cast<uint32_t>(kStateFat));
200 return kFatLocked;
201 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700202 }
203 }
204
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800205 uint32_t ReadBarrierState() const {
206 return (value_ >> kReadBarrierStateShift) & kReadBarrierStateMask;
207 }
208
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700209 uint32_t GCState() const {
210 return (value_ & kGCStateMaskShifted) >> kGCStateShift;
211 }
212
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700213 void SetReadBarrierState(uint32_t rb_state) {
214 DCHECK_EQ(rb_state & ~kReadBarrierStateMask, 0U);
Roland Levillain14e5a292018-06-28 12:00:56 +0100215 DCHECK(rb_state == ReadBarrier::NonGrayState() ||
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700216 rb_state == ReadBarrier::GrayState()) << rb_state;
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700217 DCHECK_NE(static_cast<uint32_t>(GetState()), static_cast<uint32_t>(kForwardingAddress));
218 // Clear and or the bits.
219 value_ &= ~(kReadBarrierStateMask << kReadBarrierStateShift);
220 value_ |= (rb_state & kReadBarrierStateMask) << kReadBarrierStateShift;
221 }
222
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700223
224 uint32_t MarkBitState() const {
225 return (value_ >> kMarkBitStateShift) & kMarkBitStateMask;
226 }
227
228 void SetMarkBitState(uint32_t mark_bit) {
229 DCHECK_EQ(mark_bit & ~kMarkBitStateMask, 0U);
230 DCHECK_NE(static_cast<uint32_t>(GetState()), static_cast<uint32_t>(kForwardingAddress));
231 // Clear and or the bits.
232 value_ &= kMarkBitStateMaskShiftedToggled;
233 value_ |= mark_bit << kMarkBitStateShift;
234 }
235
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700236 // Return the owner thin lock thread id.
237 uint32_t ThinLockOwner() const;
238
Hans Boehm65c18a22020-01-03 23:37:13 +0000239 // Return the number of times a lock value has been re-locked. Only valid in thin-locked state.
240 // If the lock is held only once the return value is zero.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700241 uint32_t ThinLockCount() const;
242
243 // Return the Monitor encoded in a fat lock.
244 Monitor* FatLockMonitor() const;
245
Mathieu Chartier590fee92013-09-13 13:46:47 -0700246 // Return the forwarding address stored in the monitor.
247 size_t ForwardingAddress() const;
248
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700249 // Constructor a lock word for inflation to use a Monitor.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700250 LockWord(Monitor* mon, uint32_t gc_state);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700251
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700252 // Return the hash code stored in the lock word, must be kHashCode state.
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700253 int32_t GetHashCode() const;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700254
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800255 template <bool kIncludeReadBarrierState>
256 static bool Equal(LockWord lw1, LockWord lw2) {
257 if (kIncludeReadBarrierState) {
258 return lw1.GetValue() == lw2.GetValue();
259 }
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700260 return lw1.GetValueWithoutGCState() == lw2.GetValueWithoutGCState();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700261 }
262
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700263 void Dump(std::ostream& os) {
264 os << "LockWord:" << std::hex << value_;
265 }
266
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700267 private:
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800268 // Default constructor with no lock ownership.
269 LockWord();
270
271 explicit LockWord(uint32_t val) : value_(val) {
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700272 // Make sure adding the overflow causes an overflow.
273 constexpr uint64_t overflow = static_cast<uint64_t>(kStateForwardingAddressShifted) +
274 static_cast<uint64_t>(kStateForwardingAddressOverflow);
275 constexpr bool is_larger = overflow > static_cast<uint64_t>(0xFFFFFFFF);
276 static_assert(is_larger, "should have overflowed");
Mathieu Chartier6f198e32016-11-03 11:15:04 -0700277 static_assert(
278 (~kStateForwardingAddress & kStateMask) == 0,
279 "READ_BARRIER_MARK_REG relies on the forwarding address state being only one bits");
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800280 CheckReadBarrierState();
281 }
282
283 // Disallow this in favor of explicit Equal() with the
284 // kIncludeReadBarrierState param to make clients be aware of the
285 // read barrier state.
286 bool operator==(const LockWord& rhs) = delete;
287
288 void CheckReadBarrierState() const {
289 if (kIsDebugBuild && ((value_ >> kStateShift) & kStateMask) != kStateForwardingAddress) {
290 uint32_t rb_state = ReadBarrierState();
291 if (!kUseReadBarrier) {
292 DCHECK_EQ(rb_state, 0U);
293 } else {
Roland Levillain14e5a292018-06-28 12:00:56 +0100294 DCHECK(rb_state == ReadBarrier::NonGrayState() ||
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700295 rb_state == ReadBarrier::GrayState()) << rb_state;
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800296 }
297 }
298 }
299
300 // Note GetValue() includes the read barrier bits and comparing (==)
301 // GetValue() between two lock words to compare the lock states may
302 // not work. Prefer Equal() or GetValueWithoutReadBarrierState().
303 uint32_t GetValue() const {
304 CheckReadBarrierState();
305 return value_;
306 }
307
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700308 uint32_t GetValueWithoutGCState() const {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800309 CheckReadBarrierState();
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700310 return value_ & kGCStateMaskShiftedToggled;
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800311 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700312
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700313 // Only Object should be converting LockWords to/from uints.
314 friend class mirror::Object;
315
316 // The encoded value holding all the state.
317 uint32_t value_;
318};
319std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code);
320
321} // namespace art
322
323
324#endif // ART_RUNTIME_LOCK_WORD_H_