/* JVM_ENTRY is a preprocessor macro that adds some boilerplate code that is common for all functions of HotSpot JVM API. This API is a connection layer between the native code of JDK class library and the JVM.
intptr_t ObjectSynchronizer::FastHashCode (Thread * Self, oop obj) { //是否开启了偏向锁(Biased:偏向,倾向) if (UseBiasedLocking) { //如果当前对象处于偏向锁状态 if (obj->mark()->has_bias_pattern()) { Handle hobj (Self, obj) ; assert (Universe::verify_in_progress() || !SafepointSynchronize::is_at_safepoint(), "biases should not be seen by VM thread here"); //那么就撤销偏向锁(达到无锁状态,revoke:废除) BiasedLocking::revoke_and_rebias(hobj, false, JavaThread::current()); obj = hobj() ; //断言下,看看是否撤销成功(撤销后为无锁状态) assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now"); } }
//是否撤销了偏向锁(也就是无锁状态)(neutral:中立,不偏不斜的) if (mark->is_neutral()) { //从mark头上取hash值 hash = mark->hash(); //如果有,直接返回这个hashcode(xor) if (hash) { // if it has hash, just return it return hash; } //如果没有就新生成一个(get_next_hash) hash = get_next_hash(Self, obj); // allocate a new hash code //生成后,原子性设置,将hash放在对象头里去,这样下次就可以直接取了 temp = mark->copy_set_hash(hash); // merge the hash code into header // use (machine word version) atomic operation to install the hash test = (markOop) Atomic::cmpxchg_ptr(temp, obj->mark_addr(), mark); if (test == mark) { return hash; } // If atomic operation failed, we must inflate the header // into heavy weight monitor. We could add more code here // for fast path, but it does not worth the complexity. //如果已经升级成了重量级锁,那么找到它的monitor //也就是我们所说的内置锁(objectMonitor),这是c里的数据类型 //因为锁升级后,mark里的bit位已经不再存储hashcode,而是指向monitor的地址 //而升级的markword呢?被移到了c的monitor里 } else if (mark->has_monitor()) { //沿着monitor找header,也就是对象头 monitor = mark->monitor(); temp = monitor->header(); assert (temp->is_neutral(), "invariant") ; //找到header后取hash返回 hash = temp->hash(); if (hash) { return hash; } // Skip to the following code to reduce code size } else if (Self->is_lock_owned((address)mark->locker())) { //轻量级锁的话,也是从java对象头移到了c里,叫helper temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned assert (temp->is_neutral(), "invariant") ; hash = temp->hash(); // by current thread, check if the displaced //找到,返回 if (hash) { // header contains hash code return hash; } }
......略
问:
为什么要先撤销偏向锁到无锁状态,再来生成hashcode呢?这跟锁有什么关系?
答:
mark word里,hashcode存储的字节位置被偏向锁给占了!偏向锁存储了锁持有者的线程id
(参考上面的markword图)
扩展:关于hashCode的生成算法(了解)
// hashCode() generation : // 涉及到c++算法领域,感兴趣的同学自行研究 // Possibilities: // * MD5Digest of {obj,stwRandom} // * CRC32 of {obj,stwRandom} or any linear-feedback shift register function. // * A DES- or AES-style SBox[] mechanism // * One of the Phi-based schemes, such as: // 2654435761 = 2^32 * Phi (golden ratio) // HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stwRandom ; // * A variation of Marsaglia's shift-xor RNG scheme. // * (obj ^ stwRandom) is appealing, but can result // in undesirable regularity in the hashCode values of adjacent objects // (objects allocated back-to-back, in particular). This could potentially // result in hashtable collisions and reduced hashtable efficiency. // There are simple ways to "diffuse" the middle address bits over the // generated hashCode values: // static inline intptr_t get_next_hash(Thread * Self, oop obj) { intptr_t value = 0 ; if (hashCode == 0) { // This form uses an unguarded global Park-Miller RNG, // so it's possible for two threads to race and generate the same RNG. // On MP system we'll have lots of RW access to a global, so the // mechanism induces lots of coherency traffic. value = os::random() ;//返回随机数 } else if (hashCode == 1) { // This variation has the property of being stable (idempotent) // between STW operations. This can be useful in some of the 1-0 // synchronization schemes. //和地址相关,但不是地址;右移+异或算法 intptr_t addrBits = cast_from_oop<intptr_t>(obj) >> 3 ; value = addrBits ^ (addrBits >> 5) ^ GVars.stwRandom ;//随机数位移异或计算 } else if (hashCode == 2) { value = 1 ; // 返回1 } else if (hashCode == 3) { value = ++GVars.hcSequence ;//返回一个Sequence序列号 } else if (hashCode == 4) { value = cast_from_oop<intptr_t>(obj) ;//也不是地址 } else { //常用 // Marsaglia's xor-shift scheme with thread-specific state // This is probably the best overall implementation -- we'll // likely make this the default in future releases. //马萨利亚教授写的xor-shift 随机数算法(异或随机算法) unsigned t = Self->_hashStateX ; t ^= (t << 11) ; Self->_hashStateX = Self->_hashStateY ; Self->_hashStateY = Self->_hashStateZ ; Self->_hashStateZ = Self->_hashStateW ; unsigned v = Self->_hashStateW ; v = (v ^ (v >> 19)) ^ (t ^ (t >> 8)) ; Self->_hashStateW = v ; value = v ; }