synchronized源码

bytecodeInterpreter.cpp

// Lock method if synchronized.
if (METHOD->is_synchronized()) {
 // oop rcvr = locals[0].j.r;
 oop rcvr;
 if (METHOD->is_static()) {
   rcvr = METHOD->constants()->pool_holder()->java_mirror();
 } else {
   rcvr = LOCALS_OBJECT(0);
   VERIFY_OOP(rcvr);
 }
 // The initial monitor is ours for the taking.
 // Monitor not filled in frame manager any longer as this caused race condition with biased locking.
 BasicObjectLock* mon = &istate->monitor_base()[-1];
 mon->set_obj(rcvr);
 bool success = false;
 uintptr_t epoch_mask_in_place = (uintptr_t)markOopDesc::epoch_mask_in_place;
 markOop mark = rcvr->mark();
 intptr_t hash = (intptr_t) markOopDesc::no_hash;
 // Implies UseBiasedLocking.
 if (mark->has_bias_pattern()) {
   uintptr_t thread_ident;
   uintptr_t anticipated_bias_locking_value;
   thread_ident = (uintptr_t)istate->thread();
   anticipated_bias_locking_value =
     (((uintptr_t)rcvr->klass()->prototype_header() | thread_ident) ^ (uintptr_t)mark) &
     ~((uintptr_t) markOopDesc::age_mask_in_place);

   if (anticipated_bias_locking_value == 0) {
     // Already biased towards this thread, nothing to do.
     if (PrintBiasedLockingStatistics) {
       (* BiasedLocking::biased_lock_entry_count_addr())++;
     }
     success = true;
   } else if ((anticipated_bias_locking_value & markOopDesc::biased_lock_mask_in_place) != 0) {
     // Try to revoke bias.
     markOop header = rcvr->klass()->prototype_header();
     if (hash != markOopDesc::no_hash) {
       header = header->copy_set_hash(hash);
     }
     if (rcvr->cas_set_mark(header, mark) == mark) {
       if (PrintBiasedLockingStatistics)
         (*BiasedLocking::revoked_lock_entry_count_addr())++;
     }
   } else if ((anticipated_bias_locking_value & epoch_mask_in_place) != 0) {
     // Try to rebias.
     markOop new_header = (markOop) ( (intptr_t) rcvr->klass()->prototype_header() | thread_ident);
     if (hash != markOopDesc::no_hash) {
       new_header = new_header->copy_set_hash(hash);
     }
     if (rcvr->cas_set_mark(new_header, mark) == mark) {
       if (PrintBiasedLockingStatistics) {
         (* BiasedLocking::rebiased_lock_entry_count_addr())++;
       }
     } else {
       CALL_VM(InterpreterRuntime::monitorenter(THREAD, mon), handle_exception);
     }
     success = true;
   } else {
     // Try to bias towards thread in case object is anonymously biased.
     markOop header = (markOop) ((uintptr_t) mark &
                                 ((uintptr_t)markOopDesc::biased_lock_mask_in_place |
                                  (uintptr_t)markOopDesc::age_mask_in_place | epoch_mask_in_place));
     if (hash != markOopDesc::no_hash) {
       header = header->copy_set_hash(hash);
     }
     markOop new_header = (markOop) ((uintptr_t) header | thread_ident);
     // Debugging hint.
     DEBUG_ONLY(mon->lock()->set_displaced_header((markOop) (uintptr_t) 0xdeaddead);)
     if (rcvr->cas_set_mark(new_header, header) == header) {
       if (PrintBiasedLockingStatistics) {
         (* BiasedLocking::anonymously_biased_lock_entry_count_addr())++;
       }
     } else {
       CALL_VM(InterpreterRuntime::monitorenter(THREAD, mon), handle_exception);
     }
     success = true;
   }
 }

 // Traditional lightweight locking.
 if (!success) {
   markOop displaced = rcvr->mark()->set_unlocked();
   mon->lock()->set_displaced_header(displaced);
   bool call_vm = UseHeavyMonitors;
   if (call_vm || rcvr->cas_set_mark((markOop)mon, displaced) != displaced) {
     // Is it simple recursive case?
     if (!call_vm && THREAD->is_lock_owned((address) displaced->clear_lock_bits())) {
       mon->lock()->set_displaced_header(NULL);
     } else {
       CALL_VM(InterpreterRuntime::monitorenter(THREAD, mon), handle_exception);
     }
   }
 }
}
THREAD->clr_do_not_unlock();

synchronizer.cpp

intptr_t ObjectSynchronizer::FastHashCode(Thread * Self, oop obj) {
  if (UseBiasedLocking) {
    // NOTE: many places throughout the JVM do not expect a safepoint
    // to be taken here, in particular most operations on perm gen
    // objects. However, we only ever bias Java instances and all of
    // the call sites of identity_hash that might revoke biases have
    // been checked to make sure they can handle a safepoint. The
    // added check of the bias pattern is to avoid useless calls to
    // thread-local storage.
    if (obj->mark()->has_bias_pattern()) {
      // Handle for oop obj in case of STW safepoint
      Handle hobj(Self, obj);
      // Relaxing assertion for bug 6320749.
      assert(Universe::verify_in_progress() ||
             !SafepointSynchronize::is_at_safepoint(),
             "biases should not be seen by VM thread here");
      BiasedLocking::revoke_and_rebias(hobj, false, JavaThread::current());
      obj = hobj();
      assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
    }
  }

  // hashCode() is a heap mutator ...
  // Relaxing assertion for bug 6320749.
  assert(Universe::verify_in_progress() || DumpSharedSpaces ||
         !SafepointSynchronize::is_at_safepoint(), "invariant");
  assert(Universe::verify_in_progress() || DumpSharedSpaces ||
         Self->is_Java_thread() , "invariant");
  assert(Universe::verify_in_progress() || DumpSharedSpaces ||
         ((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant");

  ObjectMonitor* monitor = NULL;
  markOop temp, test;
  intptr_t hash;
  markOop mark = ReadStableMark(obj);

  // object should remain ineligible for biased locking
  assert(!mark->has_bias_pattern(), "invariant");

  if (mark->is_neutral()) {
    hash = mark->hash();              // this is a normal header
    if (hash) {                       // if it has hash, just return it
      return hash;
    }
    hash = get_next_hash(Self, obj);  // allocate a new hash code
    temp = mark->copy_set_hash(hash); // merge the hash code into header
    // use (machine word version) atomic operation to install the hash
    test = obj->cas_set_mark(temp, 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.
  } else if (mark->has_monitor()) {
    monitor = mark->monitor();
    temp = monitor->header();
    assert(temp->is_neutral(), "invariant");
    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())) {
    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;
    }
    // WARNING:
    //   The displaced header is strictly immutable.
    // It can NOT be changed in ANY cases. So we have
    // to inflate the header into heavyweight monitor
    // even the current thread owns the lock. The reason
    // is the BasicLock (stack slot) will be asynchronously
    // read by other threads during the inflate() function.
    // Any change to stack may not propagate to other threads
    // correctly.
  }

  // Inflate the monitor to set hash code
  monitor = ObjectSynchronizer::inflate(Self, obj, inflate_cause_hash_code);
  // Load displaced header and check it has hash code
  mark = monitor->header();
  assert(mark->is_neutral(), "invariant");
  hash = mark->hash();
  if (hash == 0) {
    hash = get_next_hash(Self, obj);
    temp = mark->copy_set_hash(hash); // merge hash code into header
    assert(temp->is_neutral(), "invariant");
    test = Atomic::cmpxchg(temp, monitor->header_addr(), mark);
    if (test != mark) {
      // The only update to the header in the monitor (outside GC)
      // is install the hash code. If someone add new usage of
      // displaced header, please update this code
      hash = test->hash();
      assert(test->is_neutral(), "invariant");
      assert(hash != 0, "Trivial unexpected object/monitor header usage.");
    }
  }
  // We finally get the hash
  return hash;
}

轻量级

// -----------------------------------------------------------------------------
// Interpreter/Compiler Slow Case
// This routine is used to handle interpreter/compiler slow case
// We don't need to use fast path here, because it must have been
// failed in the interpreter/compiler code.
void ObjectSynchronizer::slow_enter(Handle obj, BasicLock* lock, TRAPS) {
  markOop mark = obj->mark();
  assert(!mark->has_bias_pattern(), "should not see bias pattern here");

  if (mark->is_neutral()) {
    // Anticipate successful CAS -- the ST of the displaced mark must
    // be visible <= the ST performed by the CAS.
    lock->set_displaced_header(mark);
    if (mark == obj()->cas_set_mark((markOop) lock, mark)) {
      return;
    }
    // Fall through to inflate() ...
  } else if (mark->has_locker() &&
             THREAD->is_lock_owned((address)mark->locker())) {
    assert(lock != mark->locker(), "must not re-lock the same lock");
    assert(lock != (BasicLock*)obj->mark(), "don't relock with same BasicLock");
    lock->set_displaced_header(NULL);
    return;
  }

  // The object header will never be displaced to this lock,
  // so it does not matter what the value is, except that it
  // must be non-zero to avoid looking like a re-entrant lock,
  // and must not look locked either.
  lock->set_displaced_header(markOopDesc::unused_mark());
  ObjectSynchronizer::inflate(THREAD,
                              obj(),
                              inflate_cause_monitor_enter)->enter(THREAD);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lang20150928

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值