(turn) CLH queue lock
CLH lock is Craig, Landin, and Hagersten (CLH) locks, CLH lock is a spin lock, can ensure no hunger, provide fairness first come first service.
The CLH lock is a scalable, high performance, fairness and spin lock based on the list, the application thread spin only on a local variable, it constantly polling the precursor state, if it is found that the pre release lock end spin.
SMP(Symmetric Multi-Processor), Symmetric multi processor architecture, referring to multiple CPU symmetry in the server, the time required for each CPU to access the same memory address. Its main characteristic is shared, include CPU, memory, I/O share. The advantage of the SMP is to ensure memory consistency, the disadvantage is that these shared resources may become a performance bottleneck, with the increase in the number of CPU, each CPU must have access to the same memory resources, likely to cause memory access conflict, may cause the CPU waste of resources. PC machine used to belong to this.
NUMA(Non-Uniform Memory Access)Non uniform memory access, CPU is divided into CPU blocks, each CPU module is composed of a plurality of CPU, and have independent local memory, I/O slot, the modules are mutually accessible through Internet access local memory module, the speed will be much higher than the access remote memory (the other nodes in the system speed, memory) this is consistent with non origin memory access NUMA. NUMA advantage is that it can effectively solve the problem extends the original SMP system, due to the shortcomings of remote memory access latency than local memory, so when CPU increases, the system performance can not increase linearly.
When a thread needs to acquire the lock time, Will create a new QNode, The locked is set to true expressed the need to acquire the lock, Then call the getAndSet method on the tail thread, Make oneself become the queue tail, At the same time to obtain a pointer to its predecessor cited myPred, then the thread in the locked field precursor node rotation, Release the lock until the predecessor node. When a thread needs to release the lock, the locked domain for the current node is set to false, at the same time, recovery of precursor node. As shown below, the thread A need to acquire the lock, the myNode domain of true, some tail pointing to the thread of A nodes, then the thread B is added to the thread behind A, tail refers to the B thread node. Then the thread A and B in the myPred domain of its rotation, locked field of a myPred node it into the false, it can acquire the lock sweep line. Obvious thread A locked domain for false myPred, the thread A access to the lock.
The CLH code is as follows, which uses the ThreadLocal class, binding QNode to each thread, also used the AtomicReference, the tail pointer modification is called getAndSet (it) operation, it can guarantee the atomically update object reference.
[java] view plaincopy
As you can see from the code of lock method in a while loop, which is a locked domain in waiting for the predecessor node to the false, this is a process of spin waiting. The unlock method is very simple, only needs to be your locked domain can be set to false.
Thread lock series(1): CLH Lock
The Art of Multiprocessor Programming
Posted by Heidi at August 21, 2014 - 7:59 PM