blob: 46c3bd4a9934eb9d4b949c3a5a9528a55d509446 [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
23#include "base/logging.h"
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080024#include "read_barrier.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070025#include "utils.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.
97 kHashShift = 0,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080098 kHashSize = 32 - kStateSize - kReadBarrierStateSize,
Mathieu Chartierad2541a2013-10-25 10:05:23 -070099 kHashMask = (1 << kHashSize) - 1,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700100 kMaxHash = kHashMask,
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800101
102 kMonitorIdShift = kHashShift,
103 kMonitorIdSize = kHashSize,
104 kMonitorIdMask = kHashMask,
105 kMonitorIdAlignmentShift = 32 - kMonitorIdSize,
106 kMonitorIdAlignment = 1 << kMonitorIdAlignmentShift,
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700107 kMaxMonitorId = kMaxHash
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700108 };
109
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800110 static LockWord FromThinLockId(uint32_t thread_id, uint32_t count, uint32_t rb_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700111 CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockMaxOwner));
112 CHECK_LE(count, static_cast<uint32_t>(kThinLockMaxCount));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700113 return LockWord((thread_id << kThinLockOwnerShift) | (count << kThinLockCountShift) |
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800114 (rb_state << kReadBarrierStateShift) |
115 (kStateThinOrUnlocked << kStateShift));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700116 }
117
Mathieu Chartier590fee92013-09-13 13:46:47 -0700118 static LockWord FromForwardingAddress(size_t target) {
119 DCHECK(IsAligned < 1 << kStateSize>(target));
120 return LockWord((target >> kStateSize) | (kStateForwardingAddress << kStateShift));
121 }
122
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800123 static LockWord FromHashCode(uint32_t hash_code, uint32_t rb_state) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700124 CHECK_LE(hash_code, static_cast<uint32_t>(kMaxHash));
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800125 return LockWord((hash_code << kHashShift) |
126 (rb_state << kReadBarrierStateShift) |
127 (kStateHash << kStateShift));
128 }
129
130 static LockWord FromDefault(uint32_t rb_state) {
131 return LockWord(rb_state << kReadBarrierStateShift);
132 }
133
134 static bool IsDefault(LockWord lw) {
135 return LockWord().GetValue() == lw.GetValue();
136 }
137
138 static LockWord Default() {
139 return LockWord();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700140 }
141
142 enum LockState {
143 kUnlocked, // No lock owners.
144 kThinLocked, // Single uncontended owner.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700145 kFatLocked, // See associated monitor.
146 kHashCode, // Lock word contains an identity hash.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700147 kForwardingAddress, // Lock word contains the forwarding address of an object.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700148 };
149
150 LockState GetState() const {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800151 CheckReadBarrierState();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700152 if (UNLIKELY(value_ == 0)) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700153 return kUnlocked;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700154 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700155 uint32_t internal_state = (value_ >> kStateShift) & kStateMask;
156 switch (internal_state) {
157 case kStateThinOrUnlocked:
158 return kThinLocked;
159 case kStateHash:
160 return kHashCode;
161 case kStateForwardingAddress:
162 return kForwardingAddress;
163 default:
164 DCHECK_EQ(internal_state, static_cast<uint32_t>(kStateFat));
165 return kFatLocked;
166 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700167 }
168 }
169
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800170 uint32_t ReadBarrierState() const {
171 return (value_ >> kReadBarrierStateShift) & kReadBarrierStateMask;
172 }
173
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700174 // Return the owner thin lock thread id.
175 uint32_t ThinLockOwner() const;
176
177 // Return the number of times a lock value has been locked.
178 uint32_t ThinLockCount() const;
179
180 // Return the Monitor encoded in a fat lock.
181 Monitor* FatLockMonitor() const;
182
Mathieu Chartier590fee92013-09-13 13:46:47 -0700183 // Return the forwarding address stored in the monitor.
184 size_t ForwardingAddress() const;
185
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700186 // Constructor a lock word for inflation to use a Monitor.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800187 explicit LockWord(Monitor* mon, uint32_t rb_state);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700188
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700189 // Return the hash code stored in the lock word, must be kHashCode state.
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700190 int32_t GetHashCode() const;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700191
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800192 template <bool kIncludeReadBarrierState>
193 static bool Equal(LockWord lw1, LockWord lw2) {
194 if (kIncludeReadBarrierState) {
195 return lw1.GetValue() == lw2.GetValue();
196 }
197 return lw1.GetValueWithoutReadBarrierState() == lw2.GetValueWithoutReadBarrierState();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700198 }
199
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700200 private:
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800201 // Default constructor with no lock ownership.
202 LockWord();
203
204 explicit LockWord(uint32_t val) : value_(val) {
205 CheckReadBarrierState();
206 }
207
208 // Disallow this in favor of explicit Equal() with the
209 // kIncludeReadBarrierState param to make clients be aware of the
210 // read barrier state.
211 bool operator==(const LockWord& rhs) = delete;
212
213 void CheckReadBarrierState() const {
214 if (kIsDebugBuild && ((value_ >> kStateShift) & kStateMask) != kStateForwardingAddress) {
215 uint32_t rb_state = ReadBarrierState();
216 if (!kUseReadBarrier) {
217 DCHECK_EQ(rb_state, 0U);
218 } else {
219 DCHECK(rb_state == ReadBarrier::white_ptr_ ||
220 rb_state == ReadBarrier::gray_ptr_ ||
221 rb_state == ReadBarrier::black_ptr_) << rb_state;
222 }
223 }
224 }
225
226 // Note GetValue() includes the read barrier bits and comparing (==)
227 // GetValue() between two lock words to compare the lock states may
228 // not work. Prefer Equal() or GetValueWithoutReadBarrierState().
229 uint32_t GetValue() const {
230 CheckReadBarrierState();
231 return value_;
232 }
233
234 uint32_t GetValueWithoutReadBarrierState() const {
235 CheckReadBarrierState();
236 return value_ & ~(kReadBarrierStateMask << kReadBarrierStateShift);
237 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700238
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700239 // Only Object should be converting LockWords to/from uints.
240 friend class mirror::Object;
241
242 // The encoded value holding all the state.
243 uint32_t value_;
244};
245std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code);
246
247} // namespace art
248
249
250#endif // ART_RUNTIME_LOCK_WORD_H_