blob: fac1a7597df205b6a8bb580f9d3d83051ffc6792 [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
37 * address. When the lock word is in the "thin" state and its bits are formatted as follows:
Ian Rogersd9c4fc92013-10-01 19:45:43 -070038 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070039 * |33|2|2|222222221111|1111110000000000|
40 * |10|9|8|765432109876|5432109876543210|
41 * |00|m|r| lock count |thread id owner |
Ian Rogersd9c4fc92013-10-01 19:45:43 -070042 *
Mathieu Chartierad2541a2013-10-25 10:05:23 -070043 * When the lock word is in the "fat" state and its bits are formatted as follows:
Ian Rogersd9c4fc92013-10-01 19:45:43 -070044 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070045 * |33|2|2|2222222211111111110000000000|
46 * |10|9|8|7654321098765432109876543210|
47 * |01|m|r| MonitorId |
Mathieu Chartierad2541a2013-10-25 10:05:23 -070048 *
49 * When the lock word is in hash state and its bits are formatted as follows:
50 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070051 * |33|2|2|2222222211111111110000000000|
52 * |10|9|8|7654321098765432109876543210|
53 * |10|m|r| HashCode |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080054 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070055 * When the lock word is in forwarding address state and its bits are formatted as follows:
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080056 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070057 * |33|2|22222222211111111110000000000|
58 * |10|9|87654321098765432109876543210|
59 * |11|0| ForwardingAddress |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080060 *
Roland Levillainba650a42017-03-06 13:52:32 +000061 * The `r` bit stores the read barrier state.
62 * The `m` bit stores the mark state.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070063 */
64class LockWord {
65 public:
Mathieu Chartier1cf194f2016-11-01 20:13:24 -070066 enum SizeShiftsAndMasks : uint32_t { // private marker to avoid generate-operator-out.py from processing.
Mathieu Chartierad2541a2013-10-25 10:05:23 -070067 // Number of bits to encode the state, currently just fat or thin/unlocked or hash code.
68 kStateSize = 2,
Mathieu Chartier36a270a2016-07-28 18:08:51 -070069 kReadBarrierStateSize = 1,
70 kMarkBitStateSize = 1,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070071 // Number of bits to encode the thin lock owner.
72 kThinLockOwnerSize = 16,
73 // Remaining bits are the recursive lock count.
Mathieu Chartier36a270a2016-07-28 18:08:51 -070074 kThinLockCountSize = 32 - kThinLockOwnerSize - kStateSize - kReadBarrierStateSize -
75 kMarkBitStateSize,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070076 // Thin lock bits. Owner in lowest bits.
Mathieu Chartierad2541a2013-10-25 10:05:23 -070077
Ian Rogersd9c4fc92013-10-01 19:45:43 -070078 kThinLockOwnerShift = 0,
79 kThinLockOwnerMask = (1 << kThinLockOwnerSize) - 1,
nikolay serdjukd8481cc2014-07-28 17:40:16 +070080 kThinLockMaxOwner = kThinLockOwnerMask,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070081 // Count in higher bits.
82 kThinLockCountShift = kThinLockOwnerSize + kThinLockOwnerShift,
Dmitry Petrochenko8d82de52014-07-28 17:40:16 +070083 kThinLockCountMask = (1 << kThinLockCountSize) - 1,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070084 kThinLockMaxCount = kThinLockCountMask,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080085 kThinLockCountOne = 1 << kThinLockCountShift, // == 65536 (0x10000)
Ian Rogersd9c4fc92013-10-01 19:45:43 -070086
87 // State in the highest bits.
Mathieu Chartier36a270a2016-07-28 18:08:51 -070088 kStateShift = kReadBarrierStateSize + kThinLockCountSize + kThinLockCountShift +
89 kMarkBitStateSize,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070090 kStateMask = (1 << kStateSize) - 1,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080091 kStateMaskShifted = kStateMask << kStateShift,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070092 kStateThinOrUnlocked = 0,
93 kStateFat = 1,
Mathieu Chartierad2541a2013-10-25 10:05:23 -070094 kStateHash = 2,
Mathieu Chartier590fee92013-09-13 13:46:47 -070095 kStateForwardingAddress = 3,
Mathieu Chartier1cf194f2016-11-01 20:13:24 -070096 kStateForwardingAddressShifted = kStateForwardingAddress << kStateShift,
97 kStateForwardingAddressOverflow = (1 + kStateMask - kStateForwardingAddress) << kStateShift,
Mathieu Chartier36a270a2016-07-28 18:08:51 -070098
99 // Read barrier bit.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800100 kReadBarrierStateShift = kThinLockCountSize + kThinLockCountShift,
101 kReadBarrierStateMask = (1 << kReadBarrierStateSize) - 1,
102 kReadBarrierStateMaskShifted = kReadBarrierStateMask << kReadBarrierStateShift,
103 kReadBarrierStateMaskShiftedToggled = ~kReadBarrierStateMaskShifted,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700104
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700105 // Mark bit.
106 kMarkBitStateShift = kReadBarrierStateSize + kReadBarrierStateShift,
107 kMarkBitStateMask = (1 << kMarkBitStateSize) - 1,
108 kMarkBitStateMaskShifted = kMarkBitStateMask << kMarkBitStateShift,
109 kMarkBitStateMaskShiftedToggled = ~kMarkBitStateMaskShifted,
110
111 // GC state is mark bit and read barrier state.
112 kGCStateSize = kReadBarrierStateSize + kMarkBitStateSize,
113 kGCStateShift = kReadBarrierStateShift,
114 kGCStateMaskShifted = kReadBarrierStateMaskShifted | kMarkBitStateMaskShifted,
115 kGCStateMaskShiftedToggled = ~kGCStateMaskShifted,
116
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700117 // When the state is kHashCode, the non-state bits hold the hashcode.
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700118 // Note Object.hashCode() has the hash code layout hardcoded.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700119 kHashShift = 0,
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700120 kHashSize = 32 - kStateSize - kReadBarrierStateSize - kMarkBitStateSize,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700121 kHashMask = (1 << kHashSize) - 1,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700122 kMaxHash = kHashMask,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800123
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700124 // Forwarding address shift.
125 kForwardingAddressShift = kObjectAlignmentShift,
126
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800127 kMonitorIdShift = kHashShift,
128 kMonitorIdSize = kHashSize,
129 kMonitorIdMask = kHashMask,
130 kMonitorIdAlignmentShift = 32 - kMonitorIdSize,
131 kMonitorIdAlignment = 1 << kMonitorIdAlignmentShift,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700132 kMaxMonitorId = kMaxHash
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700133 };
134
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700135 static LockWord FromThinLockId(uint32_t thread_id, uint32_t count, uint32_t gc_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700136 CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockMaxOwner));
137 CHECK_LE(count, static_cast<uint32_t>(kThinLockMaxCount));
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700138 // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U);
139 return LockWord((thread_id << kThinLockOwnerShift) |
140 (count << kThinLockCountShift) |
141 (gc_state << kGCStateShift) |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800142 (kStateThinOrUnlocked << kStateShift));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700143 }
144
Mathieu Chartier590fee92013-09-13 13:46:47 -0700145 static LockWord FromForwardingAddress(size_t target) {
Roland Levillain14d90572015-07-16 10:52:26 +0100146 DCHECK_ALIGNED(target, (1 << kStateSize));
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700147 return LockWord((target >> kForwardingAddressShift) | kStateForwardingAddressShifted);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700148 }
149
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700150 static LockWord FromHashCode(uint32_t hash_code, uint32_t gc_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700151 CHECK_LE(hash_code, static_cast<uint32_t>(kMaxHash));
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700152 // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800153 return LockWord((hash_code << kHashShift) |
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700154 (gc_state << kGCStateShift) |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800155 (kStateHash << kStateShift));
156 }
157
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700158 static LockWord FromDefault(uint32_t gc_state) {
159 return LockWord(gc_state << kGCStateShift);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800160 }
161
162 static bool IsDefault(LockWord lw) {
163 return LockWord().GetValue() == lw.GetValue();
164 }
165
166 static LockWord Default() {
167 return LockWord();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700168 }
169
170 enum LockState {
171 kUnlocked, // No lock owners.
172 kThinLocked, // Single uncontended owner.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700173 kFatLocked, // See associated monitor.
174 kHashCode, // Lock word contains an identity hash.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700175 kForwardingAddress, // Lock word contains the forwarding address of an object.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700176 };
177
178 LockState GetState() const {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800179 CheckReadBarrierState();
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700180 if ((!kUseReadBarrier && UNLIKELY(value_ == 0)) ||
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700181 (kUseReadBarrier && UNLIKELY((value_ & kGCStateMaskShiftedToggled) == 0))) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700182 return kUnlocked;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700183 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700184 uint32_t internal_state = (value_ >> kStateShift) & kStateMask;
185 switch (internal_state) {
186 case kStateThinOrUnlocked:
187 return kThinLocked;
188 case kStateHash:
189 return kHashCode;
190 case kStateForwardingAddress:
191 return kForwardingAddress;
192 default:
193 DCHECK_EQ(internal_state, static_cast<uint32_t>(kStateFat));
194 return kFatLocked;
195 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700196 }
197 }
198
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800199 uint32_t ReadBarrierState() const {
200 return (value_ >> kReadBarrierStateShift) & kReadBarrierStateMask;
201 }
202
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700203 uint32_t GCState() const {
204 return (value_ & kGCStateMaskShifted) >> kGCStateShift;
205 }
206
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700207 void SetReadBarrierState(uint32_t rb_state) {
208 DCHECK_EQ(rb_state & ~kReadBarrierStateMask, 0U);
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700209 DCHECK(rb_state == ReadBarrier::WhiteState() ||
210 rb_state == ReadBarrier::GrayState()) << rb_state;
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700211 DCHECK_NE(static_cast<uint32_t>(GetState()), static_cast<uint32_t>(kForwardingAddress));
212 // Clear and or the bits.
213 value_ &= ~(kReadBarrierStateMask << kReadBarrierStateShift);
214 value_ |= (rb_state & kReadBarrierStateMask) << kReadBarrierStateShift;
215 }
216
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700217
218 uint32_t MarkBitState() const {
219 return (value_ >> kMarkBitStateShift) & kMarkBitStateMask;
220 }
221
222 void SetMarkBitState(uint32_t mark_bit) {
223 DCHECK_EQ(mark_bit & ~kMarkBitStateMask, 0U);
224 DCHECK_NE(static_cast<uint32_t>(GetState()), static_cast<uint32_t>(kForwardingAddress));
225 // Clear and or the bits.
226 value_ &= kMarkBitStateMaskShiftedToggled;
227 value_ |= mark_bit << kMarkBitStateShift;
228 }
229
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700230 // Return the owner thin lock thread id.
231 uint32_t ThinLockOwner() const;
232
233 // Return the number of times a lock value has been locked.
234 uint32_t ThinLockCount() const;
235
236 // Return the Monitor encoded in a fat lock.
237 Monitor* FatLockMonitor() const;
238
Mathieu Chartier590fee92013-09-13 13:46:47 -0700239 // Return the forwarding address stored in the monitor.
240 size_t ForwardingAddress() const;
241
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700242 // Constructor a lock word for inflation to use a Monitor.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700243 LockWord(Monitor* mon, uint32_t gc_state);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700244
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700245 // Return the hash code stored in the lock word, must be kHashCode state.
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700246 int32_t GetHashCode() const;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700247
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800248 template <bool kIncludeReadBarrierState>
249 static bool Equal(LockWord lw1, LockWord lw2) {
250 if (kIncludeReadBarrierState) {
251 return lw1.GetValue() == lw2.GetValue();
252 }
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700253 return lw1.GetValueWithoutGCState() == lw2.GetValueWithoutGCState();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700254 }
255
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700256 void Dump(std::ostream& os) {
257 os << "LockWord:" << std::hex << value_;
258 }
259
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700260 private:
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800261 // Default constructor with no lock ownership.
262 LockWord();
263
264 explicit LockWord(uint32_t val) : value_(val) {
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700265 // Make sure adding the overflow causes an overflow.
266 constexpr uint64_t overflow = static_cast<uint64_t>(kStateForwardingAddressShifted) +
267 static_cast<uint64_t>(kStateForwardingAddressOverflow);
268 constexpr bool is_larger = overflow > static_cast<uint64_t>(0xFFFFFFFF);
269 static_assert(is_larger, "should have overflowed");
Mathieu Chartier6f198e32016-11-03 11:15:04 -0700270 static_assert(
271 (~kStateForwardingAddress & kStateMask) == 0,
272 "READ_BARRIER_MARK_REG relies on the forwarding address state being only one bits");
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800273 CheckReadBarrierState();
274 }
275
276 // Disallow this in favor of explicit Equal() with the
277 // kIncludeReadBarrierState param to make clients be aware of the
278 // read barrier state.
279 bool operator==(const LockWord& rhs) = delete;
280
281 void CheckReadBarrierState() const {
282 if (kIsDebugBuild && ((value_ >> kStateShift) & kStateMask) != kStateForwardingAddress) {
283 uint32_t rb_state = ReadBarrierState();
284 if (!kUseReadBarrier) {
285 DCHECK_EQ(rb_state, 0U);
286 } else {
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700287 DCHECK(rb_state == ReadBarrier::WhiteState() ||
288 rb_state == ReadBarrier::GrayState()) << rb_state;
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800289 }
290 }
291 }
292
293 // Note GetValue() includes the read barrier bits and comparing (==)
294 // GetValue() between two lock words to compare the lock states may
295 // not work. Prefer Equal() or GetValueWithoutReadBarrierState().
296 uint32_t GetValue() const {
297 CheckReadBarrierState();
298 return value_;
299 }
300
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700301 uint32_t GetValueWithoutGCState() const {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800302 CheckReadBarrierState();
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700303 return value_ & kGCStateMaskShiftedToggled;
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800304 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700305
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700306 // Only Object should be converting LockWords to/from uints.
307 friend class mirror::Object;
308
309 // The encoded value holding all the state.
310 uint32_t value_;
311};
312std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code);
313
314} // namespace art
315
316
317#endif // ART_RUNTIME_LOCK_WORD_H_