标签:分析 set ssi cpp force 获取 oss rpo obj
网上有许多讲偏向锁,轻量级锁的文章,但对偏向锁如何升级讲的不够明白,有些文章还相互矛盾,经过对jvm源码(biasedLocking.cpp)的仔细分析和追踪,基本升级过程有了一个清晰的过程,现将升级流程阐述如下:
因为偏向锁,锁住对象时,会写入对象头相应的标识,我们先把对象头(官方叫法为:Mark Word)的图示如下(借用了网友的图片):
通过上面的图片,我们可以知道,对象处于偏向锁时,mark word中的偏向锁标记为1,锁标志位为01;下面是分析过jvm源码(biasedLocking.cpp)解析的偏向锁升级流程(忽略一些细节),示例中:线程1当前拥有偏向锁对象,线程2是需要竞争到偏向锁。
// The threads lock must be owned at this pointassert_locked_or_safepoint(Threads_lock);// See the comment for this method in thread.hpp for its purpose and// why it is called here.p->initialize_queues();p->set_next(_thread_list);_thread_list = p;_number_of_threads++;oop threadObj = p->threadObj();bool daemon = true;// Bootstrapping problem: threadObj can be null for initial// JavaThread (or for threads attached via JNI)if ((!force_daemon) && (threadObj == NULL || !java_lang_Thread::is_daemon(threadObj))) {_number_of_non_daemon_threads++;daemon = false;}p->set_safepoint_visible(true);ThreadService::add_thread(p, daemon);// Possible GC point.
Events::log(p, "Thread added: " INTPTR_FORMAT, p);
}
for (JavaThread* cur_thread = Threads::first(); cur_thread != NULL; cur_thread = cur_thread->next()) {
标签:分析 set ssi cpp force 获取 oss rpo obj
原文地址:https://www.cnblogs.com/tiancai/p/9382542.html