proc.c 24.4 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * proc.c
4
 *	  routines to manage per-process shared memory data structure
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.61 1999/09/24 00:24:41 tgl Exp $
11 12 13 14
 *
 *-------------------------------------------------------------------------
 */
/*
15 16
 *	Each postgres backend gets one of these.  We'll use it to
 *	clean up after the process should the process suddenly die.
17 18 19
 *
 *
 * Interface (a):
20 21 22
 *		ProcSleep(), ProcWakeup(), ProcWakeupNext(),
 *		ProcQueueAlloc() -- create a shm queue for sleeping processes
 *		ProcQueueInit() -- create a queue without allocing memory
23 24 25 26 27 28 29 30 31 32
 *
 * 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 this process,
 * ProcKill -- destroys the shared memory state (and locks)
33
 *		associated with the process.
34 35
 *
 * 5/15/91 -- removed the buffer pool based lock chain in favor
36 37 38 39 40 41
 *		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.
42 43
 *
 * 4/7/95 -- instead of allocating a set of 1 semaphore per process, we
44 45 46 47
 *		allocate a semaphore from a set of PROC_NSEMS_PER_SET semaphores
 *		shared among backends (we keep a few sets of semaphores around).
 *		This is so that we can support more backends. (system-wide semaphore
 *		sets run out pretty fast.)				  -ay 4/95
48
 *
49
 * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.61 1999/09/24 00:24:41 tgl Exp $
50 51 52
 */
#include <sys/time.h>
#include <unistd.h>
53
#include <signal.h>
54
#include <sys/types.h>
M
Marc G. Fournier 已提交
55

B
Bruce Momjian 已提交
56
#if defined(solaris_sparc)
57 58 59 60
#include <sys/ipc.h>
#include <sys/sem.h>
#endif

M
Marc G. Fournier 已提交
61
#include "postgres.h"
62
#include "miscadmin.h"
63
#include "libpq/pqsignal.h"
64 65


66 67
/* In Ultrix, sem.h must be included after ipc.h */
#include <sys/sem.h>
B
Bruce Momjian 已提交
68

B
Bruce Momjian 已提交
69
#include "storage/lmgr.h"
70
#include "storage/proc.h"
M
 
Marc G. Fournier 已提交
71
#include "utils/trace.h"
72

B
Bruce Momjian 已提交
73
static void HandleDeadLock(int sig);
74
static void ProcFreeAllSemaphores(void);
75

M
 
Marc G. Fournier 已提交
76 77
#define DeadlockCheckTimer pg_options[OPT_DEADLOCKTIMEOUT]

78 79 80 81 82 83 84
/* --------------------
 * Spin lock for manipulating the shared process data structure:
 * ProcGlobal.... Adding an extra spin lock seemed like the smallest
 * hack to get around reading and updating this structure in shared
 * memory. -mer 17 July 1991
 * --------------------
 */
85
SPINLOCK	ProcStructLock;
86 87 88 89 90

/*
 * For cleanup routines.  Don't cleanup if the initialization
 * has not happened.
 */
91
static bool ProcInitialized = FALSE;
92 93 94

static PROC_HDR *ProcGlobal = NULL;

95
PROC	   *MyProc = NULL;
96

97
static void ProcKill(int exitStatus, int pid);
98
static void ProcGetNewSemKeyAndNum(IPCKey *key, int *semNum);
99
static void ProcFreeSem(IpcSemaphoreKey semKey, int semNum);
100

V
Vadim B. Mikheev 已提交
101 102
static char *DeadLockMessage = "Deadlock detected -- See the lock(l) manual page for a possible cause.";

103 104
/*
 * InitProcGlobal -
105
 *	  initializes the global process table. We put it here so that
106
 *	  the postmaster can do this initialization. (ProcFreeAllSemaphores needs
107 108 109
 *	  to read this table on exiting the postmaster. If we have the first
 *	  backend do this, starting up and killing the postmaster without
 *	  starting any backends will be a problem.)
110 111 112 113 114 115 116 117 118 119 120
 *
 *	  We also allocate 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.
121 122
 */
void
123
InitProcGlobal(IPCKey key, int maxBackends)
124
{
125
	bool		found = false;
126

127 128 129
	/* attach to the free list */
	ProcGlobal = (PROC_HDR *)
		ShmemInitStruct("Proc Header", (unsigned) sizeof(PROC_HDR), &found);
130

131 132
	/* --------------------
	 * We're the first - initialize.
133 134
	 * XXX if found should ever be true, it is a sign of impending doom ...
	 * ought to complain if so?
135 136 137
	 * --------------------
	 */
	if (!found)
138
	{
139
		int			i;
140

141 142 143 144
		ProcGlobal->freeProcs = INVALID_OFFSET;
		ProcGlobal->currKey = IPCGetProcessSemaphoreInitKey(key);
		for (i = 0; i < MAX_PROC_SEMS / PROC_NSEMS_PER_SET; i++)
			ProcGlobal->freeSemMap[i] = 0;
145

B
Bruce Momjian 已提交
146 147 148
		/*
		 * Arrange to delete semas on exit --- set this up now so that we
		 * will clean up if pre-allocation fails...
149 150 151
		 */
		on_shmem_exit(ProcFreeAllSemaphores, NULL);

B
Bruce Momjian 已提交
152 153
		/*
		 * Pre-create the semaphores for the first maxBackends processes,
154 155 156
		 * unless we are running as a standalone backend.
		 */
		if (key != PrivateIPCKey)
157
		{
158
			for (i = 0;
B
Bruce Momjian 已提交
159
				 i < (maxBackends + PROC_NSEMS_PER_SET - 1) / PROC_NSEMS_PER_SET;
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
				 i++)
			{
				IPCKey		semKey = ProcGlobal->currKey + i;
				int			semId;
				int			semstat;

				semId = IpcSemaphoreCreate(semKey,
										   PROC_NSEMS_PER_SET,
										   IPCProtection,
										   IpcSemaphoreDefaultStartValue,
										   0,
										   &semstat);
				/* mark this sema set allocated */
				ProcGlobal->freeSemMap[i] = (1 << PROC_NSEMS_PER_SET);
			}
175
		}
176 177 178 179 180 181 182 183 184 185 186
	}
}

/* ------------------------
 * InitProc -- create a per-process data structure for this process
 * used by the lock manager on semaphore queues.
 * ------------------------
 */
void
InitProcess(IPCKey key)
{
187 188 189 190
	bool		found = false;
	int			semstat;
	unsigned long location,
				myOffset;
191 192 193 194 195

	/* ------------------
	 * Routine called if deadlock timer goes off. See ProcSleep()
	 * ------------------
	 */
B
Bruce Momjian 已提交
196 197
	pqsignal(SIGALRM, HandleDeadLock);

198 199 200 201 202 203
	SpinAcquire(ProcStructLock);

	/* attach to the free list */
	ProcGlobal = (PROC_HDR *)
		ShmemInitStruct("Proc Header", (unsigned) sizeof(PROC_HDR), &found);
	if (!found)
204
	{
205
		/* this should not happen. InitProcGlobal() is called before this. */
206
		elog(ERROR, "InitProcess: Proc Header uninitialized");
207
	}
208 209

	if (MyProc != NULL)
210
	{
211
		SpinRelease(ProcStructLock);
212
		elog(ERROR, "ProcInit: you already exist");
213
		return;
214
	}
215 216 217 218 219 220

	/* try to get a proc from the free list first */

	myOffset = ProcGlobal->freeProcs;

	if (myOffset != INVALID_OFFSET)
221
	{
222 223 224 225 226 227 228
		MyProc = (PROC *) MAKE_PTR(myOffset);
		ProcGlobal->freeProcs = MyProc->links.next;
	}
	else
	{

		/*
229 230 231 232
		 * have to allocate one.  We can't use the normal shmem index
		 * table mechanism because the proc structure is stored by PID
		 * instead of by a global name (need to look it up by PID when we
		 * cleanup dead processes).
233 234 235 236
		 */

		MyProc = (PROC *) ShmemAlloc((unsigned) sizeof(PROC));
		if (!MyProc)
237
		{
238 239
			SpinRelease(ProcStructLock);
			elog(FATAL, "cannot create new proc: out of memory");
240
		}
241 242 243

		/* this cannot be initialized until after the buffer pool */
		SHMQueueInit(&(MyProc->lockQueue));
244
	}
245

246
	/*
247 248 249
	 * zero out the spin lock counts and set the sLocks field for
	 * ProcStructLock to 1 as we have acquired this spinlock above but
	 * didn't record it since we didn't have MyProc until now.
250
	 */
B
Bruce Momjian 已提交
251
	MemSet(MyProc->sLocks, 0, sizeof(MyProc->sLocks));
252 253 254 255 256
	MyProc->sLocks[ProcStructLock] = 1;


	if (IsUnderPostmaster)
	{
257 258 259 260
		IPCKey		semKey;
		int			semNum;
		int			semId;
		union semun semun;
261 262 263

		ProcGetNewSemKeyAndNum(&semKey, &semNum);

B
Bruce Momjian 已提交
264 265 266 267 268
		/*
		 * Note: because of the pre-allocation done in InitProcGlobal,
		 * this call should always attach to an existing semaphore. It
		 * will (try to) create a new group of semaphores only if the
		 * postmaster tries to start more backends than it said it would.
269
		 */
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
		semId = IpcSemaphoreCreate(semKey,
								   PROC_NSEMS_PER_SET,
								   IPCProtection,
								   IpcSemaphoreDefaultStartValue,
								   0,
								   &semstat);

		/*
		 * we might be reusing a semaphore that belongs to a dead backend.
		 * So be careful and reinitialize its value here.
		 */
		semun.val = IpcSemaphoreDefaultStartValue;
		semctl(semId, semNum, SETVAL, semun);

		IpcSemaphoreLock(semId, semNum, IpcExclusiveLock);
		MyProc->sem.semId = semId;
		MyProc->sem.semNum = semNum;
		MyProc->sem.semKey = semKey;
	}
	else
		MyProc->sem.semId = -1;

	/* ----------------------
	 * Release the lock.
	 * ----------------------
	 */
	SpinRelease(ProcStructLock);

B
Bruce Momjian 已提交
298
	MyProc->pid = MyProcPid;
299
	MyProc->databaseId = MyDatabaseId;
300
	MyProc->xid = InvalidTransactionId;
301
	MyProc->xmin = InvalidTransactionId;
302 303 304 305 306 307

	/* ----------------
	 * Start keeping spin lock stats from here on.	Any botch before
	 * this initialization is forever botched
	 * ----------------
	 */
B
Bruce Momjian 已提交
308
	MemSet(MyProc->sLocks, 0, MAX_SPINS * sizeof(*MyProc->sLocks));
309 310

	/* -------------------------
311
	 * Install ourselves in the shmem index table.	The name to
312 313 314 315 316 317
	 * use is determined by the OS-assigned process id.  That
	 * allows the cleanup process to find us after any untimely
	 * exit.
	 * -------------------------
	 */
	location = MAKE_OFFSET(MyProc);
B
Bruce Momjian 已提交
318
	if ((!ShmemPIDLookup(MyProcPid, &location)) || (location != MAKE_OFFSET(MyProc)))
319 320 321 322 323
		elog(FATAL, "InitProc: ShmemPID table broken");

	MyProc->errType = NO_ERROR;
	SHMQueueElemInit(&(MyProc->links));

324
	on_shmem_exit(ProcKill, (caddr_t) MyProcPid);
325 326

	ProcInitialized = TRUE;
327 328 329 330 331 332 333 334 335
}

/*
 * ProcReleaseLocks() -- release all locks associated with this process
 *
 */
void
ProcReleaseLocks()
{
336 337 338
	if (!MyProc)
		return;
	LockReleaseAll(1, &MyProc->lockQueue);
339 340 341 342
}

/*
 * ProcRemove -
343 344 345 346 347
 *	  used by the postmaster to clean up the global tables. This also frees
 *	  up the semaphore used for the lmgr of the process. (We have to do
 *	  this is the postmaster instead of doing a IpcSemaphoreKill on exiting
 *	  the process because the semaphore set is shared among backends and
 *	  we don't want to remove other's semaphores on exit.)
348 349 350 351
 */
bool
ProcRemove(int pid)
{
352 353
	SHMEM_OFFSET location;
	PROC	   *proc;
354 355 356 357 358

	location = INVALID_OFFSET;

	location = ShmemPIDDestroy(pid);
	if (location == INVALID_OFFSET)
359
		return FALSE;
360 361 362 363 364 365 366 367 368 369 370
	proc = (PROC *) MAKE_PTR(location);

	SpinAcquire(ProcStructLock);

	ProcFreeSem(proc->sem.semKey, proc->sem.semNum);

	proc->links.next = ProcGlobal->freeProcs;
	ProcGlobal->freeProcs = MAKE_OFFSET(proc);

	SpinRelease(ProcStructLock);

371
	return TRUE;
372 373 374 375
}

/*
 * ProcKill() -- Destroy the per-proc data structure for
376
 *		this process. Release any of its held spin locks.
377 378 379 380
 */
static void
ProcKill(int exitStatus, int pid)
{
381 382
	PROC	   *proc;
	SHMEM_OFFSET location;
383 384 385 386 387 388 389 390 391 392

	/* --------------------
	 * If this is a FATAL exit the postmaster will have to kill all the
	 * existing backends and reinitialize shared memory.  So all we don't
	 * need to do anything here.
	 * --------------------
	 */
	if (exitStatus != 0)
		return;

B
Bruce Momjian 已提交
393
	ShmemPIDLookup(MyProcPid, &location);
394 395 396 397 398
	if (location == INVALID_OFFSET)
		return;

	proc = (PROC *) MAKE_PTR(location);

399 400 401
	Assert(proc == MyProc || pid != MyProcPid);

	MyProc = NULL;
402 403 404 405 406 407

	/* ---------------
	 * Assume one lock table.
	 * ---------------
	 */
	ProcReleaseSpins(proc);
M
 
Marc G. Fournier 已提交
408
	LockReleaseAll(DEFAULT_LOCKMETHOD, &proc->lockQueue);
409

410
#ifdef USER_LOCKS
411

M
 
Marc G. Fournier 已提交
412 413 414 415
	/*
	 * Assume we have a second lock table.
	 */
	LockReleaseAll(USER_LOCKMETHOD, &proc->lockQueue);
416 417
#endif

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
	/* ----------------
	 * get off the wait queue
	 * ----------------
	 */
	LockLockTable();
	if (proc->links.next != INVALID_OFFSET)
	{
		Assert(proc->waitLock->waitProcs.size > 0);
		SHMQueueDelete(&(proc->links));
		--proc->waitLock->waitProcs.size;
	}
	SHMQueueElemInit(&(proc->links));
	UnlockLockTable();

	return;
433 434 435 436
}

/*
 * ProcQueue package: routines for putting processes to sleep
437
 *		and  waking them up
438 439 440 441 442 443 444 445
 */

/*
 * ProcQueueAlloc -- alloc/attach to a shared memory process queue
 *
 * Returns: a pointer to the queue or NULL
 * Side Effects: Initializes the queue if we allocated one
 */
446
#ifdef NOT_USED
447
PROC_QUEUE *
448 449
ProcQueueAlloc(char *name)
{
450 451
	bool		found;
	PROC_QUEUE *queue = (PROC_QUEUE *)
452 453 454
	ShmemInitStruct(name, (unsigned) sizeof(PROC_QUEUE), &found);

	if (!queue)
455
		return NULL;
456 457
	if (!found)
		ProcQueueInit(queue);
458
	return queue;
459
}
460

461
#endif
462 463 464 465 466

/*
 * ProcQueueInit -- initialize a shared memory process queue
 */
void
467
ProcQueueInit(PROC_QUEUE *queue)
468
{
469 470
	SHMQueueInit(&(queue->links));
	queue->size = 0;
471 472 473 474 475 476 477 478 479 480 481 482
}



/*
 * ProcSleep -- put a process to sleep
 *
 * P() on the semaphore should put us to sleep.  The process
 * semaphore is cleared by default, so the first time we try
 * to acquire it, we sleep.
 *
 * ASSUME: that no one will fiddle with the queue until after
483
 *		we release the spin lock.
484 485 486 487
 *
 * NOTES: The process queue is now a priority queue for locking.
 */
int
488
ProcSleep(PROC_QUEUE *waitQueue,/* lock->waitProcs */
489
		  LOCKMETHODCTL *lockctl,
490
		  int token,			/* lockmode */
V
Vadim B. Mikheev 已提交
491
		  LOCK *lock)
492
{
493
	int			i;
V
Vadim B. Mikheev 已提交
494
	SPINLOCK	spinlock = lockctl->masterLock;
495
	PROC	   *proc;
V
Vadim B. Mikheev 已提交
496 497 498 499 500
	int			myMask = (1 << token);
	int			waitMask = lock->waitMask;
	int			aheadHolders[MAX_LOCKMODES];
	bool		selfConflict = (lockctl->conflictTab[token] & myMask),
				prevSame = false;
B
Bruce Momjian 已提交
501 502 503
	bool		deadlock_checked = false;
	struct itimerval timeval,
				dummy;
504

V
Vadim B. Mikheev 已提交
505 506 507
	MyProc->token = token;
	MyProc->waitLock = lock;

B
Bruce Momjian 已提交
508
	proc = (PROC *) MAKE_PTR(waitQueue->links.prev);
509

V
Vadim B. Mikheev 已提交
510 511 512
	/* if we don't conflict with any waiter - be first in queue */
	if (!(lockctl->conflictTab[token] & waitMask))
		goto ins;
513

V
Vadim B. Mikheev 已提交
514 515 516
	for (i = 1; i < MAX_LOCKMODES; i++)
		aheadHolders[i] = lock->activeHolders[i];
	(aheadHolders[token])++;
517

V
Vadim B. Mikheev 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
	for (i = 0; i < waitQueue->size; i++)
	{
		/* am I waiting for him ? */
		if (lockctl->conflictTab[token] & proc->holdLock)
		{
			/* is he waiting for me ? */
			if (lockctl->conflictTab[proc->token] & MyProc->holdLock)
			{
				MyProc->errType = STATUS_ERROR;
				elog(NOTICE, DeadLockMessage);
				goto rt;
			}
			/* being waiting for him - go past */
		}
		/* if he waits for me */
		else if (lockctl->conflictTab[proc->token] & MyProc->holdLock)
			break;
		/* if conflicting locks requested */
		else if (lockctl->conflictTab[proc->token] & myMask)
		{
B
Bruce Momjian 已提交
538

V
Vadim B. Mikheev 已提交
539
			/*
B
Bruce Momjian 已提交
540 541
			 * If I request non self-conflicting lock and there are others
			 * requesting the same lock just before me - stay here.
V
Vadim B. Mikheev 已提交
542 543 544 545
			 */
			if (!selfConflict && prevSame)
				break;
		}
B
Bruce Momjian 已提交
546

V
Vadim B. Mikheev 已提交
547
		/*
B
Bruce Momjian 已提交
548 549
		 * Last attempt to don't move any more: if we don't conflict with
		 * rest waiters in queue.
V
Vadim B. Mikheev 已提交
550 551 552
		 */
		else if (!(lockctl->conflictTab[token] & waitMask))
			break;
553

V
Vadim B. Mikheev 已提交
554 555 556
		prevSame = (proc->token == token);
		(aheadHolders[proc->token])++;
		if (aheadHolders[proc->token] == lock->holders[proc->token])
B
Bruce Momjian 已提交
557
			waitMask &= ~(1 << proc->token);
V
Vadim B. Mikheev 已提交
558 559
		proc = (PROC *) MAKE_PTR(proc->links.prev);
	}
560

V
Vadim B. Mikheev 已提交
561
ins:;
562 563 564 565 566 567
	/* -------------------
	 * assume that these two operations are atomic (because
	 * of the spinlock).
	 * -------------------
	 */
	SHMQueueInsertTL(&(proc->links), &(MyProc->links));
B
Bruce Momjian 已提交
568
	waitQueue->size++;
569

V
Vadim B. Mikheev 已提交
570
	lock->waitMask |= myMask;
571 572 573
	SpinRelease(spinlock);

	/* --------------
B
Bruce Momjian 已提交
574
	 * We set this so we can wake up periodically and check for a deadlock.
B
Bruce Momjian 已提交
575 576
	 * If a deadlock is detected, the handler releases the processes
	 * semaphore and aborts the current transaction.
B
Bruce Momjian 已提交
577 578 579
	 *
	 * Need to zero out struct to set the interval and the micro seconds fields
	 * to 0.
580 581
	 * --------------
	 */
B
Bruce Momjian 已提交
582 583 584
	MemSet(&timeval, 0, sizeof(struct itimerval));
	timeval.it_value.tv_sec = \
		(DeadlockCheckTimer ? DeadlockCheckTimer : DEADLOCK_CHECK_TIMER);
585

B
Bruce Momjian 已提交
586 587
	do
	{
588
		MyProc->errType = NO_ERROR;		/* reset flag after deadlock check */
589

B
Bruce Momjian 已提交
590 591 592 593 594
		if (!deadlock_checked)
			if (setitimer(ITIMER_REAL, &timeval, &dummy))
				elog(FATAL, "ProcSleep: Unable to set timer for process wakeup");
		deadlock_checked = true;

B
Bruce Momjian 已提交
595 596 597 598 599 600
		/* --------------
		 * if someone wakes us between SpinRelease and IpcSemaphoreLock,
		 * IpcSemaphoreLock will not block.  The wakeup is "saved" by
		 * the semaphore implementation.
		 * --------------
		 */
M
 
Marc G. Fournier 已提交
601 602
		IpcSemaphoreLock(MyProc->sem.semId, MyProc->sem.semNum,
						 IpcExclusiveLock);
603 604 605
	} while (MyProc->errType == STATUS_NOT_FOUND);		/* sleep after deadlock
														 * check */

B
Bruce Momjian 已提交
606 607 608 609 610 611 612 613
	/* ---------------
	 * We were awoken before a timeout - now disable the timer
	 * ---------------
	 */
	timeval.it_value.tv_sec = 0;
	if (setitimer(ITIMER_REAL, &timeval, &dummy))
		elog(FATAL, "ProcSleep: Unable to diable timer for process wakeup");

614 615 616 617 618 619 620
	/* ----------------
	 * We were assumed to be in a critical section when we went
	 * to sleep.
	 * ----------------
	 */
	SpinAcquire(spinlock);

V
Vadim B. Mikheev 已提交
621 622
rt:;

M
 
Marc G. Fournier 已提交
623 624
#ifdef LOCK_MGR_DEBUG
	/* Just to get meaningful debug messages from DumpLocks() */
625
	MyProc->waitLock = (LOCK *) NULL;
M
 
Marc G. Fournier 已提交
626 627
#endif

628
	return MyProc->errType;
629 630 631 632 633 634
}


/*
 * ProcWakeup -- wake up a process by releasing its private semaphore.
 *
635 636
 *	 remove the process from the wait queue and set its links invalid.
 *	 RETURN: the next process in the wait queue.
637
 */
B
Bruce Momjian 已提交
638
PROC *
639
ProcWakeup(PROC *proc, int errType)
640
{
641
	PROC	   *retProc;
642 643 644 645 646

	/* assume that spinlock has been acquired */

	if (proc->links.prev == INVALID_OFFSET ||
		proc->links.next == INVALID_OFFSET)
647
		return (PROC *) NULL;
648 649 650 651 652 653 654 655 656 657 658 659

	retProc = (PROC *) MAKE_PTR(proc->links.prev);

	/* you have to update waitLock->waitProcs.size yourself */
	SHMQueueDelete(&(proc->links));
	SHMQueueElemInit(&(proc->links));

	proc->errType = errType;

	IpcSemaphoreUnlock(proc->sem.semId, proc->sem.semNum, IpcExclusiveLock);

	return retProc;
660 661 662 663
}

/*
 * ProcLockWakeup -- routine for waking up processes when a lock is
664
 *		released.
665 666
 */
int
667
ProcLockWakeup(PROC_QUEUE *queue, LOCKMETHOD lockmethod, LOCK *lock)
668
{
669
	PROC	   *proc;
V
Vadim B. Mikheev 已提交
670
	int			count = 0;
M
 
Marc G. Fournier 已提交
671
	int			trace_flag;
V
Vadim B. Mikheev 已提交
672
	int			last_locktype = 0;
M
 
Marc G. Fournier 已提交
673 674 675
	int			queue_size = queue->size;

	Assert(queue->size >= 0);
676 677

	if (!queue->size)
678
		return STATUS_NOT_FOUND;
679 680

	proc = (PROC *) MAKE_PTR(queue->links.prev);
M
 
Marc G. Fournier 已提交
681 682
	while ((queue_size--) && (proc))
	{
683

M
 
Marc G. Fournier 已提交
684
		/*
685 686
		 * This proc will conflict as the previous one did, don't even
		 * try.
M
 
Marc G. Fournier 已提交
687 688 689 690 691
		 */
		if (proc->token == last_locktype)
			continue;

		/*
V
Vadim B. Mikheev 已提交
692
		 * Does this proc conflict with locks held by others ?
M
 
Marc G. Fournier 已提交
693 694
		 */
		if (LockResolveConflicts(lockmethod,
695
								 lock,
696
								 proc->token,
M
 
Marc G. Fournier 已提交
697 698 699
								 proc->xid,
								 (XIDLookupEnt *) NULL) != STATUS_OK)
		{
V
Vadim B. Mikheev 已提交
700 701
			if (count != 0)
				break;
M
 
Marc G. Fournier 已提交
702 703 704
			last_locktype = proc->token;
			continue;
		}
705 706 707 708 709 710 711

		/*
		 * there was a waiting process, grant it the lock before waking it
		 * up.	This will prevent another process from seizing the lock
		 * between the time we release the lock master (spinlock) and the
		 * time that the awoken process begins executing again.
		 */
712
		GrantLock(lock, proc->token);
713 714 715

		/*
		 * ProcWakeup removes proc from the lock waiting process queue and
716
		 * returns the next proc in chain.
717 718 719
		 */

		count++;
M
 
Marc G. Fournier 已提交
720 721
		queue->size--;
		proc = ProcWakeup(proc, NO_ERROR);
722
	}
723

M
 
Marc G. Fournier 已提交
724 725
	Assert(queue->size >= 0);

726
	if (count)
727
		return STATUS_OK;
728 729
	else
	{
730
		/* Something is still blocking us.	May have deadlocked. */
M
 
Marc G. Fournier 已提交
731 732 733 734 735 736 737 738 739
		trace_flag = (lock->tag.lockmethod == USER_LOCKMETHOD) ? \
			TRACE_USERLOCKS : TRACE_LOCKS;
		TPRINTF(trace_flag,
				"ProcLockWakeup: lock(%x) can't wake up any process",
				MAKE_OFFSET(lock));
#ifdef DEADLOCK_DEBUG
		if (pg_options[trace_flag] >= 2)
			DumpAllLocks();
#endif
740
		return STATUS_NOT_FOUND;
M
 
Marc G. Fournier 已提交
741
	}
742 743 744
}

void
745
ProcAddLock(SHM_QUEUE *elem)
746
{
747
	SHMQueueInsertTL(&MyProc->lockQueue, elem);
748 749 750
}

/* --------------------
B
Bruce Momjian 已提交
751 752 753
 * We only get to this routine if we got SIGALRM after DEADLOCK_CHECK_TIMER
 * while waiting for a lock to be released by some other process.  If we have
 * a real deadlock, we must also indicate that I'm no longer waiting
754
 * on a lock so that other processes don't try to wake me up and screw
755 756 757
 * up my semaphore.
 * --------------------
 */
758
static void
B
Bruce Momjian 已提交
759
HandleDeadLock(int sig)
760
{
B
Bruce Momjian 已提交
761
	LOCK	   *mywaitlock;
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799

	LockLockTable();

	/* ---------------------
	 * Check to see if we've been awoken by anyone in the interim.
	 *
	 * If we have we can return and resume our transaction -- happy day.
	 * Before we are awoken the process releasing the lock grants it to
	 * us so we know that we don't have to wait anymore.
	 *
	 * Damn these names are LONG! -mer
	 * ---------------------
	 */
	if (IpcSemaphoreGetCount(MyProc->sem.semId, MyProc->sem.semNum) ==
		IpcSemaphoreDefaultStartValue)
	{
		UnlockLockTable();
		return;
	}

	/*
	 * you would think this would be unnecessary, but...
	 *
	 * this also means we've been removed already.  in some ports (e.g.,
	 * sparc and aix) the semop(2) implementation is such that we can
	 * actually end up in this handler after someone has removed us from
	 * the queue and bopped the semaphore *but the test above fails to
	 * detect the semaphore update* (presumably something weird having to
	 * do with the order in which the semaphore wakeup signal and SIGALRM
	 * get handled).
	 */
	if (MyProc->links.prev == INVALID_OFFSET ||
		MyProc->links.next == INVALID_OFFSET)
	{
		UnlockLockTable();
		return;
	}

800
#ifdef DEADLOCK_DEBUG
M
 
Marc G. Fournier 已提交
801
	DumpAllLocks();
802 803
#endif

B
Bruce Momjian 已提交
804 805
	MyProc->errType = STATUS_NOT_FOUND;
	if (!DeadLockCheck(MyProc, MyProc->waitLock))
B
Bruce Momjian 已提交
806 807 808 809 810 811 812
	{
		UnlockLockTable();
		return;
	}

	mywaitlock = MyProc->waitLock;

813 814 815 816
	/* ------------------------
	 * Get this process off the lock's wait queue
	 * ------------------------
	 */
B
Bruce Momjian 已提交
817 818
	Assert(mywaitlock->waitProcs.size > 0);
	--mywaitlock->waitProcs.size;
819 820 821 822 823 824 825 826
	SHMQueueDelete(&(MyProc->links));
	SHMQueueElemInit(&(MyProc->links));

	/* ------------------
	 * Unlock my semaphore so that the count is right for next time.
	 * I was awoken by a signal, not by someone unlocking my semaphore.
	 * ------------------
	 */
M
 
Marc G. Fournier 已提交
827 828
	IpcSemaphoreUnlock(MyProc->sem.semId, MyProc->sem.semNum,
					   IpcExclusiveLock);
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843

	/* -------------
	 * Set MyProc->errType to STATUS_ERROR so that we abort after
	 * returning from this handler.
	 * -------------
	 */
	MyProc->errType = STATUS_ERROR;

	/*
	 * if this doesn't follow the IpcSemaphoreUnlock then we get lock
	 * table corruption ("LockReplace: xid table corrupted") due to race
	 * conditions.	i don't claim to understand this...
	 */
	UnlockLockTable();

V
Vadim B. Mikheev 已提交
844
	elog(NOTICE, DeadLockMessage);
845
	return;
846 847 848
}

void
849
ProcReleaseSpins(PROC *proc)
850
{
851
	int			i;
852 853 854 855 856 857 858

	if (!proc)
		proc = MyProc;

	if (!proc)
		return;
	for (i = 0; i < (int) MAX_SPINS; i++)
859
	{
860
		if (proc->sLocks[i])
861
		{
862 863
			Assert(proc->sLocks[i] == 1);
			SpinRelease(i);
864 865 866 867 868
		}
	}
}

/*****************************************************************************
869
 *
870 871 872 873
 *****************************************************************************/

/*
 * ProcGetNewSemKeyAndNum -
874 875 876 877
 *	  scan the free semaphore bitmap and allocate a single semaphore from
 *	  a semaphore set. (If the semaphore set doesn't exist yet,
 *	  IpcSemaphoreCreate will create it. Otherwise, we use the existing
 *	  semaphore set.)
878 879
 */
static void
880
ProcGetNewSemKeyAndNum(IPCKey *key, int *semNum)
881
{
882 883
	int			i;
	int32	   *freeSemMap = ProcGlobal->freeSemMap;
B
Bruce Momjian 已提交
884
	int32		fullmask = (1 << (PROC_NSEMS_PER_SET + 1)) - 1;
885

886 887 888 889
	/*
	 * we hold ProcStructLock when entering this routine. We scan through
	 * the bitmap to look for a free semaphore.
	 */
890

891 892
	for (i = 0; i < MAX_PROC_SEMS / PROC_NSEMS_PER_SET; i++)
	{
893 894
		int			mask = 1;
		int			j;
895 896

		if (freeSemMap[i] == fullmask)
897
			continue;			/* this set is fully allocated */
898 899 900 901 902 903 904

		for (j = 0; j < PROC_NSEMS_PER_SET; j++)
		{
			if ((freeSemMap[i] & mask) == 0)
			{

				/*
B
Bruce Momjian 已提交
905 906
				 * a free semaphore found. Mark it as allocated. Also set
				 * the bit indicating whole set is allocated.
907
				 */
908
				freeSemMap[i] |= mask + (1 << PROC_NSEMS_PER_SET);
909 910 911 912 913 914 915

				*key = ProcGlobal->currKey + i;
				*semNum = j;
				return;
			}
			mask <<= 1;
		}
916 917
	}

918
	/* if we reach here, all the semaphores are in use. */
919
	elog(ERROR, "InitProc: cannot allocate a free semaphore");
920 921 922 923
}

/*
 * ProcFreeSem -
924
 *	  free up our semaphore in the semaphore set.
925 926 927 928
 */
static void
ProcFreeSem(IpcSemaphoreKey semKey, int semNum)
{
929 930 931
	int			mask;
	int			i;
	int32	   *freeSemMap = ProcGlobal->freeSemMap;
932

933 934 935
	i = semKey - ProcGlobal->currKey;
	mask = ~(1 << semNum);
	freeSemMap[i] &= mask;
936

B
Bruce Momjian 已提交
937 938 939 940
	/*
	 * Formerly we'd release a semaphore set if it was now completely
	 * unused, but now we keep the semaphores to ensure we won't run out
	 * when starting new backends --- cf. InitProcGlobal.  Note that the
941 942 943
	 * PROC_NSEMS_PER_SET+1'st bit of the freeSemMap entry remains set to
	 * indicate it is still allocated; ProcFreeAllSemaphores() needs that.
	 */
944 945 946 947
}

/*
 * ProcFreeAllSemaphores -
948 949 950
 *	  called at shmem_exit time, ie when exiting the postmaster or
 *	  destroying shared state for a failed set of backends.
 *	  Free up all the semaphores allocated to the lmgrs of the backends.
951
 */
952
static void
953 954
ProcFreeAllSemaphores()
{
955 956
	int			i;
	int32	   *freeSemMap = ProcGlobal->freeSemMap;
957

958 959 960 961 962
	for (i = 0; i < MAX_PROC_SEMS / PROC_NSEMS_PER_SET; i++)
	{
		if (freeSemMap[i] != 0)
			IpcSemaphoreKill(ProcGlobal->currKey + i);
	}
963
}