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| |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 45 | * |01| MonitorId | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 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, |
nikolay serdjuk | d8481cc | 2014-07-28 17:40:16 +0700 | [diff] [blame^] | 66 | kThinLockMaxOwner = kThinLockOwnerMask, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 67 | // Count in higher bits. |
| 68 | kThinLockCountShift = kThinLockOwnerSize + kThinLockOwnerShift, |
Dmitry Petrochenko | 8d82de5 | 2014-07-28 17:40:16 +0700 | [diff] [blame] | 69 | kThinLockCountMask = (1 << kThinLockCountSize) - 1, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 70 | 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 Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 77 | kStateHash = 2, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 78 | kStateForwardingAddress = 3, |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 79 | |
| 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 serdjuk | d8481cc | 2014-07-28 17:40:16 +0700 | [diff] [blame^] | 84 | kMaxHash = kHashMask, |
| 85 | kMaxMonitorId = kMaxHash |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | static LockWord FromThinLockId(uint32_t thread_id, uint32_t count) { |
nikolay serdjuk | d8481cc | 2014-07-28 17:40:16 +0700 | [diff] [blame^] | 89 | CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockMaxOwner)); |
| 90 | CHECK_LE(count, static_cast<uint32_t>(kThinLockMaxCount)); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 91 | return LockWord((thread_id << kThinLockOwnerShift) | (count << kThinLockCountShift) | |
| 92 | (kStateThinOrUnlocked << kStateShift)); |
| 93 | } |
| 94 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 95 | static LockWord FromForwardingAddress(size_t target) { |
| 96 | DCHECK(IsAligned < 1 << kStateSize>(target)); |
| 97 | return LockWord((target >> kStateSize) | (kStateForwardingAddress << kStateShift)); |
| 98 | } |
| 99 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 100 | static LockWord FromHashCode(uint32_t hash_code) { |
nikolay serdjuk | d8481cc | 2014-07-28 17:40:16 +0700 | [diff] [blame^] | 101 | CHECK_LE(hash_code, static_cast<uint32_t>(kMaxHash)); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 102 | return LockWord((hash_code << kHashShift) | (kStateHash << kStateShift)); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | enum LockState { |
| 106 | kUnlocked, // No lock owners. |
| 107 | kThinLocked, // Single uncontended owner. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 108 | kFatLocked, // See associated monitor. |
| 109 | kHashCode, // Lock word contains an identity hash. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 110 | kForwardingAddress, // Lock word contains the forwarding address of an object. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | LockState GetState() const { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 114 | if (UNLIKELY(value_ == 0)) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 115 | return kUnlocked; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 116 | } else { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 117 | 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 Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 129 | } |
| 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 Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 141 | // Return the forwarding address stored in the monitor. |
| 142 | size_t ForwardingAddress() const; |
| 143 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 144 | // 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 Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 150 | bool operator==(const LockWord& rhs) const { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 151 | return GetValue() == rhs.GetValue(); |
| 152 | } |
| 153 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 154 | // 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] | 155 | int32_t GetHashCode() const; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 156 | |
| 157 | uint32_t GetValue() const { |
| 158 | return value_; |
| 159 | } |
| 160 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 161 | private: |
| 162 | explicit LockWord(uint32_t val) : value_(val) {} |
| 163 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 164 | // 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 | }; |
| 170 | std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code); |
| 171 | |
| 172 | } // namespace art |
| 173 | |
| 174 | |
| 175 | #endif // ART_RUNTIME_LOCK_WORD_H_ |