内核提供了一个自旋锁的读者/写者形式, 直接模仿我们在本章前面见到的读者/写者旗标. 这些锁允许任何数目的读者同时进入临界区, 但是写者必须是排他的存取.
1)多个读者可以同时进行读
2)写者必须互斥(只允许一个写者写,也不能读者写者同时进行)
3)写者优先于读者(一旦有写者,则后续读者必须等待,唤醒时优先考虑写者)
读者写者锁有一个类型 rwlock_t, 在 <linux/spinlokc.h> 中定义.
它们可以以 2 种方式被声明和被初始化:
rwlock_t my_rwlock = RW_LOCK_UNLOCKED; /* Static way */ rwlock_t my_rwlock; rwlock_init(&my_rwlock); /* Dynamic way */
可用函数的列表现在应当看来相当类似. 对于读者, 下列函数是可用的:
void read_lock(rwlock_t *lock); void read_lock_irqsave(rwlock_t *lock, unsigned long flags); void read_lock_irq(rwlock_t *lock); void read_lock_bh(rwlock_t *lock); void read_unlock(rwlock_t *lock); void read_unlock_irqrestore(rwlock_t *lock, unsigned long flags); void read_unlock_irq(rwlock_t *lock); void read_unlock_bh(rwlock_t *lock);
对于写存取的函数是类似的:
void write_lock(rwlock_t *lock); void write_lock_irqsave(rwlock_t *lock, unsigned long flags); void write_lock_irq(rwlock_t *lock); void write_lock_bh(rwlock_t *lock); int write_trylock(rwlock_t *lock); void write_unlock(rwlock_t *lock); void write_unlock_irqrestore(rwlock_t *lock, unsigned long flags); void write_unlock_irq(rwlock_t *lock); void write_unlock_bh(rwlock_t *lock);
---- rq_list_lock Matches (26 in 1 files) ----
Sg.c (drivers\scsi): rwlock_t rq_list_lock; /* protect access to list in req_arr */Sg.c (drivers\scsi): write_lock_irq(&sfp->rq_list_lock);
Sg.c (drivers\scsi): write_unlock_irq(&sfp->rq_list_lock);
Sg.c (drivers\scsi): read_lock_irqsave(&sfp->rq_list_lock, iflags);
Sg.c (drivers\scsi): read_unlock_irqrestore(&sfp->rq_list_lock,
Sg.c (drivers\scsi): write_lock_irqsave(&sfp->rq_list_lock, iflags);
Sg.c (drivers\scsi): write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
Sg.c (drivers\scsi): rwlock_init(&sfp->rq_list_lock);
Sg.c (drivers\scsi): read_lock(&fp->rq_list_lock); /* irqs already disabled */
Sg.c (drivers\scsi): read_unlock(&fp->rq_list_lock);