Oberon || Library || Module Index || Search Engine || Definition || Module
TYPE Semaphore = POINTER TO SemaphoreRec;
TYPE SemaphoreRec = RECORD (Disciplines.ObjectRec) END;
TYPE SemaOp = PROCEDURE (sema: Semaphore);
TYPE StatusProc = PROCEDURE (sema: Semaphore) : INTEGER;
TYPE GetConditionProc = PROCEDURE (sema: Semaphore;
VAR condition: Conditions.Condition);
TYPE DropProc = PROCEDURE (condition: Conditions.Condition);
TYPE Interface = POINTER TO InterfaceRec;
TYPE InterfaceRec =
RECORD
p: SemaOp; (* optional: may be NIL *)
v: SemaOp;
getcondition: GetConditionProc;
drop: DropProc;
status: StatusProc;
END;
PROCEDURE Init(sema: Semaphore; if: Interface);
PROCEDURE P(sema: Semaphore);
PROCEDURE V(sema: Semaphore);
PROCEDURE GetCondition(sema: Semaphore; VAR condition: Conditions.Condition);
PROCEDURE Drop(condition: Conditions.Condition);
PROCEDURE Status(sema: Semaphore) : INTEGER;
Init initializes a newly created semaphore and associates it with the given interface if. The interface procedures are expected to meet the specifications following:
P and V realize Dijkstra's semaphore operations which claim and release a resource.
GetCondition returns a condition which evaluates to TRUE when the resource has been claimed. Note that the condition returned by GetCondition does not indicate the availability of the resource only but claims the resource itself, i.e. P must not be called additionally if Conditions.TestCondition returns TRUE. For this reason conditions may not be recycled, i.e. for each new try to claim a resource a new condition has to be created.
To undo GetCondition, Drop may be called. Drop either unqueues the condition from the list of conditions waiting for one of the resource becoming available, or releases the resource if it was already claimed by the condition.
Status reports the current status of sema. If positive, the returned value indicates the number of free resources. If less or equal to zero, the absolute value of the returned number gives the number of processes which waits for the resource to become available.
Following runtime tests are performed by assertions:
Events.AssertPriority(Priorities.interrupts); Semaphores.P(mutex); (* ... critical region ... *) Semaphores.V(mutex); Events.ExitPriority;
Oberon || Library || Module Index || Search Engine || Definition || Module