Hello,
Gaurav Dhiman wrote:
If we look at the rwlock_t structure shown above (which is actually the read write lock), it is same as spinlock with only one lock element (lock). Now, my question is, as there is only one lock element in the structure, how can a second or third reader come to know if the lock has been acquired by a reader or by a writer. If the lock has been acquired by reader, then this second or third reader should be allowed to enter c-region, else if it has already been acquired by writer, this reader should not be allowed to enter, but how we will come to know who actually acquired the lock (was that a reader or writer). I think there should be two lock elemets in the read-write spinlocks to differentiate between the concept of read lock and write lock.
You're right, but in fact, the implementation has been so that both information are encoded inside a single integer. You don't really need two counters, but :
- a counter of readers
- a bit indicating whether a writer owns the lock or not
So, in fact, one bit of the integer is reserved to "mark" the rwlock as owned by a writer or not. Have a look at include/asm-i386/rwlock.h, and particularly the definition of RW_LOCK_BIAS.
Have a look at the implementation of __build_read_lock_const() and __build_write_lock_const() is the same file.
Sincerly,
Thomas -- Thomas Petazzoni thomas.petazzoni@xxxxxxxx
-- Kernelnewbies: Help each other learn about the Linux kernel. Archive: http://mail.nl.linux.org/kernelnewbies/ FAQ: http://kernelnewbies.org/faq/