HPlogo HP-UX MultiProcessing: White Paper > Chapter 1 MultiProcessing

Beta Semaphores

» 

Technical documentation

Complete book in PDF

 » Table of Contents

In some instances the rules governing alpha semaphores are too strict to meet the needs of the kernel. Another class of semaphores exist, known as beta semaphores.

NOTE: Unlike alpha semaphores, beta semaphores can be held while a process sleeps.

Beta semaphores are created in the kernel by a call to b_initsema(). Beta semaphores have services similar to alpha semaphore services. The following table describes the principal kernel interface routines for beta semaphore operations.

Table 1-14 Interface routines used for beta semaphore operations

RoutinePurpose
b_initsema()

Create a beta semaphore, add it to the hash table, link it to the global list of semaphores.

b_termsema()

Unlink beta semaphore from hash chain,

b_psema()

Acquire the semaphore and possibly sleep if not available. Operative assertions: beta semaphore is valid, no spinlocks are held, interrupts are disabled, not in interrupt context.

b_cpsema()

Acquire semaphore and return 0 if available. If not available fail and return 1. Operative assertions: Beta semaphore is valid, interrupts are enabledwhen not on the boot path.

b_vsema()

Release the semaphore. Operative assertions: beta semaphore is valid, interrupts are enabled, semaphore is locked and allowed to unlock.

b_disowns_sema()

Returns true if current kthread does not own the specified beta semaphore.

 

Beta semaphores use a hash table to access the associated spinlock and wait list information (linked list of kthreads).

A kthread at the head of a semaphore's wait queue is allowed to be awakened and yet miss the semaphore a maximum of BETA_MISS_LIMIT times. Other executing code is allowed to acquire the semaphore between the time the semaphore is unlocked by the V operation and the time the awakened kthread can execute and lock it. If the miss limit is reached, the semaphore is passed to the waiting kthread.

The number of misses the kthread at the head of the semaphore's wait queue has taken is maintained in the kthread's proc structure. Each V operation on the semaphore will awaken the kthread at the head of the wait queue and unlock the semaphore if the miss limit has not been reached. If the miss limit is reached, the V operation will awaken the kthread at the end of the wait queue but will not unlock the semaphore, preventing other code from acquiring the semaphore. The awaken kthread notices that the semaphore ownership has been passed to it. This is indicated by the miss count being equal to BETA_MISS_MAX.

Beta Semaphore Structures

The beta semaphore itself contains only the lock and owner information. Both beta semaphore and its hash table are defined in sem_beta.h.

Figure 1-9 Beta semaphore structures

[Beta semaphore structures]

Beta Semaphore Type Definition

The beta semaphore is defined as typedef b_sema_t (also defined as vm_sema_t) and consists of the three fields.

Table 1-15 elements in struct b_sema

ElementPurpose
b_lock

Lock state for the semaphore, which may have one for the following values:

  • 0 = Available

  • 1 = SEM_LOCKED (Semaphore is locked)

  • 2 = SEM_WANT (Semaphore is locked but a thread is waiting on it).

b_order

Indicator of what order the semaphore should be locking in.

*b_owner

Pointer to the kthread structure of the thread of control claiming the semaphore. (This is the only thread information in the beta semaphore structure.)

 

Note, the b_lock field is not a spinlock. The spinlock guarding the beta semaphore is in the hash table.

Beta Semaphore Hash Table

The address of the beta semaphore is indexed into the beta semaphore hash table (bh_sema_t) to obtain the spinlock and waiter information.

Table 1-16 elements in struct bh_sema_t

ElementPurpose
*beta_spinlock

Pointer to the spinlock (type lock_t)

*fp, *bp

Pointers to the struct kthread that comprise a wait list for the beta semaphore. The waiters are linked together using the kthread.kt_wait_list and kthread.kt_rwait_list fields in the thread structure.

*link

Pointer to the link list of beta semaphores.