Reader Writer Locks

The mechanisms described above have one disadvantage. They do not differentiate between situations in which data structures are simply read and those in which they are actively manipulated. Usually, any number of processes are granted concurrent read access to data structures, whereas write access must be restricted exclusively to a single task.

The kernel therefore provides additional semaphore and spinlock versions to cater for the above — these are known accordingly as Reader/Writer semaphores and Reader/Writer spinlocks.

The rwlock_t data type is defined for Reader/Writer spinlocks. Locks must be acquired in different ways in order to differentiate between read and write access.

□ read_lock and read_unlock must be executed before and after a critical region to which a process requires read access. The kernel grants any number of read processes concurrent access to the critical region.

□ write_lock and write_unlock are used for write access. The kernel ensures that only one writer (and no readers) is in the region.

An _irq _irqsave variant is also available and functions in the same way as normal spinlocks. Variants ending in _bh are also available. They disable software interrupts, but leave hardware interrupts still enabled.

Read/write semaphores are used in a similar way. The equivalent data structure is struct rw_semaphore, and down_read and up_read are used to obtain read access to the critical region. Write access is performed with the help of down_write and up_write. The _trylock variants are also available for all commands — they also function as described above.

Continue reading here: The Big Kernel Lock

Was this article helpful?

0 0