blob: 2d5c71bb93f93cec9e6461b9b5f3a8c1b5be29d0 [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"
Mathieu Chartier590fee92013-09-13 13:46:47 -070024#include "utils.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070025
26namespace art {
27namespace mirror {
28 class Object;
29} // namespace mirror
30
31class Monitor;
32
Mathieu Chartierad2541a2013-10-25 10:05:23 -070033/* The lock value itself as stored in mirror::Object::monitor_. The two most significant bits of
34 * the state. The three possible states are fat locked, thin/unlocked, and hash code.
35 * When the lock word is in the "thin" state and its bits are formatted as follows:
Ian Rogersd9c4fc92013-10-01 19:45:43 -070036 *
Mathieu Chartierad2541a2013-10-25 10:05:23 -070037 * |33|22222222221111|1111110000000000|
38 * |10|98765432109876|5432109876543210|
39 * |00| lock count |thread id owner |
Ian Rogersd9c4fc92013-10-01 19:45:43 -070040 *
Mathieu Chartierad2541a2013-10-25 10:05:23 -070041 * When the lock word is in the "fat" state and its bits are formatted as follows:
Ian Rogersd9c4fc92013-10-01 19:45:43 -070042 *
Mathieu Chartierad2541a2013-10-25 10:05:23 -070043 * |33|222222222211111111110000000000|
44 * |10|987654321098765432109876543210|
Ian Rogersef7d42f2014-01-06 12:55:46 -080045 * |01| MonitorId |
Mathieu Chartierad2541a2013-10-25 10:05:23 -070046 *
47 * When the lock word is in hash state and its bits are formatted as follows:
48 *
49 * |33|222222222211111111110000000000|
50 * |10|987654321098765432109876543210|
51 * |10| HashCode |
Ian Rogersd9c4fc92013-10-01 19:45:43 -070052 */
53class LockWord {
54 public:
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070055 enum SizeShiftsAndMasks { // private marker to avoid generate-operator-out.py from processing.
Mathieu Chartierad2541a2013-10-25 10:05:23 -070056 // Number of bits to encode the state, currently just fat or thin/unlocked or hash code.
57 kStateSize = 2,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070058 // Number of bits to encode the thin lock owner.
59 kThinLockOwnerSize = 16,
60 // Remaining bits are the recursive lock count.
61 kThinLockCountSize = 32 - kThinLockOwnerSize - kStateSize,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070062 // Thin lock bits. Owner in lowest bits.
Mathieu Chartierad2541a2013-10-25 10:05:23 -070063
Ian Rogersd9c4fc92013-10-01 19:45:43 -070064 kThinLockOwnerShift = 0,
65 kThinLockOwnerMask = (1 << kThinLockOwnerSize) - 1,
nikolay serdjukd8481cc2014-07-28 17:40:16 +070066 kThinLockMaxOwner = kThinLockOwnerMask,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070067 // Count in higher bits.
68 kThinLockCountShift = kThinLockOwnerSize + kThinLockOwnerShift,
Dmitry Petrochenko8d82de52014-07-28 17:40:16 +070069 kThinLockCountMask = (1 << kThinLockCountSize) - 1,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070070 kThinLockMaxCount = kThinLockCountMask,
71
72 // State in the highest bits.
73 kStateShift = kThinLockCountSize + kThinLockCountShift,
74 kStateMask = (1 << kStateSize) - 1,
75 kStateThinOrUnlocked = 0,
76 kStateFat = 1,
Mathieu Chartierad2541a2013-10-25 10:05:23 -070077 kStateHash = 2,
Mathieu Chartier590fee92013-09-13 13:46:47 -070078 kStateForwardingAddress = 3,
Mathieu Chartierad2541a2013-10-25 10:05:23 -070079
80 // When the state is kHashCode, the non-state bits hold the hashcode.
81 kHashShift = 0,
82 kHashSize = 32 - kStateSize,
83 kHashMask = (1 << kHashSize) - 1,
nikolay serdjukd8481cc2014-07-28 17:40:16 +070084 kMaxHash = kHashMask,
85 kMaxMonitorId = kMaxHash
Ian Rogersd9c4fc92013-10-01 19:45:43 -070086 };
87
88 static LockWord FromThinLockId(uint32_t thread_id, uint32_t count) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +070089 CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockMaxOwner));
90 CHECK_LE(count, static_cast<uint32_t>(kThinLockMaxCount));
Mathieu Chartierad2541a2013-10-25 10:05:23 -070091 return LockWord((thread_id << kThinLockOwnerShift) | (count << kThinLockCountShift) |
92 (kStateThinOrUnlocked << kStateShift));
93 }
94
Mathieu Chartier590fee92013-09-13 13:46:47 -070095 static LockWord FromForwardingAddress(size_t target) {
96 DCHECK(IsAligned < 1 << kStateSize>(target));
97 return LockWord((target >> kStateSize) | (kStateForwardingAddress << kStateShift));
98 }
99
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700100 static LockWord FromHashCode(uint32_t hash_code) {
nikolay serdjukd8481cc2014-07-28 17:40:16 +0700101 CHECK_LE(hash_code, static_cast<uint32_t>(kMaxHash));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700102 return LockWord((hash_code << kHashShift) | (kStateHash << kStateShift));
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700103 }
104
105 enum LockState {
106 kUnlocked, // No lock owners.
107 kThinLocked, // Single uncontended owner.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700108 kFatLocked, // See associated monitor.
109 kHashCode, // Lock word contains an identity hash.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700110 kForwardingAddress, // Lock word contains the forwarding address of an object.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700111 };
112
113 LockState GetState() const {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700114 if (UNLIKELY(value_ == 0)) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700115 return kUnlocked;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700116 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700117 uint32_t internal_state = (value_ >> kStateShift) & kStateMask;
118 switch (internal_state) {
119 case kStateThinOrUnlocked:
120 return kThinLocked;
121 case kStateHash:
122 return kHashCode;
123 case kStateForwardingAddress:
124 return kForwardingAddress;
125 default:
126 DCHECK_EQ(internal_state, static_cast<uint32_t>(kStateFat));
127 return kFatLocked;
128 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700129 }
130 }
131
132 // Return the owner thin lock thread id.
133 uint32_t ThinLockOwner() const;
134
135 // Return the number of times a lock value has been locked.
136 uint32_t ThinLockCount() const;
137
138 // Return the Monitor encoded in a fat lock.
139 Monitor* FatLockMonitor() const;
140
Mathieu Chartier590fee92013-09-13 13:46:47 -0700141 // Return the forwarding address stored in the monitor.
142 size_t ForwardingAddress() const;
143
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700144 // Default constructor with no lock ownership.
145 LockWord();
146
147 // Constructor a lock word for inflation to use a Monitor.
148 explicit LockWord(Monitor* mon);
149
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700150 bool operator==(const LockWord& rhs) const {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700151 return GetValue() == rhs.GetValue();
152 }
153
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700154 // Return the hash code stored in the lock word, must be kHashCode state.
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700155 int32_t GetHashCode() const;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700156
157 uint32_t GetValue() const {
158 return value_;
159 }
160
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700161 private:
162 explicit LockWord(uint32_t val) : value_(val) {}
163
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700164 // Only Object should be converting LockWords to/from uints.
165 friend class mirror::Object;
166
167 // The encoded value holding all the state.
168 uint32_t value_;
169};
170std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code);
171
172} // namespace art
173
174
175#endif // ART_RUNTIME_LOCK_WORD_H_