Sequantial Lock in Java

Source: Internet
Author: User

1 Overview
Common synchronization mechanisms in Linux kernel include Atomic Operation, Spin Locks, Semaphore, and Mutex. Both Spin Locks and Semaphore support read/write Locks. In addition, the Linux kernel supports a more lightweight read/write Lock mechanism: Sequential Lock. Compared with other read/write locking mechanisms, Sequential Lock has the following features:
Write locks are preferred when obtaining locks. As long as others do not hold the write lock, the operation to obtain the write lock will always succeed.
The read operation does not require a lock (that is, the read operation will never be blocked). However, in some cases, the read operation must be retried.
Sequential Lock is more lightweight and has better scalability.
2 Linux Kernels Implementation
The Sequential Lock implementation in kernel 2.6 is as follows:
C code
Typedef struct {
Unsigned sequence;
Spinlock_t lock;
} Seqlock_t;

Static inline void write_seqlock (seqlock_t * sl)
{
Spin_lock (& sl-> lock );
++ Sl-> sequence;
Smp_wmb ();
}

Static inline void write_sequnlock (seqlock_t * sl)
{
Smp_wmb ();
Sl-> sequence ++;
Spin_unlock (& sl-> lock );
}

Static _ always_inline unsigned read_seqbegin (const seqlock_t * sl)
{
Unsigned ret = sl-> sequence;
Smp_ RMB ();
Return ret;
}

Static _ always_inline int read_seqretry (const seqlock_t * sl, unsigned iv)
{
Smp_ RMB ();
Return (iv & 1) | (sl-> sequence ^ iv );
}
In the above Code, spin lock ensures the atomicity of write operations. Note that Linux spin lock is a non-reentrant lock, which is also the reason why the read_seqretry method can judge whether a write operation is in progress through (iv & 1. In addition, Linux spin lock can only ensure atomicity. In order to avoid the re-sorting of commands by the compiler or processor (for example, the x86 platform does not resort write operations, but it may cause potential problems due to the re-sorting of read operations. seqlock uses memory barrier (smp_wmb and smp_ RMB) to ensure memory visibility.

The use of seqlock is as follows:
To define a seqlock:
C code
Seqlock_t mr_seq_lock = DEFINE_SEQLOCK (mr_seq_lock );
The write path:
C code
Write_seqlock (& mr_seq_lock );
/* Write lock is obtained ...*/
Write_sequnlock (& mr_seq_lock );
The read path:
C code
Unsigned long seq;
Do {
Seq = read_seqbegin (& mr_seq_lock );
/* Read data here ...*/
} While (read_seqretry (& mr_seq_lock, seq ));
 
The typical use cases of seqlock are as follows:
Compared with the number of read operations, the number of write operations is very small.
The write operation is preferred when the lock is obtained.
Considerations for using seqlock:
The read operation time should be relatively short; otherwise, it may lead to multiple read retries.
Since the write operation may be modifying data during reading, the read operation may read an inconsistent state. Therefore, during reading, protective verification is required to avoid reading errors due to inconsistent error data.
3 Sequential Lock in Java
I wanted to implement Sequential lock of the Java version by myself, but after Google, I found that Makoto YUI has provided a better implementation. The implementation after slight modification is as follows:
Java code
Public final class SequentialLock {
//
Private volatile int counter;

/**
*
*/
Public int readBegin (){
Return this. counter;
}

Public boolean readRetry (int v ){
Return (v & 1) = 1 | this. counter! = V;
}

Public synchronized void writeLock (){
//
If (this. counter & 1 )! = 0 ){
Throw new IllegalStateException ("sequential lock is NOT reentrantable ");
}

//
++ This. counter;
}

Public synchronized void writeUnlock (){
++ This. counter;
}
}
First, SequentialLock is not reentrant. Because the read operation does not obtain the lock, additional mechanisms are required between read and write operations to ensure memory visibility: read and Write its volitle counter member variables ensure the happens bofore semantics between data protected by Sequential Lock.

4 Reference
Linux Kernel Development 3rd Edition, Robert Love

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.