-- BBAction.mesa
-- Russ Atkinson, November 1, 1982 3:30 pm

DIRECTORY
AMEvents USING [Event];

BBAction: CEDAR DEFINITIONS = BEGIN

ActionId: TYPE = INT;
-- unique id for the action, these are never reused
-- only positive integers are used

Action: TYPE = REF ActionRep;

Event: TYPE = AMEvents.Event;

ActionKind: TYPE = {break, signal, other};

ActionStatus: TYPE = {new, busy, pendingIn, pendingOut, dead};

ActionRep: TYPE = RECORD
[id: ActionId,
rest: Action,
kind: ActionKind,
status: ActionStatus,
event: Event,
in,out: REF
];

ActionError: ERROR;
-- error given when pre-condition not met

NewAction: PROC [event: Event, kind: ActionKind ← other] RETURNS [Action];
-- creates a new action with new id and given kind

WaitForDataGiven: PROC [action: Action] RETURNS [REF];
-- suspends caller (as pendingIn) until data is given
-- data is returned from the call when proceding

WaitForDataTaken: PROC [action: Action, out: REF];
-- suspends caller (as pendingOut) until data (out) is taken
-- when the data is taken, the process proceeds (busy)

WaitForChange: PROC [changeCount: INT ← 0] RETURNS [INT];
-- suspends caller until the change count is different than
-- the given changeCount, then returns the new change count
-- the changeCount will change for every time a process becomes pending,
-- busy, or dead; the change count may wrap around, but will always
-- be > 0; therefore the default will not wait, but will return the
-- current count

ForceChange: PROC;
-- forces a change to the change count

GetChangeCount: PROC RETURNS [INT];
-- get the current change count

NextPendingAction: PROC [action: Action ← NIL] RETURNS [Action];
-- returns the next eldest pending action
-- returns NIL if no pending actions in list
-- start with NIL to get most recent

NextAction: PROC [action: Action ← NIL] RETURNS [Action];
-- returns the next eldest action
-- returns NIL if no actions in list
-- start with NIL to get most recent

GiveData: PROC [action: Action, in: REF];
-- supplies the input data, allows the action to proceed

TakeData: PROC [action: Action] RETURNS [REF];
-- obtains the output data, allows the action to proceed

PeekData: PROC [action: Action] RETURNS [REF];
-- obtains the output data, does not allow process to proceed

Abort: PROC [action: Action];
-- aborts the pending action
-- causes ERROR ABORTED in the associated process

END.