[toc]
- Posted by 微博@Yangsc_o
- 原創(chuàng)文章陵刹,版權(quán)聲明:自由轉(zhuǎn)載-非商用-非衍生-保持署名 | Creative Commons BY-NC-ND 3.0
摘要
本文主要介紹park啃洋、unpark的功能以及hotspot實現(xiàn)原理霞怀,為下一篇介紹ReentrantLock做鋪墊惫东!
park、unpark
LockSupport類是Java6引入的一個類毙石,提供了基本的線程同步原語廉沮。LockSupport實際上是調(diào)用了Unsafe類里的函數(shù),歸結(jié)到Unsafe里徐矩,兩個函數(shù)但為上層提供了強大的同步原語滞时。
public native void unpark(Thread jthread);
public native void park(boolean isAbsolute, long time);
// isAbsolute參數(shù)是指明時間是絕對的,還是相對的滤灯。
unpark函數(shù)為線程提供“許可(permit)”坪稽,線程調(diào)用park函數(shù)則等待“許可”。這個有點像信號量力喷,但是這個“許可”是不能疊加的刽漂,“許可”是一次性的。
比如線程B連續(xù)調(diào)用了三次unpark函數(shù)弟孟,當線程A調(diào)用park函數(shù)就使用掉這個“許可”贝咙,如果線程A再次調(diào)用park,則進入等待狀態(tài)拂募。
看一下hotspot實現(xiàn)
在Parker類里的_counter字段庭猩,就是用來記錄所謂的“許可”的。
位置:hotspot/src/share/vm/runtime/park.hpp
簡化代碼如下
class Parker : public os::PlatformParker {
private:
volatile int _counter ;
Parker * FreeNext ;
JavaThread * AssociatedWith ; // Current association
public:
Parker() : PlatformParker() {
_counter = 0 ;
FreeNext = NULL ;
AssociatedWith = NULL ;
}
public:
// For simplicity of interface with Java, all forms of park (indefinite,
// relative, and absolute) are multiplexed into one call.
void park(bool isAbsolute, jlong time);
void unpark();
};
-
park
簡化代碼如下:當調(diào)用park時陈症,先嘗試直接能否直接拿到“許可”蔼水,即_counter>0時,如果成功录肯,則把_counter設置為0,并返回;
如果不成功趴腋,則構(gòu)造一個ThreadBlockInVM,然后檢查_counter是不是>0,如果是优炬,則把_counter設置為0颁井,unlock mutex并返回;
-
Java dos里提到,當下面三種情況下park函數(shù)會返回蠢护,在這段代碼里也可以體現(xiàn)雅宾;
Some other thread invokes unpark with the current thread as the target; or
Some other thread interrupts the current thread; or
The call spuriously (that is, for no reason) returns.
void Parker::park(bool isAbsolute, jlong time) {
if (Atomic::xchg(0, &_counter) > 0) return;
Thread* thread = Thread::current();
assert(thread->is_Java_thread(), "Must be JavaThread");
JavaThread *jt = (JavaThread *)thread;
if (Thread::is_interrupted(thread, false)) {
return;
}
// Next, demultiplex/decode time arguments
struct timespec absTime;
if (time < 0 || (isAbsolute && time == 0) ) { // don't wait at all
return;
}
if (time > 0) {
unpackTime(&absTime, isAbsolute, time);
}
...
if (Thread::is_interrupted(thread, false) || pthread_mutex_trylock(_mutex) != 0) {
return;
}
int status ;
if (_counter > 0) { // no wait needed
_counter = 0;
status = pthread_mutex_unlock(_mutex);
assert (status == 0, "invariant") ;
// Paranoia to ensure our locked and lock-free paths interact
// correctly with each other and Java-level accesses.
OrderAccess::fence();
return;
}
- unpark
unpark時,設置_counter為1葵硕;如果_counter = 1眉抬,unlock mutext返回。如果_counter=0懈凹,需要調(diào)用pthread_cond_signal喚醒在park中等待的線程:
void Parker::unpark() {
int s, status ;
status = pthread_mutex_lock(_mutex);
assert (status == 0, "invariant") ;
s = _counter;
_counter = 1;
if (s < 1) {
if (WorkAroundNPTLTimedWaitHang) {
status = pthread_cond_signal (_cond) ;
assert (status == 0, "invariant") ;
status = pthread_mutex_unlock(_mutex);
assert (status == 0, "invariant") ;
} else {
status = pthread_mutex_unlock(_mutex);
assert (status == 0, "invariant") ;
status = pthread_cond_signal (_cond) ;
assert (status == 0, "invariant") ;
}
} else {
pthread_mutex_unlock(_mutex);
assert (status == 0, "invariant") ;
}
}
自此park蜀变、unpark已經(jīng)分析完畢;
參考
Java的LockSupport.park()實現(xiàn)分析