proc.h 3.55 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * proc.h
4
 *
5 6 7 8
 *
 *
 * Copyright (c) 1994, Regents of the University of California
 *
9
 * $Id: proc.h,v 1.26 1999/09/24 00:25:27 tgl Exp $
10 11 12 13 14 15
 *
 *-------------------------------------------------------------------------
 */
#ifndef _PROC_H_
#define _PROC_H_

16
#include "storage/lock.h"
17

18 19
typedef struct
{
20 21 22
	int			sleeplock;
	int			semNum;
	IpcSemaphoreId semId;
23
	IpcSemaphoreKey semKey;
24
} SEMA;
25 26 27 28

/*
 * Each backend has:
 */
29 30
typedef struct proc
{
31

32
	/* proc->links MUST BE THE FIRST ELEMENT OF STRUCT (see ProcWakeup()) */
33

34 35 36
	SHM_QUEUE	links;			/* proc can be waiting for one event(lock) */
	SEMA		sem;			/* ONE semaphore to sleep on */
	int			errType;		/* error code tells why we woke up */
37

38
	int			critSects;		/* If critSects > 0, we are in sensitive
39 40
								 * routines that cannot be recovered when
								 * the process fails. */
41

42
	int			prio;			/* priority for sleep queue */
43

44
	TransactionId xid;			/* transaction currently being executed by
45
								 * this proc */
46

47 48 49
	TransactionId xmin;			/* minimal running XID as it was when we
								 * were starting our xact: vacuum must not
								 * remove tuples deleted by xid >= xmin ! */
50

Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
51 52 53
	LOCK	   *waitLock;		/* Lock we're sleeping on ... */
	int			token;			/* type of lock we sleeping for */
	int			holdLock;		/* while holding these locks */
54 55
	int			pid;			/* This backend's process id */
	Oid			databaseId;		/* OID of database this backend is using */
56 57
	short		sLocks[MAX_SPINS];		/* Spin lock stats */
	SHM_QUEUE	lockQueue;		/* locks associated with current
58
								 * transaction */
59
} PROC;
60 61 62


/*
63 64 65 66 67
 * PROC_NSEMS_PER_SET is the number of semaphores in each sys-V semaphore set
 * we allocate.  It must be *less than* 32 (or however many bits in an int
 * on your machine), or our free-semaphores bitmap won't work.  You also must
 * not set it higher than your kernel's SEMMSL (max semaphores per set)
 * parameter, which is often around 25.
68
 *
69
 * MAX_PROC_SEMS is the maximum number of per-process semaphores (those used
70 71
 * by the lock mgr) we can keep track of.  It must be a multiple of
 * PROC_NSEMS_PER_SET.
72
 */
73
#define  PROC_NSEMS_PER_SET		16
74
#define  MAX_PROC_SEMS			(((MAXBACKENDS-1)/PROC_NSEMS_PER_SET+1)*PROC_NSEMS_PER_SET)
75

76 77
typedef struct procglobal
{
78 79 80
	SHMEM_OFFSET freeProcs;
	IPCKey		currKey;
	int32		freeSemMap[MAX_PROC_SEMS / PROC_NSEMS_PER_SET];
Bruce Momjian's avatar
Bruce Momjian committed
81 82

	/*
83 84 85
	 * In each freeSemMap entry, the PROC_NSEMS_PER_SET least-significant bits
	 * flag whether individual semaphores are in use, and the next higher bit
	 * is set to show that the entire set is allocated.
86
	 */
87
} PROC_HDR;
88

89
extern PROC *MyProc;
90

91 92 93 94 95 96 97 98 99
#define PROC_INCR_SLOCK(lock) \
do { \
	if (MyProc) (MyProc->sLocks[(lock)])++; \
} while (0)

#define PROC_DECR_SLOCK(lock) \
do { \
	if (MyProc) (MyProc->sLocks[(lock)])--; \
} while (0)
100 101 102 103

/*
 * flags explaining why process woke up
 */
104 105
#define NO_ERROR		0
#define ERR_TIMEOUT		1
106 107
#define ERR_BUFFER_IO	2

108 109
#define MAX_PRIO		50
#define MIN_PRIO		(-1)
110 111 112 113 114 115

extern SPINLOCK ProcStructLock;

/*
 * Function Prototypes
 */
116 117 118
extern void InitProcess(IPCKey key);
extern void ProcReleaseLocks(void);
extern bool ProcRemove(int pid);
119

120 121 122
/* extern bool ProcKill(int exitStatus, int pid); */
/* make static in storage/lmgr/proc.c -- jolly */

123
extern void ProcQueueInit(PROC_QUEUE *queue);
124
extern int ProcSleep(PROC_QUEUE *queue, LOCKMETHODCTL *lockctl, int token,
Bruce Momjian's avatar
Bruce Momjian committed
125
		  LOCK *lock);
Bruce Momjian's avatar
Bruce Momjian committed
126
extern PROC *ProcWakeup(PROC *proc, int errType);
127 128
extern int ProcLockWakeup(PROC_QUEUE *queue, LOCKMETHOD lockmethod,
			   LOCK *lock);
129 130
extern void ProcAddLock(SHM_QUEUE *elem);
extern void ProcReleaseSpins(PROC *proc);
131

132
#endif	 /* PROC_H */