proc.c 26.3 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.83 2000/10/07 14:39:13 momjian 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 32 33
 *
 * 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)
34
 *		associated with the process.
35 36
 *
 * 5/15/91 -- removed the buffer pool based lock chain in favor
37 38 39 40 41 42
 *		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.
43 44
 *
 * 4/7/95 -- instead of allocating a set of 1 semaphore per process, we
45 46 47 48
 *		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
49
 *
50
 * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.83 2000/10/07 14:39:13 momjian Exp $
51
 */
52 53
#include "postgres.h"

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

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

#include "miscadmin.h"
65 66


67
/* In Ultrix and QNX, sem.h must be included after ipc.h */
68
#ifdef HAVE_SYS_SEM_H
69
#include <sys/sem.h>
70
#endif
B
Bruce Momjian 已提交
71

72 73
#include "storage/proc.h"

74
void		HandleDeadLock(SIGNAL_ARGS);
75
static void ProcFreeAllSemaphores(void);
76
static bool GetOffWaitqueue(PROC *);
77

78
int DeadlockTimeout = 1000;
M
 
Marc G. Fournier 已提交
79

80 81 82 83 84 85 86
/* --------------------
 * 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
 * --------------------
 */
87
SPINLOCK	ProcStructLock;
88 89 90

static PROC_HDR *ProcGlobal = NULL;

91
PROC	   *MyProc = NULL;
92

93
static void ProcKill(int exitStatus, Datum pid);
94
static void ProcGetNewSemKeyAndNum(IPCKey *key, int *semNum);
95
static void ProcFreeSem(IpcSemaphoreKey semKey, int semNum);
96

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

99 100
/*
 * InitProcGlobal -
101
 *	  initializes the global process table. We put it here so that
102
 *	  the postmaster can do this initialization. (ProcFreeAllSemaphores needs
103 104 105
 *	  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.)
106 107 108 109 110 111 112 113 114 115 116
 *
 *	  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.
117 118
 */
void
119
InitProcGlobal(IPCKey key, int maxBackends)
120
{
121
	bool		found = false;
122

123 124
	/* attach to the free list */
	ProcGlobal = (PROC_HDR *)
125
		ShmemInitStruct("Proc Header", sizeof(PROC_HDR), &found);
126

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

137 138 139 140
		ProcGlobal->freeProcs = INVALID_OFFSET;
		ProcGlobal->currKey = IPCGetProcessSemaphoreInitKey(key);
		for (i = 0; i < MAX_PROC_SEMS / PROC_NSEMS_PER_SET; i++)
			ProcGlobal->freeSemMap[i] = 0;
141

B
Bruce Momjian 已提交
142 143 144
		/*
		 * Arrange to delete semas on exit --- set this up now so that we
		 * will clean up if pre-allocation fails...
145
		 */
146
		on_shmem_exit(ProcFreeAllSemaphores, 0);
147

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

				semId = IpcSemaphoreCreate(semKey,
										   PROC_NSEMS_PER_SET,
										   IPCProtection,
										   IpcSemaphoreDefaultStartValue,
165 166 167
										   0);
				if (semId < 0)
					elog(FATAL, "InitProcGlobal: IpcSemaphoreCreate failed");
168 169 170
				/* mark this sema set allocated */
				ProcGlobal->freeSemMap[i] = (1 << PROC_NSEMS_PER_SET);
			}
171
		}
172 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
InitProcess(IPCKey key)
{
183 184 185
	bool		found = false;
	unsigned long location,
				myOffset;
186 187 188 189 190

	SpinAcquire(ProcStructLock);

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

	if (MyProc != NULL)
199
	{
200
		SpinRelease(ProcStructLock);
201
		elog(ERROR, "ProcInit: you already exist");
202
		return;
203
	}
204 205 206 207 208 209

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

	myOffset = ProcGlobal->freeProcs;

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

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

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

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

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


	if (IsUnderPostmaster)
	{
246 247 248 249
		IPCKey		semKey;
		int			semNum;
		int			semId;
		union semun semun;
250 251 252

		ProcGetNewSemKeyAndNum(&semKey, &semNum);

B
Bruce Momjian 已提交
253 254 255 256 257
		/*
		 * 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.
258
		 */
259 260 261 262
		semId = IpcSemaphoreCreate(semKey,
								   PROC_NSEMS_PER_SET,
								   IPCProtection,
								   IpcSemaphoreDefaultStartValue,
263
								   0);
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

		/*
		 * 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 已提交
286
	MyProc->pid = MyProcPid;
287
	MyProc->databaseId = MyDatabaseId;
288
	MyProc->xid = InvalidTransactionId;
289
	MyProc->xmin = InvalidTransactionId;
290 291 292 293 294 295

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

	/* -------------------------
299
	 * Install ourselves in the shmem index table.	The name to
300 301 302 303 304 305
	 * 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 已提交
306
	if ((!ShmemPIDLookup(MyProcPid, &location)) || (location != MAKE_OFFSET(MyProc)))
307
		elog(STOP, "InitProc: ShmemPID table broken");
308 309 310 311

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

312
	on_shmem_exit(ProcKill, (Datum) MyProcPid);
313 314
}

H
Hiroshi Inoue 已提交
315 316 317 318
/* -----------------------
 * get off the wait queue
 * -----------------------
 */
319
static bool
H
Hiroshi Inoue 已提交
320 321
GetOffWaitqueue(PROC *proc)
{
322 323
	bool		getoffed = false;

H
Hiroshi Inoue 已提交
324 325 326
	LockLockTable();
	if (proc->links.next != INVALID_OFFSET)
	{
327
		int			lockmode = proc->token;
328
		LOCK	*waitLock = proc->waitLock;
329

330 331
		Assert(waitLock);
		Assert(waitLock->waitProcs.size > 0);
H
Hiroshi Inoue 已提交
332
		SHMQueueDelete(&(proc->links));
333 334 335 336 337 338 339 340 341
		--waitLock->waitProcs.size;
		Assert(waitLock->nHolding > 0);
		Assert(waitLock->nHolding > proc->waitLock->nActive);
		--waitLock->nHolding;
		Assert(waitLock->holders[lockmode] > 0);
		--waitLock->holders[lockmode];
		if (waitLock->activeHolders[lockmode] == waitLock->holders[lockmode])
			waitLock->waitMask &= ~(1 << lockmode);
		ProcLockWakeup(&(waitLock->waitProcs), LOCK_LOCKMETHOD(*waitLock), waitLock);
342
		getoffed = true;
H
Hiroshi Inoue 已提交
343 344 345 346
	}
	SHMQueueElemInit(&(proc->links));
	UnlockLockTable();

347
	return getoffed;
H
Hiroshi Inoue 已提交
348
}
349

350 351 352 353 354 355 356
/*
 * ProcReleaseLocks() -- release all locks associated with this process
 *
 */
void
ProcReleaseLocks()
{
357 358 359
	if (!MyProc)
		return;
	LockReleaseAll(1, &MyProc->lockQueue);
H
Hiroshi Inoue 已提交
360
	GetOffWaitqueue(MyProc);
361 362 363 364
}

/*
 * ProcRemove -
365 366 367 368 369
 *	  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.)
370 371 372 373
 */
bool
ProcRemove(int pid)
{
374 375
	SHMEM_OFFSET location;
	PROC	   *proc;
376 377 378 379 380

	location = INVALID_OFFSET;

	location = ShmemPIDDestroy(pid);
	if (location == INVALID_OFFSET)
381
		return FALSE;
382 383 384 385 386 387 388 389 390 391 392
	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);

393
	return TRUE;
394 395 396 397
}

/*
 * ProcKill() -- Destroy the per-proc data structure for
398
 *		this process. Release any of its held spin locks.
399 400
 */
static void
401
ProcKill(int exitStatus, Datum pid)
402
{
403 404
	PROC	   *proc;
	SHMEM_OFFSET location;
405 406 407 408 409 410 411 412 413 414

	/* --------------------
	 * 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 已提交
415
	ShmemPIDLookup(MyProcPid, &location);
416 417 418 419 420
	if (location == INVALID_OFFSET)
		return;

	proc = (PROC *) MAKE_PTR(location);

421
	Assert(proc == MyProc || (int)pid != MyProcPid);
422 423

	MyProc = NULL;
424 425 426 427 428 429

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

432
#ifdef USER_LOCKS
433

M
 
Marc G. Fournier 已提交
434 435 436 437
	/*
	 * Assume we have a second lock table.
	 */
	LockReleaseAll(USER_LOCKMETHOD, &proc->lockQueue);
438 439
#endif

440 441 442 443
	/* ----------------
	 * get off the wait queue
	 * ----------------
	 */
H
Hiroshi Inoue 已提交
444
	GetOffWaitqueue(proc);
445 446

	return;
447 448 449 450
}

/*
 * ProcQueue package: routines for putting processes to sleep
451
 *		and  waking them up
452 453 454 455 456 457 458 459
 */

/*
 * 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
 */
460
#ifdef NOT_USED
461
PROC_QUEUE *
462 463
ProcQueueAlloc(char *name)
{
464 465
	bool		found;
	PROC_QUEUE *queue = (PROC_QUEUE *)
466
		ShmemInitStruct(name, sizeof(PROC_QUEUE), &found);
467 468

	if (!queue)
469
		return NULL;
470 471
	if (!found)
		ProcQueueInit(queue);
472
	return queue;
473
}
474

475
#endif
476 477 478 479 480

/*
 * ProcQueueInit -- initialize a shared memory process queue
 */
void
481
ProcQueueInit(PROC_QUEUE *queue)
482
{
483 484
	SHMQueueInit(&(queue->links));
	queue->size = 0;
485 486 487
}


488 489 490 491
/*
 *	Handling cancel request while waiting for lock
 *
 */
492 493 494
static bool lockWaiting = false;
void
SetWaitingForLock(bool waiting)
495
{
496 497
	if (waiting == lockWaiting)
		return;
498
	lockWaiting = waiting;
499 500
	if (lockWaiting)
	{
501 502 503 504 505 506
		/* The lock was already released ? */
		if (MyProc->links.next == INVALID_OFFSET)
		{
			lockWaiting = false;
			return;
		}
507
		if (QueryCancel)		/* cancel request pending */
508 509 510 511 512 513 514 515
		{
			if (GetOffWaitqueue(MyProc))
			{
				lockWaiting = false;
				elog(ERROR, "Query cancel requested while waiting lock");
			}
		}
	}
516
}
517 518
void
LockWaitCancel(void)
519
{
520 521 522
/* BeOS doesn't have setitimer, but has set_alarm */
#ifndef __BEOS__ 	
struct itimerval timeval,
523
				dummy;
524

525 526
	if (!lockWaiting)
		return;
527 528 529 530
	lockWaiting = false;
	/* Deadlock timer off */
	MemSet(&timeval, 0, sizeof(struct itimerval));
	setitimer(ITIMER_REAL, &timeval, &dummy);
531 532 533 534 535 536 537 538
#else
	if (!lockWaiting)
		return;
	lockWaiting = false;
	/* Deadlock timer off */
    set_alarm(B_INFINITE_TIMEOUT, B_PERIODIC_ALARM);
#endif /* __BEOS__ */
        
539 540 541
	if (GetOffWaitqueue(MyProc))
		elog(ERROR, "Query cancel requested while waiting lock");
}
542 543 544 545 546 547 548 549 550

/*
 * 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
551
 *		we release the spin lock.
552 553 554 555
 *
 * NOTES: The process queue is now a priority queue for locking.
 */
int
556
ProcSleep(PROC_QUEUE *waitQueue,/* lock->waitProcs */
557
		  LOCKMETHODCTL *lockctl,
558
		  int token,			/* lockmode */
V
Vadim B. Mikheev 已提交
559
		  LOCK *lock)
560
{
561
	int			i;
V
Vadim B. Mikheev 已提交
562
	SPINLOCK	spinlock = lockctl->masterLock;
563
	PROC	   *proc;
V
Vadim B. Mikheev 已提交
564 565 566 567 568
	int			myMask = (1 << token);
	int			waitMask = lock->waitMask;
	int			aheadHolders[MAX_LOCKMODES];
	bool		selfConflict = (lockctl->conflictTab[token] & myMask),
				prevSame = false;
B
Bruce Momjian 已提交
569
	bool		deadlock_checked = false;
570
#ifndef __BEOS__
B
Bruce Momjian 已提交
571 572
	struct itimerval timeval,
				dummy;
573 574 575
#else
    bigtime_t time_interval;
#endif
576

V
Vadim B. Mikheev 已提交
577 578 579
	MyProc->token = token;
	MyProc->waitLock = lock;

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

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

V
Vadim B. Mikheev 已提交
586 587 588
	for (i = 1; i < MAX_LOCKMODES; i++)
		aheadHolders[i] = lock->activeHolders[i];
	(aheadHolders[token])++;
589

V
Vadim B. Mikheev 已提交
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
	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 已提交
610

V
Vadim B. Mikheev 已提交
611
			/*
B
Bruce Momjian 已提交
612 613
			 * If I request non self-conflicting lock and there are others
			 * requesting the same lock just before me - stay here.
V
Vadim B. Mikheev 已提交
614 615 616 617
			 */
			if (!selfConflict && prevSame)
				break;
		}
B
Bruce Momjian 已提交
618

V
Vadim B. Mikheev 已提交
619
		/*
B
Bruce Momjian 已提交
620 621
		 * Last attempt to don't move any more: if we don't conflict with
		 * rest waiters in queue.
V
Vadim B. Mikheev 已提交
622 623 624
		 */
		else if (!(lockctl->conflictTab[token] & waitMask))
			break;
625

V
Vadim B. Mikheev 已提交
626 627 628
		prevSame = (proc->token == token);
		(aheadHolders[proc->token])++;
		if (aheadHolders[proc->token] == lock->holders[proc->token])
B
Bruce Momjian 已提交
629
			waitMask &= ~(1 << proc->token);
V
Vadim B. Mikheev 已提交
630 631
		proc = (PROC *) MAKE_PTR(proc->links.prev);
	}
632

V
Vadim B. Mikheev 已提交
633
ins:;
634 635 636 637 638 639
	/* -------------------
	 * assume that these two operations are atomic (because
	 * of the spinlock).
	 * -------------------
	 */
	SHMQueueInsertTL(&(proc->links), &(MyProc->links));
B
Bruce Momjian 已提交
640
	waitQueue->size++;
641

V
Vadim B. Mikheev 已提交
642
	lock->waitMask |= myMask;
643 644 645
	SpinRelease(spinlock);

	/* --------------
B
Bruce Momjian 已提交
646
	 * We set this so we can wake up periodically and check for a deadlock.
B
Bruce Momjian 已提交
647 648
	 * If a deadlock is detected, the handler releases the processes
	 * semaphore and aborts the current transaction.
B
Bruce Momjian 已提交
649 650 651
	 *
	 * Need to zero out struct to set the interval and the micro seconds fields
	 * to 0.
652 653
	 * --------------
	 */
654
#ifndef __BEOS__
B
Bruce Momjian 已提交
655
	MemSet(&timeval, 0, sizeof(struct itimerval));
656 657
	timeval.it_value.tv_sec = DeadlockTimeout / 1000;
	timeval.it_value.tv_usec = (DeadlockTimeout % 1000) * 1000;
658 659 660 661
#else
    /* usecs */
    time_interval = DeadlockTimeout * 1000000;
#endif
662

663
	SetWaitingForLock(true);
B
Bruce Momjian 已提交
664 665
	do
	{
666
		MyProc->errType = NO_ERROR;		/* reset flag after deadlock check */
667

B
Bruce Momjian 已提交
668
		if (!deadlock_checked)
669
#ifndef __BEOS__
B
Bruce Momjian 已提交
670
			if (setitimer(ITIMER_REAL, &timeval, &dummy))
671 672 673
#else
            if (set_alarm(time_interval, B_ONE_SHOT_RELATIVE_ALARM) < 0)
#endif
B
Bruce Momjian 已提交
674 675 676
				elog(FATAL, "ProcSleep: Unable to set timer for process wakeup");
		deadlock_checked = true;

B
Bruce Momjian 已提交
677 678 679 680 681 682
		/* --------------
		 * if someone wakes us between SpinRelease and IpcSemaphoreLock,
		 * IpcSemaphoreLock will not block.  The wakeup is "saved" by
		 * the semaphore implementation.
		 * --------------
		 */
M
 
Marc G. Fournier 已提交
683 684
		IpcSemaphoreLock(MyProc->sem.semId, MyProc->sem.semNum,
						 IpcExclusiveLock);
685 686
	} while (MyProc->errType == STATUS_NOT_FOUND);		/* sleep after deadlock
														 * check */
687
	lockWaiting = false;
688

B
Bruce Momjian 已提交
689 690 691 692
	/* ---------------
	 * We were awoken before a timeout - now disable the timer
	 * ---------------
	 */
693
#ifndef __BEOS__
B
Bruce Momjian 已提交
694
	timeval.it_value.tv_sec = 0;
695
	timeval.it_value.tv_usec = 0;
B
Bruce Momjian 已提交
696
	if (setitimer(ITIMER_REAL, &timeval, &dummy))
697 698 699
#else
    if (set_alarm(B_INFINITE_TIMEOUT, B_PERIODIC_ALARM) < 0)
#endif
B
Bruce Momjian 已提交
700 701
		elog(FATAL, "ProcSleep: Unable to diable timer for process wakeup");

702 703 704 705 706 707 708
	/* ----------------
	 * We were assumed to be in a critical section when we went
	 * to sleep.
	 * ----------------
	 */
	SpinAcquire(spinlock);

V
Vadim B. Mikheev 已提交
709 710
rt:;

711
#ifdef LOCK_DEBUG
M
 
Marc G. Fournier 已提交
712
	/* Just to get meaningful debug messages from DumpLocks() */
713
	MyProc->waitLock = (LOCK *) NULL;
M
 
Marc G. Fournier 已提交
714 715
#endif

716
	return MyProc->errType;
717 718 719 720 721 722
}


/*
 * ProcWakeup -- wake up a process by releasing its private semaphore.
 *
723 724
 *	 remove the process from the wait queue and set its links invalid.
 *	 RETURN: the next process in the wait queue.
725
 */
B
Bruce Momjian 已提交
726
PROC *
727
ProcWakeup(PROC *proc, int errType)
728
{
729
	PROC	   *retProc;
730 731 732 733 734

	/* assume that spinlock has been acquired */

	if (proc->links.prev == INVALID_OFFSET ||
		proc->links.next == INVALID_OFFSET)
735
		return (PROC *) NULL;
736 737 738 739 740 741 742 743 744 745 746 747

	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;
748 749 750 751
}

/*
 * ProcLockWakeup -- routine for waking up processes when a lock is
752
 *		released.
753 754
 */
int
755
ProcLockWakeup(PROC_QUEUE *queue, LOCKMETHOD lockmethod, LOCK *lock)
756
{
757
	PROC	   *proc;
V
Vadim B. Mikheev 已提交
758 759
	int			count = 0;
	int			last_locktype = 0;
M
 
Marc G. Fournier 已提交
760 761 762
	int			queue_size = queue->size;

	Assert(queue->size >= 0);
763 764

	if (!queue->size)
765
		return STATUS_NOT_FOUND;
766 767

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

M
 
Marc G. Fournier 已提交
771
		/*
772 773
		 * This proc will conflict as the previous one did, don't even
		 * try.
M
 
Marc G. Fournier 已提交
774 775 776 777 778
		 */
		if (proc->token == last_locktype)
			continue;

		/*
V
Vadim B. Mikheev 已提交
779
		 * Does this proc conflict with locks held by others ?
M
 
Marc G. Fournier 已提交
780 781
		 */
		if (LockResolveConflicts(lockmethod,
782
								 lock,
783
								 proc->token,
M
 
Marc G. Fournier 已提交
784 785 786
								 proc->xid,
								 (XIDLookupEnt *) NULL) != STATUS_OK)
		{
V
Vadim B. Mikheev 已提交
787 788
			if (count != 0)
				break;
M
 
Marc G. Fournier 已提交
789 790 791
			last_locktype = proc->token;
			continue;
		}
792 793 794 795 796 797 798

		/*
		 * 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.
		 */
799
		GrantLock(lock, proc->token);
800 801 802

		/*
		 * ProcWakeup removes proc from the lock waiting process queue and
803
		 * returns the next proc in chain.
804 805 806
		 */

		count++;
M
 
Marc G. Fournier 已提交
807 808
		queue->size--;
		proc = ProcWakeup(proc, NO_ERROR);
809
	}
810

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

813
	if (count)
814
		return STATUS_OK;
815 816
	else
	{
817
		/* Something is still blocking us.	May have deadlocked. */
818 819 820 821 822
#ifdef LOCK_DEBUG
		if (lock->tag.lockmethod == USER_LOCKMETHOD ? Trace_userlocks : Trace_locks)
		{
			elog(DEBUG, "ProcLockWakeup: lock(%lx) can't wake up any process", MAKE_OFFSET(lock));
			if (Debug_deadlocks)
M
 
Marc G. Fournier 已提交
823
			DumpAllLocks();
824
		}
M
 
Marc G. Fournier 已提交
825
#endif
826
		return STATUS_NOT_FOUND;
M
 
Marc G. Fournier 已提交
827
	}
828 829 830
}

void
831
ProcAddLock(SHM_QUEUE *elem)
832
{
833
	SHMQueueInsertTL(&MyProc->lockQueue, elem);
834 835 836
}

/* --------------------
837
 * We only get to this routine if we got SIGALRM after DeadlockTimeout
B
Bruce Momjian 已提交
838 839
 * 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
840
 * on a lock so that other processes don't try to wake me up and screw
841 842 843
 * up my semaphore.
 * --------------------
 */
844
void
845
HandleDeadLock(SIGNAL_ARGS)
846
{
B
Bruce Momjian 已提交
847
	LOCK	   *mywaitlock;
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885

	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;
	}

886 887 888
#ifdef LOCK_DEBUG
    if (Debug_deadlocks)
        DumpAllLocks();
889 890
#endif

B
Bruce Momjian 已提交
891 892
	MyProc->errType = STATUS_NOT_FOUND;
	if (!DeadLockCheck(MyProc, MyProc->waitLock))
B
Bruce Momjian 已提交
893 894 895 896 897 898 899
	{
		UnlockLockTable();
		return;
	}

	mywaitlock = MyProc->waitLock;

900 901 902 903
	/* ------------------------
	 * Get this process off the lock's wait queue
	 * ------------------------
	 */
B
Bruce Momjian 已提交
904
	Assert(mywaitlock->waitProcs.size > 0);
905
	lockWaiting = false;
B
Bruce Momjian 已提交
906
	--mywaitlock->waitProcs.size;
907 908 909 910 911 912 913 914
	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 已提交
915 916
	IpcSemaphoreUnlock(MyProc->sem.semId, MyProc->sem.semNum,
					   IpcExclusiveLock);
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931

	/* -------------
	 * 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 已提交
932
	elog(NOTICE, DeadLockMessage);
933
	return;
934 935 936
}

void
937
ProcReleaseSpins(PROC *proc)
938
{
939
	int			i;
940 941 942 943 944 945 946

	if (!proc)
		proc = MyProc;

	if (!proc)
		return;
	for (i = 0; i < (int) MAX_SPINS; i++)
947
	{
948
		if (proc->sLocks[i])
949
		{
950 951
			Assert(proc->sLocks[i] == 1);
			SpinRelease(i);
952 953
		}
	}
H
 
Hiroshi Inoue 已提交
954
	AbortBufferIO();
955 956 957
}

/*****************************************************************************
958
 *
959 960 961 962
 *****************************************************************************/

/*
 * ProcGetNewSemKeyAndNum -
963 964 965 966
 *	  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.)
967 968
 */
static void
969
ProcGetNewSemKeyAndNum(IPCKey *key, int *semNum)
970
{
971 972
	int			i;
	int32	   *freeSemMap = ProcGlobal->freeSemMap;
B
Bruce Momjian 已提交
973
	int32		fullmask = (1 << (PROC_NSEMS_PER_SET + 1)) - 1;
974

975 976 977 978
	/*
	 * we hold ProcStructLock when entering this routine. We scan through
	 * the bitmap to look for a free semaphore.
	 */
979

980 981
	for (i = 0; i < MAX_PROC_SEMS / PROC_NSEMS_PER_SET; i++)
	{
982 983
		int			mask = 1;
		int			j;
984 985

		if (freeSemMap[i] == fullmask)
986
			continue;			/* this set is fully allocated */
987 988 989 990 991 992 993

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

				/*
B
Bruce Momjian 已提交
994 995
				 * a free semaphore found. Mark it as allocated. Also set
				 * the bit indicating whole set is allocated.
996
				 */
997
				freeSemMap[i] |= mask + (1 << PROC_NSEMS_PER_SET);
998 999 1000 1001 1002 1003 1004

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

1007
	/* if we reach here, all the semaphores are in use. */
1008
	elog(ERROR, "InitProc: cannot allocate a free semaphore");
1009 1010 1011 1012
}

/*
 * ProcFreeSem -
1013
 *	  free up our semaphore in the semaphore set.
1014 1015 1016 1017
 */
static void
ProcFreeSem(IpcSemaphoreKey semKey, int semNum)
{
1018 1019 1020
	int			mask;
	int			i;
	int32	   *freeSemMap = ProcGlobal->freeSemMap;
1021

1022 1023 1024
	i = semKey - ProcGlobal->currKey;
	mask = ~(1 << semNum);
	freeSemMap[i] &= mask;
1025

B
Bruce Momjian 已提交
1026 1027 1028 1029
	/*
	 * 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
1030 1031 1032
	 * PROC_NSEMS_PER_SET+1'st bit of the freeSemMap entry remains set to
	 * indicate it is still allocated; ProcFreeAllSemaphores() needs that.
	 */
1033 1034 1035 1036
}

/*
 * ProcFreeAllSemaphores -
1037 1038 1039
 *	  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.
1040
 */
1041
static void
1042 1043
ProcFreeAllSemaphores()
{
1044 1045
	int			i;
	int32	   *freeSemMap = ProcGlobal->freeSemMap;
1046

1047 1048 1049 1050 1051
	for (i = 0; i < MAX_PROC_SEMS / PROC_NSEMS_PER_SET; i++)
	{
		if (freeSemMap[i] != 0)
			IpcSemaphoreKill(ProcGlobal->currKey + i);
	}
1052
}