blob: d24a3bbecc832fddac2212cfacc8e32914b2973f [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|
45 * |01| Monitor* >> kStateSize |
46 *
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:
55 enum {
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,
66 // Count in higher bits.
67 kThinLockCountShift = kThinLockOwnerSize + kThinLockOwnerShift,
68 kThinLockCountMask = (1 << kThinLockCountShift) - 1,
69 kThinLockMaxCount = kThinLockCountMask,
70
71 // State in the highest bits.
72 kStateShift = kThinLockCountSize + kThinLockCountShift,
73 kStateMask = (1 << kStateSize) - 1,
74 kStateThinOrUnlocked = 0,
75 kStateFat = 1,
Mathieu Chartierad2541a2013-10-25 10:05:23 -070076 kStateHash = 2,
Mathieu Chartier590fee92013-09-13 13:46:47 -070077 kStateForwardingAddress = 3,
Mathieu Chartierad2541a2013-10-25 10:05:23 -070078
79 // When the state is kHashCode, the non-state bits hold the hashcode.
80 kHashShift = 0,
81 kHashSize = 32 - kStateSize,
82 kHashMask = (1 << kHashSize) - 1,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070083 };
84
85 static LockWord FromThinLockId(uint32_t thread_id, uint32_t count) {
86 CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockOwnerMask));
Mathieu Chartierad2541a2013-10-25 10:05:23 -070087 return LockWord((thread_id << kThinLockOwnerShift) | (count << kThinLockCountShift) |
88 (kStateThinOrUnlocked << kStateShift));
89 }
90
Mathieu Chartier590fee92013-09-13 13:46:47 -070091 static LockWord FromForwardingAddress(size_t target) {
92 DCHECK(IsAligned < 1 << kStateSize>(target));
93 return LockWord((target >> kStateSize) | (kStateForwardingAddress << kStateShift));
94 }
95
Mathieu Chartierad2541a2013-10-25 10:05:23 -070096 static LockWord FromHashCode(uint32_t hash_code) {
97 CHECK_LE(hash_code, static_cast<uint32_t>(kHashMask));
98 return LockWord((hash_code << kHashShift) | (kStateHash << kStateShift));
Ian Rogersd9c4fc92013-10-01 19:45:43 -070099 }
100
101 enum LockState {
102 kUnlocked, // No lock owners.
103 kThinLocked, // Single uncontended owner.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700104 kFatLocked, // See associated monitor.
105 kHashCode, // Lock word contains an identity hash.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700106 kForwardingAddress, // Lock word contains the forwarding address of an object.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700107 };
108
109 LockState GetState() const {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700110 if (UNLIKELY(value_ == 0)) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700111 return kUnlocked;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700112 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700113 uint32_t internal_state = (value_ >> kStateShift) & kStateMask;
114 switch (internal_state) {
115 case kStateThinOrUnlocked:
116 return kThinLocked;
117 case kStateHash:
118 return kHashCode;
119 case kStateForwardingAddress:
120 return kForwardingAddress;
121 default:
122 DCHECK_EQ(internal_state, static_cast<uint32_t>(kStateFat));
123 return kFatLocked;
124 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700125 }
126 }
127
128 // Return the owner thin lock thread id.
129 uint32_t ThinLockOwner() const;
130
131 // Return the number of times a lock value has been locked.
132 uint32_t ThinLockCount() const;
133
134 // Return the Monitor encoded in a fat lock.
135 Monitor* FatLockMonitor() const;
136
Mathieu Chartier590fee92013-09-13 13:46:47 -0700137 // Return the forwarding address stored in the monitor.
138 size_t ForwardingAddress() const;
139
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700140 // Default constructor with no lock ownership.
141 LockWord();
142
143 // Constructor a lock word for inflation to use a Monitor.
144 explicit LockWord(Monitor* mon);
145
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700146 bool operator==(const LockWord& rhs) const {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700147 return GetValue() == rhs.GetValue();
148 }
149
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700150 // Return the hash code stored in the lock word, must be kHashCode state.
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700151 int32_t GetHashCode() const;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700152
153 uint32_t GetValue() const {
154 return value_;
155 }
156
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700157 private:
158 explicit LockWord(uint32_t val) : value_(val) {}
159
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700160 // Only Object should be converting LockWords to/from uints.
161 friend class mirror::Object;
162
163 // The encoded value holding all the state.
164 uint32_t value_;
165};
166std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code);
167
168} // namespace art
169
170
171#endif // ART_RUNTIME_LOCK_WORD_H_