UnixShm0.mesa
Copyright Ó 1990, 1991 by Xerox Corporation. All rights reserved.
Created by Christian Jacobi, January 29, 1990
Christian Jacobi, January 24, 1991 5:30 pm PST
DIRECTORY UnixTypes;
UnixShm0: CEDAR DEFINITIONS =
BEGIN
Procedures and types to access the Unix shared memory features.
The procedures are left with original Unix parameters and are not nicely Cedar packaged so that the Unix documentation and .h files still point to the corresponding features.
Described types found in <sys/ipc.h> and <sys/shm.h>.
IpcPerm: TYPE = MACHINE DEPENDENT RECORD [--common IPC access structure
uid: UnixTypes.UID, -- (owner's) user id
gid: UnixTypes.GID, -- (owner's) group id
cuid: UnixTypes.UID, -- creator's user id
cgid: UnixTypes.GID, -- creator's group id
mode: CARD16,  -- access modes; r/w permission; (set op sets only low 9 bits)
seq: CARD16,  -- slot usage sequence number
key: CARD-- key
];
ShmIdDs: TYPE = MACHINE DEPENDENT RECORD [
--shared memory descriptor structure
ipcPerm: IpcPerm,  -- operation permission struct
segsz: INT,  -- size of segment in bytes
lpid: UnixTypes.UID,  -- process ID of last shmop
cpid: UnixTypes.UID,  -- process ID of creator
nAttch: CARD16,  -- number of current attaches
unused1: CARD16, -- depending on C compiler's padding algorithm !
aTime: UnixTypes.Time,  -- last shmat time
dTime: UnixTypes.Time, -- last shmdt time
cTime: UnixTypes.Time, -- last change time
amp: POINTER-- segment anon¬map pointer
];
SharedMemoryIdentifier: TYPE = RECORD [val: CARD];
errorSharedMemoryIdentifier: SharedMemoryIdentifier = [CARD.LAST];
SharedMemoryKey: TYPE = RECORD [val: CARD];
ipcPRIVATE: SharedMemoryKey = [0]; --private key to create new SharedMemoryIdentifier
ShmGetFlags: TYPE = RECORD [val: CARD];
--low order 9 bits for permission stuff
ipcALLOC: ShmGetFlags = [0100000B]; -- entry currently allocated
ipcCREAT: ShmGetFlags = [0001000B]; -- create entry if key doesn't exist
ipcEXCL:  ShmGetFlags = [0002000B]; -- fail if key exists
allPerms:  ShmGetFlags = [0000777B]; -- (not in found ipc.h)
ShmGet: PROC [key: SharedMemoryKey, size: INT, shmflg: ShmGetFlags] RETURNS [shmId: SharedMemoryIdentifier] = TRUSTED MACHINE CODE {
Returns shared memory identifier or errorSharedMemoryIdentifier.
"<xr/UIO.h>.XR←ShmGet"
};
IPCCtlCmd: TYPE = RECORD [val: CARD];
ipcRMID:  IPCCtlCmd = [0]; -- remove identifier
ipcSET:  IPCCtlCmd = [1]; -- set options
ipcSTAT:  IPCCtlCmd = [2]; -- get options
ShmCtl: PROC [shmId: SharedMemoryIdentifier, cmd: IPCCtlCmd, buf: POINTER TO ShmIdDs] RETURNS [UnixTypes.SysCallResult] = TRUSTED MACHINE CODE {
Shared memory control operations.
"<xr/UIO.h>.XR←ShmCtl"
};
ShmAtFlags: TYPE = RECORD [CARD];
noFlags:  ShmAtFlags = [000000];
shmRDOnly: ShmAtFlags = [010000]; -- attach read-only (else read-write)
shmRnd:  ShmAtFlags = [020000]; -- round attach address to SHMBA
RawVM: TYPE = POINTER;
ShmAt: PROC [id: SharedMemoryIdentifier, addr: RawVM, flags: ShmAtFlags ¬ noFlags] RETURNS [INT] = TRUSTED MACHINE CODE {
Attaches shared memory segment.
Returns address of memory segment or -1 on failure.
WARNING:
Do explicitely allocate the memory with VMReserve.
A 0 addr would confuse the garbage collector.
"<xr/UIO.h>.XR←ShmAt"
};
ShmDt: PROC [addr: RawVM] RETURNS [UnixTypes.SysCallResult] = TRUSTED MACHINE CODE {
Unmaps shared memory segment at addr from calling processes address space.
But segment and contents are retained.
"<xr/UIO.h>.XR←ShmDt"
};
--The following procedures do not look like belonging here from a purist point of view. However, they are always needed together with the real shared memory stuff.
VMReserve: PROC [nbytes: CARD] RETURNS [address: RawVM] = TRUSTED MACHINE CODE {
Returns page aligned address.
"<xr/ThreadsSharedMem.h>.XR←VMReserve"
};
--VMReserve is not a Unix shared memory feature, but it is always needed together with ShmAt.
UNTHREADEDShmCtl: UNSAFE PROC [shmId: SharedMemoryIdentifier, cmd: IPCCtlCmd, buf: POINTER TO ShmIdDs] RETURNS [UnixTypes.SysCallResult] = TRUSTED MACHINE CODE {
Shared memory control operations, version NOT to be called from a regular Cedar/Mesa thread.
"*#include <sys/types.h>\n";
"#include <sys/ipc.h>\n";
"#include <sys/shm.h>\n";
".shmctl"
};
--UNTHREADEDShmCtl must not be used by normal Cedar code but only by a registered cleanup procedure from a Unix process.
It is needed for returning allocated segments when the process dies.
RegisterUNTHREADEDTerminationCleanupProc: UNSAFE PROC [cleanup: PROC [REF], data: REF ¬ NIL] RETURNS [UnixTypes.SysCallResult] = TRUSTED MACHINE CODE {
Register a cleanup proc to be called automatically just before PCR terminates. The cleanup proc is called from a Unix process rather than a thread -- it should do native Unix system calls and avoid thread operations such as entering monitors.
"<xr/ThreadsTermination.h>.XR←RegisterTerminationCleanupProc"
};
--RegisterUNTHREADEDTerminationCleanupProc is not a Unix shared memory feature, but it is always necessary to provide cleanup when using Unix shared memory..
END.