Complex Locks
Complex locks are read-write locks that protect thread-thread critical sections. Complex locks are preemptable, meaning that a kernel thread can be preempted by another, higher priority kernel thread while it holds a complex lock.
The complex lock kernel services are:
Item | Description |
---|---|
lock_init | Initializes a complex lock. |
lock_islocked | Tests whether a complex lock is locked. |
lock_done | Unlocks a complex lock. |
lock_read, lock_try_read | Locks a complex lock in shared-read mode. |
lock_read_to_write, lock_try_read_to_write | Upgrades a complex lock from shared-read mode to exclusive-write mode. |
lock_write, lock_try_write | Locks a complex lock in exclusive-write mode. |
lock_write_to_read | Downgrades a complex lock from exclusive-write mode to shared-read mode. |
lock_set_recursive | Prepares a complex lock for recursive use. |
lock_clear_recursive | Prevents a complex lock from being acquired recursively. |
By default, complex locks are not recursive (they cannot be acquired in exclusive-write mode multiple times by a single thread). A complex lock can become recursive through the lock_set_recursive kernel service. A recursive complex lock is not freed until lock_done is called once for each time that the lock was locked.
Complex locks are not spin locks; a kernel thread that attempts to acquire a complex lock may spin briefly (busy-wait: repeatedly execute instructions which do nothing) if the lock is not free. The table shows the behavior of kernel threads that attempt to acquire a busy complex lock:
Current Lock Mode | Owner is Running and no Other Thread is Asleep on This Lock | Owner is Sleeping |
---|---|---|
Exclusive-write | Caller spins initially, but sleeps if the maximum spin threshold is crossed, or if the owner later sleeps. | Caller sleeps immediately. |
Shared-read being acquired for exclusive-write | Caller sleeps immediately. | |
Shared-read being acquired for shared-read | Lock granted immediately |
- On uniprocessor systems, the maximum spin threshold is set to one, meaning that a kernel thread will never spin waiting for a lock.
- The concept of a single owner does not apply to a lock held in shared-read mode.