Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6059c5a

Browse files
committed
Update for deadlock detection.
1 parent 06c120e commit 6059c5a

File tree

1 file changed

+29
-18
lines changed
  • src/backend/storage/lmgr

1 file changed

+29
-18
lines changed

src/backend/storage/lmgr/proc.c

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.46 1998/12/29 18:36:29 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.47 1998/12/29 19:32:08 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -46,7 +46,7 @@
4646
* This is so that we can support more backends. (system-wide semaphore
4747
* sets run out pretty fast.) -ay 4/95
4848
*
49-
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.46 1998/12/29 18:36:29 momjian Exp $
49+
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.47 1998/12/29 19:32:08 momjian Exp $
5050
*/
5151
#include <sys/time.h>
5252
#include <unistd.h>
@@ -77,7 +77,7 @@
7777
#include "storage/proc.h"
7878
#include "utils/trace.h"
7979

80-
static void HandleDeadLock(void);
80+
static void HandleDeadLock(int sig);
8181
static PROC *ProcWakeup(PROC *proc, int errType);
8282

8383
#define DeadlockCheckTimer pg_options[OPT_DEADLOCKTIMEOUT]
@@ -154,6 +154,8 @@ InitProcess(IPCKey key)
154154
* Routine called if deadlock timer goes off. See ProcSleep()
155155
* ------------------
156156
*/
157+
pqsignal(SIGALRM, HandleDeadLock);
158+
157159
SpinAcquire(ProcStructLock);
158160

159161
/* attach to the free list */
@@ -447,8 +449,10 @@ ProcSleep(PROC_QUEUE *waitQueue,/* lock->waitProcs */
447449
TransactionId xid) /* needed by user locks, see below */
448450
{
449451
int i;
450-
bool deadlock_checked = false;
451452
PROC *proc;
453+
bool deadlock_checked = false;
454+
struct itimerval timeval,
455+
dummy;
452456

453457
/*
454458
* If the first entries in the waitQueue have a greater priority than
@@ -512,28 +516,27 @@ ProcSleep(PROC_QUEUE *waitQueue,/* lock->waitProcs */
512516
SpinRelease(spinlock);
513517

514518
/* --------------
515-
* We set this so we can wake up after one second to check for a deadlock.
519+
* We set this so we can wake up periodically and check for a deadlock.
516520
* If a deadlock is detected, the handler releases the processes
517521
* semaphore and aborts the current transaction.
522+
*
523+
* Need to zero out struct to set the interval and the micro seconds fields
524+
* to 0.
518525
* --------------
519526
*/
527+
MemSet(&timeval, 0, sizeof(struct itimerval));
528+
timeval.it_value.tv_sec = \
529+
(DeadlockCheckTimer ? DeadlockCheckTimer : DEADLOCK_CHECK_TIMER);
520530

521531
do
522532
{
523533
MyProc->errType = NO_ERROR; /* reset flag after deadlock check */
524534

525-
if (deadlock_checked == false)
526-
{
527-
if (sleep(DeadlockCheckTimer ? DeadlockCheckTimer : DEADLOCK_CHECK_TIMER)
528-
== 0 /* no signal interruption */ )
529-
{
530-
HandleDeadLock();
531-
deadlock_checked = true;
532-
}
533-
}
534-
else
535-
pause();
536-
535+
if (!deadlock_checked)
536+
if (setitimer(ITIMER_REAL, &timeval, &dummy))
537+
elog(FATAL, "ProcSleep: Unable to set timer for process wakeup");
538+
deadlock_checked = true;
539+
537540
/* --------------
538541
* if someone wakes us between SpinRelease and IpcSemaphoreLock,
539542
* IpcSemaphoreLock will not block. The wakeup is "saved" by
@@ -545,6 +548,14 @@ ProcSleep(PROC_QUEUE *waitQueue,/* lock->waitProcs */
545548
} while (MyProc->errType == STATUS_NOT_FOUND); /* sleep after deadlock
546549
* check */
547550

551+
/* ---------------
552+
* We were awoken before a timeout - now disable the timer
553+
* ---------------
554+
*/
555+
timeval.it_value.tv_sec = 0;
556+
if (setitimer(ITIMER_REAL, &timeval, &dummy))
557+
elog(FATAL, "ProcSleep: Unable to diable timer for process wakeup");
558+
548559
/* ----------------
549560
* We were assumed to be in a critical section when we went
550561
* to sleep.
@@ -687,7 +698,7 @@ ProcAddLock(SHM_QUEUE *elem)
687698
* --------------------
688699
*/
689700
static void
690-
HandleDeadLock()
701+
HandleDeadLock(int sig)
691702
{
692703
LOCK *mywaitlock;
693704

0 commit comments

Comments
 (0)