blob: e9d06b3d28518ed1e156093fc39129777071eab2 [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);
207 DCHECK_NE(static_cast<uint32_t>(GetState()), static_cast<uint32_t>(kForwardingAddress));
208 // Clear and or the bits.
209 value_ &= ~(kReadBarrierStateMask << kReadBarrierStateShift);
210 value_ |= (rb_state & kReadBarrierStateMask) << kReadBarrierStateShift;
211 }
212
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700213
214 uint32_t MarkBitState() const {
215 return (value_ >> kMarkBitStateShift) & kMarkBitStateMask;
216 }
217
218 void SetMarkBitState(uint32_t mark_bit) {
219 DCHECK_EQ(mark_bit & ~kMarkBitStateMask, 0U);
220 DCHECK_NE(static_cast<uint32_t>(GetState()), static_cast<uint32_t>(kForwardingAddress));
221 // Clear and or the bits.
222 value_ &= kMarkBitStateMaskShiftedToggled;
223 value_ |= mark_bit << kMarkBitStateShift;
224 }
225
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700226 // Return the owner thin lock thread id.
227 uint32_t ThinLockOwner() const;
228
229 // Return the number of times a lock value has been locked.
230 uint32_t ThinLockCount() const;
231
232 // Return the Monitor encoded in a fat lock.
233 Monitor* FatLockMonitor() const;
234
Mathieu Chartier590fee92013-09-13 13:46:47 -0700235 // Return the forwarding address stored in the monitor.
236 size_t ForwardingAddress() const;
237
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700238 // Constructor a lock word for inflation to use a Monitor.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700239 LockWord(Monitor* mon, uint32_t gc_state);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700240
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700241 // Return the hash code stored in the lock word, must be kHashCode state.
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700242 int32_t GetHashCode() const;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700243
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800244 template <bool kIncludeReadBarrierState>
245 static bool Equal(LockWord lw1, LockWord lw2) {
246 if (kIncludeReadBarrierState) {
247 return lw1.GetValue() == lw2.GetValue();
248 }
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700249 return lw1.GetValueWithoutGCState() == lw2.GetValueWithoutGCState();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700250 }
251
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700252 void Dump(std::ostream& os) {
253 os << "LockWord:" << std::hex << value_;
254 }
255
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700256 private:
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800257 // Default constructor with no lock ownership.
258 LockWord();
259
260 explicit LockWord(uint32_t val) : value_(val) {
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700261 // Make sure adding the overflow causes an overflow.
262 constexpr uint64_t overflow = static_cast<uint64_t>(kStateForwardingAddressShifted) +
263 static_cast<uint64_t>(kStateForwardingAddressOverflow);
264 constexpr bool is_larger = overflow > static_cast<uint64_t>(0xFFFFFFFF);
265 static_assert(is_larger, "should have overflowed");
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800266 CheckReadBarrierState();
267 }
268
269 // Disallow this in favor of explicit Equal() with the
270 // kIncludeReadBarrierState param to make clients be aware of the
271 // read barrier state.
272 bool operator==(const LockWord& rhs) = delete;
273
274 void CheckReadBarrierState() const {
275 if (kIsDebugBuild && ((value_ >> kStateShift) & kStateMask) != kStateForwardingAddress) {
276 uint32_t rb_state = ReadBarrierState();
277 if (!kUseReadBarrier) {
278 DCHECK_EQ(rb_state, 0U);
279 } else {
280 DCHECK(rb_state == ReadBarrier::white_ptr_ ||
281 rb_state == ReadBarrier::gray_ptr_ ||
282 rb_state == ReadBarrier::black_ptr_) << rb_state;
283 }
284 }
285 }
286
287 // Note GetValue() includes the read barrier bits and comparing (==)
288 // GetValue() between two lock words to compare the lock states may
289 // not work. Prefer Equal() or GetValueWithoutReadBarrierState().
290 uint32_t GetValue() const {
291 CheckReadBarrierState();
292 return value_;
293 }
294
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700295 uint32_t GetValueWithoutGCState() const {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800296 CheckReadBarrierState();
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700297 return value_ & kGCStateMaskShiftedToggled;
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800298 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700299
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700300 // Only Object should be converting LockWords to/from uints.
301 friend class mirror::Object;
302
303 // The encoded value holding all the state.
304 uint32_t value_;
305};
306std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code);
307
308} // namespace art
309
310
311#endif // ART_RUNTIME_LOCK_WORD_H_