I know that ReentrantLock use AbstractQueuedSynchronizer(AQS) to implement Lock. But the detail of the realization, I can not understand.
I know that AQS use volatile, CAS and spin for synchronizing. But, these actions only control the "state" member.
Although, LockSupport.park and LockSupport.unpark can synchronize the cacheline of the thread. But if there is never contention, LockSupport methods will never be called. Like this: 1.Thread A start and run
2.Thread B start and run
3.Thread A:
lock.lock();
try{
//modify some shared members
....
}finally{
lock.unlock();
}
4.Then Thread B:
lock.lock();
try{
//read shared members
....
}finally{
lock.unlock();
}
No contention, thread B does not call LockSupport methods.
lock.lock() only CAS "state" member, and lock.unlock() modify the volatile "state" to 0.
Why thread B can seen the modification of thread A about shared members?
Why ReentrantLock can use as "synchronized"?
I did not see any code like fullFence to synchronize memory.
Which code realize the synchronizing of the cacheline of the thread ?
I saw the source code in android art vm and HotSpot vm.
In Android:
It uses std::atomic ->compare_exchange_strong and Heap::WriteBarrierField. These functions like only modify a field, not all the cachelines of a thread.
In HotSpot:
It uses Atomic::cmpxchg_ptr(It's asm code modify a field) and update_barrier_set. These functions also like only modify a field, not update all the cachelines of a thread.
Do I misunderstand?