94
K
A
DAK
AMX Semaphore Manager
Counting Semaphore
A counting semaphore is created by specifying an initial semaphore count greater than or
equal to zero in the call to ajsmcre. When used for mutual exclusion, the semaphore
should be given an initial value of one.
If a semaphore is initialized with a semaphore value of n, it can be used to control access
to n resources of a particular type. For instance, a counting semaphore with an initial
count of three could be used to control access to three printers in a system. When used in
this fashion, the Semaphore Manager assures that no more than three tasks can own a
printer at any one time. However, the Semaphore Manager does not provide any
guidance as to which of the three available printers a task can use.
Access to the resource controlled by a counting semaphore is acquired with a call to
procedure ajsmwat. Only tasks are allowed to call this procedure because the caller may
have to wait for the resource to become available. 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.
If the resource controlled by the semaphore is available, the Semaphore Manager gives it
to the calling task immediately. If the resource is unavailable, the Semaphore Manager
will add the task to the semaphore's wait queue at the priority it said it was willing to
wait. Thus tasks which require high priority access to the resource can preempt lower
priority waiting tasks in the wait queue.
When the task is finished using the resource, it signals its release by calling procedure
ajsmsig. The Semaphore Manager releases the resource and checks the semaphore's
wait queue. If any tasks are waiting on the queue, the resource is immediately given to
the task at the head of the wait queue. If that 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.
If the semaphore does not become available within the timeout interval specified by the
task, the task will be removed from the semaphore wait queue and will resume execution
with a timeout indication.
Tasks, ISPs or Timer Procedures which need to use a resource but which cannot wait for
the resource must call ajsmget to get use of the resource. Unlike ajsmwat, procedure
ajsmget will not allow the caller to wait on the semaphore queue. If the resource cannot
be immediately granted to the caller, ajsmget returns an error indication.