⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 proc.c

📁 PostgreSQL7.4.6 for Linux
💻 C
📖 第 1 页 / 共 3 页
字号:
/*------------------------------------------------------------------------- * * proc.c *	  routines to manage per-process shared memory data structure * * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION *	  $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.136 2003/10/16 20:59:35 tgl Exp $ * *------------------------------------------------------------------------- *//* * Interface (a): *		ProcSleep(), ProcWakeup(), *		ProcQueueAlloc() -- create a shm queue for sleeping processes *		ProcQueueInit() -- create a queue without allocing memory * * Locking and waiting for buffers can cause the backend to be * put to sleep.  Whoever releases the lock, etc. wakes the * process up again (and gives it an error code so it knows * whether it was awoken on an error condition). * * Interface (b): * * ProcReleaseLocks -- frees the locks associated with current transaction * * ProcKill -- destroys the shared memory state (and locks) *		associated with the process. * * 5/15/91 -- removed the buffer pool based lock chain in favor *		of a shared memory lock chain.	The write-protection is *		more expensive if the lock chain is in the buffer pool. *		The only reason I kept the lock chain in the buffer pool *		in the first place was to allow the lock table to grow larger *		than available shared memory and that isn't going to work *		without a lot of unimplemented support anyway. */#include "postgres.h"#include <errno.h>#include <signal.h>#include <unistd.h>#include <sys/time.h>#include "miscadmin.h"#include "access/xact.h"#include "storage/ipc.h"#include "storage/proc.h"#include "storage/sinval.h"#include "storage/spin.h"/* GUC variables */int			DeadlockTimeout = 1000;int			StatementTimeout = 0;/* Pointer to this process's PGPROC struct, if any */PGPROC	   *MyProc = NULL;/* * This spinlock protects the freelist of recycled PGPROC structures. * We cannot use an LWLock because the LWLock manager depends on already * having a PGPROC and a wait semaphore!  But these structures are touched * relatively infrequently (only at backend startup or shutdown) and not for * very long, so a spinlock is okay. */static slock_t *ProcStructLock = NULL;static PROC_HDR *ProcGlobal = NULL;static PGPROC *DummyProc = NULL;static bool waitingForLock = false;static bool waitingForSignal = false;/* Mark these volatile because they can be changed by signal handler */static volatile bool statement_timeout_active = false;static volatile bool deadlock_timeout_active = false;/* statement_fin_time is valid only if statement_timeout_active is true */static struct timeval statement_fin_time;static void ProcKill(void);static void DummyProcKill(void);static bool CheckStatementTimeout(void);/* * Report number of semaphores needed by InitProcGlobal. */intProcGlobalSemas(int maxBackends){	/* We need a sema per backend, plus one for the dummy process. */	return maxBackends + 1;}/* * InitProcGlobal - *	  initializes the global process table. We put it here so that *	  the postmaster can do this initialization. * *	  We also create all the per-process semaphores we will need to support *	  the requested number of backends.  We used to allocate semaphores *	  only when backends were actually started up, but that is bad because *	  it lets Postgres fail under load --- a lot of Unix systems are *	  (mis)configured with small limits on the number of semaphores, and *	  running out when trying to start another backend is a common failure. *	  So, now we grab enough semaphores to support the desired max number *	  of backends immediately at initialization --- if the sysadmin has set *	  MaxBackends higher than his kernel will support, he'll find out sooner *	  rather than later. * *	  Another reason for creating semaphores here is that the semaphore *	  implementation typically requires us to create semaphores in the *	  postmaster, not in backends. */voidInitProcGlobal(int maxBackends){	bool		found = false;	/* Create or attach to the ProcGlobal shared structure */	ProcGlobal = (PROC_HDR *)		ShmemInitStruct("Proc Header", sizeof(PROC_HDR), &found);	/* --------------------	 * We're the first - initialize.	 * XXX if found should ever be true, it is a sign of impending doom ...	 * ought to complain if so?	 * --------------------	 */	if (!found)	{		int			i;		ProcGlobal->freeProcs = INVALID_OFFSET;		/*		 * Pre-create the PGPROC structures and create a semaphore for		 * each.		 */		for (i = 0; i < maxBackends; i++)		{			PGPROC	   *proc;			proc = (PGPROC *) ShmemAlloc(sizeof(PGPROC));			if (!proc)				ereport(FATAL,						(errcode(ERRCODE_OUT_OF_MEMORY),						 errmsg("out of shared memory")));			MemSet(proc, 0, sizeof(PGPROC));			PGSemaphoreCreate(&proc->sem);			proc->links.next = ProcGlobal->freeProcs;			ProcGlobal->freeProcs = MAKE_OFFSET(proc);		}		/*		 * Pre-allocate a PGPROC structure for dummy (checkpoint)		 * processes, too.	This does not get linked into the freeProcs		 * list.		 */		DummyProc = (PGPROC *) ShmemAlloc(sizeof(PGPROC));		if (!DummyProc)			ereport(FATAL,					(errcode(ERRCODE_OUT_OF_MEMORY),					 errmsg("out of shared memory")));		MemSet(DummyProc, 0, sizeof(PGPROC));		DummyProc->pid = 0;		/* marks DummyProc as not in use */		PGSemaphoreCreate(&DummyProc->sem);		/* Create ProcStructLock spinlock, too */		ProcStructLock = (slock_t *) ShmemAlloc(sizeof(slock_t));		SpinLockInit(ProcStructLock);	}}/* * InitProcess -- initialize a per-process data structure for this backend */voidInitProcess(void){	SHMEM_OFFSET myOffset;	/* use volatile pointer to prevent code rearrangement */	volatile PROC_HDR *procglobal = ProcGlobal;	/*	 * ProcGlobal should be set by a previous call to InitProcGlobal (if	 * we are a backend, we inherit this by fork() from the postmaster).	 */	if (procglobal == NULL)		elog(PANIC, "proc header uninitialized");	if (MyProc != NULL)		elog(ERROR, "you already exist");	/*	 * Try to get a proc struct from the free list.  If this fails, we	 * must be out of PGPROC structures (not to mention semaphores).	 */	SpinLockAcquire(ProcStructLock);	myOffset = procglobal->freeProcs;	if (myOffset != INVALID_OFFSET)	{		MyProc = (PGPROC *) MAKE_PTR(myOffset);		procglobal->freeProcs = MyProc->links.next;		SpinLockRelease(ProcStructLock);	}	else	{		/*		 * If we reach here, all the PGPROCs are in use.  This is one of		 * the possible places to detect "too many backends", so give the		 * standard error message.		 */		SpinLockRelease(ProcStructLock);		ereport(FATAL,				(errcode(ERRCODE_TOO_MANY_CONNECTIONS),				 errmsg("sorry, too many clients already")));	}	/*	 * Initialize all fields of MyProc, except for the semaphore which was	 * prepared for us by InitProcGlobal.	 */	SHMQueueElemInit(&(MyProc->links));	MyProc->errType = STATUS_OK;	MyProc->xid = InvalidTransactionId;	MyProc->xmin = InvalidTransactionId;	MyProc->pid = MyProcPid;	MyProc->databaseId = MyDatabaseId;	MyProc->logRec.xrecoff = 0;	MyProc->lwWaiting = false;	MyProc->lwExclusive = false;	MyProc->lwWaitLink = NULL;	MyProc->waitLock = NULL;	MyProc->waitHolder = NULL;	SHMQueueInit(&(MyProc->procHolders));	/*	 * Arrange to clean up at backend exit.	 */	on_shmem_exit(ProcKill, 0);	/*	 * We might be reusing a semaphore that belonged to a failed process.	 * So be careful and reinitialize its value here.	 */	PGSemaphoreReset(&MyProc->sem);	/*	 * Now that we have a PGPROC, we could try to acquire locks, so	 * initialize the deadlock checker.	 */	InitDeadLockChecking();}/* * InitDummyProcess -- create a dummy per-process data structure * * This is called by checkpoint processes so that they will have a MyProc * value that's real enough to let them wait for LWLocks.  The PGPROC and * sema that are assigned are the extra ones created during InitProcGlobal. */voidInitDummyProcess(void){	/*	 * ProcGlobal should be set by a previous call to InitProcGlobal (we	 * inherit this by fork() from the postmaster).	 */	if (ProcGlobal == NULL || DummyProc == NULL)		elog(PANIC, "proc header uninitialized");	if (MyProc != NULL)		elog(ERROR, "you already exist");	/*	 * DummyProc should not presently be in use by anyone else	 */	if (DummyProc->pid != 0)		elog(FATAL, "DummyProc is in use by PID %d", DummyProc->pid);	MyProc = DummyProc;	/*	 * Initialize all fields of MyProc, except MyProc->sem which was set	 * up by InitProcGlobal.	 */	MyProc->pid = MyProcPid;	/* marks DummyProc as in use by me */	SHMQueueElemInit(&(MyProc->links));	MyProc->errType = STATUS_OK;	MyProc->xid = InvalidTransactionId;	MyProc->xmin = InvalidTransactionId;	MyProc->databaseId = MyDatabaseId;	MyProc->logRec.xrecoff = 0;	MyProc->lwWaiting = false;	MyProc->lwExclusive = false;	MyProc->lwWaitLink = NULL;	MyProc->waitLock = NULL;	MyProc->waitHolder = NULL;	SHMQueueInit(&(MyProc->procHolders));	/*	 * Arrange to clean up at process exit.	 */	on_shmem_exit(DummyProcKill, 0);	/*	 * We might be reusing a semaphore that belonged to a failed process.	 * So be careful and reinitialize its value here.	 */	PGSemaphoreReset(&MyProc->sem);}/* * Cancel any pending wait for lock, when aborting a transaction. * * Returns true if we had been waiting for a lock, else false. * * (Normally, this would only happen if we accept a cancel/die * interrupt while waiting; but an ereport(ERROR) while waiting is * within the realm of possibility, too.) */boolLockWaitCancel(void){	/* Nothing to do if we weren't waiting for a lock */	if (!waitingForLock)		return false;	waitingForLock = false;	/* Turn off the deadlock timer, if it's still running (see ProcSleep) */	disable_sig_alarm(false);	/* Unlink myself from the wait queue, if on it (might not be anymore!) */	LWLockAcquire(LockMgrLock, LW_EXCLUSIVE);	if (MyProc->links.next != INVALID_OFFSET)		RemoveFromWaitQueue(MyProc);	LWLockRelease(LockMgrLock);	/*	 * Reset the proc wait semaphore to zero.  This is necessary in the	 * scenario where someone else granted us the lock we wanted before we	 * were able to remove ourselves from the wait-list.  The semaphore	 * will have been bumped to 1 by the would-be grantor, and since we	 * are no longer going to wait on the sema, we have to force it back	 * to zero. Otherwise, our next attempt to wait for a lock will fall	 * through prematurely.	 */	PGSemaphoreReset(&MyProc->sem);	/*	 * Return true even if we were kicked off the lock before we were able	 * to remove ourselves.	 */	return true;}/* * ProcReleaseLocks() -- release locks associated with current transaction *			at transaction commit or abort * * At commit, we release only locks tagged with the current transaction's XID, * leaving those marked with XID 0 (ie, session locks) undisturbed.  At abort, * we release all locks including XID 0, because we need to clean up after * a failure.  This logic will need extension if we ever support nested * transactions. * * Note that user locks are not released in either case. */voidProcReleaseLocks(bool isCommit){	if (!MyProc)		return;	/* If waiting, get off wait queue (should only be needed after error) */	LockWaitCancel();	/* Release locks */	LockReleaseAll(DEFAULT_LOCKMETHOD, MyProc,				   !isCommit, GetCurrentTransactionId());}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -