blob: 538b6ebbba3f89a2f8ebc8d809ffd982466c7e7b [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:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070064 enum SizeShiftsAndMasks { // 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 Chartier36a270a2016-07-28 18:08:51 -070094
95 // Read barrier bit.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080096 kReadBarrierStateShift = kThinLockCountSize + kThinLockCountShift,
97 kReadBarrierStateMask = (1 << kReadBarrierStateSize) - 1,
98 kReadBarrierStateMaskShifted = kReadBarrierStateMask << kReadBarrierStateShift,
99 kReadBarrierStateMaskShiftedToggled = ~kReadBarrierStateMaskShifted,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700100
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700101 // Mark bit.
102 kMarkBitStateShift = kReadBarrierStateSize + kReadBarrierStateShift,
103 kMarkBitStateMask = (1 << kMarkBitStateSize) - 1,
104 kMarkBitStateMaskShifted = kMarkBitStateMask << kMarkBitStateShift,
105 kMarkBitStateMaskShiftedToggled = ~kMarkBitStateMaskShifted,
106
107 // GC state is mark bit and read barrier state.
108 kGCStateSize = kReadBarrierStateSize + kMarkBitStateSize,
109 kGCStateShift = kReadBarrierStateShift,
110 kGCStateMaskShifted = kReadBarrierStateMaskShifted | kMarkBitStateMaskShifted,
111 kGCStateMaskShiftedToggled = ~kGCStateMaskShifted,
112
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700113 // When the state is kHashCode, the non-state bits hold the hashcode.
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700114 // Note Object.hashCode() has the hash code layout hardcoded.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700115 kHashShift = 0,
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700116 kHashSize = 32 - kStateSize - kReadBarrierStateSize - kMarkBitStateSize,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700117 kHashMask = (1 << kHashSize) - 1,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700118 kMaxHash = kHashMask,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800119
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700120 // Forwarding address shift.
121 kForwardingAddressShift = kObjectAlignmentShift,
122
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800123 kMonitorIdShift = kHashShift,
124 kMonitorIdSize = kHashSize,
125 kMonitorIdMask = kHashMask,
126 kMonitorIdAlignmentShift = 32 - kMonitorIdSize,
127 kMonitorIdAlignment = 1 << kMonitorIdAlignmentShift,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700128 kMaxMonitorId = kMaxHash
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700129 };
130
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700131 static LockWord FromThinLockId(uint32_t thread_id, uint32_t count, uint32_t gc_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700132 CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockMaxOwner));
133 CHECK_LE(count, static_cast<uint32_t>(kThinLockMaxCount));
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700134 // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U);
135 return LockWord((thread_id << kThinLockOwnerShift) |
136 (count << kThinLockCountShift) |
137 (gc_state << kGCStateShift) |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800138 (kStateThinOrUnlocked << kStateShift));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700139 }
140
Mathieu Chartier590fee92013-09-13 13:46:47 -0700141 static LockWord FromForwardingAddress(size_t target) {
Roland Levillain14d90572015-07-16 10:52:26 +0100142 DCHECK_ALIGNED(target, (1 << kStateSize));
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700143 return LockWord((target >> kForwardingAddressShift) | (kStateForwardingAddress << kStateShift));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700144 }
145
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700146 static LockWord FromHashCode(uint32_t hash_code, uint32_t gc_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700147 CHECK_LE(hash_code, static_cast<uint32_t>(kMaxHash));
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700148 // DCHECK_EQ(gc_bits & kGCStateMaskToggled, 0U);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800149 return LockWord((hash_code << kHashShift) |
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700150 (gc_state << kGCStateShift) |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800151 (kStateHash << kStateShift));
152 }
153
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700154 static LockWord FromDefault(uint32_t gc_state) {
155 return LockWord(gc_state << kGCStateShift);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800156 }
157
158 static bool IsDefault(LockWord lw) {
159 return LockWord().GetValue() == lw.GetValue();
160 }
161
162 static LockWord Default() {
163 return LockWord();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700164 }
165
166 enum LockState {
167 kUnlocked, // No lock owners.
168 kThinLocked, // Single uncontended owner.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700169 kFatLocked, // See associated monitor.
170 kHashCode, // Lock word contains an identity hash.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700171 kForwardingAddress, // Lock word contains the forwarding address of an object.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700172 };
173
174 LockState GetState() const {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800175 CheckReadBarrierState();
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700176 if ((!kUseReadBarrier && UNLIKELY(value_ == 0)) ||
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700177 (kUseReadBarrier && UNLIKELY((value_ & kGCStateMaskShiftedToggled) == 0))) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700178 return kUnlocked;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700179 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700180 uint32_t internal_state = (value_ >> kStateShift) & kStateMask;
181 switch (internal_state) {
182 case kStateThinOrUnlocked:
183 return kThinLocked;
184 case kStateHash:
185 return kHashCode;
186 case kStateForwardingAddress:
187 return kForwardingAddress;
188 default:
189 DCHECK_EQ(internal_state, static_cast<uint32_t>(kStateFat));
190 return kFatLocked;
191 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700192 }
193 }
194
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800195 uint32_t ReadBarrierState() const {
196 return (value_ >> kReadBarrierStateShift) & kReadBarrierStateMask;
197 }
198
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700199 uint32_t GCState() const {
200 return (value_ & kGCStateMaskShifted) >> kGCStateShift;
201 }
202
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700203 void SetReadBarrierState(uint32_t rb_state) {
204 DCHECK_EQ(rb_state & ~kReadBarrierStateMask, 0U);
205 DCHECK_NE(static_cast<uint32_t>(GetState()), static_cast<uint32_t>(kForwardingAddress));
206 // Clear and or the bits.
207 value_ &= ~(kReadBarrierStateMask << kReadBarrierStateShift);
208 value_ |= (rb_state & kReadBarrierStateMask) << kReadBarrierStateShift;
209 }
210
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700211
212 uint32_t MarkBitState() const {
213 return (value_ >> kMarkBitStateShift) & kMarkBitStateMask;
214 }
215
216 void SetMarkBitState(uint32_t mark_bit) {
217 DCHECK_EQ(mark_bit & ~kMarkBitStateMask, 0U);
218 DCHECK_NE(static_cast<uint32_t>(GetState()), static_cast<uint32_t>(kForwardingAddress));
219 // Clear and or the bits.
220 value_ &= kMarkBitStateMaskShiftedToggled;
221 value_ |= mark_bit << kMarkBitStateShift;
222 }
223
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700224 // Return the owner thin lock thread id.
225 uint32_t ThinLockOwner() const;
226
227 // Return the number of times a lock value has been locked.
228 uint32_t ThinLockCount() const;
229
230 // Return the Monitor encoded in a fat lock.
231 Monitor* FatLockMonitor() const;
232
Mathieu Chartier590fee92013-09-13 13:46:47 -0700233 // Return the forwarding address stored in the monitor.
234 size_t ForwardingAddress() const;
235
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700236 // Constructor a lock word for inflation to use a Monitor.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700237 LockWord(Monitor* mon, uint32_t gc_state);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700238
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700239 // Return the hash code stored in the lock word, must be kHashCode state.
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700240 int32_t GetHashCode() const;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700241
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800242 template <bool kIncludeReadBarrierState>
243 static bool Equal(LockWord lw1, LockWord lw2) {
244 if (kIncludeReadBarrierState) {
245 return lw1.GetValue() == lw2.GetValue();
246 }
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700247 return lw1.GetValueWithoutGCState() == lw2.GetValueWithoutGCState();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700248 }
249
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -0700250 void Dump(std::ostream& os) {
251 os << "LockWord:" << std::hex << value_;
252 }
253
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700254 private:
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800255 // Default constructor with no lock ownership.
256 LockWord();
257
258 explicit LockWord(uint32_t val) : value_(val) {
259 CheckReadBarrierState();
260 }
261
262 // Disallow this in favor of explicit Equal() with the
263 // kIncludeReadBarrierState param to make clients be aware of the
264 // read barrier state.
265 bool operator==(const LockWord& rhs) = delete;
266
267 void CheckReadBarrierState() const {
268 if (kIsDebugBuild && ((value_ >> kStateShift) & kStateMask) != kStateForwardingAddress) {
269 uint32_t rb_state = ReadBarrierState();
270 if (!kUseReadBarrier) {
271 DCHECK_EQ(rb_state, 0U);
272 } else {
273 DCHECK(rb_state == ReadBarrier::white_ptr_ ||
274 rb_state == ReadBarrier::gray_ptr_ ||
275 rb_state == ReadBarrier::black_ptr_) << rb_state;
276 }
277 }
278 }
279
280 // Note GetValue() includes the read barrier bits and comparing (==)
281 // GetValue() between two lock words to compare the lock states may
282 // not work. Prefer Equal() or GetValueWithoutReadBarrierState().
283 uint32_t GetValue() const {
284 CheckReadBarrierState();
285 return value_;
286 }
287
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700288 uint32_t GetValueWithoutGCState() const {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800289 CheckReadBarrierState();
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700290 return value_ & kGCStateMaskShiftedToggled;
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800291 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700292
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700293 // Only Object should be converting LockWords to/from uints.
294 friend class mirror::Object;
295
296 // The encoded value holding all the state.
297 uint32_t value_;
298};
299std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code);
300
301} // namespace art
302
303
304#endif // ART_RUNTIME_LOCK_WORD_H_