Home | History | Annotate | Download | only in runtime
      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 #include "utils.h"
     25 
     26 namespace art {
     27 namespace mirror {
     28   class Object;
     29 }  // namespace mirror
     30 
     31 class Monitor;
     32 
     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:
     36  *
     37  *  |33|22222222221111|1111110000000000|
     38  *  |10|98765432109876|5432109876543210|
     39  *  |00| lock count   |thread id owner |
     40  *
     41  * When the lock word is in the "fat" state and its bits are formatted as follows:
     42  *
     43  *  |33|222222222211111111110000000000|
     44  *  |10|987654321098765432109876543210|
     45  *  |01| MonitorId                    |
     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                     |
     52  */
     53 class LockWord {
     54  public:
     55   enum {
     56     // Number of bits to encode the state, currently just fat or thin/unlocked or hash code.
     57     kStateSize = 2,
     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,
     62     // Thin lock bits. Owner in lowest bits.
     63 
     64     kThinLockOwnerShift = 0,
     65     kThinLockOwnerMask = (1 << kThinLockOwnerSize) - 1,
     66     // Count in higher bits.
     67     kThinLockCountShift = kThinLockOwnerSize + kThinLockOwnerShift,
     68     kThinLockCountMask = (1 << kThinLockCountSize) - 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,
     76     kStateHash = 2,
     77     kStateForwardingAddress = 3,
     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,
     83   };
     84 
     85   static LockWord FromThinLockId(uint32_t thread_id, uint32_t count) {
     86     CHECK_LE(thread_id, static_cast<uint32_t>(kThinLockOwnerMask));
     87     return LockWord((thread_id << kThinLockOwnerShift) | (count << kThinLockCountShift) |
     88                      (kStateThinOrUnlocked << kStateShift));
     89   }
     90 
     91   static LockWord FromForwardingAddress(size_t target) {
     92     DCHECK(IsAligned < 1 << kStateSize>(target));
     93     return LockWord((target >> kStateSize) | (kStateForwardingAddress << kStateShift));
     94   }
     95 
     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));
     99   }
    100 
    101   enum LockState {
    102     kUnlocked,    // No lock owners.
    103     kThinLocked,  // Single uncontended owner.
    104     kFatLocked,   // See associated monitor.
    105     kHashCode,    // Lock word contains an identity hash.
    106     kForwardingAddress,  // Lock word contains the forwarding address of an object.
    107   };
    108 
    109   LockState GetState() const {
    110     if (UNLIKELY(value_ == 0)) {
    111       return kUnlocked;
    112     } else {
    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       }
    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 
    137   // Return the forwarding address stored in the monitor.
    138   size_t ForwardingAddress() const;
    139 
    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 
    146   bool operator==(const LockWord& rhs) const {
    147     return GetValue() == rhs.GetValue();
    148   }
    149 
    150   // Return the hash code stored in the lock word, must be kHashCode state.
    151   int32_t GetHashCode() const;
    152 
    153   uint32_t GetValue() const {
    154     return value_;
    155   }
    156 
    157  private:
    158   explicit LockWord(uint32_t val) : value_(val) {}
    159 
    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_
    172