proc.c 26.7 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * proc.c
4
 *	  routines to manage per-process shared memory data structure
5
 *
B
Add:  
Bruce Momjian 已提交
6 7
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
11
 *	  $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.90 2001/01/09 09:38:57 inoue Exp $
12 13 14 15
 *
 *-------------------------------------------------------------------------
 */
/*
16 17
 *	Each postgres backend gets one of these.  We'll use it to
 *	clean up after the process should the process suddenly die.
18 19 20
 *
 *
 * Interface (a):
21 22 23
 *		ProcSleep(), ProcWakeup(), ProcWakeupNext(),
 *		ProcQueueAlloc() -- create a shm queue for sleeping processes
 *		ProcQueueInit() -- create a queue without allocing memory
24 25 26 27 28 29 30 31
 *
 * 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):
 *
32 33
 * ProcReleaseLocks -- frees the locks associated with current transaction
 *
34
 * ProcKill -- destroys the shared memory state (and locks)
35
 *		associated with the process.
36 37
 *
 * 5/15/91 -- removed the buffer pool based lock chain in favor
38 39 40 41 42 43
 *		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.
44 45
 *
 * 4/7/95 -- instead of allocating a set of 1 semaphore per process, we
46 47 48 49
 *		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
50
 *
51
 * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.90 2001/01/09 09:38:57 inoue Exp $
52
 */
53 54
#include "postgres.h"

55
#include <errno.h>
56 57
#include <sys/time.h>
#include <unistd.h>
58
#include <signal.h>
59
#include <sys/types.h>
M
Marc G. Fournier 已提交
60

61
#if defined(solaris_sparc) || defined(__CYGWIN__)
62 63 64 65
#include <sys/ipc.h>
#include <sys/sem.h>
#endif

66 67
#include "miscadmin.h"

68 69 70 71
#if defined(__darwin__)
#include "port/darwin/sem.h"
#endif

72
/* In Ultrix and QNX, sem.h must be included after ipc.h */
73
#ifdef HAVE_SYS_SEM_H
74
#include <sys/sem.h>
75
#endif
B
Bruce Momjian 已提交
76

77
#include "access/xact.h"
78 79
#include "storage/proc.h"

80

81

82
void		HandleDeadLock(SIGNAL_ARGS);
83
static void ProcFreeAllSemaphores(void);
84
static bool GetOffWaitQueue(PROC *);
85

86
int DeadlockTimeout = 1000;
M
 
Marc G. Fournier 已提交
87

88 89 90 91 92 93 94
/* --------------------
 * 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
 * --------------------
 */
95
SPINLOCK	ProcStructLock;
96 97 98

static PROC_HDR *ProcGlobal = NULL;

99
PROC	   *MyProc = NULL;
100

101
static void ProcKill(int exitStatus, Datum pid);
102 103
static void ProcGetNewSemIdAndNum(IpcSemaphoreId *semId, int *semNum);
static void ProcFreeSem(IpcSemaphoreId semId, int semNum);
V
Vadim B. Mikheev 已提交
104

105 106
/*
 * InitProcGlobal -
107
 *	  initializes the global process table. We put it here so that
108
 *	  the postmaster can do this initialization. (ProcFreeAllSemaphores needs
109 110 111
 *	  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.)
112 113 114 115 116 117 118 119 120 121 122
 *
 *	  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.
123 124
 */
void
125
InitProcGlobal(int maxBackends)
126
{
127
	bool		found = false;
128

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

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

143
		ProcGlobal->freeProcs = INVALID_OFFSET;
144 145 146
		for (i = 0; i < PROC_SEM_MAP_ENTRIES; i++)
		{
			ProcGlobal->procSemIds[i] = -1;
147
			ProcGlobal->freeSemMap[i] = 0;
148
		}
149

B
Bruce Momjian 已提交
150 151
		/*
		 * Arrange to delete semas on exit --- set this up now so that we
152 153 154 155
		 * will clean up if pre-allocation fails.  We use our own freeproc,
		 * rather than IpcSemaphoreCreate's removeOnExit option, because
		 * we don't want to fill up the on_shmem_exit list with a separate
		 * entry for each semaphore set.
156
		 */
157
		on_shmem_exit(ProcFreeAllSemaphores, 0);
158

B
Bruce Momjian 已提交
159
		/*
160
		 * Pre-create the semaphores for the first maxBackends processes.
161
		 */
162 163 164
		Assert(maxBackends > 0 && maxBackends <= MAXBACKENDS);

		for (i = 0; i < ((maxBackends-1)/PROC_NSEMS_PER_SET+1); i++)
165
		{
166 167 168 169 170 171 172
			IpcSemaphoreId		semId;

			semId = IpcSemaphoreCreate(PROC_NSEMS_PER_SET,
									   IPCProtection,
									   1,
									   false);
			ProcGlobal->procSemIds[i] = semId;
173
		}
174 175 176 177 178 179 180 181 182
	}
}

/* ------------------------
 * InitProc -- create a per-process data structure for this process
 * used by the lock manager on semaphore queues.
 * ------------------------
 */
void
183
InitProcess(void)
184
{
185 186 187
	bool		found = false;
	unsigned long location,
				myOffset;
188 189 190

	SpinAcquire(ProcStructLock);

191
	/* attach to the ProcGlobal structure */
192
	ProcGlobal = (PROC_HDR *)
193
		ShmemInitStruct("Proc Header", sizeof(PROC_HDR), &found);
194
	if (!found)
195
	{
196
		/* this should not happen. InitProcGlobal() is called before this. */
197
		elog(STOP, "InitProcess: Proc Header uninitialized");
198
	}
199 200

	if (MyProc != NULL)
201
	{
202
		SpinRelease(ProcStructLock);
203
		elog(ERROR, "ProcInit: you already exist");
204
	}
205

206
	/* try to get a proc struct from the free list first */
207 208 209 210

	myOffset = ProcGlobal->freeProcs;

	if (myOffset != INVALID_OFFSET)
211
	{
212 213 214 215 216 217 218
		MyProc = (PROC *) MAKE_PTR(myOffset);
		ProcGlobal->freeProcs = MyProc->links.next;
	}
	else
	{

		/*
219 220 221 222
		 * 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).
223 224
		 */

225
		MyProc = (PROC *) ShmemAlloc(sizeof(PROC));
226
		if (!MyProc)
227
		{
228 229
			SpinRelease(ProcStructLock);
			elog(FATAL, "cannot create new proc: out of memory");
230
		}
231 232 233

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

236
	/*
237 238 239
	 * 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.
240
	 */
B
Bruce Momjian 已提交
241
	MemSet(MyProc->sLocks, 0, sizeof(MyProc->sLocks));
242 243 244 245 246
	MyProc->sLocks[ProcStructLock] = 1;


	if (IsUnderPostmaster)
	{
247 248 249
		IpcSemaphoreId	semId;
		int				semNum;
		union semun		semun;
250

251
		ProcGetNewSemIdAndNum(&semId, &semNum);
252 253 254 255 256

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

260
		IpcSemaphoreLock(semId, semNum);
261 262 263 264 265 266 267 268 269 270 271 272
		MyProc->sem.semId = semId;
		MyProc->sem.semNum = semNum;
	}
	else
		MyProc->sem.semId = -1;

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

B
Bruce Momjian 已提交
273
	MyProc->pid = MyProcPid;
274
	MyProc->databaseId = MyDatabaseId;
275
	MyProc->xid = InvalidTransactionId;
276
	MyProc->xmin = InvalidTransactionId;
277 278 279 280 281 282

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

	/* -------------------------
286
	 * Install ourselves in the shmem index table.	The name to
287 288 289 290 291 292
	 * 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 已提交
293
	if ((!ShmemPIDLookup(MyProcPid, &location)) || (location != MAKE_OFFSET(MyProc)))
294
		elog(STOP, "InitProcess: ShmemPID table broken");
295 296 297 298

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

299
	on_shmem_exit(ProcKill, (Datum) MyProcPid);
300 301
}

H
Hiroshi Inoue 已提交
302
/* -----------------------
303
 * get process off any wait queue it might be on
304 305 306 307 308 309
 *
 * NB: this does not remove the process' holder object, nor the lock object,
 * even though their holder counts might now have gone to zero.  That will
 * happen during a subsequent LockReleaseAll call, which we expect will happen
 * during transaction cleanup.  (Removal of a proc from its wait queue by
 * this routine can only happen if we are aborting the transaction.)
H
Hiroshi Inoue 已提交
310 311
 * -----------------------
 */
312
static bool
313
GetOffWaitQueue(PROC *proc)
H
Hiroshi Inoue 已提交
314
{
315
	bool		gotoff = false;
316

H
Hiroshi Inoue 已提交
317 318 319
	LockLockTable();
	if (proc->links.next != INVALID_OFFSET)
	{
320 321
		LOCK   *waitLock = proc->waitLock;
		LOCKMODE lockmode = proc->waitLockMode;
322

323
		/* Remove proc from lock's wait queue */
324 325
		Assert(waitLock);
		Assert(waitLock->waitProcs.size > 0);
H
Hiroshi Inoue 已提交
326
		SHMQueueDelete(&(proc->links));
327
		--waitLock->waitProcs.size;
328 329

		/* Undo increments of holder counts by waiting process */
330 331 332 333 334
		Assert(waitLock->nHolding > 0);
		Assert(waitLock->nHolding > proc->waitLock->nActive);
		--waitLock->nHolding;
		Assert(waitLock->holders[lockmode] > 0);
		--waitLock->holders[lockmode];
335
		/* don't forget to clear waitMask bit if appropriate */
336 337
		if (waitLock->activeHolders[lockmode] == waitLock->holders[lockmode])
			waitLock->waitMask &= ~(1 << lockmode);
338 339 340 341 342 343 344 345 346 347

		/* Clean up the proc's own state */
		SHMQueueElemInit(&(proc->links));
		proc->waitLock = NULL;
		proc->waitHolder = NULL;

		/* See if any other waiters can be woken up now */
		ProcLockWakeup(LOCK_LOCKMETHOD(*waitLock), waitLock);

		gotoff = true;
H
Hiroshi Inoue 已提交
348 349 350
	}
	UnlockLockTable();

351
	return gotoff;
H
Hiroshi Inoue 已提交
352
}
353

354
/*
355 356 357 358 359 360 361 362
 * 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.
363
 *
364
 * Note that user locks are not released in either case.
365 366
 */
void
367
ProcReleaseLocks(bool isCommit)
368
{
369 370
	if (!MyProc)
		return;
371 372 373
	GetOffWaitQueue(MyProc);
	LockReleaseAll(DEFAULT_LOCKMETHOD, MyProc,
				   !isCommit, GetCurrentTransactionId());
374 375 376 377
}

/*
 * ProcRemove -
378
 *	  used by the postmaster to clean up the global tables. This also frees
379
 *	  up the semaphore used for the lmgr of the process.
380 381 382 383
 */
bool
ProcRemove(int pid)
{
384 385
	SHMEM_OFFSET location;
	PROC	   *proc;
386 387 388 389 390

	location = INVALID_OFFSET;

	location = ShmemPIDDestroy(pid);
	if (location == INVALID_OFFSET)
391
		return FALSE;
392 393 394 395
	proc = (PROC *) MAKE_PTR(location);

	SpinAcquire(ProcStructLock);

396
	ProcFreeSem(proc->sem.semId, proc->sem.semNum);
397 398 399 400 401 402

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

	SpinRelease(ProcStructLock);

403
	return TRUE;
404 405 406 407
}

/*
 * ProcKill() -- Destroy the per-proc data structure for
408
 *		this process. Release any of its held spin locks.
409 410
 */
static void
411
ProcKill(int exitStatus, Datum pid)
412
{
413
	PROC	   *proc;
414 415 416

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

424 425 426 427 428 429 430 431 432
	if ((int) pid == MyProcPid)
	{
		proc = MyProc;
		MyProc = NULL;
	}
	else
	{
		/* This path is dead code at the moment ... */
		SHMEM_OFFSET location = INVALID_OFFSET;
433

434 435 436 437 438
		ShmemPIDLookup((int) pid, &location);
		if (location == INVALID_OFFSET)
			return;
		proc = (PROC *) MAKE_PTR(location);
	}
439

440
	Assert(proc);
441

442
	/* Release any spinlocks the proc is holding */
443 444
	ProcReleaseSpins(proc);

445 446
	/* Get the proc off any wait queue it might be on */
	GetOffWaitQueue(proc);
447

448 449
	/* Remove from the standard lock table */
	LockReleaseAll(DEFAULT_LOCKMETHOD, proc, true, InvalidTransactionId);
450

451 452 453 454
#ifdef USER_LOCKS
	/* Remove from the user lock table */
	LockReleaseAll(USER_LOCKMETHOD, proc, true, InvalidTransactionId);
#endif
455 456 457 458
}

/*
 * ProcQueue package: routines for putting processes to sleep
459
 *		and  waking them up
460 461 462 463 464 465 466 467
 */

/*
 * 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
 */
468
#ifdef NOT_USED
469
PROC_QUEUE *
470 471
ProcQueueAlloc(char *name)
{
472 473
	bool		found;
	PROC_QUEUE *queue = (PROC_QUEUE *)
474
		ShmemInitStruct(name, sizeof(PROC_QUEUE), &found);
475 476

	if (!queue)
477
		return NULL;
478 479
	if (!found)
		ProcQueueInit(queue);
480
	return queue;
481
}
482

483
#endif
484 485 486 487 488

/*
 * ProcQueueInit -- initialize a shared memory process queue
 */
void
489
ProcQueueInit(PROC_QUEUE *queue)
490
{
491 492
	SHMQueueInit(&(queue->links));
	queue->size = 0;
493 494 495
}


496 497 498 499
/*
 *	Handling cancel request while waiting for lock
 *
 */
500
static bool lockWaiting = false;
501

502 503
void
SetWaitingForLock(bool waiting)
504
{
505 506
	if (waiting == lockWaiting)
		return;
507
	lockWaiting = waiting;
508 509
	if (lockWaiting)
	{
510 511 512 513 514 515
		/* The lock was already released ? */
		if (MyProc->links.next == INVALID_OFFSET)
		{
			lockWaiting = false;
			return;
		}
516
		if (QueryCancel)		/* cancel request pending */
517
		{
518
			if (GetOffWaitQueue(MyProc))
519 520
			{
				lockWaiting = false;
521
				elog(ERROR, "Query cancel requested while waiting for lock");
522 523 524
			}
		}
	}
525
}
526

527 528
void
LockWaitCancel(void)
529
{
530
#ifndef __BEOS__ 	
531
	struct itimerval timeval,
532
				dummy;
533

534 535
	if (!lockWaiting)
		return;
536 537 538 539
	lockWaiting = false;
	/* Deadlock timer off */
	MemSet(&timeval, 0, sizeof(struct itimerval));
	setitimer(ITIMER_REAL, &timeval, &dummy);
540
#else
541
	/* BeOS doesn't have setitimer, but has set_alarm */
542 543 544 545 546 547 548
	if (!lockWaiting)
		return;
	lockWaiting = false;
	/* Deadlock timer off */
    set_alarm(B_INFINITE_TIMEOUT, B_PERIODIC_ALARM);
#endif /* __BEOS__ */
        
549 550
	if (GetOffWaitQueue(MyProc))
		elog(ERROR, "Query cancel requested while waiting for lock");
551
}
552 553 554 555 556 557 558 559

/*
 * 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.
 *
560 561
 * Result is NO_ERROR if we acquired the lock, STATUS_ERROR if not (deadlock).
 *
562
 * ASSUME: that no one will fiddle with the queue until after
563
 *		we release the spin lock.
564 565 566 567
 *
 * NOTES: The process queue is now a priority queue for locking.
 */
int
568 569 570 571
ProcSleep(LOCKMETHODCTL *lockctl,
		  LOCKMODE lockmode,
		  LOCK *lock,
		  HOLDER *holder)
572
{
573
	PROC_QUEUE *waitQueue = &(lock->waitProcs);
V
Vadim B. Mikheev 已提交
574
	SPINLOCK	spinlock = lockctl->masterLock;
575
	int			myMask = (1 << lockmode);
V
Vadim B. Mikheev 已提交
576
	int			waitMask = lock->waitMask;
577 578
	PROC	   *proc;
	int			i;
V
Vadim B. Mikheev 已提交
579
	int			aheadHolders[MAX_LOCKMODES];
580
	bool		selfConflict = (lockctl->conflictTab[lockmode] & myMask),
V
Vadim B. Mikheev 已提交
581
				prevSame = false;
582
#ifndef __BEOS__
B
Bruce Momjian 已提交
583 584
	struct itimerval timeval,
				dummy;
585 586 587
#else
    bigtime_t time_interval;
#endif
588

V
Vadim B. Mikheev 已提交
589
	MyProc->waitLock = lock;
590 591 592
	MyProc->waitHolder = holder;
	MyProc->waitLockMode = lockmode;
	/* We assume the caller set up MyProc->holdLock */
V
Vadim B. Mikheev 已提交
593

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

V
Vadim B. Mikheev 已提交
596
	/* if we don't conflict with any waiter - be first in queue */
597
	if (!(lockctl->conflictTab[lockmode] & waitMask))
V
Vadim B. Mikheev 已提交
598
		goto ins;
599

V
Vadim B. Mikheev 已提交
600 601
	for (i = 1; i < MAX_LOCKMODES; i++)
		aheadHolders[i] = lock->activeHolders[i];
602
	(aheadHolders[lockmode])++;
603

V
Vadim B. Mikheev 已提交
604 605 606
	for (i = 0; i < waitQueue->size; i++)
	{
		/* am I waiting for him ? */
607
		if (lockctl->conflictTab[lockmode] & proc->holdLock)
V
Vadim B. Mikheev 已提交
608 609
		{
			/* is he waiting for me ? */
610
			if (lockctl->conflictTab[proc->waitLockMode] & MyProc->holdLock)
V
Vadim B. Mikheev 已提交
611
			{
612
				/* Yes, report deadlock failure */
V
Vadim B. Mikheev 已提交
613 614 615 616 617 618
				MyProc->errType = STATUS_ERROR;
				goto rt;
			}
			/* being waiting for him - go past */
		}
		/* if he waits for me */
619
		else if (lockctl->conflictTab[proc->waitLockMode] & MyProc->holdLock)
V
Vadim B. Mikheev 已提交
620 621
			break;
		/* if conflicting locks requested */
622
		else if (lockctl->conflictTab[proc->waitLockMode] & myMask)
V
Vadim B. Mikheev 已提交
623
		{
B
Bruce Momjian 已提交
624

V
Vadim B. Mikheev 已提交
625
			/*
B
Bruce Momjian 已提交
626 627
			 * If I request non self-conflicting lock and there are others
			 * requesting the same lock just before me - stay here.
V
Vadim B. Mikheev 已提交
628 629 630 631
			 */
			if (!selfConflict && prevSame)
				break;
		}
B
Bruce Momjian 已提交
632

V
Vadim B. Mikheev 已提交
633
		/*
B
Bruce Momjian 已提交
634 635
		 * Last attempt to don't move any more: if we don't conflict with
		 * rest waiters in queue.
V
Vadim B. Mikheev 已提交
636
		 */
637
		else if (!(lockctl->conflictTab[lockmode] & waitMask))
V
Vadim B. Mikheev 已提交
638
			break;
639

640 641 642 643
		prevSame = (proc->waitLockMode == lockmode);
		(aheadHolders[proc->waitLockMode])++;
		if (aheadHolders[proc->waitLockMode] == lock->holders[proc->waitLockMode])
			waitMask &= ~(1 << proc->waitLockMode);
V
Vadim B. Mikheev 已提交
644 645
		proc = (PROC *) MAKE_PTR(proc->links.prev);
	}
646

V
Vadim B. Mikheev 已提交
647
ins:;
648 649 650 651 652 653
	/* -------------------
	 * assume that these two operations are atomic (because
	 * of the spinlock).
	 * -------------------
	 */
	SHMQueueInsertTL(&(proc->links), &(MyProc->links));
B
Bruce Momjian 已提交
654
	waitQueue->size++;
655

V
Vadim B. Mikheev 已提交
656
	lock->waitMask |= myMask;
657

658 659
	MyProc->errType = NO_ERROR;		/* initialize result for success */

660 661
	SpinRelease(spinlock);

662
	/* --------------
663 664 665 666 667 668 669
	 * Set timer so we can wake up after awhile and check for a deadlock.
	 * If a deadlock is detected, the handler releases the process's
	 * semaphore and sets MyProc->errType = STATUS_ERROR, allowing us to
	 * know that we must report failure rather than success.
	 *
	 * By delaying the check until we've waited for a bit, we can avoid
	 * running the rather expensive deadlock-check code in most cases.
B
Bruce Momjian 已提交
670 671 672
	 *
	 * Need to zero out struct to set the interval and the micro seconds fields
	 * to 0.
673 674
	 * --------------
	 */
675
#ifndef __BEOS__
B
Bruce Momjian 已提交
676
	MemSet(&timeval, 0, sizeof(struct itimerval));
677 678
	timeval.it_value.tv_sec = DeadlockTimeout / 1000;
	timeval.it_value.tv_usec = (DeadlockTimeout % 1000) * 1000;
679 680
	if (setitimer(ITIMER_REAL, &timeval, &dummy))
		elog(FATAL, "ProcSleep: Unable to set timer for process wakeup");
681
#else
682 683 684
    time_interval = DeadlockTimeout * 1000000; /* usecs */
	if (set_alarm(time_interval, B_ONE_SHOT_RELATIVE_ALARM) < 0)
		elog(FATAL, "ProcSleep: Unable to set timer for process wakeup");
685
#endif
686

687
	SetWaitingForLock(true);
688

689 690 691 692 693 694 695 696 697 698 699
	/* --------------
	 * If someone wakes us between SpinRelease and IpcSemaphoreLock,
	 * IpcSemaphoreLock will not block.  The wakeup is "saved" by
	 * the semaphore implementation.  Note also that if HandleDeadLock
	 * is invoked but does not detect a deadlock, IpcSemaphoreLock()
	 * will continue to wait.  There used to be a loop here, but it
	 * was useless code...
	 * --------------
	 */
	IpcSemaphoreLock(MyProc->sem.semId, MyProc->sem.semNum);

700
	lockWaiting = false;
701

B
Bruce Momjian 已提交
702
	/* ---------------
703
	 * Disable the timer, if it's still running
B
Bruce Momjian 已提交
704 705
	 * ---------------
	 */
706
#ifndef __BEOS__
B
Bruce Momjian 已提交
707
	timeval.it_value.tv_sec = 0;
708
	timeval.it_value.tv_usec = 0;
B
Bruce Momjian 已提交
709
	if (setitimer(ITIMER_REAL, &timeval, &dummy))
710
		elog(FATAL, "ProcSleep: Unable to disable timer for process wakeup");
711 712
#else
    if (set_alarm(B_INFINITE_TIMEOUT, B_PERIODIC_ALARM) < 0)
713
		elog(FATAL, "ProcSleep: Unable to disable timer for process wakeup");
714
#endif
B
Bruce Momjian 已提交
715

716 717 718 719 720 721 722
	/* ----------------
	 * We were assumed to be in a critical section when we went
	 * to sleep.
	 * ----------------
	 */
	SpinAcquire(spinlock);

V
Vadim B. Mikheev 已提交
723 724
rt:;

725 726
	MyProc->waitLock = NULL;
	MyProc->waitHolder = NULL;
M
 
Marc G. Fournier 已提交
727

728
	return MyProc->errType;
729 730 731 732 733 734
}


/*
 * ProcWakeup -- wake up a process by releasing its private semaphore.
 *
735
 *	 Also remove the process from the wait queue and set its links invalid.
736
 *	 RETURN: the next process in the wait queue.
737
 */
B
Bruce Momjian 已提交
738
PROC *
739
ProcWakeup(PROC *proc, int errType)
740
{
741
	PROC	   *retProc;
742 743 744 745 746

	/* assume that spinlock has been acquired */

	if (proc->links.prev == INVALID_OFFSET ||
		proc->links.next == INVALID_OFFSET)
747
		return (PROC *) NULL;
748 749 750 751 752

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

	SHMQueueDelete(&(proc->links));
	SHMQueueElemInit(&(proc->links));
753
	(proc->waitLock->waitProcs.size)--;
754 755 756

	proc->errType = errType;

757
	IpcSemaphoreUnlock(proc->sem.semId, proc->sem.semNum);
758 759

	return retProc;
760 761 762 763
}

/*
 * ProcLockWakeup -- routine for waking up processes when a lock is
764
 *		released.
765 766
 */
int
767
ProcLockWakeup(LOCKMETHOD lockmethod, LOCK *lock)
768
{
769
	PROC_QUEUE *queue = &(lock->waitProcs);
770
	PROC	   *proc;
771 772
	int			awoken = 0;
	LOCKMODE	last_lockmode = 0;
M
 
Marc G. Fournier 已提交
773 774
	int			queue_size = queue->size;

775
	Assert(queue_size >= 0);
776

777
	if (!queue_size)
778
		return STATUS_NOT_FOUND;
779 780

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

782 783 784 785 786 787 788 789 790 791
	while (queue_size-- > 0)
	{
		if (proc->waitLockMode == last_lockmode)
		{
			/*
			 * This proc will conflict as the previous one did, don't even
			 * try.
			 */
			goto nextProc;
		}
M
 
Marc G. Fournier 已提交
792 793

		/*
V
Vadim B. Mikheev 已提交
794
		 * Does this proc conflict with locks held by others ?
M
 
Marc G. Fournier 已提交
795 796
		 */
		if (LockResolveConflicts(lockmethod,
797
								 proc->waitLockMode,
798
								 lock,
799 800 801
								 proc->waitHolder,
								 proc,
								 NULL) != STATUS_OK)
M
 
Marc G. Fournier 已提交
802
		{
803 804
			/* Yes.  Quit if we already awoke at least one process. */
			if (awoken != 0)
V
Vadim B. Mikheev 已提交
805
				break;
806 807 808
			/* Otherwise, see if any later waiters can be awoken. */
			last_lockmode = proc->waitLockMode;
			goto nextProc;
M
 
Marc G. Fournier 已提交
809
		}
810 811

		/*
812
		 * OK to wake up this sleeping process.
813
		 */
814 815 816
		GrantLock(lock, proc->waitHolder, proc->waitLockMode);
		proc = ProcWakeup(proc, NO_ERROR);
		awoken++;
817 818

		/*
819 820
		 * ProcWakeup removes proc from the lock's waiting process queue
		 * and returns the next proc in chain; don't use prev link.
821
		 */
822
		continue;
823

824 825
nextProc:
		proc = (PROC *) MAKE_PTR(proc->links.prev);
826
	}
827

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

830
	if (awoken)
831
		return STATUS_OK;
832 833
	else
	{
834
		/* Something is still blocking us.	May have deadlocked. */
835 836 837
#ifdef LOCK_DEBUG
		if (lock->tag.lockmethod == USER_LOCKMETHOD ? Trace_userlocks : Trace_locks)
		{
838 839
			elog(DEBUG, "ProcLockWakeup: lock(%lx) can't wake up any process",
				 MAKE_OFFSET(lock));
840
			if (Debug_deadlocks)
841
				DumpAllLocks();
842
		}
M
 
Marc G. Fournier 已提交
843
#endif
844
		return STATUS_NOT_FOUND;
M
 
Marc G. Fournier 已提交
845
	}
846 847 848
}

void
849
ProcAddLock(SHM_QUEUE *elem)
850
{
851
	SHMQueueInsertTL(&MyProc->lockQueue, elem);
852 853 854
}

/* --------------------
855
 * We only get to this routine if we got SIGALRM after DeadlockTimeout
B
Bruce Momjian 已提交
856 857
 * 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
858
 * on a lock so that other processes don't try to wake me up and screw
859 860 861
 * up my semaphore.
 * --------------------
 */
862
void
863
HandleDeadLock(SIGNAL_ARGS)
864
{
865
	int			save_errno = errno;
B
Bruce Momjian 已提交
866
	LOCK	   *mywaitlock;
867
	bool	isWaitingForLock = lockWaiting; /* save waiting status */
868

869
	SetWaitingForLock(false); /* disable query cancel during this fuction */
870 871 872 873 874 875 876 877 878
	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.
	 *
879 880 881
	 * We check by looking to see if we've been unlinked from the wait queue.
	 * This is quicker than checking our semaphore's state, since no kernel
	 * call is needed, and it is safe because we hold the locktable lock.
882 883 884 885 886 887
	 * ---------------------
	 */
	if (MyProc->links.prev == INVALID_OFFSET ||
		MyProc->links.next == INVALID_OFFSET)
	{
		UnlockLockTable();
888
		errno = save_errno;
889
		SetWaitingForLock(isWaitingForLock); /* restore waiting status */
890 891 892
		return;
	}

893 894 895
#ifdef LOCK_DEBUG
    if (Debug_deadlocks)
        DumpAllLocks();
896 897
#endif

B
Bruce Momjian 已提交
898
	if (!DeadLockCheck(MyProc, MyProc->waitLock))
B
Bruce Momjian 已提交
899
	{
900
		/* No deadlock, so keep waiting */
B
Bruce Momjian 已提交
901
		UnlockLockTable();
902
		errno = save_errno;
903
		SetWaitingForLock(isWaitingForLock); /* restore waiting status */
B
Bruce Momjian 已提交
904 905 906
		return;
	}

907 908 909 910
	/* ------------------------
	 * Get this process off the lock's wait queue
	 * ------------------------
	 */
911
	mywaitlock = MyProc->waitLock;
B
Bruce Momjian 已提交
912 913
	Assert(mywaitlock->waitProcs.size > 0);
	--mywaitlock->waitProcs.size;
914 915
	SHMQueueDelete(&(MyProc->links));
	SHMQueueElemInit(&(MyProc->links));
916 917
	MyProc->waitLock = NULL;
	MyProc->waitHolder = NULL;
918
	isWaitingForLock = false; /* wait for lock no longer */
919 920

	/* ------------------
921
	 * Unlock my semaphore so that the interrupted ProcSleep() call can finish.
922 923
	 * ------------------
	 */
924
	IpcSemaphoreUnlock(MyProc->sem.semId, MyProc->sem.semNum);
925 926 927 928 929 930 931 932 933 934 935 936 937 938

	/* -------------
	 * 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();
939
	errno = save_errno;
940 941 942
}

void
943
ProcReleaseSpins(PROC *proc)
944
{
945
	int			i;
946 947 948 949 950 951 952

	if (!proc)
		proc = MyProc;

	if (!proc)
		return;
	for (i = 0; i < (int) MAX_SPINS; i++)
953
	{
954
		if (proc->sLocks[i])
955
		{
956 957
			Assert(proc->sLocks[i] == 1);
			SpinRelease(i);
958 959
		}
	}
H
 
Hiroshi Inoue 已提交
960
	AbortBufferIO();
961 962 963
}

/*****************************************************************************
964
 *
965 966 967
 *****************************************************************************/

/*
968
 * ProcGetNewSemIdAndNum -
969
 *	  scan the free semaphore bitmap and allocate a single semaphore from
970
 *	  a semaphore set.
971 972
 */
static void
973
ProcGetNewSemIdAndNum(IpcSemaphoreId *semId, int *semNum)
974
{
975
	int			i;
976
	IpcSemaphoreId *procSemIds = ProcGlobal->procSemIds;
977
	int32	   *freeSemMap = ProcGlobal->freeSemMap;
978
	int32		fullmask = (1 << PROC_NSEMS_PER_SET) - 1;
979

980 981 982 983
	/*
	 * we hold ProcStructLock when entering this routine. We scan through
	 * the bitmap to look for a free semaphore.
	 */
984

985
	for (i = 0; i < PROC_SEM_MAP_ENTRIES; i++)
986
	{
987 988
		int			mask = 1;
		int			j;
989 990

		if (freeSemMap[i] == fullmask)
991
			continue;			/* this set is fully allocated */
992 993
		if (procSemIds[i] < 0)
			continue;			/* this set hasn't been initialized */
994 995 996 997 998 999 1000

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

				/*
1001
				 * a free semaphore found. Mark it as allocated.
1002
				 */
1003
				freeSemMap[i] |= mask;
1004

1005
				*semId = procSemIds[i];
1006 1007 1008 1009 1010
				*semNum = j;
				return;
			}
			mask <<= 1;
		}
1011 1012
	}

1013
	/* if we reach here, all the semaphores are in use. */
1014
	elog(ERROR, "ProcGetNewSemIdAndNum: cannot allocate a free semaphore");
1015 1016 1017 1018
}

/*
 * ProcFreeSem -
1019
 *	  free up our semaphore in the semaphore set.
1020 1021
 */
static void
1022
ProcFreeSem(IpcSemaphoreId semId, int semNum)
1023
{
1024
	int32		mask;
1025
	int			i;
1026

1027
	mask = ~(1 << semNum);
1028

1029 1030 1031 1032 1033 1034 1035 1036 1037
	for (i = 0; i < PROC_SEM_MAP_ENTRIES; i++)
	{
		if (ProcGlobal->procSemIds[i] == semId)
		{
			ProcGlobal->freeSemMap[i] &= mask;
			return;
		}
	}
	fprintf(stderr, "ProcFreeSem: no ProcGlobal entry for semId %d\n", semId);
1038 1039 1040 1041
}

/*
 * ProcFreeAllSemaphores -
1042 1043 1044
 *	  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.
1045
 */
1046
static void
1047
ProcFreeAllSemaphores(void)
1048
{
1049
	int			i;
1050

1051
	for (i = 0; i < PROC_SEM_MAP_ENTRIES; i++)
1052
	{
1053 1054
		if (ProcGlobal->procSemIds[i] >= 0)
			IpcSemaphoreKill(ProcGlobal->procSemIds[i]);
1055
	}
1056
}