blob: 9b6c64a183555eb89d05cd6f6c028b5d56565dff [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"
24
25namespace art {
26namespace mirror {
27 class Object;
28} // namespace mirror
29
30class Monitor;
31
Mathieu Chartierad2541a2013-10-25 10:05:23 -070032/* 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 Rogersd9c4fc92013-10-01 19:45:43 -070035 *
Mathieu Chartierad2541a2013-10-25 10:05:23 -070036 * |33|22222222221111|1111110000000000|
37 * |10|98765432109876|5432109876543210|
38 * |00| lock count |thread id owner |
Ian Rogersd9c4fc92013-10-01 19:45:43 -070039 *
Mathieu Chartierad2541a2013-10-25 10:05:23 -070040 * When the lock word is in the "fat" state and its bits are formatted as follows:
Ian Rogersd9c4fc92013-10-01 19:45:43 -070041 *
Mathieu Chartierad2541a2013-10-25 10:05:23 -070042 * |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 Rogersd9c4fc92013-10-01 19:45:43 -070051 */
52class LockWord {
53 public:
54 enum {
Mathieu Chartierad2541a2013-10-25 10:05:23 -070055 // Number of bits to encode the state, currently just fat or thin/unlocked or hash code.
56 kStateSize = 2,
Ian Rogersd9c4fc92013-10-01 19:45:43 -070057 // 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 Rogersd9c4fc92013-10-01 19:45:43 -070061 // Thin lock bits. Owner in lowest bits.
Mathieu Chartierad2541a2013-10-25 10:05:23 -070062
Ian Rogersd9c4fc92013-10-01 19:45:43 -070063 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 Chartierad2541a2013-10-25 10:05:23 -070075 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 Rogersd9c4fc92013-10-01 19:45:43 -070081 };
82
83 static LockWord FromThinLockId(uint32_t thread_id, uint32_t count) {
84 CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockOwnerMask));
Mathieu Chartierad2541a2013-10-25 10:05:23 -070085 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 Rogersd9c4fc92013-10-01 19:45:43 -070092 }
93
94 enum LockState {
95 kUnlocked, // No lock owners.
96 kThinLocked, // Single uncontended owner.
Mathieu Chartierad2541a2013-10-25 10:05:23 -070097 kFatLocked, // See associated monitor.
98 kHashCode, // Lock word contains an identity hash.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070099 };
100
101 LockState GetState() const {
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700102 uint32_t internal_state = (value_ >> kStateShift) & kStateMask;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700103 if (value_ == 0) {
104 return kUnlocked;
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700105 } else if (internal_state == kStateThinOrUnlocked) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700106 return kThinLocked;
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700107 } else if (internal_state == kStateHash) {
108 return kHashCode;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700109 } else {
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700110 DCHECK_EQ(internal_state, static_cast<uint32_t>(kStateFat));
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700111 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 Chartierad2541a2013-10-25 10:05:23 -0700130 bool operator==(const LockWord& rhs) const {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700131 return GetValue() == rhs.GetValue();
132 }
133
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700134 // Return the hash code stored in the lock word, must be kHashCode state.
135 uint32_t GetHashCode() const;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700136
137 uint32_t GetValue() const {
138 return value_;
139 }
140
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700141 private:
142 explicit LockWord(uint32_t val) : value_(val) {}
143
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700144 // 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};
150std::ostream& operator<<(std::ostream& os, const LockWord::LockState& code);
151
152} // namespace art
153
154
155#endif // ART_RUNTIME_LOCK_WORD_H_