blob: 2f2565b9d0ee29addfffc53071ab46e0c9245ef4 [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
20#include <iosfwd>
21#include <stdint.h>
22
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 *
60 * The rb bits store the read barrier state.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070061 */
62class LockWord {
63 public:
Mathieu Chartier1cf194f2016-11-01 20:13:24 -070064 enum SizeShiftsAndMasks : uint32_t { // private marker to avoid generate-operator-out.py from processing.
Mathieu Chartierad2541a2013-10-25 10:05:23 -070065 // Number of bits to encode the state, currently just fat or thin/unlocked or hash code.
66 kStateSize = 2,
Mathieu Chartier36a270a2016-07-28 18:08:51 -070067 kReadBarrierStateSize = 1,
68 kMarkBitStateSize = 1,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070069 // Number of bits to encode the thin lock owner.
70 kThinLockOwnerSize = 16,
71 // Remaining bits are the recursive lock count.
Mathieu Chartier36a270a2016-07-28 18:08:51 -070072 kThinLockCountSize = 32 - kThinLockOwnerSize - kStateSize - kReadBarrierStateSize -
73 kMarkBitStateSize,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070074 // Thin lock bits. Owner in lowest bits.
Mathieu Chartierad2541a2013-10-25 10:05:23 -070075
Ian Rogersd9c4fc92013-10-01 19:45:43 -070076 kThinLockOwnerShift = 0,
77 kThinLockOwnerMask = (1 << kThinLockOwnerSize) - 1,
nikolay serdjukd8481cc2014-07-28 17:40:16 +070078 kThinLockMaxOwner = kThinLockOwnerMask,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070079 // Count in higher bits.
80 kThinLockCountShift = kThinLockOwnerSize + kThinLockOwnerShift,
Dmitry Petrochenko8d82de52014-07-28 17:40:16 +070081 kThinLockCountMask = (1 << kThinLockCountSize) - 1,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070082 kThinLockMaxCount = kThinLockCountMask,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080083 kThinLockCountOne = 1 << kThinLockCountShift, // == 65536 (0x10000)
Ian Rogersd9c4fc92013-10-01 19:45:43 -070084
85 // State in the highest bits.
Mathieu Chartier36a270a2016-07-28 18:08:51 -070086 kStateShift = kReadBarrierStateSize + kThinLockCountSize + kThinLockCountShift +
87 kMarkBitStateSize,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070088 kStateMask = (1 << kStateSize) - 1,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080089 kStateMaskShifted = kStateMask << kStateShift,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070090 kStateThinOrUnlocked = 0,
91 kStateFat = 1,
Mathieu Chartierad2541a2013-10-25 10:05:23 -070092 kStateHash = 2,
Mathieu Chartier590fee92013-09-13 13:46:47 -070093 kStateForwardingAddress = 3,
Mathieu Chartier1cf194f2016-11-01 20:13:24 -070094 kStateForwardingAddressShifted = kStateForwardingAddress << kStateShift,
95 kStateForwardingAddressOverflow = (1 + kStateMask - kStateForwardingAddress) << kStateShift,
Mathieu Chartier36a270a2016-07-28 18:08:51 -070096
97 // Read barrier bit.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080098 kReadBarrierStateShift = kThinLockCountSize + kThinLockCountShift,
99 kReadBarrierStateMask = (1 << kReadBarrierStateSize) - 1,
100 kReadBarrierStateMaskShifted = kReadBarrierStateMask << kReadBarrierStateShift,
101 kReadBarrierStateMaskShiftedToggled = ~kReadBarrierStateMaskShifted,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700102
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700103 // Mark bit.
104 kMarkBitStateShift = kReadBarrierStateSize + kReadBarrierStateShift,
105 kMarkBitStateMask = (1 << kMarkBitStateSize) - 1,
106 kMarkBitStateMaskShifted = kMarkBitStateMask << kMarkBitStateShift,
107 kMarkBitStateMaskShiftedToggled = ~kMarkBitStateMaskShifted,
108
109 // GC state is mark bit and read barrier state.
110 kGCStateSize = kReadBarrierStateSize + kMarkBitStateSize,
111 kGCStateShift = kReadBarrierStateShift,
112 kGCStateMaskShifted = kReadBarrierStateMaskShifted | kMarkBitStateMaskShifted,
113 kGCStateMaskShiftedToggled = ~kGCStateMaskShifted,
114
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700115 // When the state is kHashCode, the non-state bits hold the hashcode.
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700116 // Note Object.hashCode() has the hash code layout hardcoded.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700117 kHashShift = 0,
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700118 kHashSize = 32 - kStateSize - kReadBarrierStateSize - kMarkBitStateSize,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700119 kHashMask = (1 << kHashSize) - 1,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700120 kMaxHash = kHashMask,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800121
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700122 // Forwarding address shift.
123 kForwardingAddressShift = kObjectAlignmentShift,
124
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800125 kMonitorIdShift = kHashShift,
126 kMonitorIdSize = kHashSize,
127 kMonitorIdMask = kHashMask,
128 kMonitorIdAlignmentShift = 32 - kMonitorIdSize,
129 kMonitorIdAlignment = 1 << kMonitorIdAlignmentShift,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700130 kMaxMonitorId = kMaxHash
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700131 };
132
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700133 static LockWord FromThinLockId(uint32_t thread_id, uint32_t count, uint32_t gc_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700134 CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockMaxOwner));
135 CHECK_LE(count, static_cast<uint32_t>(kThinLockMaxCount));
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700136 // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U);
137 return LockWord((thread_id << kThinLockOwnerShift) |
138 (count << kThinLockCountShift) |
139 (gc_state << kGCStateShift) |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800140 (kStateThinOrUnlocked << kStateShift));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700141 }
142
Mathieu Chartier590fee92013-09-13 13:46:47 -0700143 static LockWord FromForwardingAddress(size_t target) {
Roland Levillain14d90572015-07-16 10:52:26 +0100144 DCHECK_ALIGNED(target, (1 << kStateSize));
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700145 return LockWord((target >> kForwardingAddressShift) | kStateForwardingAddressShifted);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700146 }
147
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700148 static LockWord FromHashCode(uint32_t hash_code, uint32_t gc_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700149 CHECK_LE(hash_code, static_cast<uint32_t>(kMaxHash));
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700150 // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800151 return LockWord((hash_code << kHashShift) |
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700152 (gc_state << kGCStateShift) |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800153 (kStateHash << kStateShift));
154 }
155
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700156 static LockWord FromDefault(uint32_t gc_state) {
157 return LockWord(gc_state << kGCStateShift);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800158 }
159
160 static bool IsDefault(LockWord lw) {
161 return LockWord().GetValue() == lw.GetValue();
162 }
163
164 static LockWord Default() {
165 return LockWord();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700166 }
167
168 enum LockState {
169 kUnlocked, // No lock owners.
170 kThinLocked, // Single uncontended owner.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700171 kFatLocked, // See associated monitor.
172 kHashCode, // Lock word contains an identity hash.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700173 kForwardingAddress, // Lock word contains the forwarding address of an object.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700174 };
175
176 LockState GetState() const {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800177 CheckReadBarrierState();
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700178 if ((!kUseReadBarrier && UNLIKELY(value_ == 0)) ||
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700179 (kUseReadBarrier && UNLIKELY((value_ & kGCStateMaskShiftedToggled) == 0))) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700180 return kUnlocked;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700181 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700182 uint32_t internal_state = (value_ >> kStateShift) & kStateMask;
183 switch (internal_state) {
184 case kStateThinOrUnlocked:
185 return kThinLocked;
186 case kStateHash:
187 return kHashCode;
188 case kStateForwardingAddress:
189 return kForwardingAddress;
190 default:
191 DCHECK_EQ(internal_state, static_cast<uint32_t>(kStateFat));
192 return kFatLocked;
193 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700194 }
195 }
196
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800197 uint32_t ReadBarrierState() const {
198 return (value_ >> kReadBarrierStateShift) & kReadBarrierStateMask;
199 }
200
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700201 uint32_t GCState() const {
202 return (value_ & kGCStateMaskShifted) >> kGCStateShift;
203 }
204
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700205 void SetReadBarrierState(uint32_t rb_state) {
206 DCHECK_EQ(rb_state & ~kReadBarrierStateMask, 0U);
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700207 DCHECK(rb_state == ReadBarrier::WhiteState() ||
208 rb_state == ReadBarrier::GrayState()) << rb_state;
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700209 DCHECK_NE(static_cast<uint32_t>(GetState()), static_cast<uint32_t>(kForwardingAddress));
210 // Clear and or the bits.
211 value_ &= ~(kReadBarrierStateMask << kReadBarrierStateShift);
212 value_ |= (rb_state & kReadBarrierStateMask) << kReadBarrierStateShift;
213 }
214
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700215
216 uint32_t MarkBitState() const {
217 return (value_ >> kMarkBitStateShift) & kMarkBitStateMask;
218 }
219
220 void SetMarkBitState(uint32_t mark_bit) {
221 DCHECK_EQ(mark_bit & ~kMarkBitStateMask, 0U);
222 DCHECK_NE(static_cast<uint32_t>(GetState()), static_cast<uint32_t>(kForwardingAddress));
223 // Clear and or the bits.
224 value_ &= kMarkBitStateMaskShiftedToggled;
225 value_ |= mark_bit << kMarkBitStateShift;
226 }
227
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700228 // Return the owner thin lock thread id.
229 uint32_t ThinLockOwner() const;
230
231 // Return the number of times a lock value has been locked.
232 uint32_t ThinLockCount() const;
233
234 // Return the Monitor encoded in a fat lock.
235 Monitor* FatLockMonitor() const;
236
Mathieu Chartier590fee92013-09-13 13:46:47 -0700237 // Return the forwarding address stored in the monitor.
238 size_t ForwardingAddress() const;
239
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700240 // Constructor a lock word for inflation to use a Monitor.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700241 LockWord(Monitor* mon, uint32_t gc_state);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700242
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700243 // Return the hash code stored in the lock word, must be kHashCode state.
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700244 int32_t GetHashCode() const;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700245
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800246 template <bool kIncludeReadBarrierState>
247 static bool Equal(LockWord lw1, LockWord lw2) {
248 if (kIncludeReadBarrierState) {
249 return lw1.GetValue() == lw2.GetValue();
250 }
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700251 return lw1.GetValueWithoutGCState() == lw2.GetValueWithoutGCState();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700252 }
253
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700254 void Dump(std::ostream& os) {
255 os << "LockWord:" << std::hex << value_;
256 }
257
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700258 private:
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800259 // Default constructor with no lock ownership.
260 LockWord();
261
262 explicit LockWord(uint32_t val) : value_(val) {
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700263 // Make sure adding the overflow causes an overflow.
264 constexpr uint64_t overflow = static_cast<uint64_t>(kStateForwardingAddressShifted) +
265 static_cast<uint64_t>(kStateForwardingAddressOverflow);
266 constexpr bool is_larger = overflow > static_cast<uint64_t>(0xFFFFFFFF);
267 static_assert(is_larger, "should have overflowed");
Mathieu Chartier6f198e32016-11-03 11:15:04 -0700268 static_assert(
269 (~kStateForwardingAddress & kStateMask) == 0,
270 "READ_BARRIER_MARK_REG relies on the forwarding address state being only one bits");
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800271 CheckReadBarrierState();
272 }
273
274 // Disallow this in favor of explicit Equal() with the
275 // kIncludeReadBarrierState param to make clients be aware of the
276 // read barrier state.
277 bool operator==(const LockWord& rhs) = delete;
278
279 void CheckReadBarrierState() const {
280 if (kIsDebugBuild && ((value_ >> kStateShift) & kStateMask) != kStateForwardingAddress) {
281 uint32_t rb_state = ReadBarrierState();
282 if (!kUseReadBarrier) {
283 DCHECK_EQ(rb_state, 0U);
284 } else {
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700285 DCHECK(rb_state == ReadBarrier::WhiteState() ||
286 rb_state == ReadBarrier::GrayState()) << rb_state;
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800287 }
288 }
289 }
290
291 // Note GetValue() includes the read barrier bits and comparing (==)
292 // GetValue() between two lock words to compare the lock states may
293 // not work. Prefer Equal() or GetValueWithoutReadBarrierState().
294 uint32_t GetValue() const {
295 CheckReadBarrierState();
296 return value_;
297 }
298
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700299 uint32_t GetValueWithoutGCState() const {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800300 CheckReadBarrierState();
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700301 return value_ & kGCStateMaskShiftedToggled;
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800302 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700303
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700304 // Only Object should be converting LockWords to/from uints.
305 friend class mirror::Object;
306
307 // The encoded value holding all the state.
308 uint32_t value_;
309};
310std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code);
311
312} // namespace art
313
314
315#endif // ART_RUNTIME_LOCK_WORD_H_