java 锁 null_java synchronized锁对象,但是当对象引用是null的时候,锁的是什么?...

/* monitorenter and monitorexit for locking/unlocking an object */

CASE(_monitorenter): {

oop lockee = STACK_OBJECT(-1);

// derefing's lockee ought to provoke implicit null check CHECK_NULL(lockee);

// find a free monitor or one already allocated for this object // if we find a matching object then we need a new monitor // since this is recursive enter BasicObjectLock* limit = istate->monitor_base();

BasicObjectLock* most_recent = (BasicObjectLock*) istate->stack_base();

BasicObjectLock* entry = NULL;

while (most_recent != limit ) {

if (most_recent->obj() == NULL) entry = most_recent;

else if (most_recent->obj() == lockee) break;

most_recent++;

}

if (entry != NULL) {

entry->set_obj(lockee);

markOop displaced = lockee->mark()->set_unlocked();

entry->lock()->set_displaced_header(displaced);

if (Atomic::cmpxchg_ptr(entry, lockee->mark_addr(), displaced) != displaced) {

// Is it simple recursive case? if (THREAD->is_lock_owned((address) displaced->clear_lock_bits())) {

entry->lock()->set_displaced_header(NULL);

} else {

CALL_VM(InterpreterRuntime::monitorenter(THREAD, entry), handle_exception);

}

}

UPDATE_PC_AND_TOS_AND_CONTINUE(1, -1);

} else {

istate->set_msg(more_monitors);

UPDATE_PC_AND_RETURN(0); // Re-execute }

}

CASE(_monitorexit): {

oop lockee = STACK_OBJECT(-1);

CHECK_NULL(lockee);

// derefing's lockee ought to provoke implicit null check // find our monitor slot BasicObjectLock* limit = istate->monitor_base();

BasicObjectLock* most_recent = (BasicObjectLock*) istate->stack_base();

while (most_recent != limit ) {

if ((most_recent)->obj() == lockee) {

BasicLock* lock = most_recent->lock();

markOop header = lock->displaced_header();

most_recent->set_obj(NULL);

// If it isn't recursive we either must swap old header or call the runtime if (header != NULL) {

if (Atomic::cmpxchg_ptr(header, lockee->mark_addr(), lock) != lock) {

// restore object for the slow case most_recent->set_obj(lockee);

CALL_VM(InterpreterRuntime::monitorexit(THREAD, most_recent), handle_exception);

}

}

UPDATE_PC_AND_TOS_AND_CONTINUE(1, -1);

}

most_recent++;

}

// Need to throw illegal monitor state exception CALL_VM(InterpreterRuntime::throw_illegal_monitor_state_exception(THREAD), handle_exception);

ShouldNotReachHere();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值