2001-09-29 06:02:27 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* lwlock.c
|
|
|
|
* Lightweight lock manager
|
|
|
|
*
|
|
|
|
* Lightweight locks are intended primarily to provide mutual exclusion of
|
|
|
|
* access to shared-memory data structures. Therefore, they offer both
|
|
|
|
* exclusive and shared lock modes (to support read/write and read-only
|
2001-10-25 07:50:21 +02:00
|
|
|
* access to a shared object). There are few other frammishes. User-level
|
2001-09-29 06:02:27 +02:00
|
|
|
* locking should be done with the full lock manager --- which depends on
|
2005-12-11 22:02:18 +01:00
|
|
|
* LWLocks to protect its shared state.
|
2001-09-29 06:02:27 +02:00
|
|
|
*
|
|
|
|
*
|
2006-03-05 16:59:11 +01:00
|
|
|
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
|
2001-09-29 06:02:27 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2006-10-16 00:04:08 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/storage/lmgr/lwlock.c,v 1.47 2006/10/15 22:04:07 tgl Exp $
|
2001-09-29 06:02:27 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2005-12-07 00:08:34 +01:00
|
|
|
#include "access/clog.h"
|
|
|
|
#include "access/multixact.h"
|
|
|
|
#include "access/subtrans.h"
|
2005-12-29 19:08:05 +01:00
|
|
|
#include "miscadmin.h"
|
2006-08-07 23:56:25 +02:00
|
|
|
#include "storage/ipc.h"
|
2001-09-29 06:02:27 +02:00
|
|
|
#include "storage/proc.h"
|
|
|
|
#include "storage/spin.h"
|
|
|
|
|
|
|
|
|
2005-10-07 23:42:38 +02:00
|
|
|
/* We use the ShmemLock spinlock to protect LWLockAssign */
|
|
|
|
extern slock_t *ShmemLock;
|
|
|
|
|
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
typedef struct LWLock
|
|
|
|
{
|
2002-06-11 15:40:53 +02:00
|
|
|
slock_t mutex; /* Protects LWLock and queue of PGPROCs */
|
2002-01-07 17:33:00 +01:00
|
|
|
bool releaseOK; /* T if ok to release waiters */
|
2001-09-29 06:02:27 +02:00
|
|
|
char exclusive; /* # of exclusive holders (0 or 1) */
|
|
|
|
int shared; /* # of shared holders (0..MaxBackends) */
|
2002-06-11 15:40:53 +02:00
|
|
|
PGPROC *head; /* head of list of waiting PGPROCs */
|
|
|
|
PGPROC *tail; /* tail of list of waiting PGPROCs */
|
2001-09-29 06:02:27 +02:00
|
|
|
/* tail is undefined when head is NULL */
|
|
|
|
} LWLock;
|
|
|
|
|
2005-09-16 02:30:05 +02:00
|
|
|
/*
|
|
|
|
* All the LWLock structs are allocated as an array in shared memory.
|
2005-10-15 04:49:52 +02:00
|
|
|
* (LWLockIds are indexes into the array.) We force the array stride to
|
2005-09-16 02:30:05 +02:00
|
|
|
* be a power of 2, which saves a few cycles in indexing, but more
|
|
|
|
* importantly also ensures that individual LWLocks don't cross cache line
|
2005-10-15 04:49:52 +02:00
|
|
|
* boundaries. This reduces cache contention problems, especially on AMD
|
2005-09-16 02:30:05 +02:00
|
|
|
* Opterons. (Of course, we have to also ensure that the array start
|
|
|
|
* address is suitably aligned.)
|
|
|
|
*
|
|
|
|
* LWLock is between 16 and 32 bytes on all known platforms, so these two
|
|
|
|
* cases are sufficient.
|
|
|
|
*/
|
|
|
|
#define LWLOCK_PADDED_SIZE (sizeof(LWLock) <= 16 ? 16 : 32)
|
|
|
|
|
|
|
|
typedef union LWLockPadded
|
|
|
|
{
|
|
|
|
LWLock lock;
|
|
|
|
char pad[LWLOCK_PADDED_SIZE];
|
|
|
|
} LWLockPadded;
|
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
/*
|
|
|
|
* This points to the array of LWLocks in shared memory. Backends inherit
|
2005-09-16 02:30:05 +02:00
|
|
|
* the pointer by fork from the postmaster (except in the EXEC_BACKEND case,
|
|
|
|
* where we have special measures to pass it down).
|
2001-09-29 06:02:27 +02:00
|
|
|
*/
|
2005-09-16 02:30:05 +02:00
|
|
|
NON_EXEC_STATIC LWLockPadded *LWLockArray = NULL;
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We use this structure to keep track of locked LWLocks for release
|
|
|
|
* during error recovery. The maximum size could be determined at runtime
|
|
|
|
* if necessary, but it seems unlikely that more than a few locks could
|
|
|
|
* ever be held simultaneously.
|
|
|
|
*/
|
|
|
|
#define MAX_SIMUL_LWLOCKS 100
|
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
static int num_held_lwlocks = 0;
|
|
|
|
static LWLockId held_lwlocks[MAX_SIMUL_LWLOCKS];
|
2001-09-29 06:02:27 +02:00
|
|
|
|
2006-10-16 00:04:08 +02:00
|
|
|
static int lock_addin_request = 0;
|
|
|
|
static bool lock_addin_request_allowed = true;
|
|
|
|
|
2006-04-21 18:45:12 +02:00
|
|
|
#ifdef LWLOCK_STATS
|
|
|
|
static int counts_for_pid = 0;
|
|
|
|
static int *sh_acquire_counts;
|
|
|
|
static int *ex_acquire_counts;
|
|
|
|
static int *block_counts;
|
|
|
|
#endif
|
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
#ifdef LOCK_DEBUG
|
|
|
|
bool Trace_lwlocks = false;
|
|
|
|
|
|
|
|
inline static void
|
2001-12-29 00:26:04 +01:00
|
|
|
PRINT_LWDEBUG(const char *where, LWLockId lockid, const volatile LWLock *lock)
|
2001-09-29 06:02:27 +02:00
|
|
|
{
|
|
|
|
if (Trace_lwlocks)
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
elog(LOG, "%s(%d): excl %d shared %d head %p rOK %d",
|
2001-09-29 06:02:27 +02:00
|
|
|
where, (int) lockid,
|
2002-01-07 17:33:00 +01:00
|
|
|
(int) lock->exclusive, lock->shared, lock->head,
|
|
|
|
(int) lock->releaseOK);
|
2001-09-29 06:02:27 +02:00
|
|
|
}
|
|
|
|
|
2001-12-29 00:26:04 +01:00
|
|
|
inline static void
|
|
|
|
LOG_LWDEBUG(const char *where, LWLockId lockid, const char *msg)
|
|
|
|
{
|
|
|
|
if (Trace_lwlocks)
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
elog(LOG, "%s(%d): %s", where, (int) lockid, msg);
|
2001-12-29 00:26:04 +01:00
|
|
|
}
|
2001-10-25 07:50:21 +02:00
|
|
|
#else /* not LOCK_DEBUG */
|
2001-09-29 06:02:27 +02:00
|
|
|
#define PRINT_LWDEBUG(a,b,c)
|
2001-12-29 00:26:04 +01:00
|
|
|
#define LOG_LWDEBUG(a,b,c)
|
2001-11-05 18:46:40 +01:00
|
|
|
#endif /* LOCK_DEBUG */
|
2001-09-29 06:02:27 +02:00
|
|
|
|
2006-04-21 18:45:12 +02:00
|
|
|
#ifdef LWLOCK_STATS
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_lwlock_stats(int code, Datum arg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int *LWLockCounter = (int *) ((char *) LWLockArray - 2 * sizeof(int));
|
|
|
|
int numLocks = LWLockCounter[1];
|
|
|
|
|
|
|
|
/* Grab an LWLock to keep different backends from mixing reports */
|
|
|
|
LWLockAcquire(0, LW_EXCLUSIVE);
|
|
|
|
|
|
|
|
for (i = 0; i < numLocks; i++)
|
|
|
|
{
|
|
|
|
if (sh_acquire_counts[i] || ex_acquire_counts[i] || block_counts[i])
|
|
|
|
fprintf(stderr, "PID %d lwlock %d: shacq %u exacq %u blk %u\n",
|
|
|
|
MyProcPid, i, sh_acquire_counts[i], ex_acquire_counts[i],
|
|
|
|
block_counts[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
LWLockRelease(0);
|
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
#endif /* LWLOCK_STATS */
|
2006-04-21 18:45:12 +02:00
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute number of LWLocks to allocate.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NumLWLocks(void)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
int numLocks;
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Possibly this logic should be spread out among the affected modules,
|
|
|
|
* the same way that shmem space estimation is done. But for now, there
|
|
|
|
* are few enough users of LWLocks that we can get away with just keeping
|
|
|
|
* the knowledge here.
|
2001-09-29 06:02:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Predefined LWLocks */
|
2006-07-23 05:07:58 +02:00
|
|
|
numLocks = (int) NumFixedLWLocks;
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
/* bufmgr.c needs two for each shared buffer */
|
|
|
|
numLocks += 2 * NBuffers;
|
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
/* clog.c needs one per CLOG buffer */
|
2005-12-07 00:08:34 +01:00
|
|
|
numLocks += NUM_CLOG_BUFFERS;
|
2001-09-29 06:02:27 +02:00
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
/* subtrans.c needs one per SubTrans buffer */
|
2005-12-07 00:08:34 +01:00
|
|
|
numLocks += NUM_SUBTRANS_BUFFERS;
|
2004-07-01 02:52:04 +02:00
|
|
|
|
2005-12-07 00:08:34 +01:00
|
|
|
/* multixact.c needs two SLRU areas */
|
|
|
|
numLocks += NUM_MXACTOFFSET_BUFFERS + NUM_MXACTMEMBER_BUFFERS;
|
2005-04-28 23:47:18 +02:00
|
|
|
|
2006-10-16 00:04:08 +02:00
|
|
|
/*
|
|
|
|
* Add any requested by loadable modules; for backwards-compatibility
|
|
|
|
* reasons, allocate at least NUM_USER_DEFINED_LWLOCKS of them even
|
|
|
|
* if there are no explicit requests.
|
|
|
|
*/
|
|
|
|
lock_addin_request_allowed = false;
|
|
|
|
numLocks += Max(lock_addin_request, NUM_USER_DEFINED_LWLOCKS);
|
2006-08-01 21:03:11 +02:00
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
return numLocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-16 00:04:08 +02:00
|
|
|
/*
|
|
|
|
* RequestAddinLWLocks
|
|
|
|
* Request that extra LWLocks be allocated for use by
|
|
|
|
* a loadable module.
|
|
|
|
*
|
|
|
|
* This is only useful if called from the _PG_init hook of a library that
|
|
|
|
* is loaded into the postmaster via shared_preload_libraries. Once
|
|
|
|
* shared memory has been allocated, calls will be ignored. (We could
|
|
|
|
* raise an error, but it seems better to make it a no-op, so that
|
|
|
|
* libraries containing such calls can be reloaded if needed.)
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RequestAddinLWLocks(int n)
|
|
|
|
{
|
|
|
|
if (IsUnderPostmaster || !lock_addin_request_allowed)
|
|
|
|
return; /* too late */
|
|
|
|
lock_addin_request += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
/*
|
|
|
|
* Compute shmem space needed for LWLocks.
|
|
|
|
*/
|
2005-08-21 01:26:37 +02:00
|
|
|
Size
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockShmemSize(void)
|
|
|
|
{
|
2005-08-21 01:26:37 +02:00
|
|
|
Size size;
|
2001-10-25 07:50:21 +02:00
|
|
|
int numLocks = NumLWLocks();
|
2001-09-29 06:02:27 +02:00
|
|
|
|
2005-09-16 02:30:05 +02:00
|
|
|
/* Space for the LWLock array. */
|
|
|
|
size = mul_size(numLocks, sizeof(LWLockPadded));
|
2005-08-21 01:26:37 +02:00
|
|
|
|
2005-10-07 23:42:38 +02:00
|
|
|
/* Space for dynamic allocation counter, plus room for alignment. */
|
2005-09-16 02:30:05 +02:00
|
|
|
size = add_size(size, 2 * sizeof(int) + LWLOCK_PADDED_SIZE);
|
2001-09-29 06:02:27 +02:00
|
|
|
|
2005-08-21 01:26:37 +02:00
|
|
|
return size;
|
2001-09-29 06:02:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate shmem space for LWLocks and initialize the locks.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
CreateLWLocks(void)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
int numLocks = NumLWLocks();
|
2005-08-21 01:26:37 +02:00
|
|
|
Size spaceLocks = LWLockShmemSize();
|
2005-09-16 02:30:05 +02:00
|
|
|
LWLockPadded *lock;
|
2005-10-07 23:42:38 +02:00
|
|
|
int *LWLockCounter;
|
2005-09-16 02:30:05 +02:00
|
|
|
char *ptr;
|
2001-10-25 07:50:21 +02:00
|
|
|
int id;
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
/* Allocate space */
|
2005-09-16 02:30:05 +02:00
|
|
|
ptr = (char *) ShmemAlloc(spaceLocks);
|
|
|
|
|
2005-10-07 23:42:38 +02:00
|
|
|
/* Leave room for dynamic allocation counter */
|
|
|
|
ptr += 2 * sizeof(int);
|
|
|
|
|
2005-09-16 02:30:05 +02:00
|
|
|
/* Ensure desired alignment of LWLock array */
|
|
|
|
ptr += LWLOCK_PADDED_SIZE - ((unsigned long) ptr) % LWLOCK_PADDED_SIZE;
|
|
|
|
|
|
|
|
LWLockArray = (LWLockPadded *) ptr;
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize all LWLocks to "unlocked" state
|
|
|
|
*/
|
|
|
|
for (id = 0, lock = LWLockArray; id < numLocks; id++, lock++)
|
|
|
|
{
|
2005-09-16 02:30:05 +02:00
|
|
|
SpinLockInit(&lock->lock.mutex);
|
|
|
|
lock->lock.releaseOK = true;
|
|
|
|
lock->lock.exclusive = 0;
|
|
|
|
lock->lock.shared = 0;
|
|
|
|
lock->lock.head = NULL;
|
|
|
|
lock->lock.tail = NULL;
|
2001-09-29 06:02:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-10-07 23:42:38 +02:00
|
|
|
* Initialize the dynamic-allocation counter, which is stored just before
|
2006-07-23 05:07:58 +02:00
|
|
|
* the first LWLock.
|
2001-09-29 06:02:27 +02:00
|
|
|
*/
|
2005-10-07 23:42:38 +02:00
|
|
|
LWLockCounter = (int *) ((char *) LWLockArray - 2 * sizeof(int));
|
2006-07-23 05:07:58 +02:00
|
|
|
LWLockCounter[0] = (int) NumFixedLWLocks;
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockCounter[1] = numLocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LWLockAssign - assign a dynamically-allocated LWLock number
|
|
|
|
*
|
2005-10-07 23:42:38 +02:00
|
|
|
* We interlock this using the same spinlock that is used to protect
|
|
|
|
* ShmemAlloc(). Interlocking is not really necessary during postmaster
|
|
|
|
* startup, but it is needed if any user-defined code tries to allocate
|
|
|
|
* LWLocks after startup.
|
2001-09-29 06:02:27 +02:00
|
|
|
*/
|
|
|
|
LWLockId
|
|
|
|
LWLockAssign(void)
|
|
|
|
{
|
2005-10-07 23:42:38 +02:00
|
|
|
LWLockId result;
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-10-12 18:55:59 +02:00
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile int *LWLockCounter;
|
2005-10-07 23:42:38 +02:00
|
|
|
|
|
|
|
LWLockCounter = (int *) ((char *) LWLockArray - 2 * sizeof(int));
|
|
|
|
SpinLockAcquire(ShmemLock);
|
2001-09-29 06:02:27 +02:00
|
|
|
if (LWLockCounter[0] >= LWLockCounter[1])
|
2005-10-07 23:42:38 +02:00
|
|
|
{
|
|
|
|
SpinLockRelease(ShmemLock);
|
|
|
|
elog(ERROR, "no more LWLockIds available");
|
|
|
|
}
|
|
|
|
result = (LWLockId) (LWLockCounter[0]++);
|
|
|
|
SpinLockRelease(ShmemLock);
|
|
|
|
return result;
|
2001-09-29 06:02:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LWLockAcquire - acquire a lightweight lock in the specified mode
|
|
|
|
*
|
|
|
|
* If the lock is not available, sleep until it is.
|
|
|
|
*
|
|
|
|
* Side effect: cancel/die interrupts are held off until lock release.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
LWLockAcquire(LWLockId lockid, LWLockMode mode)
|
|
|
|
{
|
2005-09-16 02:30:05 +02:00
|
|
|
volatile LWLock *lock = &(LWLockArray[lockid].lock);
|
2002-06-11 15:40:53 +02:00
|
|
|
PGPROC *proc = MyProc;
|
2002-01-07 17:33:00 +01:00
|
|
|
bool retry = false;
|
|
|
|
int extraWaits = 0;
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
PRINT_LWDEBUG("LWLockAcquire", lockid, lock);
|
|
|
|
|
2006-04-21 18:45:12 +02:00
|
|
|
#ifdef LWLOCK_STATS
|
|
|
|
/* Set up local count state first time through in a given process */
|
|
|
|
if (counts_for_pid != MyProcPid)
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
int *LWLockCounter = (int *) ((char *) LWLockArray - 2 * sizeof(int));
|
|
|
|
int numLocks = LWLockCounter[1];
|
2006-04-21 18:45:12 +02:00
|
|
|
|
|
|
|
sh_acquire_counts = calloc(numLocks, sizeof(int));
|
|
|
|
ex_acquire_counts = calloc(numLocks, sizeof(int));
|
|
|
|
block_counts = calloc(numLocks, sizeof(int));
|
|
|
|
counts_for_pid = MyProcPid;
|
|
|
|
on_shmem_exit(print_lwlock_stats, 0);
|
|
|
|
}
|
|
|
|
/* Count lock acquisition attempts */
|
|
|
|
if (mode == LW_EXCLUSIVE)
|
|
|
|
ex_acquire_counts[lockid]++;
|
|
|
|
else
|
|
|
|
sh_acquire_counts[lockid]++;
|
2006-10-04 02:30:14 +02:00
|
|
|
#endif /* LWLOCK_STATS */
|
2006-04-21 18:45:12 +02:00
|
|
|
|
2002-09-25 22:31:40 +02:00
|
|
|
/*
|
|
|
|
* We can't wait if we haven't got a PGPROC. This should only occur
|
2005-10-15 04:49:52 +02:00
|
|
|
* during bootstrap or shared memory initialization. Put an Assert here
|
|
|
|
* to catch unsafe coding practices.
|
2002-09-25 22:31:40 +02:00
|
|
|
*/
|
|
|
|
Assert(!(proc == NULL && IsUnderPostmaster));
|
|
|
|
|
2005-04-08 16:18:35 +02:00
|
|
|
/* Ensure we will have room to remember the lock */
|
|
|
|
if (num_held_lwlocks >= MAX_SIMUL_LWLOCKS)
|
|
|
|
elog(ERROR, "too many LWLocks taken");
|
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Lock out cancel/die interrupts until we exit the code section protected
|
|
|
|
* by the LWLock. This ensures that interrupts will not interfere with
|
|
|
|
* manipulations of data structures in shared memory.
|
2001-09-29 06:02:27 +02:00
|
|
|
*/
|
|
|
|
HOLD_INTERRUPTS();
|
|
|
|
|
2002-01-07 17:33:00 +01:00
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* Loop here to try to acquire lock after each time we are signaled by
|
|
|
|
* LWLockRelease.
|
2002-01-07 17:33:00 +01:00
|
|
|
*
|
2002-09-04 22:31:48 +02:00
|
|
|
* NOTE: it might seem better to have LWLockRelease actually grant us the
|
2005-10-15 04:49:52 +02:00
|
|
|
* lock, rather than retrying and possibly having to go back to sleep. But
|
|
|
|
* in practice that is no good because it means a process swap for every
|
|
|
|
* lock acquisition when two or more processes are contending for the same
|
|
|
|
* lock. Since LWLocks are normally used to protect not-very-long
|
|
|
|
* sections of computation, a process needs to be able to acquire and
|
|
|
|
* release the same lock many times during a single CPU time slice, even
|
|
|
|
* in the presence of contention. The efficiency of being able to do that
|
|
|
|
* outweighs the inefficiency of sometimes wasting a process dispatch
|
|
|
|
* cycle because the lock is not free when a released waiter finally gets
|
|
|
|
* to run. See pgsql-hackers archives for 29-Dec-01.
|
2002-01-07 17:33:00 +01:00
|
|
|
*/
|
|
|
|
for (;;)
|
2001-12-29 22:30:32 +01:00
|
|
|
{
|
2002-01-07 17:33:00 +01:00
|
|
|
bool mustwait;
|
|
|
|
|
|
|
|
/* Acquire mutex. Time spent holding mutex should be short! */
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockAcquire(&lock->mutex);
|
2002-01-07 17:33:00 +01:00
|
|
|
|
|
|
|
/* If retrying, allow LWLockRelease to release waiters again */
|
|
|
|
if (retry)
|
|
|
|
lock->releaseOK = true;
|
|
|
|
|
|
|
|
/* If I can get the lock, do so quickly. */
|
|
|
|
if (mode == LW_EXCLUSIVE)
|
2001-09-29 06:02:27 +02:00
|
|
|
{
|
2002-01-07 17:33:00 +01:00
|
|
|
if (lock->exclusive == 0 && lock->shared == 0)
|
|
|
|
{
|
|
|
|
lock->exclusive++;
|
|
|
|
mustwait = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mustwait = true;
|
2001-09-29 06:02:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-01-07 17:33:00 +01:00
|
|
|
if (lock->exclusive == 0)
|
|
|
|
{
|
|
|
|
lock->shared++;
|
|
|
|
mustwait = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mustwait = true;
|
2001-09-29 06:02:27 +02:00
|
|
|
}
|
|
|
|
|
2002-01-07 17:33:00 +01:00
|
|
|
if (!mustwait)
|
|
|
|
break; /* got the lock */
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
/*
|
2002-01-07 17:33:00 +01:00
|
|
|
* Add myself to wait queue.
|
|
|
|
*
|
2002-06-11 15:40:53 +02:00
|
|
|
* If we don't have a PGPROC structure, there's no way to wait. This
|
2005-10-15 04:49:52 +02:00
|
|
|
* should never occur, since MyProc should only be null during shared
|
|
|
|
* memory initialization.
|
2001-09-29 06:02:27 +02:00
|
|
|
*/
|
|
|
|
if (proc == NULL)
|
2005-12-29 19:08:05 +01:00
|
|
|
elog(PANIC, "cannot wait without a PGPROC structure");
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
proc->lwWaiting = true;
|
|
|
|
proc->lwExclusive = (mode == LW_EXCLUSIVE);
|
|
|
|
proc->lwWaitLink = NULL;
|
|
|
|
if (lock->head == NULL)
|
|
|
|
lock->head = proc;
|
|
|
|
else
|
|
|
|
lock->tail->lwWaitLink = proc;
|
|
|
|
lock->tail = proc;
|
|
|
|
|
|
|
|
/* Can release the mutex now */
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockRelease(&lock->mutex);
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait until awakened.
|
|
|
|
*
|
|
|
|
* Since we share the process wait semaphore with the regular lock
|
2005-10-15 04:49:52 +02:00
|
|
|
* manager and ProcWaitForSignal, and we may need to acquire an LWLock
|
|
|
|
* while one of those is pending, it is possible that we get awakened
|
|
|
|
* for a reason other than being signaled by LWLockRelease. If so,
|
|
|
|
* loop back and wait again. Once we've gotten the LWLock,
|
|
|
|
* re-increment the sema by the number of additional signals received,
|
|
|
|
* so that the lock manager or signal manager will see the received
|
|
|
|
* signal when it next waits.
|
2001-09-29 06:02:27 +02:00
|
|
|
*/
|
2001-12-29 00:26:04 +01:00
|
|
|
LOG_LWDEBUG("LWLockAcquire", lockid, "waiting");
|
|
|
|
|
2006-04-21 18:45:12 +02:00
|
|
|
#ifdef LWLOCK_STATS
|
|
|
|
block_counts[lockid]++;
|
|
|
|
#endif
|
|
|
|
|
2006-07-24 18:32:45 +02:00
|
|
|
PG_TRACE2(lwlock__startwait, lockid, mode);
|
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* "false" means cannot accept cancel/die interrupt here. */
|
2002-05-05 02:03:29 +02:00
|
|
|
PGSemaphoreLock(&proc->sem, false);
|
2001-09-29 06:02:27 +02:00
|
|
|
if (!proc->lwWaiting)
|
|
|
|
break;
|
|
|
|
extraWaits++;
|
|
|
|
}
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2006-07-24 18:32:45 +02:00
|
|
|
PG_TRACE2(lwlock__endwait, lockid, mode);
|
|
|
|
|
2001-12-29 00:26:04 +01:00
|
|
|
LOG_LWDEBUG("LWLockAcquire", lockid, "awakened");
|
|
|
|
|
2002-01-07 17:33:00 +01:00
|
|
|
/* Now loop back and try to acquire lock again. */
|
|
|
|
retry = true;
|
2001-09-29 06:02:27 +02:00
|
|
|
}
|
2001-12-29 22:28:18 +01:00
|
|
|
|
2002-01-07 17:33:00 +01:00
|
|
|
/* We are done updating shared state of the lock itself. */
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockRelease(&lock->mutex);
|
2002-01-07 17:33:00 +01:00
|
|
|
|
2006-07-24 18:32:45 +02:00
|
|
|
PG_TRACE2(lwlock__acquire, lockid, mode);
|
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
/* Add lock to list of locks held by this backend */
|
|
|
|
held_lwlocks[num_held_lwlocks++] = lockid;
|
2002-01-07 17:33:00 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fix the process wait semaphore's count for any absorbed wakeups.
|
|
|
|
*/
|
|
|
|
while (extraWaits-- > 0)
|
2002-05-05 02:03:29 +02:00
|
|
|
PGSemaphoreUnlock(&proc->sem);
|
2001-09-29 06:02:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LWLockConditionalAcquire - acquire a lightweight lock in the specified mode
|
|
|
|
*
|
|
|
|
* If the lock is not available, return FALSE with no side-effects.
|
|
|
|
*
|
|
|
|
* If successful, cancel/die interrupts are held off until lock release.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
LWLockConditionalAcquire(LWLockId lockid, LWLockMode mode)
|
|
|
|
{
|
2005-09-16 02:30:05 +02:00
|
|
|
volatile LWLock *lock = &(LWLockArray[lockid].lock);
|
2001-10-25 07:50:21 +02:00
|
|
|
bool mustwait;
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
PRINT_LWDEBUG("LWLockConditionalAcquire", lockid, lock);
|
|
|
|
|
2005-04-08 16:18:35 +02:00
|
|
|
/* Ensure we will have room to remember the lock */
|
|
|
|
if (num_held_lwlocks >= MAX_SIMUL_LWLOCKS)
|
|
|
|
elog(ERROR, "too many LWLocks taken");
|
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Lock out cancel/die interrupts until we exit the code section protected
|
|
|
|
* by the LWLock. This ensures that interrupts will not interfere with
|
|
|
|
* manipulations of data structures in shared memory.
|
2001-09-29 06:02:27 +02:00
|
|
|
*/
|
|
|
|
HOLD_INTERRUPTS();
|
|
|
|
|
|
|
|
/* Acquire mutex. Time spent holding mutex should be short! */
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockAcquire(&lock->mutex);
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
/* If I can get the lock, do so quickly. */
|
|
|
|
if (mode == LW_EXCLUSIVE)
|
|
|
|
{
|
|
|
|
if (lock->exclusive == 0 && lock->shared == 0)
|
|
|
|
{
|
|
|
|
lock->exclusive++;
|
|
|
|
mustwait = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mustwait = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-01-07 17:33:00 +01:00
|
|
|
if (lock->exclusive == 0)
|
2001-09-29 06:02:27 +02:00
|
|
|
{
|
|
|
|
lock->shared++;
|
|
|
|
mustwait = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mustwait = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are done updating shared state of the lock itself. */
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockRelease(&lock->mutex);
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
if (mustwait)
|
|
|
|
{
|
|
|
|
/* Failed to get lock, so release interrupt holdoff */
|
|
|
|
RESUME_INTERRUPTS();
|
2001-12-29 00:26:04 +01:00
|
|
|
LOG_LWDEBUG("LWLockConditionalAcquire", lockid, "failed");
|
2006-07-24 18:32:45 +02:00
|
|
|
PG_TRACE2(lwlock__condacquire__fail, lockid, mode);
|
2001-09-29 06:02:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Add lock to list of locks held by this backend */
|
|
|
|
held_lwlocks[num_held_lwlocks++] = lockid;
|
2006-07-24 18:32:45 +02:00
|
|
|
PG_TRACE2(lwlock__condacquire, lockid, mode);
|
2001-09-29 06:02:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return !mustwait;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LWLockRelease - release a previously acquired lock
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
LWLockRelease(LWLockId lockid)
|
|
|
|
{
|
2005-09-16 02:30:05 +02:00
|
|
|
volatile LWLock *lock = &(LWLockArray[lockid].lock);
|
2002-06-11 15:40:53 +02:00
|
|
|
PGPROC *head;
|
|
|
|
PGPROC *proc;
|
2001-10-25 07:50:21 +02:00
|
|
|
int i;
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
PRINT_LWDEBUG("LWLockRelease", lockid, lock);
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Remove lock from list of locks held. Usually, but not always, it will
|
|
|
|
* be the latest-acquired lock; so search array backwards.
|
2001-09-29 06:02:27 +02:00
|
|
|
*/
|
2001-10-25 07:50:21 +02:00
|
|
|
for (i = num_held_lwlocks; --i >= 0;)
|
2001-09-29 06:02:27 +02:00
|
|
|
{
|
|
|
|
if (lockid == held_lwlocks[i])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i < 0)
|
2003-07-25 00:04:15 +02:00
|
|
|
elog(ERROR, "lock %d is not held", (int) lockid);
|
2001-09-29 06:02:27 +02:00
|
|
|
num_held_lwlocks--;
|
|
|
|
for (; i < num_held_lwlocks; i++)
|
2001-10-25 07:50:21 +02:00
|
|
|
held_lwlocks[i] = held_lwlocks[i + 1];
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
/* Acquire mutex. Time spent holding mutex should be short! */
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockAcquire(&lock->mutex);
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
/* Release my hold on lock */
|
|
|
|
if (lock->exclusive > 0)
|
|
|
|
lock->exclusive--;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Assert(lock->shared > 0);
|
|
|
|
lock->shared--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* See if I need to awaken any waiters. If I released a non-last shared
|
|
|
|
* hold, there cannot be anything to do. Also, do not awaken any waiters
|
|
|
|
* if someone has already awakened waiters that haven't yet acquired the
|
|
|
|
* lock.
|
2001-09-29 06:02:27 +02:00
|
|
|
*/
|
|
|
|
head = lock->head;
|
|
|
|
if (head != NULL)
|
|
|
|
{
|
2002-01-07 17:33:00 +01:00
|
|
|
if (lock->exclusive == 0 && lock->shared == 0 && lock->releaseOK)
|
2001-09-29 06:02:27 +02:00
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Remove the to-be-awakened PGPROCs from the queue. If the front
|
|
|
|
* waiter wants exclusive lock, awaken him only. Otherwise awaken
|
|
|
|
* as many waiters as want shared access.
|
2001-09-29 06:02:27 +02:00
|
|
|
*/
|
|
|
|
proc = head;
|
2002-01-07 17:33:00 +01:00
|
|
|
if (!proc->lwExclusive)
|
2001-09-29 06:02:27 +02:00
|
|
|
{
|
|
|
|
while (proc->lwWaitLink != NULL &&
|
|
|
|
!proc->lwWaitLink->lwExclusive)
|
|
|
|
proc = proc->lwWaitLink;
|
|
|
|
}
|
2002-06-11 15:40:53 +02:00
|
|
|
/* proc is now the last PGPROC to be released */
|
2001-09-29 06:02:27 +02:00
|
|
|
lock->head = proc->lwWaitLink;
|
|
|
|
proc->lwWaitLink = NULL;
|
2002-01-07 17:33:00 +01:00
|
|
|
/* prevent additional wakeups until retryer gets to run */
|
|
|
|
lock->releaseOK = false;
|
2001-09-29 06:02:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* lock is still held, can't awaken anything */
|
|
|
|
head = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are done updating shared state of the lock itself. */
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockRelease(&lock->mutex);
|
2001-09-29 06:02:27 +02:00
|
|
|
|
2006-07-24 18:32:45 +02:00
|
|
|
PG_TRACE1(lwlock__release, lockid);
|
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
/*
|
|
|
|
* Awaken any waiters I removed from the queue.
|
|
|
|
*/
|
|
|
|
while (head != NULL)
|
|
|
|
{
|
2001-12-29 00:26:04 +01:00
|
|
|
LOG_LWDEBUG("LWLockRelease", lockid, "release waiter");
|
2001-09-29 06:02:27 +02:00
|
|
|
proc = head;
|
|
|
|
head = proc->lwWaitLink;
|
|
|
|
proc->lwWaitLink = NULL;
|
|
|
|
proc->lwWaiting = false;
|
2002-05-05 02:03:29 +02:00
|
|
|
PGSemaphoreUnlock(&proc->sem);
|
2001-09-29 06:02:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now okay to allow cancel/die interrupts.
|
|
|
|
*/
|
|
|
|
RESUME_INTERRUPTS();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LWLockReleaseAll - release all currently-held locks
|
|
|
|
*
|
2003-07-25 00:04:15 +02:00
|
|
|
* Used to clean up after ereport(ERROR). An important difference between this
|
2001-09-29 06:02:27 +02:00
|
|
|
* function and retail LWLockRelease calls is that InterruptHoldoffCount is
|
|
|
|
* unchanged by this operation. This is necessary since InterruptHoldoffCount
|
2003-07-25 00:04:15 +02:00
|
|
|
* has been set to an appropriate level earlier in error recovery. We could
|
2001-09-29 06:02:27 +02:00
|
|
|
* decrement it below zero if we allow it to drop for each released lock!
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
LWLockReleaseAll(void)
|
|
|
|
{
|
|
|
|
while (num_held_lwlocks > 0)
|
|
|
|
{
|
|
|
|
HOLD_INTERRUPTS(); /* match the upcoming RESUME_INTERRUPTS */
|
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
LWLockRelease(held_lwlocks[num_held_lwlocks - 1]);
|
2001-09-29 06:02:27 +02:00
|
|
|
}
|
|
|
|
}
|
2004-06-11 18:43:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LWLockHeldByMe - test whether my process currently holds a lock
|
|
|
|
*
|
|
|
|
* This is meant as debug support only. We do not distinguish whether the
|
|
|
|
* lock is held shared or exclusive.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
LWLockHeldByMe(LWLockId lockid)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
int i;
|
2004-06-11 18:43:24 +02:00
|
|
|
|
|
|
|
for (i = 0; i < num_held_lwlocks; i++)
|
|
|
|
{
|
|
|
|
if (held_lwlocks[i] == lockid)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|