Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1 | /* |
| 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 Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 24 | #include "utils.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | namespace mirror { |
| 28 | class Object; |
| 29 | } // namespace mirror |
| 30 | |
| 31 | class Monitor; |
| 32 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 33 | /* 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 Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 36 | * |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 37 | * |33|22222222221111|1111110000000000| |
| 38 | * |10|98765432109876|5432109876543210| |
| 39 | * |00| lock count |thread id owner | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 40 | * |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 41 | * When the lock word is in the "fat" state and its bits are formatted as follows: |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 42 | * |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 43 | * |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 Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 52 | */ |
| 53 | class LockWord { |
| 54 | public: |
| 55 | enum { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 56 | // Number of bits to encode the state, currently just fat or thin/unlocked or hash code. |
| 57 | kStateSize = 2, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 58 | // 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 Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 62 | // Thin lock bits. Owner in lowest bits. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 63 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 64 | 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 Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 76 | kStateHash = 2, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 77 | kStateForwardingAddress = 3, |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 78 | |
| 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 Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | static LockWord FromThinLockId(uint32_t thread_id, uint32_t count) { |
| 86 | CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockOwnerMask)); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 87 | return LockWord((thread_id << kThinLockOwnerShift) | (count << kThinLockCountShift) | |
| 88 | (kStateThinOrUnlocked << kStateShift)); |
| 89 | } |
| 90 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 91 | static LockWord FromForwardingAddress(size_t target) { |
| 92 | DCHECK(IsAligned < 1 << kStateSize>(target)); |
| 93 | return LockWord((target >> kStateSize) | (kStateForwardingAddress << kStateShift)); |
| 94 | } |
| 95 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 96 | 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 Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | enum LockState { |
| 102 | kUnlocked, // No lock owners. |
| 103 | kThinLocked, // Single uncontended owner. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 104 | kFatLocked, // See associated monitor. |
| 105 | kHashCode, // Lock word contains an identity hash. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 106 | kForwardingAddress, // Lock word contains the forwarding address of an object. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | LockState GetState() const { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 110 | if (UNLIKELY(value_ == 0)) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 111 | return kUnlocked; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 112 | } else { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 113 | 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 Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 125 | } |
| 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 Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 137 | // Return the forwarding address stored in the monitor. |
| 138 | size_t ForwardingAddress() const; |
| 139 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 140 | // 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 Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 146 | bool operator==(const LockWord& rhs) const { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 147 | return GetValue() == rhs.GetValue(); |
| 148 | } |
| 149 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 150 | // Return the hash code stored in the lock word, must be kHashCode state. |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 151 | int32_t GetHashCode() const; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 152 | |
| 153 | uint32_t GetValue() const { |
| 154 | return value_; |
| 155 | } |
| 156 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 157 | private: |
| 158 | explicit LockWord(uint32_t val) : value_(val) {} |
| 159 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 160 | // 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 | }; |
| 166 | std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code); |
| 167 | |
| 168 | } // namespace art |
| 169 | |
| 170 | |
| 171 | #endif // ART_RUNTIME_LOCK_WORD_H_ |