/*static char *SCCSID = "@(#)sem.h	6.3 92/03/11";*/
/*      SCCSID = @(#)sem.h	6.3 92/03/11 */
/* XLATOFF */
#pragma title ("Constants, Data Structures and Tables")
/* XLATON */
/* ASM
BREAK   <Constants, Data Structures and Tables>
 */

/***    SEM.INC

        Title:

        Ram and system semaphore include file

        Description:

        Semaphore and muxwait constant and structure
        definations.
*/


/* This is the selector/high word of an user process system semaphore handle */

#define USERHANDLE_HIGHWORD  2


/* This is the selector/high word of an internal system semaphore handle */

#define SYSHANDLE_HIGHWORD  3


/* The high word of the block and run ID for ram semaphores */

#define RAMSEM_BLOCK_ID  0xfffe


/* MuxSemWait block/run ID high word */

#define MUX_BLOCK_ID  0xfffd


/* Maximum number of system semaphores */

#define SYSSEM_MAXIMUM  256


/* Maximum number of events per MuxSemWait call */

#define MAX_MUX_EVENTS  16


/* Total system wide maximum number of MuxSemWait events */

#define MAX_MUX_ENTRIES  255


/* ProcBlock routine flag values */

#define UNINTERRUPTIBLE  1
#define INTERRUPTIBLE  0


/* CreateSem routine flags */

#define EXCLUSIVE  0
#define NONEXCLUSIVE  1


/* SemHandle routine flag values */

#define NOT_IN_USE  0
#define IN_USE  1


/* SysSemWait routine flag values */

#define OWNER_DIED_CHK  0x01


/* SemRequestSub routine flag values */

#define SEMREQ_FRZ_CHK 0x01       /* Check for thread to freeze */
#define SEMREQ_BLK_CHK 0x02       /* Don't block, return carry */


/* PTDA open system semaphore table flag */

#define INVALID_ENTRY  0


/* RamSemOwner/SysSemOwner values */

#define SEM_CLEAR  0
#define SEM_SET  0xff


/* RamSemFlag values */

#define RAMSEM_WAITING 0x01     /* a thread is waiting on the sem */
#define RAMSEM_MUXWAITING 0x02     /* a thread is muxwaiting on the sem */

/* Note !!! Only the first four bits can be used for RamSemFlag values because*/
/* the high order 4 nibbles are being used for an extended RamSemOwner field.*/
/* With a maximum of 512 threads we need another bit (ie. 2 to the 9th) but */
/* we are using 4 bits just in case we want to increase the limit to greater */
/* than 512. */

#define RAMSEM_OWNER_SHIFT 0x04  /* number of shifts for extended RamSemOwner */
                                 /* in RamSemFlag field. */
#define RAMSEM_OWNER_MASK  0xF0  /* Bit positions for extended RamSemOwner */
                                 /* in RamSemFlag field. */
#define RAMSEM_OWNED       0xF0FF
#define RAMSEM_FLAG_MASK   0x000F/* Bit positions for flags, RamSemFlag field */


/* SysSemFlag values */

#define SYSSEM_WAITING 0x01     /* a thread is waiting on the sem */
#define SYSSEM_MUXWAITING 0x02     /* a thread is muxwaiting on the sem */
#define SYSSEM_OWNER_DIED 0x04     /* the process/thread owning the sem died */
#define SYSSEM_EXCLUSIVE 0x08     /* indicates a exclusive system semaphore */
#define SYSSEM_NAME_CLEANUP 0x10     /* name table entry needs to be removed */
#define SYSSEM_THREAD_OWNER_DIED 0x20     /* the thread owning the sem died */
#define SYSSEM_EXITLIST_OWNER 0x40     /* the exitlist thread owns the sem */


/* Maximum system semaphore name length */

#define SYSSEM_NAMELEN  128


/* End of linked list value */

#define END_LINK  (-1)


/* Flag from SetupMuxList routine indicating no semaphores were clear */

#define NO_SEM_CLEAR  (-1)


/* MuxTableEntry MuxType flags */

#define MUXTYPE_CLEAR 0       /* the mux table entry is clear */
#define MUXTYPE_SYSSEM 1       /* the ID is a sys sem addr */
#define MUXTYPE_RAMHANDLE 2       /* the ID is a ram sem handle:offset */
#define MUXTYPE_RAMPHYS 3       /* the ID is a ram sem physical addr */
#define MUXTYPE_EVENTSEM 4      /* the ID for a 32-bit event sem */


/* System semaphore handle structure */

typedef struct SysSemHandleStruc {
ushort_t SysSemHighWord ;       /* "SYSSEM_HIGHWORD" for sys sems */
ushort_t SysSemPTDAIndex ;       /* Index into the PTDA open sem table */
} SysSemHandleStruc ;


/* Ram semaphore dword structure */

/* NOTE The first 2 elements RamSemOwner and RamSemFlag are accessed */
/* sometimes as a word, so they must stay together. */

typedef struct RamSemStruc {
uchar_t RamSemOwner ;       /* ownership flag */
uchar_t RamSemFlag ;       /* ram semaphore flag bit field */
ushort_t RamSemID ;       /* block/run ID low word */
} RamSemStruc ;

#define RamSemBusyFlag RamSemOwner     /* for dossem.asm */


/* The system semaphore table structure. There are dependences on the */
/* size of this structure being 4 bytes, but all these references */
/* have an errnz statement protecting them. */

typedef struct SysSemTblStruc {
ushort_t SysSemOwner ;     /* thread owning this semaphore */
uchar_t SysSemFlag ;       /* system semaphore flag bit field */
uchar_t SysSemRefCnt ;     /* number of references to this sys sem */
uchar_t SysSemProcCnt ;    /* number of requests for this owner */
uchar_t SysSemPad ;        /* pad byte to round structure up to word */
/*b734380ulong_t SysSempPvwBlk;*/  /* ptr to performance counter data block */ /*sfb*/
} SysSemTblStruc ;


/* This is an extension of the SysSemTblStruc that is put into */
/* high memory so we don't impact the low data segment.  It is  */
/* only used in protected mode during process/thread termination. */
/* There are dependences on the size of this structure being 2 */
/* bytes, but all these references have an errnz statement protecting */
/* them. */

typedef struct SysSemHighTableS {
ushort_t SysSemPidOwner ;       /* pid owner, the thread owner has died */
} SysSemHighTableS ;


/* The system semaphore name table structure, managed by RMP. */

typedef struct SysSemNameStruc {
ushort_t SysSemPtr ;
} SysSemNameStruc ;

/* XLATEOFF */
#define SysSemName  (sizeof(SysSemNameStruc)) /* system semaphore name */
/* XLATEON */

/* ASM
SysSemName  =       (size SysSemNameStruc)  ; system semaphore name
 */


/* The open system semaphore PTDA table structure. Note that code using
   this table depends that the entries are words. Shifts are used to
   current an index to the offset into the table. */

typedef struct SysSemPTDAStruc {
uchar_t OpenSemTable[SYSSEM_MAXIMUM] ;
} SysSemPTDAStruc ;


/* The event entries in the MuxList */

typedef struct MuxListEntry {
ushort_t MuxReserved ;
ulong_t MuxSemHandle ;
} MuxListEntry ;


/* The event list passed to MuxSemWait */

typedef struct MuxList {
ushort_t MuxSemCount ;
MuxListEntry MuxEventList ;
} MuxList ;


/*The defination of the MuxSemWait table entries.  There are dependences
  on the size of this structure being 8 bytes, but all these references
  have an errnz statement protected them. */

typedef struct MuxTableEntry {
ushort_t MuxLink ;      /* Link to next entry */
ushort_t MuxThreadID ;  /* Identifies the thread waiting */
uchar_t MuxType ;       /* Identifies MuxSemID type */
ulong_t MuxSemID ;      /* Identifies the semaphore muxwait'ing */
} MuxTableEntry ;
