blob: aafbfe4159fc6a129cebaff15cf4a1b9c4543ad0 [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 *
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080038 * |33|22|222222221111|1111110000000000|
39 * |10|98|765432109876|5432109876543210|
40 * |00|rb| 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 *
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080044 * |33|22|2222222211111111110000000000|
45 * |10|98|7654321098765432109876543210|
46 * |01|rb| 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 *
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080050 * |33|22|2222222211111111110000000000|
51 * |10|98|7654321098765432109876543210|
52 * |10|rb| HashCode |
53 *
54 * When the lock word is in fowarding address state and its bits are formatted as follows:
55 *
56 * |33|22|2222222211111111110000000000|
57 * |10|98|7654321098765432109876543210|
58 * |11| ForwardingAddress |
59 *
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,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080067 kReadBarrierStateSize = 2,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070068 // Number of bits to encode the thin lock owner.
69 kThinLockOwnerSize = 16,
70 // Remaining bits are the recursive lock count.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080071 kThinLockCountSize = 32 - kThinLockOwnerSize - kStateSize - kReadBarrierStateSize,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070072 // Thin lock bits. Owner in lowest bits.
Mathieu Chartierad2541a2013-10-25 10:05:23 -070073
Ian Rogersd9c4fc92013-10-01 19:45:43 -070074 kThinLockOwnerShift = 0,
75 kThinLockOwnerMask = (1 << kThinLockOwnerSize) - 1,
nikolay serdjukd8481cc2014-07-28 17:40:16 +070076 kThinLockMaxOwner = kThinLockOwnerMask,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070077 // Count in higher bits.
78 kThinLockCountShift = kThinLockOwnerSize + kThinLockOwnerShift,
Dmitry Petrochenko8d82de52014-07-28 17:40:16 +070079 kThinLockCountMask = (1 << kThinLockCountSize) - 1,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070080 kThinLockMaxCount = kThinLockCountMask,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080081 kThinLockCountOne = 1 << kThinLockCountShift, // == 65536 (0x10000)
Ian Rogersd9c4fc92013-10-01 19:45:43 -070082
83 // State in the highest bits.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080084 kStateShift = kReadBarrierStateSize + kThinLockCountSize + kThinLockCountShift,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070085 kStateMask = (1 << kStateSize) - 1,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080086 kStateMaskShifted = kStateMask << kStateShift,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070087 kStateThinOrUnlocked = 0,
88 kStateFat = 1,
Mathieu Chartierad2541a2013-10-25 10:05:23 -070089 kStateHash = 2,
Mathieu Chartier590fee92013-09-13 13:46:47 -070090 kStateForwardingAddress = 3,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080091 kReadBarrierStateShift = kThinLockCountSize + kThinLockCountShift,
92 kReadBarrierStateMask = (1 << kReadBarrierStateSize) - 1,
93 kReadBarrierStateMaskShifted = kReadBarrierStateMask << kReadBarrierStateShift,
94 kReadBarrierStateMaskShiftedToggled = ~kReadBarrierStateMaskShifted,
Mathieu Chartierad2541a2013-10-25 10:05:23 -070095
96 // When the state is kHashCode, the non-state bits hold the hashcode.
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -070097 // Note Object.hashCode() has the hash code layout hardcoded.
Mathieu Chartierad2541a2013-10-25 10:05:23 -070098 kHashShift = 0,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080099 kHashSize = 32 - kStateSize - kReadBarrierStateSize,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700100 kHashMask = (1 << kHashSize) - 1,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700101 kMaxHash = kHashMask,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800102
103 kMonitorIdShift = kHashShift,
104 kMonitorIdSize = kHashSize,
105 kMonitorIdMask = kHashMask,
106 kMonitorIdAlignmentShift = 32 - kMonitorIdSize,
107 kMonitorIdAlignment = 1 << kMonitorIdAlignmentShift,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700108 kMaxMonitorId = kMaxHash
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700109 };
110
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800111 static LockWord FromThinLockId(uint32_t thread_id, uint32_t count, uint32_t rb_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700112 CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockMaxOwner));
113 CHECK_LE(count, static_cast<uint32_t>(kThinLockMaxCount));
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700114 DCHECK_EQ(rb_state & ~kReadBarrierStateMask, 0U);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700115 return LockWord((thread_id << kThinLockOwnerShift) | (count << kThinLockCountShift) |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800116 (rb_state << kReadBarrierStateShift) |
117 (kStateThinOrUnlocked << kStateShift));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700118 }
119
Mathieu Chartier590fee92013-09-13 13:46:47 -0700120 static LockWord FromForwardingAddress(size_t target) {
121 DCHECK(IsAligned < 1 << kStateSize>(target));
122 return LockWord((target >> kStateSize) | (kStateForwardingAddress << kStateShift));
123 }
124
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800125 static LockWord FromHashCode(uint32_t hash_code, uint32_t rb_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700126 CHECK_LE(hash_code, static_cast<uint32_t>(kMaxHash));
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700127 DCHECK_EQ(rb_state & ~kReadBarrierStateMask, 0U);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800128 return LockWord((hash_code << kHashShift) |
129 (rb_state << kReadBarrierStateShift) |
130 (kStateHash << kStateShift));
131 }
132
133 static LockWord FromDefault(uint32_t rb_state) {
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700134 DCHECK_EQ(rb_state & ~kReadBarrierStateMask, 0U);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800135 return LockWord(rb_state << kReadBarrierStateShift);
136 }
137
138 static bool IsDefault(LockWord lw) {
139 return LockWord().GetValue() == lw.GetValue();
140 }
141
142 static LockWord Default() {
143 return LockWord();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700144 }
145
146 enum LockState {
147 kUnlocked, // No lock owners.
148 kThinLocked, // Single uncontended owner.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700149 kFatLocked, // See associated monitor.
150 kHashCode, // Lock word contains an identity hash.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700151 kForwardingAddress, // Lock word contains the forwarding address of an object.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700152 };
153
154 LockState GetState() const {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800155 CheckReadBarrierState();
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700156 if ((!kUseReadBarrier && UNLIKELY(value_ == 0)) ||
157 (kUseReadBarrier && UNLIKELY((value_ & kReadBarrierStateMaskShiftedToggled) == 0))) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700158 return kUnlocked;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700159 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700160 uint32_t internal_state = (value_ >> kStateShift) & kStateMask;
161 switch (internal_state) {
162 case kStateThinOrUnlocked:
163 return kThinLocked;
164 case kStateHash:
165 return kHashCode;
166 case kStateForwardingAddress:
167 return kForwardingAddress;
168 default:
169 DCHECK_EQ(internal_state, static_cast<uint32_t>(kStateFat));
170 return kFatLocked;
171 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700172 }
173 }
174
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800175 uint32_t ReadBarrierState() const {
176 return (value_ >> kReadBarrierStateShift) & kReadBarrierStateMask;
177 }
178
Hiroshi Yamauchi60f63f52015-04-23 16:12:40 -0700179 void SetReadBarrierState(uint32_t rb_state) {
180 DCHECK_EQ(rb_state & ~kReadBarrierStateMask, 0U);
181 DCHECK_NE(static_cast<uint32_t>(GetState()), static_cast<uint32_t>(kForwardingAddress));
182 // Clear and or the bits.
183 value_ &= ~(kReadBarrierStateMask << kReadBarrierStateShift);
184 value_ |= (rb_state & kReadBarrierStateMask) << kReadBarrierStateShift;
185 }
186
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700187 // Return the owner thin lock thread id.
188 uint32_t ThinLockOwner() const;
189
190 // Return the number of times a lock value has been locked.
191 uint32_t ThinLockCount() const;
192
193 // Return the Monitor encoded in a fat lock.
194 Monitor* FatLockMonitor() const;
195
Mathieu Chartier590fee92013-09-13 13:46:47 -0700196 // Return the forwarding address stored in the monitor.
197 size_t ForwardingAddress() const;
198
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700199 // Constructor a lock word for inflation to use a Monitor.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800200 explicit LockWord(Monitor* mon, uint32_t rb_state);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700201
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700202 // Return the hash code stored in the lock word, must be kHashCode state.
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700203 int32_t GetHashCode() const;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700204
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800205 template <bool kIncludeReadBarrierState>
206 static bool Equal(LockWord lw1, LockWord lw2) {
207 if (kIncludeReadBarrierState) {
208 return lw1.GetValue() == lw2.GetValue();
209 }
210 return lw1.GetValueWithoutReadBarrierState() == lw2.GetValueWithoutReadBarrierState();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700211 }
212
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700213 private:
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800214 // Default constructor with no lock ownership.
215 LockWord();
216
217 explicit LockWord(uint32_t val) : value_(val) {
218 CheckReadBarrierState();
219 }
220
221 // Disallow this in favor of explicit Equal() with the
222 // kIncludeReadBarrierState param to make clients be aware of the
223 // read barrier state.
224 bool operator==(const LockWord& rhs) = delete;
225
226 void CheckReadBarrierState() const {
227 if (kIsDebugBuild && ((value_ >> kStateShift) & kStateMask) != kStateForwardingAddress) {
228 uint32_t rb_state = ReadBarrierState();
229 if (!kUseReadBarrier) {
230 DCHECK_EQ(rb_state, 0U);
231 } else {
232 DCHECK(rb_state == ReadBarrier::white_ptr_ ||
233 rb_state == ReadBarrier::gray_ptr_ ||
234 rb_state == ReadBarrier::black_ptr_) << rb_state;
235 }
236 }
237 }
238
239 // Note GetValue() includes the read barrier bits and comparing (==)
240 // GetValue() between two lock words to compare the lock states may
241 // not work. Prefer Equal() or GetValueWithoutReadBarrierState().
242 uint32_t GetValue() const {
243 CheckReadBarrierState();
244 return value_;
245 }
246
247 uint32_t GetValueWithoutReadBarrierState() const {
248 CheckReadBarrierState();
249 return value_ & ~(kReadBarrierStateMask << kReadBarrierStateShift);
250 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700251
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700252 // Only Object should be converting LockWords to/from uints.
253 friend class mirror::Object;
254
255 // The encoded value holding all the state.
256 uint32_t value_;
257};
258std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code);
259
260} // namespace art
261
262
263#endif // ART_RUNTIME_LOCK_WORD_H_