-- DelayImpl.mesa Last edit: KK November 14, 1983 12:25 pm
DIRECTORY
BasicTime
USING[GetClockPulses, MicrosecondsToPulses, Pulses],
FS
USING[StreamOpen],
IO
USING[card, Close, PutF, STREAM],
Process
USING[priorityBackground, priorityNormal, SetPriority, Yield],
Rope
USING[ROPE];
DelayImpl: PROGRAM
IMPORTS BasicTime, FS, IO, Process =
BEGIN
DelayForNPages: PROCEDURE[nPages: CARDINAL] =
BEGIN
newPulseLimit, ending, desired, beginning: BasicTime.Pulses;
actual: LONG CARDINAL;
calc: LONG CARDINAL;
desired ← BasicTime.MicrosecondsToPulses[LONG[4000]*LONG[nPages]];
beginning ← BasicTime.GetClockPulses[];
newPulseLimit ← beginning + desired;
Process.SetPriority[Process.priorityBackground];
DO
Process.Yield[];
IF (ending ← BasicTime.GetClockPulses[]) >= newPulseLimit THEN EXIT;
ENDLOOP;
Process.SetPriority[Process.priorityNormal];
actual ← ending - beginning;
calc ← IF actual >= desired THEN actual - desired ELSE desired - actual;
resultsStreamHandle.PutF["desired %g actual %g off %g\n", IO.card[desired],
IO.card[actual], IO.card[calc]];
END;
Main: PROCEDURE =
BEGIN -- set up to call from CoPilot for debugging.
foo: CARDINAL ← 0;
BEGIN
ENABLE ANY => foo ← foo + 1; -- protect us from the tempcedarexec.
resultsStreamHandle ← FS.StreamOpen["Delay.Results", $create];
FOR nPages: CARDINAL IN [1..100)
DO DelayForNPages[nPages];
ENDLOOP;
resultsStreamHandle.Close[];
END;
END;
-- main line code:
resultsStreamHandle: IO.STREAM;
END.