ProcessPropsImpl.mesa
Copyright © 1985 by Xerox Corporation. All rights reserved.
Russ Atkinson, February 1, 1985 3:53:26 pm PST
DIRECTORY
Process USING [GetCurrent],
ProcessProps USING [],
List USING [AList];
ProcessPropsImpl: CEDAR MONITOR
IMPORTS Process
EXPORTS ProcessProps
= BEGIN OPEN List;
AddPropList: PUBLIC PROC [propList: AList, inner: PROC] = TRUSTED {
... adds the given properties to the current process for the duration of the call of inner. The old property list for the process will not be altered, but will be found at the end of the propety list during the call to inner. The old property list will be restored after the call has completed. The new propList will be copied if an old property list exists.
entry: ProcessEntry = GetProcessEntry[];
old: AList = entry.current;
IF old # NIL THEN {
tail: AList ← NIL;
FOR each: AList ← propList, each.rest WHILE each # NIL DO
new: AList = LIST[each.first];
IF tail = NIL THEN propList ← new ELSE tail.rest ← new;
tail ← new;
ENDLOOP;
IF tail = NIL THEN propList ← old ELSE tail.rest ← old;
};
entry.current ← propList;
inner[! UNWIND => entry.current ← old];
entry.current ← old;
};
PushPropList: PUBLIC PROC [propList: AList, inner: PROC] = {
... sets the property list for the current process for the duration of the call of inner. The old property list will be restored after the call has completed. The new propList will not be copied. Most applications should use AddPropList to properly inherit properties.
entry: ProcessEntry = GetProcessEntry[];
old: AList = entry.current;
entry.current ← propList;
inner[! UNWIND => entry.current ← old];
entry.current ← old;
};
GetPropList: PUBLIC PROC RETURNS [propList: AList ← NIL] = TRUSTED {
... returns the current property list for the current process. Although there is no prohibition from altering the returned list, users are encouraged to follow the style of adding new properties, rather than changing old associations.
entry: ProcessEntry = GetProcessEntry[];
propList ← entry.current;
};
GetProp: PUBLIC PROC [key: REF] RETURNS [prop: REFNIL] = TRUSTED {
... returns the current property associated with the given key for the current process. This procedure is preferred to GetPropList for most applications.
entry: ProcessEntry = GetProcessEntry[];
propList: AList ← entry.current;
WHILE propList # NIL DO
IF propList.first.key = key THEN RETURN [propList.first.val];
propList ← propList.rest;
ENDLOOP;
};
The following two operations are safe, but operations on PROCESS values are, in general, not safe, since processes are reused. These operations should be regarded as instantaneous samples, where the results are likely to change without notice. However, statistics-style applications and may find these operations useful despite these caveats. It is also OK to ask whether a process still has some property if that property will not be reused.
GetSpecificPropList: PUBLIC PROC
[process: PROCESS] RETURNS [propList: AList ← NIL] = TRUSTED {
... returns the current property list for the specified process
entry: ProcessEntry = GetProcessEntry[process];
IF entry # NIL THEN propList ← entry.current;
};
GetNextPropList: PUBLIC ENTRY PROC [process: PROCESSNIL] RETURNS [next: PROCESSNIL, propList: AList ← NIL] = {
... returns the next process with a non-NIL propList. Use GetNextPropList[NIL] to start the iteration. [NIL, NIL] will be returned if no more processes exist. The processes are produced in increasing order. There is a very small probability of a race with AddPropList and PushPropList, but it must result in a bounded number of retries.
ENABLE UNWIND => NULL;
DO
best: ProcessEntry ← NIL;
FOR i: NAT IN [0..16) DO
FOR each: ProcessEntry ← processVector[i], each.next UNTIL each = NIL DO
eachProcess: CARDINAL = LOOPHOLE[each.process];
IF each.current = NIL THEN LOOP;
IF eachProcess <= LOOPHOLE[process, CARDINAL] THEN LOOP;
IF best = NIL OR eachProcess < LOOPHOLE[best.process, CARDINAL]
THEN best ← each;
ENDLOOP;
ENDLOOP;
IF best = NIL THEN EXIT;
propList ← best.current;
IF propList = NIL THEN {races ← races + 1; LOOP}; -- rats! a race occurred
next ← best.process;
EXIT;
ENDLOOP;
};
ProcessBits: TYPE = MACHINE DEPENDENT RECORD [
lead: [0..1024), hash: [0..16), spare: [0..4)
];
processVector: ProcessVector ← NEW[ProcessVectorRep ← ALL[NIL]];
entries: NAT ← 0;
races: INT ← 0; -- I am curious, Jello
ProcessVector: TYPE = REF ProcessVectorRep;
ProcessVectorRep: TYPE = ARRAY [0..16) OF ProcessEntry;
ProcessEntry: TYPE = REF ProcessEntryRep;
ProcessEntryRep: TYPE = RECORD [
next: ProcessEntry ← NIL,
process: PROCESSNIL,
current: AList ← NIL
];
GetProcessEntry: PROC
[process: PROCESSNIL] RETURNS [entry: ProcessEntry ← NIL] = TRUSTED {
... returns a non-NIL ProcessEntry for the specified process. We are only locked out for short bursts of time, and NOT while allocating!
bits: ProcessBits;
findEntry: ENTRY PROC = TRUSTED {
.. sets entry to be an existing ProcessEntry object (if any), for the process.
ENABLE UNWIND => NULL;
FOR each: ProcessEntry ← processVector[bits.hash], each.next UNTIL each = NIL DO
IF each.process = process THEN {entry ← each; RETURN};
ENDLOOP;
};
makeEntry: ENTRY PROC = TRUSTED {
.. sets entry to be an existing ProcessEntry object (if any), for the process. If there is no such entry, then the fields of entry are filled in, and the entry is placed in the hash table.
ENABLE UNWIND => NULL;
first: ProcessEntry = processVector[bits.hash];
FOR each: ProcessEntry ← first, each.next UNTIL each = NIL DO
IF each.process = process THEN {entry ← each; RETURN};
ENDLOOP;
entry.next ← first;
entry.process ← process;
processVector[bits.hash] ← entry;
entries ← entries + 1;
};
IF process = NIL THEN process ← LOOPHOLE[Process.GetCurrent[]];
bits ← LOOPHOLE[process];
findEntry[];
IF entry # NIL THEN RETURN;
entry ← NEW[ProcessEntryRep ← []];
makeEntry[];
};
END.