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

Alpha Semaphore Services

» 

Technical documentation

Complete book in PDF

 » Table of Contents

The kernel uses several different kinds of routines to manage alpha semaphores:

  • Initialize an alpha semaphore.

  • Acquire/release a semphore while adjust priority.

  • Acquire/release semaphore across empires.

  • Bind/unbind semaphore to a kernel thread.

  • Test for whether a kernel thread owns semaphores.

  • Arbitrate for an alpha semaphore.

NOTE: The services to acquire a semaphore begin with the letter P; those to release a semaphore begin with the letter V. These derive from the Dutch words Proberen, meaning "to test" and Verhogen, meaning "to increment."

Acquire and Release an Alpha Semaphore

Table 1-10 Acquisition and release of an alpha semaphore

External InterfacePurpose
initsema(semaphore,value,priority,order)

Initialize a mutual-exclusion semaphore

  • Must be called before a semaphore is used.

  • Must not be called when a semaphore is actively being used by the kernel.

psema(semaphore)
vsema(semaphore)

Acquire, release a mutual exclusion semaphore.

  • psema() acquires the semaphore by decrementing the semaphore count.

    • If the count is non-negative, the thread acquires the semaphore.

    • If the count is negative, the priority of the calling thread is raised and the thread blocks until the semaphore is available.

  • vsema() releases the semaphore; it does not adjust the priority, but delays this until the process leaves the kernel.

pxsema(semphore, save)
vxsema(semaphore, save)

Acquire, release semaphores when crossing into and out of another empire.

 

Bind and Unbind a Semaphore to a Kernel Thread

These routines serve primarily to maintain the kt_sema field in the kthread structure. This field keeps track of currently held alpha semaphores.

As a thread acquires a semaphore, sema_add() links semaphores together through these field. You can obtain all of the semaphores owned by a thread by following kthread->kt_sema->sa_next.

Semaphores are bound to threads to maintain the list of semaphores held when a thread goes to sleep. All bound semaphores are released at that time and by following this list, they can be reaquired when awakened.

Table 1-11 Bind and unbind an alpha semaphore

Internal functionPurpose
sema_add(kthread, semaphore)

Add a reference to a newly acquired semaphore into the thread's kthread structure.

Update the kthread's priority.

sema_delete(kthread, semaphore)

Remove a reference to a thread's kthread structure and recompute the kthread's priority.

 

Test for Ownership of Semaphore

Table 1-12 Tests for ownership of an alpha semaphore

FunctionPurpose
owns_sema(semaphore)

Returns true if the current thread owns the semaphore. The routine compares semaphore->sa_owner with u.u_kthreadp.

kthread_owns_semas(*kthread,sema)

Returns true if a kthread owns one or more semaphores; otherwise returns false.

 

Wait for an Alpha Semaphore

Numerous routines govern the kernel's decision about whether to switch to a thread of control that needs a semaphore.

Table 1-13 Tests for whether to switch thread of control

FunctionPurpose
asema_available(semaphore)

Routine determines whether the kernel should switch to a process that needs a semaphore, based on performance and priority.

asema_miss_ins (semaphore,CPU)

Called after a psema miss to insert the CPU number into the miss table.

asema_miss_del(semaphore,CPU)

Remove the CPU entry from the miss table, recompute priorities.

asema_miss_pri(semaphore,CPU) 

Find prority of CPU's earliest miss.

psema_choose_turn(semaphore)

Determine if CPU deserves to take its turn.

psema_spin_[1|n](semaphore) 

Wait on a locked semaphore.

The thread spins cycles depending on whether it is the CPU's turn.

psema_switch_[1|n](caller)

Spin for a semaphore without arbitrating.