Writing Device Drivers

Readers/Writer Locks

A readers/writer lock regulates access to a set of data. The readers/writer lock is so called because many threads can hold the lock simultaneously for reading, but only one thread can hold it for writing.

Most device drivers do not use readers/writer locks. These locks are slower than mutexes and provide a performance gain only when protecting data that is not frequently written but is commonly read by many concurrent threads. In this case, contention for a mutex could become a bottleneck, so using a readers/writer lock might be more efficient. The readers/writer functions are summarized in the following table. See the rwlock(9F) man page for detailed information.

Table 3–2 Readers/Writer Locks

Name 

Description 

rw_init(9F)

Initializes a readers/writer lock 

rw_destroy(9F)

Destroys a readers/writer lock 

rw_enter(9F)

Acquires a readers/writer lock 

rw_tryenter

Attempts to acquire a reader/writer lock without waiting 

rw_tryupgrade(9F)

Attempts to upgrade readers/writer lock holding from reader to writer 

rw_downgrade(9F)

Downgrades a readers/writer lock holding from writer to reader 

rw_exit(9F)

Releases a readers/writer lock 

rw_read_locked(9F)

Determines whether readers/writer lock is held for read or write