IRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorexit(JavaThread* thread, BasicObjectLock* elem))
#ifdef ASSERT
thread->last_frame().interpreter_frame_verify_monitor(elem);
#endif
Handle h_obj(thread, elem->obj());
assert(Universe::heap()->is_in_reserved_or_null(h_obj()),
"must be NULL or an object");
if (elem == NULL || h_obj()->is_unlocked()) {
THROW(vmSymbols::java_lang_IllegalMonitorStateException());
}
// 直接调用 slow_exit
ObjectSynchronizer::slow_exit(h_obj(), elem->lock(), thread);
// Free entry. This must be done here, since a pending exception might be installed on
// exit. If it is not cleared, the exception handling code will try to unlock the monitor again.
elem->set_obj(NULL);
#ifdef ASSERT
thread->last_frame().interpreter_frame_verify_monitor(elem);
#endif
IRT_END
synchronized 分析
获取轻量级锁
bytecodeInterpreter | #define ARRAY_STOREFROM64 | CASE(_monitorenter)
源码: https://github.com/openjdk/jdk8u/blob/master/hotspot/src/share/vm/interpreter/bytecodeInterpreter.cpp#L1816-L1921
竞争轻量级锁
ObjectSynchronizer::slow_enter 方法分析
源码: https://github.com/openjdk/jdk8u/blob/master/hotspot/src/share/vm/runtime/synchronizer.cpp#L229-L266
释放轻量级锁
bytecodeInterpreter 方法 bytecodeInterpreter | #define ARRAY_STOREFROM64 | CASE(_monitorexit)
源码: https://github.com/openjdk/jdk8u/blob/master/hotspot/src/share/vm/interpreter/bytecodeInterpreter.cpp#L1923-L1953
InterpreterRuntime::monitorexit 方法
源码: https://github.com/openjdk/jdk8u/blob/master/hotspot/src/share/vm/interpreter/interpreterRuntime.cpp#L645-L662
ObjectSynchronizer::slow_exit 方法
源码: https://github.com/openjdk/jdk8u/blob/master/hotspot/src/share/vm/runtime/synchronizer.cpp#L272-L274
ObjectSynchronizer:: fast_exit 方法
源码: https://github.com/openjdk/jdk8u/blob/master/hotspot/src/share/vm/runtime/synchronizer.cpp#L186-L222