1 /*-------------------------------------------------------------------------
4 * per-process shared memory data structures
7 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $Id: proc.h,v 1.43 2001/05/25 15:45:34 momjian Exp $
12 *-------------------------------------------------------------------------
17 #include "access/xlog.h"
18 #include "storage/lock.h"
20 /* configurable option */
21 extern int DeadlockTimeout;
25 IpcSemaphoreId semId; /* SysV semaphore set ID */
26 int semNum; /* semaphore number within set */
30 * Each backend has a PROC struct in shared memory. There is also a list of
31 * currently-unused PROC structs that will be reallocated to new backends.
33 * links: list link for any list the PROC is in. When waiting for a lock,
34 * the PROC is linked into that lock's waitProcs queue. A recycled PROC
35 * is linked into ProcGlobal's freeProcs list.
39 /* proc->links MUST BE FIRST IN STRUCT (see ProcSleep,ProcWakeup,etc) */
41 SHM_QUEUE links; /* list link if process is in a list */
43 SEMA sem; /* ONE semaphore to sleep on */
44 int errType; /* STATUS_OK or STATUS_ERROR after wakeup */
46 TransactionId xid; /* transaction currently being executed by
49 TransactionId xmin; /* minimal running XID as it was when we
50 * were starting our xact: vacuum must not
51 * remove tuples deleted by xid >= xmin ! */
54 * XLOG location of first XLOG record written by this backend's
55 * current transaction. If backend is not in a transaction or hasn't
56 * yet modified anything, logRec.xrecoff is zero.
60 /* Info about lock the process is currently waiting for, if any. */
61 /* waitLock and waitHolder are NULL if not currently waiting. */
62 LOCK *waitLock; /* Lock object we're sleeping on ... */
63 HOLDER *waitHolder; /* Per-holder info for awaited lock */
64 LOCKMODE waitLockMode; /* type of lock we're waiting for */
65 LOCKMASK heldLocks; /* bitmask for lock types already held on
66 * this lock object by this backend */
68 int pid; /* This backend's process id */
69 Oid databaseId; /* OID of database this backend is using */
71 short sLocks[MAX_SPINS]; /* Spin lock stats */
72 SHM_QUEUE procHolders; /* list of HOLDER objects for locks held
73 * or awaited by this backend */
76 /* NOTE: "typedef struct proc PROC" appears in storage/lock.h. */
81 extern SPINLOCK ProcStructLock;
84 #define PROC_INCR_SLOCK(lock) \
86 if (MyProc) (MyProc->sLocks[(lock)])++; \
89 #define PROC_DECR_SLOCK(lock) \
91 if (MyProc) (MyProc->sLocks[(lock)])--; \
96 * There is one ProcGlobal struct for the whole installation.
98 * PROC_NSEMS_PER_SET is the number of semaphores in each sys-V semaphore set
99 * we allocate. It must be no more than 32 (or however many bits in an int
100 * on your machine), or our free-semaphores bitmap won't work. It also must
101 * be *less than* your kernel's SEMMSL (max semaphores per set) parameter,
102 * which is often around 25. (Less than, because we allocate one extra sema
103 * in each set for identification purposes.)
105 * PROC_SEM_MAP_ENTRIES is the number of semaphore sets we need to allocate
106 * to keep track of up to MAXBACKENDS backends.
108 #define PROC_NSEMS_PER_SET 16
109 #define PROC_SEM_MAP_ENTRIES ((MAXBACKENDS-1)/PROC_NSEMS_PER_SET+1)
111 typedef struct procglobal
113 /* Head of list of free PROC structures */
114 SHMEM_OFFSET freeProcs;
116 /* Info about semaphore sets used for per-process semaphores */
117 IpcSemaphoreId procSemIds[PROC_SEM_MAP_ENTRIES];
118 int32 freeSemMap[PROC_SEM_MAP_ENTRIES];
121 * In each freeSemMap entry, bit i is set if the i'th semaphore of the
122 * set is allocated to a process. (i counts from 0 at the LSB)
127 * Function Prototypes
129 extern void InitProcGlobal(int maxBackends);
130 extern void InitProcess(void);
131 extern void ProcReleaseLocks(bool isCommit);
132 extern bool ProcRemove(int pid);
134 extern void ProcQueueInit(PROC_QUEUE *queue);
135 extern int ProcSleep(LOCKMETHODTABLE *lockMethodTable, LOCKMODE lockmode,
136 LOCK *lock, HOLDER *holder);
137 extern PROC *ProcWakeup(PROC *proc, int errType);
138 extern void ProcLockWakeup(LOCKMETHODTABLE *lockMethodTable, LOCK *lock);
139 extern void ProcReleaseSpins(PROC *proc);
140 extern bool LockWaitCancel(void);
141 extern void HandleDeadLock(SIGNAL_ARGS);