Locking and Synchronization in Solaris kernel

1. Parallel System Architecture
SMP - Symmetric multiprocessor with a shared memory model; single kernel image
MPP - Message-based model; multiple kernel images
NUMA/ccNUMA - Shared memory model; single kernel images

2. Hardware Considerations for Locks and Syncronization
Consideration:
(1) need For an automatic test-and-set instruction for locking primitives
(2) Data global visibility issue because of the use of hardware load and store buffers and instruction reordering
Solution:
(1) Use of native machine instructions (cmpxchgl on x86)
(2) Use of memory barrier instructions

3. Synchronization Objects in Solaris
(1) mutex lock (exclusive read and write access to data)
(2) read/write lock (multiple readers are allowable but only one writer is allowed)
(3) semaphores (access to a finite number of resources)
(4) dispatcher lock (??)
(5) condition variables (not a type of lock, used for thread syncrhronization and an integral part of the kernel sleep/wakeup facility)

4. Mutex lock
The kernel implements two types of mutex locks: spin locks and adaptive locks. Spin locks spin in a tight loop if a desired lock is being held when a thread attempts to acquire the lock. Adaptive locks are the most common type of lock used and are designed to dynamically either spin or block when a lock is being held, depending on the state of the holder. If the lock holder is running on a processor, the thread attempting to get the lock will spin, otherwise, block (sleep and yield the processor to other threads).
High level interrupts (interrupt levels 11-15) are not allowed to block, so only spin locks can be used in high-level interrupt handlers. Also, spin locks can raise the interrupt level of the processor when the lock is acquired. In a word, spin locks are not allowed to used in any places when sleep and context switch is not allowed.
Mutex lock interface:

mutex_init() initialize a lock, the type of the lock is specified (spin or adaptive)
mutex_enter() accquire the lock
mutex_exit() release the lock

mutex(9F) for more information.

5. Read write lock
The interface for read write lock is:

rw_init() initialize a rw lock
rw_enter() accquire the lock
rw_exit() release the lock

rwlock(9F) for more information

你可能感兴趣的:(thread,Solaris,Access)