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" |
| 24 | |
| 25 | namespace art { |
| 26 | namespace mirror { |
| 27 | class Object; |
| 28 | } // namespace mirror |
| 29 | |
| 30 | class Monitor; |
| 31 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame^] | 32 | /* The lock value itself as stored in mirror::Object::monitor_. The two most significant bits of |
| 33 | * the state. The three possible states are fat locked, thin/unlocked, and hash code. |
| 34 | * 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] | 35 | * |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame^] | 36 | * |33|22222222221111|1111110000000000| |
| 37 | * |10|98765432109876|5432109876543210| |
| 38 | * |00| lock count |thread id owner | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 39 | * |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame^] | 40 | * 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] | 41 | * |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame^] | 42 | * |33|222222222211111111110000000000| |
| 43 | * |10|987654321098765432109876543210| |
| 44 | * |01| Monitor* >> kStateSize | |
| 45 | * |
| 46 | * When the lock word is in hash state and its bits are formatted as follows: |
| 47 | * |
| 48 | * |33|222222222211111111110000000000| |
| 49 | * |10|987654321098765432109876543210| |
| 50 | * |10| HashCode | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 51 | */ |
| 52 | class LockWord { |
| 53 | public: |
| 54 | enum { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame^] | 55 | // Number of bits to encode the state, currently just fat or thin/unlocked or hash code. |
| 56 | kStateSize = 2, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 57 | // Number of bits to encode the thin lock owner. |
| 58 | kThinLockOwnerSize = 16, |
| 59 | // Remaining bits are the recursive lock count. |
| 60 | kThinLockCountSize = 32 - kThinLockOwnerSize - kStateSize, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 61 | // Thin lock bits. Owner in lowest bits. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame^] | 62 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 63 | kThinLockOwnerShift = 0, |
| 64 | kThinLockOwnerMask = (1 << kThinLockOwnerSize) - 1, |
| 65 | // Count in higher bits. |
| 66 | kThinLockCountShift = kThinLockOwnerSize + kThinLockOwnerShift, |
| 67 | kThinLockCountMask = (1 << kThinLockCountShift) - 1, |
| 68 | kThinLockMaxCount = kThinLockCountMask, |
| 69 | |
| 70 | // State in the highest bits. |
| 71 | kStateShift = kThinLockCountSize + kThinLockCountShift, |
| 72 | kStateMask = (1 << kStateSize) - 1, |
| 73 | kStateThinOrUnlocked = 0, |
| 74 | kStateFat = 1, |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame^] | 75 | kStateHash = 2, |
| 76 | |
| 77 | // When the state is kHashCode, the non-state bits hold the hashcode. |
| 78 | kHashShift = 0, |
| 79 | kHashSize = 32 - kStateSize, |
| 80 | kHashMask = (1 << kHashSize) - 1, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | static LockWord FromThinLockId(uint32_t thread_id, uint32_t count) { |
| 84 | CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockOwnerMask)); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame^] | 85 | return LockWord((thread_id << kThinLockOwnerShift) | (count << kThinLockCountShift) | |
| 86 | (kStateThinOrUnlocked << kStateShift)); |
| 87 | } |
| 88 | |
| 89 | static LockWord FromHashCode(uint32_t hash_code) { |
| 90 | CHECK_LE(hash_code, static_cast<uint32_t>(kHashMask)); |
| 91 | return LockWord((hash_code << kHashShift) | (kStateHash << kStateShift)); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | enum LockState { |
| 95 | kUnlocked, // No lock owners. |
| 96 | kThinLocked, // Single uncontended owner. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame^] | 97 | kFatLocked, // See associated monitor. |
| 98 | kHashCode, // Lock word contains an identity hash. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | LockState GetState() const { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame^] | 102 | uint32_t internal_state = (value_ >> kStateShift) & kStateMask; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 103 | if (value_ == 0) { |
| 104 | return kUnlocked; |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame^] | 105 | } else if (internal_state == kStateThinOrUnlocked) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 106 | return kThinLocked; |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame^] | 107 | } else if (internal_state == kStateHash) { |
| 108 | return kHashCode; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 109 | } else { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame^] | 110 | DCHECK_EQ(internal_state, static_cast<uint32_t>(kStateFat)); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 111 | return kFatLocked; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Return the owner thin lock thread id. |
| 116 | uint32_t ThinLockOwner() const; |
| 117 | |
| 118 | // Return the number of times a lock value has been locked. |
| 119 | uint32_t ThinLockCount() const; |
| 120 | |
| 121 | // Return the Monitor encoded in a fat lock. |
| 122 | Monitor* FatLockMonitor() const; |
| 123 | |
| 124 | // Default constructor with no lock ownership. |
| 125 | LockWord(); |
| 126 | |
| 127 | // Constructor a lock word for inflation to use a Monitor. |
| 128 | explicit LockWord(Monitor* mon); |
| 129 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame^] | 130 | bool operator==(const LockWord& rhs) const { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 131 | return GetValue() == rhs.GetValue(); |
| 132 | } |
| 133 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame^] | 134 | // Return the hash code stored in the lock word, must be kHashCode state. |
| 135 | uint32_t GetHashCode() const; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 136 | |
| 137 | uint32_t GetValue() const { |
| 138 | return value_; |
| 139 | } |
| 140 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame^] | 141 | private: |
| 142 | explicit LockWord(uint32_t val) : value_(val) {} |
| 143 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 144 | // Only Object should be converting LockWords to/from uints. |
| 145 | friend class mirror::Object; |
| 146 | |
| 147 | // The encoded value holding all the state. |
| 148 | uint32_t value_; |
| 149 | }; |
| 150 | std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code); |
| 151 | |
| 152 | } // namespace art |
| 153 | |
| 154 | |
| 155 | #endif // ART_RUNTIME_LOCK_WORD_H_ |