blob: 14f638ee783a72a7d0c085c252790472d38f7533 [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
Vladimir Marko80afd022015-05-19 18:08:00 +010023#include "base/bit_utils.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070024#include "base/logging.h"
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080025#include "read_barrier.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070026
27namespace art {
28namespace mirror {
29 class Object;
30} // namespace mirror
31
32class Monitor;
33
Mathieu Chartierad2541a2013-10-25 10:05:23 -070034/* The lock value itself as stored in mirror::Object::monitor_. The two most significant bits of
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080035 * 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:
Ian Rogersd9c4fc92013-10-01 19:45:43 -070037 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070038 * |33|2|2|222222221111|1111110000000000|
39 * |10|9|8|765432109876|5432109876543210|
40 * |00|m|r| lock count |thread id owner |
Ian Rogersd9c4fc92013-10-01 19:45:43 -070041 *
Mathieu Chartierad2541a2013-10-25 10:05:23 -070042 * When the lock word is in the "fat" state and its bits are formatted as follows:
Ian Rogersd9c4fc92013-10-01 19:45:43 -070043 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070044 * |33|2|2|2222222211111111110000000000|
45 * |10|9|8|7654321098765432109876543210|
46 * |01|m|r| MonitorId |
Mathieu Chartierad2541a2013-10-25 10:05:23 -070047 *
48 * When the lock word is in hash state and its bits are formatted as follows:
49 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070050 * |33|2|2|2222222211111111110000000000|
51 * |10|9|8|7654321098765432109876543210|
52 * |10|m|r| HashCode |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080053 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070054 * When the lock word is in forwarding address state and its bits are formatted as follows:
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080055 *
Mathieu Chartier36a270a2016-07-28 18:08:51 -070056 * |33|2|22222222211111111110000000000|
57 * |10|9|87654321098765432109876543210|
58 * |11|0| ForwardingAddress |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080059 *
Roland Levillainba650a42017-03-06 13:52:32 +000060 * The `r` bit stores the read barrier state.
61 * The `m` bit stores the mark state.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070062 */
63class LockWord {
64 public:
Mathieu Chartier1cf194f2016-11-01 20:13:24 -070065 enum SizeShiftsAndMasks : uint32_t { // private marker to avoid generate-operator-out.py from processing.
Mathieu Chartierad2541a2013-10-25 10:05:23 -070066 // Number of bits to encode the state, currently just fat or thin/unlocked or hash code.
67 kStateSize = 2,
Mathieu Chartier36a270a2016-07-28 18:08:51 -070068 kReadBarrierStateSize = 1,
69 kMarkBitStateSize = 1,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070070 // Number of bits to encode the thin lock owner.
71 kThinLockOwnerSize = 16,
72 // Remaining bits are the recursive lock count.
Mathieu Chartier36a270a2016-07-28 18:08:51 -070073 kThinLockCountSize = 32 - kThinLockOwnerSize - kStateSize - kReadBarrierStateSize -
74 kMarkBitStateSize,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070075 // Thin lock bits. Owner in lowest bits.
Mathieu Chartierad2541a2013-10-25 10:05:23 -070076
Ian Rogersd9c4fc92013-10-01 19:45:43 -070077 kThinLockOwnerShift = 0,
78 kThinLockOwnerMask = (1 << kThinLockOwnerSize) - 1,
nikolay serdjukd8481cc2014-07-28 17:40:16 +070079 kThinLockMaxOwner = kThinLockOwnerMask,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070080 // Count in higher bits.
81 kThinLockCountShift = kThinLockOwnerSize + kThinLockOwnerShift,
Dmitry Petrochenko8d82de52014-07-28 17:40:16 +070082 kThinLockCountMask = (1 << kThinLockCountSize) - 1,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070083 kThinLockMaxCount = kThinLockCountMask,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080084 kThinLockCountOne = 1 << kThinLockCountShift, // == 65536 (0x10000)
Ian Rogersd9c4fc92013-10-01 19:45:43 -070085
86 // State in the highest bits.
Mathieu Chartier36a270a2016-07-28 18:08:51 -070087 kStateShift = kReadBarrierStateSize + kThinLockCountSize + kThinLockCountShift +
88 kMarkBitStateSize,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070089 kStateMask = (1 << kStateSize) - 1,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080090 kStateMaskShifted = kStateMask << kStateShift,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070091 kStateThinOrUnlocked = 0,
92 kStateFat = 1,
Mathieu Chartierad2541a2013-10-25 10:05:23 -070093 kStateHash = 2,
Mathieu Chartier590fee92013-09-13 13:46:47 -070094 kStateForwardingAddress = 3,
Mathieu Chartier1cf194f2016-11-01 20:13:24 -070095 kStateForwardingAddressShifted = kStateForwardingAddress << kStateShift,
96 kStateForwardingAddressOverflow = (1 + kStateMask - kStateForwardingAddress) << kStateShift,
Mathieu Chartier36a270a2016-07-28 18:08:51 -070097
98 // Read barrier bit.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080099 kReadBarrierStateShift = kThinLockCountSize + kThinLockCountShift,
100 kReadBarrierStateMask = (1 << kReadBarrierStateSize) - 1,
101 kReadBarrierStateMaskShifted = kReadBarrierStateMask << kReadBarrierStateShift,
102 kReadBarrierStateMaskShiftedToggled = ~kReadBarrierStateMaskShifted,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700103
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700104 // Mark bit.
105 kMarkBitStateShift = kReadBarrierStateSize + kReadBarrierStateShift,
106 kMarkBitStateMask = (1 << kMarkBitStateSize) - 1,
107 kMarkBitStateMaskShifted = kMarkBitStateMask << kMarkBitStateShift,
108 kMarkBitStateMaskShiftedToggled = ~kMarkBitStateMaskShifted,
109
110 // GC state is mark bit and read barrier state.
111 kGCStateSize = kReadBarrierStateSize + kMarkBitStateSize,
112 kGCStateShift = kReadBarrierStateShift,
113 kGCStateMaskShifted = kReadBarrierStateMaskShifted | kMarkBitStateMaskShifted,
114 kGCStateMaskShiftedToggled = ~kGCStateMaskShifted,
115
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700116 // When the state is kHashCode, the non-state bits hold the hashcode.
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700117 // Note Object.hashCode() has the hash code layout hardcoded.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700118 kHashShift = 0,
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700119 kHashSize = 32 - kStateSize - kReadBarrierStateSize - kMarkBitStateSize,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700120 kHashMask = (1 << kHashSize) - 1,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700121 kMaxHash = kHashMask,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800122
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700123 // Forwarding address shift.
124 kForwardingAddressShift = kObjectAlignmentShift,
125
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800126 kMonitorIdShift = kHashShift,
127 kMonitorIdSize = kHashSize,
128 kMonitorIdMask = kHashMask,
129 kMonitorIdAlignmentShift = 32 - kMonitorIdSize,
130 kMonitorIdAlignment = 1 << kMonitorIdAlignmentShift,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700131 kMaxMonitorId = kMaxHash
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700132 };
133
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700134 static LockWord FromThinLockId(uint32_t thread_id, uint32_t count, uint32_t gc_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700135 CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockMaxOwner));
136 CHECK_LE(count, static_cast<uint32_t>(kThinLockMaxCount));
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700137 // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U);
138 return LockWord((thread_id << kThinLockOwnerShift) |
139 (count << kThinLockCountShift) |
140 (gc_state << kGCStateShift) |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800141 (kStateThinOrUnlocked << kStateShift));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700142 }
143
Mathieu Chartier590fee92013-09-13 13:46:47 -0700144 static LockWord FromForwardingAddress(size_t target) {
Roland Levillain14d90572015-07-16 10:52:26 +0100145 DCHECK_ALIGNED(target, (1 << kStateSize));
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700146 return LockWord((target >> kForwardingAddressShift) | kStateForwardingAddressShifted);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700147 }
148
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700149 static LockWord FromHashCode(uint32_t hash_code, uint32_t gc_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700150 CHECK_LE(hash_code, static_cast<uint32_t>(kMaxHash));
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700151 // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800152 return LockWord((hash_code << kHashShift) |
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700153 (gc_state << kGCStateShift) |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800154 (kStateHash << kStateShift));
155 }
156
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700157 static LockWord FromDefault(uint32_t gc_state) {
158 return LockWord(gc_state << kGCStateShift);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800159 }
160
161 static bool IsDefault(LockWord lw) {
162 return LockWord().GetValue() == lw.GetValue();
163 }
164
165 static LockWord Default() {
166 return LockWord();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700167 }
168
169 enum LockState {
170 kUnlocked, // No lock owners.
171 kThinLocked, // Single uncontended owner.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700172 kFatLocked, // See associated monitor.
173 kHashCode, // Lock word contains an identity hash.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700174 kForwardingAddress, // Lock word contains the forwarding address of an object.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700175 };
176
177 LockState GetState() const {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800178 CheckReadBarrierState();
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700179 if ((!kUseReadBarrier && UNLIKELY(value_ == 0)) ||
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700180 (kUseReadBarrier && UNLIKELY((value_ & kGCStateMaskShiftedToggled) == 0))) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700181 return kUnlocked;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700182 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700183 uint32_t internal_state = (value_ >> kStateShift) & kStateMask;
184 switch (internal_state) {
185 case kStateThinOrUnlocked:
186 return kThinLocked;
187 case kStateHash:
188 return kHashCode;
189 case kStateForwardingAddress:
190 return kForwardingAddress;
191 default:
192 DCHECK_EQ(internal_state, static_cast<uint32_t>(kStateFat));
193 return kFatLocked;
194 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700195 }
196 }
197
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800198 uint32_t ReadBarrierState() const {
199 return (value_ >> kReadBarrierStateShift) & kReadBarrierStateMask;
200 }
201
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700202 uint32_t GCState() const {
203 return (value_ & kGCStateMaskShifted) >> kGCStateShift;
204 }
205
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700206 void SetReadBarrierState(uint32_t rb_state) {
207 DCHECK_EQ(rb_state & ~kReadBarrierStateMask, 0U);
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700208 DCHECK(rb_state == ReadBarrier::WhiteState() ||
209 rb_state == ReadBarrier::GrayState()) << rb_state;
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700210 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;
214 }
215
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700216
217 uint32_t MarkBitState() const {
218 return (value_ >> kMarkBitStateShift) & kMarkBitStateMask;
219 }
220
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;
227 }
228
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700229 // Return the owner thin lock thread id.
230 uint32_t ThinLockOwner() const;
231
232 // Return the number of times a lock value has been locked.
233 uint32_t ThinLockCount() const;
234
235 // Return the Monitor encoded in a fat lock.
236 Monitor* FatLockMonitor() const;
237
Mathieu Chartier590fee92013-09-13 13:46:47 -0700238 // Return the forwarding address stored in the monitor.
239 size_t ForwardingAddress() const;
240
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700241 // Constructor a lock word for inflation to use a Monitor.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700242 LockWord(Monitor* mon, uint32_t gc_state);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700243
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700244 // Return the hash code stored in the lock word, must be kHashCode state.
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700245 int32_t GetHashCode() const;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700246
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800247 template <bool kIncludeReadBarrierState>
248 static bool Equal(LockWord lw1, LockWord lw2) {
249 if (kIncludeReadBarrierState) {
250 return lw1.GetValue() == lw2.GetValue();
251 }
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700252 return lw1.GetValueWithoutGCState() == lw2.GetValueWithoutGCState();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700253 }
254
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700255 void Dump(std::ostream& os) {
256 os << "LockWord:" << std::hex << value_;
257 }
258
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700259 private:
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800260 // Default constructor with no lock ownership.
261 LockWord();
262
263 explicit LockWord(uint32_t val) : value_(val) {
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700264 // 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");
Mathieu Chartier6f198e32016-11-03 11:15:04 -0700269 static_assert(
270 (~kStateForwardingAddress & kStateMask) == 0,
271 "READ_BARRIER_MARK_REG relies on the forwarding address state being only one bits");
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800272 CheckReadBarrierState();
273 }
274
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;
279
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);
285 } else {
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700286 DCHECK(rb_state == ReadBarrier::WhiteState() ||
287 rb_state == ReadBarrier::GrayState()) << rb_state;
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800288 }
289 }
290 }
291
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();
297 return value_;
298 }
299
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700300 uint32_t GetValueWithoutGCState() const {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800301 CheckReadBarrierState();
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700302 return value_ & kGCStateMaskShiftedToggled;
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800303 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700304
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700305 // Only Object should be converting LockWords to/from uints.
306 friend class mirror::Object;
307
308 // The encoded value holding all the state.
309 uint32_t value_;
310};
311std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code);
312
313} // namespace art
314
315
316#endif // ART_RUNTIME_LOCK_WORD_H_