Hardware Reference
In-Depth Information
/*-----------------------------------------------------------*/
/* newsem -- allocates and initializes a semaphore */
/*-----------------------------------------------------------*/
sem
newsem (int n)
sem
s;
<disable cpu interrupts>
s = freesem; /* first free semaphore index */
if (s == NIL) abort(NO SEM);
freesem = vsem[s].next;
/* update the freesem list */
vsem[s].count = n;
/* initialize counter
*/
vsem[s].qsem = NIL;
/* initialize sem. queue
*/
<enable cpu interrupts>
return(s);
}
/*-----------------------------------------------------------*/
/* delsem -- deallocates a semaphore */
/*-----------------------------------------------------------*/
void delsem (sem s)
{ <disable cpu interrupts>
vsem[s].next = freesem;
/* inserts s at the head
*/
freesem = s;
/* of the freesem list
*/
<enable cpu interrupts>
}
The wait primitive is used by a task to wait for an event associated with a semaphore.
If the semaphore counter is positive, it is decremented, and the task continues its exe-
cution; if the counter is less than or equal to zero, the task is blocked, and it is inserted
in the semaphore queue. In this case, the first ready task is assigned to the processor
by the dispatch primitive.
Search WWH ::




Custom Search