
[ Example of task arbitration ]
As an example, here it is assumed that there are three sets of
resources which are shared between five tasks. The following example
shows a method to manage assignment of these resources among tasks.
+-----------+
Signal │ resources │ Task Queue Wait
<====== │ TASK-A │ <=== (TASK-D)==(TASK-E)==
│ TASK-B │
│ TASK-C │
+-----------+
1) Create semaphore by os_make_sem() and specify "3" , which is the
number of available resource set, for the initial value of the
semaphore counter.
2) Before using a set of resources , get semaphore by os_wait_sem()
function. If resources are not available, the execution of the
calling task is suspended.
3) When resources have become unnecessary, release the semaphore by
os_sign_sem() function. If there are suspended tasks waiting for
semaphore, the task at the top of the queue gets semaphore and
awaked from the suspended state.
In the following example, maximum three tasks can have resources
assigned, and the execution of the other tasks, for which there are no
available resources, are suspended to wait for the release of
resources.
semaphore
TASK-A TASK-B TASK-C TASK-D TASK-E counter value
│ │ │ │ │ 3
Wait SEM │ │ │ │ 2
X Wait SEM │ │ │ 1
X X Wait SEM │ │ 0
X X X Wait SEM │ -1
X X X (Wait) Wait SEM -2
X X X (Wait)
Signal SEM X X X -1
│ Signal SEM X X X 0
│ │ Signal SEM X X 1
│ │ │ Signal SEM X 2
│ │ │ │ Signal SEM 3
│ │ │ │ │
V V V V V
[Semaphore counter value (semval) and what it represents]
initial value : total number of resource sets
if semval=+n : n= number of available resource sets
if semval=-n : n= number of suspended tasks (n>=0)