YggWorkerMapImpl.mesa
Copyright Ó 1985, 1988 by Xerox Corporation. All rights reserved.
Exports YggTransactionMap global operations: Module initialization and Creating, finding,
and deleting Handles
Last edited by
Taft on 1-Feb-82 10:14:46
MBrown on January 30, 1984 11:38:56 am PST
Carl Hauser, February 24, 1987 4:57:29 pm PST
Bob Hagmann May 2, 1988 2:30:59 pm PDT
DIRECTORY
YggEnvironment USING [],
YggInternal USING [],
Basics,
YggConcreteTransID USING [TransID, Equal],
YggTransactionMap USING [EnumProc],
YggWorker USING [Handle, Object],
YggWorkerControl USING [];
YggWorkerMapImpl: CEDAR MONITOR
IMPORTS
Basics,
YggConcreteTransID
EXPORTS
YggEnvironment,
YggInternal,
YggTransactionMap,
YggWorkerControl
= BEGIN
Handle: TYPE = YggWorker.Handle;
TransID: PUBLIC TYPE = YggConcreteTransID.TransID;
YggEnvironment.TransID.
TransObject: PUBLIC TYPE = YggWorker.Object;
YggInternal.TransObject.
Module initialization
Initialize: PUBLIC ENTRY PROC [hashArraySize: NAT] = {
YggWorkerControl.Initialize
InitializeHashTable [
numHashSlotsDesired: hashArraySize,
hashHandle: NEW[YggWorker.Object ← [
timeOfLastStartWork: TRASH]]];
};
Creating, finding, and deleting Handles
Register: PUBLIC ENTRY PROC [self: Handle] RETURNS [alreadyRegistered: BOOL] = {
If a worker object with TransID self.trans exists, return alreadyRegistered = TRUE.
Otherwise insert self into the transactionmap and return alreadyRegistered = FALSE.
alreadyRegistered ← FALSE;
Insert[self ! HashPkgDuplicateKey => { alreadyRegistered ← TRUE; CONTINUE }];
RETURN [alreadyRegistered];
};
GetHandle: PUBLIC ENTRY PROC [transID: TransID] RETURNS [handle: Handle] = {
RETURN[Lookup[transID]];
};
Unregister: PUBLIC ENTRY PROC [self: Handle] = {
Delete[self];
self.next ← NIL;
};
LockedEnumerate: PUBLIC ENTRY PROC [proc: YggTransactionMap.EnumProc] = {
EnumerateWithProc[proc];
};
UnlockedEnumerate: PUBLIC PROC [proc: YggTransactionMap.EnumProc] = {
Next: ENTRY PROC [h: Handle] RETURNS [Handle] = INLINE {
RETURN [EnumerateNext[h]];
};
h: Handle ← NIL;
DO
IF (h ← Next[h]) = NIL OR proc[h].stop THEN EXIT;
ENDLOOP;
};
Client-supplied parameters to hash package begin here:
HashHandle: TYPE = Handle;
Key: TYPE = TransID;
ClientHashInit: PROC [numHashSlotsDesired: NAT]
RETURNS [numHashSlotsAllowed: NAT] = {
hashMask ← numHashSlotsDesired - 1;
IF Basics.BITAND[hashMask, numHashSlotsDesired] # 0 THEN
ERROR HashPkgCallerProgrammingError;
RETURN [numHashSlotsDesired]
};
hashMask: WORD;
ClientHash: INTERNAL PROC [hashHandle: HashHandle]
RETURNS [NAT--[0..range)--] = INLINE {
RETURN [Basics.BITAND[hashMask,
LOOPHOLE[
LOOPHOLE[hashHandle.transID, YggConcreteTransID.TransID].randomBits,
Basics.LongNumber.pair].lo]];
};
ClientEqualKeys: INTERNAL PROC [hashHandle1, hashHandle2: HashHandle]
RETURNS [equal: BOOL] = INLINE {
RETURN [YggConcreteTransID.Equal[hashHandle1.transID, hashHandle2.transID]];
};
ClientSetKey: INTERNAL PROC [hashHandle: HashHandle, key: Key] = INLINE {
hashHandle.transID ← key;
};
Client-supplied parameters to hash package end here.
Hash table package.
Explanation of client-supplied parameters:
The procedure ClientHashInit is called during hash table initialization, to allow the hash
function to precompute values based on the range and to make any small adjustments to
the range that are necessary.
HashHandle must:
be a REF type.
contain a field "next" of type HashHandle, under the exclusive control of the
hash package.
Key is an arbitrary type.
SetKey sets the "key value" associated with a HashHandle. The key value must
not change between the time the handle is Inserted into the table and the time
it is deleted from the table.
Hash must be a function of the key value of the parameter "hashHandle".
EqualKeys must be the equality relation on the key values of the parameters
"hashHandle1" and "hashHandle2".
Interface description:
InitializeHashTable: INTERNAL PROCEDURE[numHashSlotsDesired: NAT, hashTableZone:
ZONE, hashHandle: HashHandle];
errors: HashPkgCallerProgrammingError (numHashSlotsDesired = 0).
Insert: INTERNAL PROCEDURE[hashHandle: HashHandle];
errors: HashPkgDuplicateKey.
Lookup: INTERNAL PROCEDURE[key: Key] RETURNS [hashHandle: HashHandle];
returns hashHandle = NIL if not found.
Delete: INTERNAL PROCEDURE[hashHandle: HashHandle];
errors: HashPkgCallerProgrammingError (not found).
EnumerateNext: INTERNAL PROCEDURE[prevHashHandle: HashHandle] RETURNS
[hashHandle: HashHandle];
errors: none.
prevHashHandle = NIL starts the enumeration, returned hashHandle = NIL is the end
of the enumeration. This procedure guarantees that any hashHandle in existence throughout
the entire enumeration will be seen. Other handles may or not not be seen. HashHandles
may be seen more than once.
EnumerateWithProc: INTERNAL PROCEDURE[proc: PROCEDURE[hashHandle: HashHandle]
RETURNS[stop: BOOLEAN]];
errors: none.
start of invariant hash package code:
The INTERNAL procedures below expect to be called from a client procedure holding the module monitor lock, which protects the following data structures:
hashSlots: REF HashSlots ← NIL;
HashSlots: TYPE = RECORD[SEQUENCE nSlots: NAT OF HashHandle];
numHashSlots: NAT ← 0; -- boy, will they be sorry if they don't init this package.
lookupHashHandle: HashHandle ← NIL; -- for the "package's" use only.
errors:
HashPkgCallerProgrammingError: ERROR = CODE; -- various fatal conditions.
HashPkgDuplicateKey: ERROR = CODE; -- from Insert.
InitializeHashTable: INTERNAL PROCEDURE[numHashSlotsDesired: NAT, hashHandle: HashHandle] =
BEGIN -- errors: HashPkgCallerProgrammingError (numHashSlotsDesired = 0).
numHashSlots ← ClientHashInit[numHashSlotsDesired];
IF numHashSlots = 0 THEN ERROR HashPkgCallerProgrammingError;
lookupHashHandle ← hashHandle;
hashSlots ← NEW[HashSlots[numHashSlots]];
FOR index: NAT IN [0..numHashSlots)
DO hashSlots[index] ← NIL; ENDLOOP;
END;
Insert: INTERNAL PROCEDURE[hashHandle: HashHandle] = INLINE
BEGIN -- errors: HashPkgDuplicateKey.
index: NAT ← ClientHash[hashHandle];
FOR newHashHandle: HashHandle ← hashSlots[index], newHashHandle.next
UNTIL newHashHandle = NIL
DO
IF ClientEqualKeys[newHashHandle, hashHandle]
THEN ERROR HashPkgDuplicateKey;
ENDLOOP;
hashHandle.next ← hashSlots[index];
hashSlots[index] ← hashHandle;
END;
Lookup: INTERNAL PROCEDURE[key: Key] RETURNS [hashHandle: HashHandle] = INLINE
BEGIN -- returns hashHandle = NIL if not found.
ClientSetKey[lookupHashHandle, key];
FOR hashHandle ← hashSlots[ClientHash[lookupHashHandle]], hashHandle.next
UNTIL hashHandle = NIL
DO IF ClientEqualKeys[hashHandle, lookupHashHandle] THEN RETURN;
ENDLOOP;
RETURN[NIL];
END;
Delete: INTERNAL PROCEDURE[hashHandle: HashHandle] = INLINE
BEGIN -- errors: HashPkgCallerProgrammingError (not found).
index: NAT ← ClientHash[hashHandle];
prevHashHandle: HashHandle ← NIL;
FOR newHashHandle: HashHandle ← hashSlots[index], newHashHandle.next
UNTIL newHashHandle = NIL
DO
IF ClientEqualKeys[newHashHandle, hashHandle] THEN EXIT;
prevHashHandle ← newHashHandle;
REPEAT FINISHED => ERROR HashPkgCallerProgrammingError;
ENDLOOP;
IF prevHashHandle = NIL
THEN hashSlots[index] ← hashHandle.next
ELSE prevHashHandle.next ← hashHandle.next;
hashHandle.next ← NIL;
END;
prevHashHandle = NIL starts the enumeration, returned hashHandle = NIL is the end of the enumeration. This procedure guarantees that any hashHandle in existence throughout the entire enumeration will be seen. Other handles may or not not be seen. HashHandles may be seen more than once.
EnumerateNext: INTERNAL PROCEDURE[prevHashHandle: HashHandle] RETURNS
[hashHandle: HashHandle] =
BEGIN -- errors: none.
index: NAT;
IF prevHashHandle = NIL
THEN index ← 0
ELSE BEGIN index ← ClientHash[prevHashHandle];
FOR hashHandle ← hashSlots[index], hashHandle.next
UNTIL hashHandle = NIL
DO
IF ClientEqualKeys[hashHandle, prevHashHandle] THEN GOTO found;
REPEAT
found => BEGIN
IF hashHandle.next # NIL THEN RETURN[hashHandle.next];
index ← index + 1;
END;
ENDLOOP;
END;
UNTIL index >= numHashSlots
DO
IF hashSlots[index] # NIL THEN RETURN[hashSlots[index]];
index ← index + 1;
ENDLOOP;
RETURN[NIL];
END;
EnumerateWithProc: INTERNAL PROCEDURE[proc: PROCEDURE[hashHandle: HashHandle]
RETURNS[stop: BOOLEAN]] =
BEGIN -- errors: none.
FOR index: NAT IN [0..numHashSlots)
DO
FOR hashHandle: HashHandle ← hashSlots[index], hashHandle.next
UNTIL hashHandle = NIL
DO IF proc[hashHandle] THEN RETURN;
ENDLOOP;
ENDLOOP;
END;
end of invariant hash package code.
END.--WorkerMapImpl
CHANGE LOG
Changed by MBrown on February 9, 1983 11:54 am
Change Unregister to NOT set continueWorker ← NIL.
Hauser, March 8, 1985 11:17:11 am PST
Nodified, added copyright.