|
|
HP-UX MultiProcessing: White Paper > Chapter 1 MultiProcessingSemaphores |
|
Semaphores are routines that ensure orderly access to regions of code. Like spinlocks, semaphores guard kernel data structures by controlling access to regions of code associated with a set of data structures. Unlike spinlocks, semaphores require the waiting thread to relinquish the CPU while awaiting the lock. Semaphores are implemented using a swtch() to allow another thread to run. Semaphores serve two functions mutual exclusion and synchronization. Mutual-exclusion semaphores protect data and are further classified by their degree of restrictiveness. Mutual-exclusion semaphores provide mutually exclusive access to regions of code that are associated with a set of data structures. In a mutual-exclusion semaphore, a processor attempting to acquire a semaphore already held by another processor puts its current thread of control to sleep and switches to another. It is assumed that the expected time duration the thread will wait while the lock is busy will be much greater than the overhead of a process switch. The kernel makes available two types of mutual-exclusion semaphores:
Synchronization semaphores signal events rather than block access to data structures and are used when events are awaited. They synchronize a thread with other threads and external events. The table that follows describes some of these differences in practice. Data protection (blocking or mutual exclusion) semaphores and synchronization semaphores differ in four ways.
Table 1-7 Differences between mutual-exclusion and synchronization semaphores
The table below shows some of the semaphores the kernel creates at initialization time from init_semaphores() and realmain(). They are listed only by type and name. You can look at the kernel source to observe how each are used. Table 1-8 A Sampling of semaphores
Some alpha semaphores are classified as empire semaphores, because they protect data structures for an entire subsystem. Empire semaphores are locked when any of the structures within the set must be modified. Because they control access to an entire subsystem, empire semaphores are used to serialize operations within the subsystem. For example, the filesystem empire (filesys_sema) is locked when calling sync(), to prevent other threads from invalidating pages of data that are being flushed from cache. Empire semaphores are acquired and released with pxsema()/vxsema() calls. The up_io_sema empire semaphore provides "single threading" (access control) for I/O drivers that are not MP safe. An MP-safe driver is one that synchronizes multiple accesses to code and structures, so that more than one instance of the driver may be active at any given time without contention. |
|