Guardian Programmer's Guide

Table Of Contents
Synchronizing Processes
Guardian Programmer’s Guide 421922-014
26 - 7
Unlocking a Binary Semaphore
not wait indefinitely for a lock. If the timeout value is reached before the process is
selected to receive the lock, the process is awakened and EAGAIN is returned
from BINSEM_LOCK.
When the process currently holding the lock unlocks the binary semaphore, the wait
group is checked for waiting processes. If the wait group contains waiting processes,
a process is selected from the wait group to receive ownership of the lock and resume
execution at the point of suspension. The selected process is then free to access the
shared resource.
If a process holding a lock on a semaphore terminates without unlocking the
semaphore, that semaphore is said to be forsaken. The next process in the wait group
or, if the wait group is empty, the next process to call BINSEM_LOCK_, gets the lock
but is informed by the system that the semaphore has not been unlocked by the
original process. Because the shared resource may be in an inconsistent state, the
process requesting the lock should perform an application-dependent recovery before
using the resource.
In the following example, a lock is requested and given a timeout value of 500
seconds:
TIMEOUT := 50000; !timeout is expressed in units of 10
milliseconds
ERROR := BINSEM_LOCK_ (SEMID, TIMEOUT);
Unlocking a Binary Semaphore
When a process has finished executing a critical section and is ready to relinquish a
lock on a binary semaphore, it should unlock the binary semaphore. To unlock a
binary semaphore, call BINSEM_UNLOCK_.
The BINSEM_UNLOCK_ procedure unlocks a binary semaphore and makes it
available to other processes. If other processes are in the wait group when
BINSEM_UNLOCK_ is called, a waiting process is selected to receive the lock and the
binary semaphore is immediately locked. If no processes are in the wait group, the
binary semaphore is available to the next process that requests the lock.
BINSEM_UNLOCK_ cannot unlock a binary semaphore that is currently locked by
another process; to do so, use the BINSEM_FORCELOCK_ procedure to force a lock
on the binary semaphore, then call BINSEM_UNLOCK_.
Testing Ownership of a Binary Semaphore
Software should be designed so that a process is always aware of a locked binary
semaphore, but sometimes a common-code sequence can be invoked either with or
without the semaphore. Such code can call the BINSEM_ISMINE_ procedure to
determine whether or not a binary semaphore is locked by this process.
Note. The order in which processes are selected from the wait group to receive ownership of a
lock is indeterminate; processes are not necessarily selected in the order in which they entered
the group nor are they selected in order of priority.