blob: ac7890c12f44631ff000a1892ef6fab0225bca1f [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 *
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,
Mathieu Chartierad2541a2013-10-25 10:05:23 -070078
Vladimir Markoc8b1d5e2018-05-15 16:07:12 +010079 // Thin lock bits. Owner in lowest bits.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070080 kThinLockOwnerShift = 0,
81 kThinLockOwnerMask = (1 << kThinLockOwnerSize) - 1,
Vladimir Markoc8b1d5e2018-05-15 16:07:12 +010082 kThinLockOwnerMaskShifted = kThinLockOwnerMask << kThinLockOwnerShift,
nikolay serdjukd8481cc2014-07-28 17:40:16 +070083 kThinLockMaxOwner = kThinLockOwnerMask,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070084 // Count in higher bits.
85 kThinLockCountShift = kThinLockOwnerSize + kThinLockOwnerShift,
Dmitry Petrochenko8d82de52014-07-28 17:40:16 +070086 kThinLockCountMask = (1 << kThinLockCountSize) - 1,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070087 kThinLockMaxCount = kThinLockCountMask,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080088 kThinLockCountOne = 1 << kThinLockCountShift, // == 65536 (0x10000)
Vladimir Markoc8b1d5e2018-05-15 16:07:12 +010089 kThinLockCountMaskShifted = kThinLockCountMask << kThinLockCountShift,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070090
91 // State in the highest bits.
Mathieu Chartier36a270a2016-07-28 18:08:51 -070092 kStateShift = kReadBarrierStateSize + kThinLockCountSize + kThinLockCountShift +
93 kMarkBitStateSize,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070094 kStateMask = (1 << kStateSize) - 1,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080095 kStateMaskShifted = kStateMask << kStateShift,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070096 kStateThinOrUnlocked = 0,
97 kStateFat = 1,
Mathieu Chartierad2541a2013-10-25 10:05:23 -070098 kStateHash = 2,
Mathieu Chartier590fee92013-09-13 13:46:47 -070099 kStateForwardingAddress = 3,
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700100 kStateForwardingAddressShifted = kStateForwardingAddress << kStateShift,
101 kStateForwardingAddressOverflow = (1 + kStateMask - kStateForwardingAddress) << kStateShift,
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700102
103 // Read barrier bit.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800104 kReadBarrierStateShift = kThinLockCountSize + kThinLockCountShift,
105 kReadBarrierStateMask = (1 << kReadBarrierStateSize) - 1,
106 kReadBarrierStateMaskShifted = kReadBarrierStateMask << kReadBarrierStateShift,
107 kReadBarrierStateMaskShiftedToggled = ~kReadBarrierStateMaskShifted,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700108
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700109 // 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 Chartierad2541a2013-10-25 10:05:23 -0700121 // When the state is kHashCode, the non-state bits hold the hashcode.
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700122 // Note Object.hashCode() has the hash code layout hardcoded.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700123 kHashShift = 0,
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700124 kHashSize = 32 - kStateSize - kReadBarrierStateSize - kMarkBitStateSize,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700125 kHashMask = (1 << kHashSize) - 1,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700126 kMaxHash = kHashMask,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800127
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700128 // Forwarding address shift.
129 kForwardingAddressShift = kObjectAlignmentShift,
130
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800131 kMonitorIdShift = kHashShift,
132 kMonitorIdSize = kHashSize,
133 kMonitorIdMask = kHashMask,
134 kMonitorIdAlignmentShift = 32 - kMonitorIdSize,
135 kMonitorIdAlignment = 1 << kMonitorIdAlignmentShift,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700136 kMaxMonitorId = kMaxHash
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700137 };
138
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700139 static LockWord FromThinLockId(uint32_t thread_id, uint32_t count, uint32_t gc_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700140 CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockMaxOwner));
141 CHECK_LE(count, static_cast<uint32_t>(kThinLockMaxCount));
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700142 // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U);
143 return LockWord((thread_id << kThinLockOwnerShift) |
144 (count << kThinLockCountShift) |
145 (gc_state << kGCStateShift) |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800146 (kStateThinOrUnlocked << kStateShift));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700147 }
148
Mathieu Chartier590fee92013-09-13 13:46:47 -0700149 static LockWord FromForwardingAddress(size_t target) {
Roland Levillain14d90572015-07-16 10:52:26 +0100150 DCHECK_ALIGNED(target, (1 << kStateSize));
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700151 return LockWord((target >> kForwardingAddressShift) | kStateForwardingAddressShifted);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700152 }
153
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700154 static LockWord FromHashCode(uint32_t hash_code, uint32_t gc_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700155 CHECK_LE(hash_code, static_cast<uint32_t>(kMaxHash));
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700156 // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800157 return LockWord((hash_code << kHashShift) |
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700158 (gc_state << kGCStateShift) |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800159 (kStateHash << kStateShift));
160 }
161
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700162 static LockWord FromDefault(uint32_t gc_state) {
163 return LockWord(gc_state << kGCStateShift);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800164 }
165
166 static bool IsDefault(LockWord lw) {
167 return LockWord().GetValue() == lw.GetValue();
168 }
169
170 static LockWord Default() {
171 return LockWord();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700172 }
173
174 enum LockState {
175 kUnlocked, // No lock owners.
176 kThinLocked, // Single uncontended owner.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700177 kFatLocked, // See associated monitor.
178 kHashCode, // Lock word contains an identity hash.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700179 kForwardingAddress, // Lock word contains the forwarding address of an object.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700180 };
181
182 LockState GetState() const {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800183 CheckReadBarrierState();
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700184 if ((!kUseReadBarrier && UNLIKELY(value_ == 0)) ||
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700185 (kUseReadBarrier && UNLIKELY((value_ & kGCStateMaskShiftedToggled) == 0))) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700186 return kUnlocked;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700187 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700188 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 Rogersd9c4fc92013-10-01 19:45:43 -0700200 }
201 }
202
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800203 uint32_t ReadBarrierState() const {
204 return (value_ >> kReadBarrierStateShift) & kReadBarrierStateMask;
205 }
206
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700207 uint32_t GCState() const {
208 return (value_ & kGCStateMaskShifted) >> kGCStateShift;
209 }
210
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700211 void SetReadBarrierState(uint32_t rb_state) {
212 DCHECK_EQ(rb_state & ~kReadBarrierStateMask, 0U);
Roland Levillain14e5a292018-06-28 12:00:56 +0100213 DCHECK(rb_state == ReadBarrier::NonGrayState() ||
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700214 rb_state == ReadBarrier::GrayState()) << rb_state;
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700215 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 Chartier36a270a2016-07-28 18:08:51 -0700221
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 Rogersd9c4fc92013-10-01 19:45:43 -0700234 // 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 Chartier590fee92013-09-13 13:46:47 -0700243 // Return the forwarding address stored in the monitor.
244 size_t ForwardingAddress() const;
245
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700246 // Constructor a lock word for inflation to use a Monitor.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700247 LockWord(Monitor* mon, uint32_t gc_state);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700248
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700249 // Return the hash code stored in the lock word, must be kHashCode state.
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700250 int32_t GetHashCode() const;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700251
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800252 template <bool kIncludeReadBarrierState>
253 static bool Equal(LockWord lw1, LockWord lw2) {
254 if (kIncludeReadBarrierState) {
255 return lw1.GetValue() == lw2.GetValue();
256 }
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700257 return lw1.GetValueWithoutGCState() == lw2.GetValueWithoutGCState();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700258 }
259
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700260 void Dump(std::ostream& os) {
261 os << "LockWord:" << std::hex << value_;
262 }
263
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700264 private:
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800265 // Default constructor with no lock ownership.
266 LockWord();
267
268 explicit LockWord(uint32_t val) : value_(val) {
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700269 // 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 Chartier6f198e32016-11-03 11:15:04 -0700274 static_assert(
275 (~kStateForwardingAddress & kStateMask) == 0,
276 "READ_BARRIER_MARK_REG relies on the forwarding address state being only one bits");
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800277 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 Levillain14e5a292018-06-28 12:00:56 +0100291 DCHECK(rb_state == ReadBarrier::NonGrayState() ||
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700292 rb_state == ReadBarrier::GrayState()) << rb_state;
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800293 }
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 Chartier36a270a2016-07-28 18:08:51 -0700305 uint32_t GetValueWithoutGCState() const {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800306 CheckReadBarrierState();
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700307 return value_ & kGCStateMaskShiftedToggled;
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800308 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700309
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700310 // 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};
316std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code);
317
318} // namespace art
319
320
321#endif // ART_RUNTIME_LOCK_WORD_H_