AMX Semaphore Manager
K
A
DAK
95
Resource Semaphore
AMX resource semaphores provide the simplest mechanism for controlling the access to
critical resources. Resources may include disk files, I/O devices, database components,
regions of memory, specific words of memory or any other entity which is considered to
be a resource.
An application task requests ownership of a resource with a call to the Semaphore
Manager. If the resource is available, the task is granted immediate ownership of it. If
the resource is unavailable, the task is inserted on a list of tasks waiting for the resource
at a wait priority specified by the task. The task can optionally specify the maximum
time interval it is prepared to wait for access. When the task which currently owns the
resource releases it with a call to the Semaphore Manager, the resource will be given to
the task with the highest waiting priority.
Although a resource semaphore uses a binary semaphore for controlling access to the
resource, it differs from the general counting semaphore facility provided by the AMX
Semaphore Manager in one significant feature. Resource ownership is tied to a specific
task. Only the task owning such a resource is permitted to signal its release. The
Semaphore Manager does not permit more than one task to share ownership of such a
resource.
A resource semaphore is created by specifying an initial semaphore count of -1 in the call
to ajsmcre. The Semaphore Manager creates a resource semaphore and automatic ally
gives it an initial value of one indicating that the resource is free.
A resource is reserved by calling procedure ajsmrsv using the semaphore id allocated to
the particular resource semaphore when it was created. Only tasks can reserve a resource
controlled by a resource semaphore. The task which owns the resource can reserve it
again and again resulting in a nested reservation.
When requesting use of a resource, the task specifies the interval, measured in system
ticks, which it is willing to wait for the resource if the resource is unavailable at the time
of the call. The task also specifies the priority at which it is prepared to wait, zero being
the highest priority.
The resource is released with a call to ajsmrls. The task which owns the resource must
release the resource once for each nested reserve that it has made. The resource becomes
free when the nesting count reaches zero. Alternatively, the resource can be released
unconditionally with a call to ajsmfre.
If the resource is in use when ajsmrsv is called, the task will be added to the resource
semaphore queue forcing the task to wait at the priority specified by the task in its call to
reserve the resource. When the current owner of the resource releases it, the Semaphore
Manager gives the resource to the task (if any) waiting at the beginning of the resource
semaphore queue. Hence, a task must wait until all other tasks ahead of it in the queue
use and release the resource.
If the task is of higher priority than the task which is releasing the resource, a task switch
will occur giving the higher priority task an immediate opportunity to use the resource. If
the task being granted use of the resource is of lower priority than the task which is
releasing it, the new owner will have to wait until the currently executing task
relinquishes control of the processor.