mirror of
https://github.com/postgres/postgres.git
synced 2025-11-10 17:42:29 +03:00
Significant cleanups in SysV IPC handling (shared mem and semaphores).
IPC key assignment will now work correctly even when multiple postmasters are using same logical port number (which is possible given -k switch). There is only one shared-mem segment per postmaster now, not 3. Rip out broken code for non-TAS case in bufmgr and xlog, substitute a complete S_LOCK emulation using semaphores in spin.c. TAS and non-TAS logic is now exactly the same. When deadlock is detected, "Deadlock detected" is now the elog(ERROR) message, rather than a NOTICE that comes out before an unhelpful ERROR.
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -8,148 +8,91 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/ipci.c,v 1.34 2000/11/21 21:16:01 petere Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/ipci.c,v 1.35 2000/11/28 23:27:56 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "postgres.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "miscadmin.h"
|
||||
#include "access/xlog.h"
|
||||
#include "storage/bufmgr.h"
|
||||
#include "storage/proc.h"
|
||||
#include "storage/sinval.h"
|
||||
#include "storage/spin.h"
|
||||
|
||||
/*
|
||||
* SystemPortAddressCreateMemoryKey
|
||||
* Returns a memory key given a port address.
|
||||
*/
|
||||
IPCKey
|
||||
SystemPortAddressCreateIPCKey(SystemPortAddress address)
|
||||
{
|
||||
Assert(address < 32768); /* XXX */
|
||||
|
||||
return SystemPortAddressGetIPCKey(address);
|
||||
}
|
||||
|
||||
/*
|
||||
* CreateSharedMemoryAndSemaphores
|
||||
* Creates and initializes shared memory and semaphores.
|
||||
*
|
||||
* This is called by the postmaster or by a standalone backend.
|
||||
* It is NEVER called by a backend forked from the postmaster;
|
||||
* for such a backend, the shared memory is already ready-to-go.
|
||||
*
|
||||
* If "private" is true then we only need private memory, not shared
|
||||
* memory. This is true for a standalone backend, false for a postmaster.
|
||||
*/
|
||||
/**************************************************
|
||||
|
||||
CreateSharedMemoryAndSemaphores
|
||||
is called exactly *ONCE* by the postmaster.
|
||||
It is *NEVER* called by the postgres backend,
|
||||
except in the case of a standalone backend.
|
||||
|
||||
0) destroy any existing semaphores for both buffer
|
||||
and lock managers.
|
||||
1) create the appropriate *SHARED* memory segments
|
||||
for the two resource managers.
|
||||
2) create shared semaphores as needed.
|
||||
|
||||
**************************************************/
|
||||
|
||||
void
|
||||
CreateSharedMemoryAndSemaphores(IPCKey key, int maxBackends)
|
||||
CreateSharedMemoryAndSemaphores(bool private, int maxBackends)
|
||||
{
|
||||
int size;
|
||||
|
||||
#ifdef HAS_TEST_AND_SET
|
||||
PGShmemHeader *seghdr;
|
||||
|
||||
/*
|
||||
* Create shared memory for slocks
|
||||
*/
|
||||
CreateAndInitSLockMemory(IPCKeyGetSLockSharedMemoryKey(key));
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Kill and create the buffer manager buffer pool (and semaphore)
|
||||
*/
|
||||
CreateSpinlocks(IPCKeyGetSpinLockSemaphoreKey(key));
|
||||
|
||||
/*
|
||||
* Size of the primary shared-memory block is estimated via
|
||||
* Size of the Postgres shared-memory block is estimated via
|
||||
* moderately-accurate estimates for the big hogs, plus 100K for the
|
||||
* stuff that's too small to bother with estimating.
|
||||
*/
|
||||
size = BufferShmemSize() + LockShmemSize(maxBackends) + XLOGShmemSize();
|
||||
size = BufferShmemSize() + LockShmemSize(maxBackends) +
|
||||
XLOGShmemSize() + SLockShmemSize() + SInvalShmemSize(maxBackends);
|
||||
#ifdef STABLE_MEMORY_STORAGE
|
||||
size += MMShmemSize();
|
||||
#endif
|
||||
size += 100000;
|
||||
/* might as well round it off to a multiple of a K or so... */
|
||||
size += 1024 - (size % 1024);
|
||||
/* might as well round it off to a multiple of a typical page size */
|
||||
size += 8192 - (size % 8192);
|
||||
|
||||
if (DebugLvl > 1)
|
||||
{
|
||||
fprintf(stderr, "binding ShmemCreate(key=%x, size=%d)\n",
|
||||
IPCKeyGetBufferMemoryKey(key), size);
|
||||
}
|
||||
ShmemCreate(IPCKeyGetBufferMemoryKey(key), size);
|
||||
ShmemIndexReset();
|
||||
InitShmem(key, size);
|
||||
XLOGShmemInit();
|
||||
InitBufferPool(key);
|
||||
fprintf(stderr, "invoking IpcMemoryCreate(size=%d)\n", size);
|
||||
|
||||
/* ----------------
|
||||
* do the lock table stuff
|
||||
* ----------------
|
||||
/*
|
||||
* Create the shmem segment
|
||||
*/
|
||||
seghdr = IpcMemoryCreate(size, private, IPCProtection);
|
||||
|
||||
/*
|
||||
* First initialize spinlocks --- needed by InitShmemAllocation()
|
||||
*/
|
||||
CreateSpinlocks(seghdr);
|
||||
|
||||
/*
|
||||
* Set up shmem.c hashtable
|
||||
*/
|
||||
InitShmemAllocation(seghdr);
|
||||
|
||||
/*
|
||||
* Set up xlog and buffers
|
||||
*/
|
||||
XLOGShmemInit();
|
||||
InitBufferPool();
|
||||
|
||||
/*
|
||||
* Set up lock manager
|
||||
*/
|
||||
InitLocks();
|
||||
if (InitLockTable() == INVALID_TABLEID)
|
||||
elog(FATAL, "Couldn't create the lock table");
|
||||
|
||||
/* ----------------
|
||||
* do process table stuff
|
||||
* ----------------
|
||||
/*
|
||||
* Set up process table
|
||||
*/
|
||||
InitProcGlobal(key, maxBackends);
|
||||
InitProcGlobal(maxBackends);
|
||||
|
||||
CreateSharedInvalidationState(key, maxBackends);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* AttachSharedMemoryAndSemaphores
|
||||
* Attachs existant shared memory and semaphores.
|
||||
*/
|
||||
void
|
||||
AttachSharedMemoryAndSemaphores(IPCKey key)
|
||||
{
|
||||
/* ----------------
|
||||
* create rather than attach if using private key
|
||||
* ----------------
|
||||
*/
|
||||
if (key == PrivateIPCKey)
|
||||
{
|
||||
CreateSharedMemoryAndSemaphores(key, 16);
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef HAS_TEST_AND_SET
|
||||
/* ----------------
|
||||
* attach the slock shared memory
|
||||
* ----------------
|
||||
*/
|
||||
AttachSLockMemory(IPCKeyGetSLockSharedMemoryKey(key));
|
||||
#endif
|
||||
/* ----------------
|
||||
* attach the buffer manager buffer pool (and semaphore)
|
||||
* ----------------
|
||||
*/
|
||||
InitShmem(key, 0);
|
||||
InitBufferPool(key);
|
||||
|
||||
/* ----------------
|
||||
* initialize lock table stuff
|
||||
* ----------------
|
||||
*/
|
||||
InitLocks();
|
||||
if (InitLockTable() == INVALID_TABLEID)
|
||||
elog(FATAL, "Couldn't attach to the lock table");
|
||||
|
||||
AttachSharedInvalidationState(key);
|
||||
/*
|
||||
* Set up shared-inval messaging
|
||||
*/
|
||||
CreateSharedInvalidationState(maxBackends);
|
||||
}
|
||||
|
||||
@@ -8,14 +8,14 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmem.c,v 1.54 2000/11/21 21:16:01 petere Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmem.c,v 1.55 2000/11/28 23:27:56 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
/*
|
||||
* POSTGRES processes share one or more regions of shared memory.
|
||||
* The shared memory is created by a postmaster and is inherited
|
||||
* by each backends via fork(). The routines in this file are used for
|
||||
* by each backend via fork(). The routines in this file are used for
|
||||
* allocating and binding to shared memory data structures.
|
||||
*
|
||||
* NOTES:
|
||||
@@ -56,153 +56,57 @@
|
||||
*
|
||||
* See InitSem() in sem.c for an example of how to use the
|
||||
* shmem index.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "postgres.h"
|
||||
|
||||
#include "access/transam.h"
|
||||
#include "utils/tqual.h"
|
||||
|
||||
/* shared memory global variables */
|
||||
|
||||
unsigned long ShmemBase = 0; /* start and end address of shared memory */
|
||||
static unsigned long ShmemEnd = 0;
|
||||
static unsigned long ShmemSize = 0; /* current size (and default) */
|
||||
static PGShmemHeader *ShmemSegHdr; /* shared mem segment header */
|
||||
|
||||
SHMEM_OFFSET ShmemBase; /* start address of shared memory */
|
||||
|
||||
static SHMEM_OFFSET ShmemEnd; /* end+1 address of shared memory */
|
||||
|
||||
SPINLOCK ShmemLock; /* lock for shared memory allocation */
|
||||
|
||||
SPINLOCK ShmemIndexLock; /* lock for shmem index access */
|
||||
|
||||
static unsigned long *ShmemFreeStart = NULL; /* pointer to the OFFSET
|
||||
* of first free shared
|
||||
* memory */
|
||||
static unsigned long *ShmemIndexOffset = NULL; /* start of the shmem
|
||||
* index table (for
|
||||
* bootstrap) */
|
||||
static int ShmemBootstrap = FALSE; /* flag becomes true when shared
|
||||
* mem is created by POSTMASTER */
|
||||
static HTAB *ShmemIndex = NULL; /* primary index hashtable for shmem */
|
||||
|
||||
static HTAB *ShmemIndex = NULL;
|
||||
static bool ShmemBootstrap = false; /* bootstrapping shmem index? */
|
||||
|
||||
/* ---------------------
|
||||
* ShmemIndexReset() - Resets the shmem index to NULL....
|
||||
* useful when the postmaster destroys existing shared memory
|
||||
* and creates all new segments after a backend crash.
|
||||
* ----------------------
|
||||
*/
|
||||
void
|
||||
ShmemIndexReset(void)
|
||||
{
|
||||
ShmemIndex = (HTAB *) NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* CreateSharedRegion()
|
||||
*
|
||||
* This routine is called once by the postmaster to
|
||||
* initialize the shared buffer pool. Assume there is
|
||||
* only one postmaster so no synchronization is necessary
|
||||
* until after this routine completes successfully.
|
||||
*
|
||||
* key is a unique identifier for the shmem region.
|
||||
* size is the size of the region.
|
||||
* InitShmemAllocation() --- set up shared-memory allocation and index table.
|
||||
*/
|
||||
static IpcMemoryId ShmemId;
|
||||
|
||||
void
|
||||
ShmemCreate(unsigned int key, unsigned int size)
|
||||
InitShmemAllocation(PGShmemHeader *seghdr)
|
||||
{
|
||||
if (size)
|
||||
ShmemSize = size;
|
||||
/* create shared mem region */
|
||||
if ((ShmemId = IpcMemoryCreate(key, ShmemSize, IPCProtection))
|
||||
== IpcMemCreationFailed)
|
||||
{
|
||||
elog(FATAL, "ShmemCreate: cannot create region");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/*
|
||||
* ShmemBootstrap is true if shared memory has been created, but not
|
||||
* yet initialized. Only the postmaster/creator-of-all-things should
|
||||
* have this flag set.
|
||||
*/
|
||||
ShmemBootstrap = TRUE;
|
||||
}
|
||||
|
||||
/*
|
||||
* InitShmem() -- map region into process address space
|
||||
* and initialize shared data structures.
|
||||
*
|
||||
*/
|
||||
int
|
||||
InitShmem(unsigned int key, unsigned int size)
|
||||
{
|
||||
Pointer sharedRegion;
|
||||
unsigned long currFreeSpace;
|
||||
|
||||
HASHCTL info;
|
||||
int hash_flags;
|
||||
ShmemIndexEnt *result,
|
||||
item;
|
||||
bool found;
|
||||
IpcMemoryId shmid;
|
||||
|
||||
/* if zero key, use default memory size */
|
||||
if (size)
|
||||
ShmemSize = size;
|
||||
|
||||
/* default key is 0 */
|
||||
|
||||
/* attach to shared memory region (SysV or BSD OS specific) */
|
||||
if (ShmemBootstrap && key == PrivateIPCKey)
|
||||
/* if we are running backend alone */
|
||||
shmid = ShmemId;
|
||||
else
|
||||
shmid = IpcMemoryIdGet(IPCKeyGetBufferMemoryKey(key), ShmemSize);
|
||||
sharedRegion = IpcMemoryAttach(shmid);
|
||||
if (sharedRegion == NULL)
|
||||
{
|
||||
elog(FATAL, "AttachSharedRegion: couldn't attach to shmem\n");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* get pointers to the dimensions of shared memory */
|
||||
ShmemBase = (unsigned long) sharedRegion;
|
||||
ShmemEnd = (unsigned long) sharedRegion + ShmemSize;
|
||||
|
||||
/* First long in shared memory is the available-space pointer */
|
||||
ShmemFreeStart = (unsigned long *) ShmemBase;
|
||||
/* next is a shmem pointer to the shmem index */
|
||||
ShmemIndexOffset = ShmemFreeStart + 1;
|
||||
/* next is ShmemVariableCache */
|
||||
ShmemVariableCache = (VariableCache) (ShmemIndexOffset + 1);
|
||||
|
||||
/* here is where to start dynamic allocation */
|
||||
currFreeSpace = MAXALIGN(sizeof(*ShmemFreeStart) +
|
||||
sizeof(*ShmemIndexOffset) +
|
||||
sizeof(*ShmemVariableCache));
|
||||
/* Set up basic pointers to shared memory */
|
||||
ShmemSegHdr = seghdr;
|
||||
ShmemBase = (SHMEM_OFFSET) seghdr;
|
||||
ShmemEnd = ShmemBase + seghdr->totalsize;
|
||||
|
||||
/*
|
||||
* bootstrap initialize spin locks so we can start to use the
|
||||
* allocator and shmem index.
|
||||
* Since ShmemInitHash calls ShmemInitStruct, which expects the
|
||||
* ShmemIndex hashtable to exist already, we have a bit of a circularity
|
||||
* problem in initializing the ShmemIndex itself. We set ShmemBootstrap
|
||||
* to tell ShmemInitStruct to fake it.
|
||||
*/
|
||||
InitSpinLocks();
|
||||
ShmemIndex = (HTAB *) NULL;
|
||||
ShmemBootstrap = true;
|
||||
|
||||
/*
|
||||
* We have just allocated additional space for two spinlocks. Now
|
||||
* setup the global free space count
|
||||
*/
|
||||
if (ShmemBootstrap)
|
||||
{
|
||||
*ShmemFreeStart = currFreeSpace;
|
||||
memset(ShmemVariableCache, 0, sizeof(*ShmemVariableCache));
|
||||
}
|
||||
|
||||
/* if ShmemFreeStart is NULL, then the allocator won't work */
|
||||
Assert(*ShmemFreeStart);
|
||||
|
||||
/* create OR attach to the shared memory shmem index */
|
||||
/* create the shared memory shmem index */
|
||||
info.keysize = SHMEM_INDEX_KEYSIZE;
|
||||
info.datasize = SHMEM_INDEX_DATASIZE;
|
||||
hash_flags = HASH_ELEM;
|
||||
@@ -211,60 +115,43 @@ InitShmem(unsigned int key, unsigned int size)
|
||||
ShmemIndex = ShmemInitHash("ShmemIndex",
|
||||
SHMEM_INDEX_SIZE, SHMEM_INDEX_SIZE,
|
||||
&info, hash_flags);
|
||||
|
||||
if (!ShmemIndex)
|
||||
{
|
||||
elog(FATAL, "InitShmem: couldn't initialize Shmem Index");
|
||||
return FALSE;
|
||||
}
|
||||
elog(FATAL, "InitShmemAllocation: couldn't initialize Shmem Index");
|
||||
|
||||
/*
|
||||
* Now, check the shmem index for an entry to the shmem index. If
|
||||
* there is an entry there, someone else created the table. Otherwise,
|
||||
* we did and we have to initialize it.
|
||||
* Now, create an entry in the hashtable for the index itself.
|
||||
*/
|
||||
MemSet(item.key, 0, SHMEM_INDEX_KEYSIZE);
|
||||
strncpy(item.key, "ShmemIndex", SHMEM_INDEX_KEYSIZE);
|
||||
|
||||
result = (ShmemIndexEnt *)
|
||||
hash_search(ShmemIndex, (char *) &item, HASH_ENTER, &found);
|
||||
|
||||
|
||||
if (!result)
|
||||
{
|
||||
elog(FATAL, "InitShmem: corrupted shmem index");
|
||||
return FALSE;
|
||||
}
|
||||
elog(FATAL, "InitShmemAllocation: corrupted shmem index");
|
||||
|
||||
if (!found)
|
||||
{
|
||||
Assert(ShmemBootstrap && !found);
|
||||
|
||||
/*
|
||||
* bootstrapping shmem: we have to initialize the shmem index now.
|
||||
*/
|
||||
result->location = MAKE_OFFSET(ShmemIndex->hctl);
|
||||
result->size = SHMEM_INDEX_SIZE;
|
||||
|
||||
Assert(ShmemBootstrap);
|
||||
result->location = MAKE_OFFSET(ShmemIndex->hctl);
|
||||
*ShmemIndexOffset = result->location;
|
||||
result->size = SHMEM_INDEX_SIZE;
|
||||
ShmemBootstrap = false;
|
||||
|
||||
ShmemBootstrap = FALSE;
|
||||
|
||||
}
|
||||
else
|
||||
Assert(!ShmemBootstrap);
|
||||
/* now release the lock acquired in ShmemHashInit */
|
||||
/* now release the lock acquired in ShmemInitStruct */
|
||||
SpinRelease(ShmemIndexLock);
|
||||
|
||||
Assert(result->location == MAKE_OFFSET(ShmemIndex->hctl));
|
||||
|
||||
return TRUE;
|
||||
/*
|
||||
* Initialize ShmemVariableCache for transaction manager.
|
||||
*/
|
||||
ShmemVariableCache = (VariableCache)
|
||||
ShmemAlloc(sizeof(*ShmemVariableCache));
|
||||
memset(ShmemVariableCache, 0, sizeof(*ShmemVariableCache));
|
||||
}
|
||||
|
||||
/*
|
||||
* ShmemAlloc -- allocate max-aligned byte string from shared memory
|
||||
* ShmemAlloc -- allocate max-aligned chunk from shared memory
|
||||
*
|
||||
* Assumes ShmemLock and ShmemSegHdr are initialized.
|
||||
*
|
||||
* Assumes ShmemLock and ShmemFreeStart are initialized.
|
||||
* Returns: real pointer to memory or NULL if we are out
|
||||
* of space. Has to return a real pointer in order
|
||||
* to be compatible with malloc().
|
||||
@@ -272,7 +159,7 @@ InitShmem(unsigned int key, unsigned int size)
|
||||
void *
|
||||
ShmemAlloc(Size size)
|
||||
{
|
||||
unsigned long tmpFree;
|
||||
uint32 newFree;
|
||||
void *newSpace;
|
||||
|
||||
/*
|
||||
@@ -280,15 +167,15 @@ ShmemAlloc(Size size)
|
||||
*/
|
||||
size = MAXALIGN(size);
|
||||
|
||||
Assert(*ShmemFreeStart);
|
||||
Assert(ShmemSegHdr);
|
||||
|
||||
SpinAcquire(ShmemLock);
|
||||
|
||||
tmpFree = *ShmemFreeStart + size;
|
||||
if (tmpFree <= ShmemSize)
|
||||
newFree = ShmemSegHdr->freeoffset + size;
|
||||
if (newFree <= ShmemSegHdr->totalsize)
|
||||
{
|
||||
newSpace = (void *) MAKE_PTR(*ShmemFreeStart);
|
||||
*ShmemFreeStart += size;
|
||||
newSpace = (void *) MAKE_PTR(ShmemSegHdr->freeoffset);
|
||||
ShmemSegHdr->freeoffset = newFree;
|
||||
}
|
||||
else
|
||||
newSpace = NULL;
|
||||
@@ -306,7 +193,7 @@ ShmemAlloc(Size size)
|
||||
*
|
||||
* Returns TRUE if the pointer is valid.
|
||||
*/
|
||||
int
|
||||
bool
|
||||
ShmemIsValid(unsigned long addr)
|
||||
{
|
||||
return (addr < ShmemEnd) && (addr >= ShmemBase);
|
||||
@@ -394,16 +281,15 @@ ShmemPIDLookup(int pid, SHMEM_OFFSET *locationPtr)
|
||||
sprintf(item.key, "PID %d", pid);
|
||||
|
||||
SpinAcquire(ShmemIndexLock);
|
||||
|
||||
result = (ShmemIndexEnt *)
|
||||
hash_search(ShmemIndex, (char *) &item, HASH_ENTER, &found);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
|
||||
SpinRelease(ShmemIndexLock);
|
||||
elog(ERROR, "ShmemInitPID: ShmemIndex corrupted");
|
||||
return FALSE;
|
||||
|
||||
}
|
||||
|
||||
if (found)
|
||||
@@ -438,19 +324,19 @@ ShmemPIDDestroy(int pid)
|
||||
sprintf(item.key, "PID %d", pid);
|
||||
|
||||
SpinAcquire(ShmemIndexLock);
|
||||
|
||||
result = (ShmemIndexEnt *)
|
||||
hash_search(ShmemIndex, (char *) &item, HASH_REMOVE, &found);
|
||||
|
||||
if (found)
|
||||
location = result->location;
|
||||
|
||||
SpinRelease(ShmemIndexLock);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
|
||||
elog(ERROR, "ShmemPIDDestroy: PID table corrupted");
|
||||
return INVALID_OFFSET;
|
||||
|
||||
}
|
||||
|
||||
if (found)
|
||||
@@ -487,53 +373,31 @@ ShmemInitStruct(char *name, Size size, bool *foundPtr)
|
||||
|
||||
if (!ShmemIndex)
|
||||
{
|
||||
#ifdef USE_ASSERT_CHECKING
|
||||
char *strname = "ShmemIndex";
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* If the shmem index doesn't exist, we fake it.
|
||||
* If the shmem index doesn't exist, we are bootstrapping: we must
|
||||
* be trying to init the shmem index itself.
|
||||
*
|
||||
* If we are creating the first shmem index, then let shmemalloc()
|
||||
* allocate the space for a new HTAB. Otherwise, find the old one
|
||||
* and return that. Notice that the ShmemIndexLock is held until
|
||||
* the shmem index has been completely initialized.
|
||||
* Notice that the ShmemIndexLock is held until the shmem index has
|
||||
* been completely initialized.
|
||||
*/
|
||||
Assert(strcmp(name, strname) == 0);
|
||||
if (ShmemBootstrap)
|
||||
{
|
||||
/* in POSTMASTER/Single process */
|
||||
|
||||
*foundPtr = FALSE;
|
||||
return ShmemAlloc(size);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert(*ShmemIndexOffset);
|
||||
|
||||
*foundPtr = TRUE;
|
||||
return (void *) MAKE_PTR(*ShmemIndexOffset);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
/* look it up in the shmem index */
|
||||
result = (ShmemIndexEnt *)
|
||||
hash_search(ShmemIndex, (char *) &item, HASH_ENTER, foundPtr);
|
||||
Assert(strcmp(name, "ShmemIndex") == 0);
|
||||
Assert(ShmemBootstrap);
|
||||
*foundPtr = FALSE;
|
||||
return ShmemAlloc(size);
|
||||
}
|
||||
|
||||
/* look it up in the shmem index */
|
||||
result = (ShmemIndexEnt *)
|
||||
hash_search(ShmemIndex, (char *) &item, HASH_ENTER, foundPtr);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
SpinRelease(ShmemIndexLock);
|
||||
|
||||
elog(ERROR, "ShmemInitStruct: Shmem Index corrupted");
|
||||
return NULL;
|
||||
|
||||
}
|
||||
else if (*foundPtr)
|
||||
|
||||
if (*foundPtr)
|
||||
{
|
||||
|
||||
/*
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/sinval.c,v 1.23 2000/11/12 20:51:51 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/sinval.c,v 1.24 2000/11/28 23:27:56 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -27,52 +27,23 @@
|
||||
SPINLOCK SInvalLock = (SPINLOCK) NULL;
|
||||
|
||||
/****************************************************************************/
|
||||
/* CreateSharedInvalidationState() Create a buffer segment */
|
||||
/* CreateSharedInvalidationState() Initialize SI buffer */
|
||||
/* */
|
||||
/* should be called only by the POSTMASTER */
|
||||
/****************************************************************************/
|
||||
void
|
||||
CreateSharedInvalidationState(IPCKey key, int maxBackends)
|
||||
CreateSharedInvalidationState(int maxBackends)
|
||||
{
|
||||
int status;
|
||||
|
||||
/* SInvalLock gets set in spin.c, during spinlock init */
|
||||
status = SISegmentInit(true, IPCKeyGetSIBufferMemoryBlock(key),
|
||||
maxBackends);
|
||||
|
||||
if (status == -1)
|
||||
elog(FATAL, "CreateSharedInvalidationState: failed segment init");
|
||||
}
|
||||
|
||||
/****************************************************************************/
|
||||
/* AttachSharedInvalidationState(key) Attach to existing buffer segment */
|
||||
/* */
|
||||
/* should be called by each backend during startup */
|
||||
/****************************************************************************/
|
||||
void
|
||||
AttachSharedInvalidationState(IPCKey key)
|
||||
{
|
||||
int status;
|
||||
|
||||
if (key == PrivateIPCKey)
|
||||
{
|
||||
CreateSharedInvalidationState(key, 16);
|
||||
return;
|
||||
}
|
||||
/* SInvalLock gets set in spin.c, during spinlock init */
|
||||
status = SISegmentInit(false, IPCKeyGetSIBufferMemoryBlock(key), 0);
|
||||
|
||||
if (status == -1)
|
||||
elog(FATAL, "AttachSharedInvalidationState: failed segment init");
|
||||
/* SInvalLock must be initialized already, during spinlock init */
|
||||
SIBufferInit(maxBackends);
|
||||
}
|
||||
|
||||
/*
|
||||
* InitSharedInvalidationState
|
||||
* InitBackendSharedInvalidationState
|
||||
* Initialize new backend's state info in buffer segment.
|
||||
* Must be called after AttachSharedInvalidationState().
|
||||
*/
|
||||
void
|
||||
InitSharedInvalidationState(void)
|
||||
InitBackendSharedInvalidationState(void)
|
||||
{
|
||||
SpinAcquire(SInvalLock);
|
||||
if (!SIBackendInit(shmInvalBuffer))
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/sinvaladt.c,v 1.35 2000/11/12 20:51:51 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/sinvaladt.c,v 1.36 2000/11/28 23:27:56 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -25,95 +25,38 @@
|
||||
|
||||
SISeg *shmInvalBuffer;
|
||||
|
||||
static void SISegmentAttach(IpcMemoryId shmid);
|
||||
static void SISegInit(SISeg *segP, int maxBackends);
|
||||
static void CleanupInvalidationState(int status, Datum arg);
|
||||
static void SISetProcStateInvalid(SISeg *segP);
|
||||
|
||||
|
||||
/*
|
||||
* SISegmentInit
|
||||
* Create a new SI memory segment, or attach to an existing one
|
||||
*
|
||||
* This is called with createNewSegment = true by the postmaster (or by
|
||||
* a standalone backend), and subsequently with createNewSegment = false
|
||||
* by backends started by the postmaster.
|
||||
*
|
||||
* Note: maxBackends param is only valid when createNewSegment is true
|
||||
* SInvalShmemSize --- return shared-memory space needed
|
||||
*/
|
||||
int
|
||||
SISegmentInit(bool createNewSegment, IPCKey key, int maxBackends)
|
||||
SInvalShmemSize(int maxBackends)
|
||||
{
|
||||
/*
|
||||
* Figure space needed. Note sizeof(SISeg) includes the first
|
||||
* ProcState entry.
|
||||
*/
|
||||
return sizeof(SISeg) + sizeof(ProcState) * (maxBackends - 1);
|
||||
}
|
||||
|
||||
/*
|
||||
* SIBufferInit
|
||||
* Create and initialize a new SI message buffer
|
||||
*/
|
||||
void
|
||||
SIBufferInit(int maxBackends)
|
||||
{
|
||||
int segSize;
|
||||
IpcMemoryId shmId;
|
||||
|
||||
if (createNewSegment)
|
||||
{
|
||||
/* Kill existing segment, if any */
|
||||
IpcMemoryKill(key);
|
||||
|
||||
/*
|
||||
* Figure space needed. Note sizeof(SISeg) includes the first
|
||||
* ProcState entry.
|
||||
*/
|
||||
segSize = sizeof(SISeg) + sizeof(ProcState) * (maxBackends - 1);
|
||||
|
||||
/* Get a shared segment */
|
||||
shmId = IpcMemoryCreate(key, segSize, IPCProtection);
|
||||
if (shmId < 0)
|
||||
{
|
||||
perror("SISegmentInit: segment create failed");
|
||||
return -1; /* an error */
|
||||
}
|
||||
|
||||
/* Attach to the shared cache invalidation segment */
|
||||
/* sets the global variable shmInvalBuffer */
|
||||
SISegmentAttach(shmId);
|
||||
|
||||
/* Init shared memory contents */
|
||||
SISegInit(shmInvalBuffer, maxBackends);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* find existing segment */
|
||||
shmId = IpcMemoryIdGet(key, 0);
|
||||
if (shmId < 0)
|
||||
{
|
||||
perror("SISegmentInit: segment get failed");
|
||||
return -1; /* an error */
|
||||
}
|
||||
|
||||
/* Attach to the shared cache invalidation segment */
|
||||
/* sets the global variable shmInvalBuffer */
|
||||
SISegmentAttach(shmId);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* SISegmentAttach
|
||||
* Attach to specified shared memory segment
|
||||
*/
|
||||
static void
|
||||
SISegmentAttach(IpcMemoryId shmid)
|
||||
{
|
||||
shmInvalBuffer = (SISeg *) IpcMemoryAttach(shmid);
|
||||
|
||||
if (shmInvalBuffer == IpcMemAttachFailed)
|
||||
{
|
||||
/* XXX use validity function */
|
||||
elog(FATAL, "SISegmentAttach: Could not attach segment: %m");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* SISegInit
|
||||
* Initialize contents of a new shared memory sinval segment
|
||||
*/
|
||||
static void
|
||||
SISegInit(SISeg *segP, int maxBackends)
|
||||
{
|
||||
SISeg *segP;
|
||||
int i;
|
||||
|
||||
/* Allocate space in shared memory */
|
||||
segSize = SInvalShmemSize(maxBackends);
|
||||
shmInvalBuffer = segP = (SISeg *) ShmemAlloc(segSize);
|
||||
|
||||
/* Clear message counters, save size of procState array */
|
||||
segP->minMsgNum = 0;
|
||||
segP->maxMsgNum = 0;
|
||||
|
||||
@@ -3,31 +3,24 @@
|
||||
* spin.c
|
||||
* routines for managing spin locks
|
||||
*
|
||||
* POSTGRES has two kinds of locks: semaphores (which put the
|
||||
* process to sleep) and spinlocks (which are supposed to be
|
||||
* short term locks). Spinlocks are implemented via test-and-set (TAS)
|
||||
* instructions if possible, else via semaphores. The semaphore method
|
||||
* is too slow to be useful :-(
|
||||
*
|
||||
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/Attic/spin.c,v 1.25 2000/05/31 00:28:29 petere Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/Attic/spin.c,v 1.26 2000/11/28 23:27:56 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
/*
|
||||
* POSTGRES has two kinds of locks: semaphores (which put the
|
||||
* process to sleep) and spinlocks (which are supposed to be
|
||||
* short term locks). Currently both are implemented as SysV
|
||||
* semaphores, but presumably this can change if we move to
|
||||
* a machine with a test-and-set (TAS) instruction. Its probably
|
||||
* a good idea to think about (and allocate) short term and long
|
||||
* term semaphores separately anyway.
|
||||
*
|
||||
* NOTE: These routines are not supposed to be widely used in Postgres.
|
||||
* They are preserved solely for the purpose of porting Mark Sullivan's
|
||||
* buffer manager to Postgres.
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include "postgres.h"
|
||||
|
||||
#include <errno.h>
|
||||
#ifndef HAS_TEST_AND_SET
|
||||
#include <sys/sem.h>
|
||||
#endif
|
||||
@@ -35,39 +28,33 @@
|
||||
#include "storage/proc.h"
|
||||
#include "storage/s_lock.h"
|
||||
|
||||
|
||||
/* globals used in this file */
|
||||
IpcSemaphoreId SpinLockId;
|
||||
|
||||
#ifdef HAS_TEST_AND_SET
|
||||
/* real spin lock implementations */
|
||||
|
||||
void
|
||||
CreateSpinlocks(IPCKey key)
|
||||
{
|
||||
/* the spin lock shared memory must have been created by now */
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
InitSpinLocks(void)
|
||||
{
|
||||
extern SPINLOCK ShmemLock;
|
||||
extern SPINLOCK ShmemIndexLock;
|
||||
extern SPINLOCK BufMgrLock;
|
||||
extern SPINLOCK LockMgrLock;
|
||||
extern SPINLOCK ProcStructLock;
|
||||
extern SPINLOCK SInvalLock;
|
||||
extern SPINLOCK OidGenLockId;
|
||||
extern SPINLOCK XidGenLockId;
|
||||
extern SPINLOCK ControlFileLockId;
|
||||
/* Probably should move these to an appropriate header file */
|
||||
extern SPINLOCK ShmemLock;
|
||||
extern SPINLOCK ShmemIndexLock;
|
||||
extern SPINLOCK BufMgrLock;
|
||||
extern SPINLOCK LockMgrLock;
|
||||
extern SPINLOCK ProcStructLock;
|
||||
extern SPINLOCK SInvalLock;
|
||||
extern SPINLOCK OidGenLockId;
|
||||
extern SPINLOCK XidGenLockId;
|
||||
extern SPINLOCK ControlFileLockId;
|
||||
|
||||
#ifdef STABLE_MEMORY_STORAGE
|
||||
extern SPINLOCK MMCacheLock;
|
||||
extern SPINLOCK MMCacheLock;
|
||||
|
||||
#endif
|
||||
|
||||
/* These six spinlocks have fixed location is shmem */
|
||||
|
||||
/*
|
||||
* Initialize identifiers for permanent spinlocks during startup
|
||||
*
|
||||
* The same identifiers are used for both TAS and semaphore implementations,
|
||||
* although in one case they are indexes into a shmem array and in the other
|
||||
* they are semaphore numbers.
|
||||
*/
|
||||
static void
|
||||
InitSpinLockIDs(void)
|
||||
{
|
||||
ShmemLock = (SPINLOCK) SHMEMLOCKID;
|
||||
ShmemIndexLock = (SPINLOCK) SHMEMINDEXLOCKID;
|
||||
BufMgrLock = (SPINLOCK) BUFMGRLOCKID;
|
||||
@@ -81,11 +68,18 @@ InitSpinLocks(void)
|
||||
#ifdef STABLE_MEMORY_STORAGE
|
||||
MMCacheLock = (SPINLOCK) MMCACHELOCKID;
|
||||
#endif
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
#ifdef HAS_TEST_AND_SET
|
||||
|
||||
/* real spin lock implementation */
|
||||
|
||||
typedef struct slock
|
||||
{
|
||||
slock_t shlock;
|
||||
} SLock;
|
||||
|
||||
#ifdef LOCK_DEBUG
|
||||
bool Trace_spinlocks = false;
|
||||
|
||||
@@ -93,193 +87,268 @@ inline static void
|
||||
PRINT_SLDEBUG(const char * where, SPINLOCK lockid, const SLock * lock)
|
||||
{
|
||||
if (Trace_spinlocks)
|
||||
elog(DEBUG,
|
||||
"%s: id=%d (locklock=%d, flag=%d, nshlocks=%d, shlock=%d, exlock=%d)",
|
||||
where, lockid,
|
||||
lock->locklock, lock->flag, lock->nshlocks, lock->shlock, lock->exlock);
|
||||
elog(DEBUG, "%s: id=%d", where, lockid);
|
||||
}
|
||||
#else /* not LOCK_DEBUG */
|
||||
#define PRINT_SLDEBUG(a,b,c)
|
||||
#endif /* not LOCK_DEBUG */
|
||||
|
||||
|
||||
/* from ipc.c */
|
||||
extern SLock *SLockArray;
|
||||
static SLock *SLockArray = NULL;
|
||||
|
||||
#define SLOCKMEMORYSIZE ((int) MAX_SPINS * sizeof(SLock))
|
||||
|
||||
/*
|
||||
* SLockShmemSize --- return shared-memory space needed
|
||||
*/
|
||||
int
|
||||
SLockShmemSize(void)
|
||||
{
|
||||
return MAXALIGN(SLOCKMEMORYSIZE);
|
||||
}
|
||||
|
||||
/*
|
||||
* CreateSpinlocks --- create and initialize spinlocks during startup
|
||||
*/
|
||||
void
|
||||
CreateSpinlocks(PGShmemHeader *seghdr)
|
||||
{
|
||||
int id;
|
||||
|
||||
/*
|
||||
* We must allocate the space "by hand" because shmem.c isn't up yet
|
||||
*/
|
||||
SLockArray = (SLock *) (((char *) seghdr) + seghdr->freeoffset);
|
||||
seghdr->freeoffset += MAXALIGN(SLOCKMEMORYSIZE);
|
||||
Assert(seghdr->freeoffset <= seghdr->totalsize);
|
||||
|
||||
/*
|
||||
* Initialize all spinlocks to "unlocked" state
|
||||
*/
|
||||
for (id = 0; id < (int) MAX_SPINS; id++)
|
||||
{
|
||||
SLock *slckP = &(SLockArray[id]);
|
||||
|
||||
S_INIT_LOCK(&(slckP->shlock));
|
||||
}
|
||||
|
||||
/*
|
||||
* Assign indexes for fixed spinlocks
|
||||
*/
|
||||
InitSpinLockIDs();
|
||||
}
|
||||
|
||||
void
|
||||
SpinAcquire(SPINLOCK lockid)
|
||||
{
|
||||
SLock *slckP;
|
||||
SLock *slckP = &(SLockArray[lockid]);
|
||||
|
||||
/* This used to be in ipc.c, but move here to reduce function calls */
|
||||
slckP = &(SLockArray[lockid]);
|
||||
PRINT_SLDEBUG("SpinAcquire", lockid, slckP);
|
||||
ex_try_again:
|
||||
S_LOCK(&(slckP->locklock));
|
||||
switch (slckP->flag)
|
||||
{
|
||||
case NOLOCK:
|
||||
slckP->flag = EXCLUSIVELOCK;
|
||||
S_LOCK(&(slckP->exlock));
|
||||
S_LOCK(&(slckP->shlock));
|
||||
S_UNLOCK(&(slckP->locklock));
|
||||
PRINT_SLDEBUG("OUT", lockid, slckP);
|
||||
break;
|
||||
case SHAREDLOCK:
|
||||
case EXCLUSIVELOCK:
|
||||
S_UNLOCK(&(slckP->locklock));
|
||||
S_LOCK(&(slckP->exlock));
|
||||
S_UNLOCK(&(slckP->exlock));
|
||||
goto ex_try_again;
|
||||
}
|
||||
S_LOCK(&(slckP->shlock));
|
||||
PROC_INCR_SLOCK(lockid);
|
||||
PRINT_SLDEBUG("SpinAcquire/success", lockid, slckP);
|
||||
PRINT_SLDEBUG("SpinAcquire/done", lockid, slckP);
|
||||
}
|
||||
|
||||
void
|
||||
SpinRelease(SPINLOCK lockid)
|
||||
{
|
||||
SLock *slckP;
|
||||
|
||||
/* This used to be in ipc.c, but move here to reduce function calls */
|
||||
slckP = &(SLockArray[lockid]);
|
||||
SLock *slckP = &(SLockArray[lockid]);
|
||||
|
||||
/*
|
||||
* Check that we are actually holding the lock we are releasing. This
|
||||
* can be done only after MyProc has been initialized.
|
||||
*/
|
||||
Assert(!MyProc || MyProc->sLocks[lockid] > 0);
|
||||
Assert(slckP->flag != NOLOCK);
|
||||
|
||||
|
||||
PROC_DECR_SLOCK(lockid);
|
||||
PRINT_SLDEBUG("SpinRelease", lockid, slckP);
|
||||
S_LOCK(&(slckP->locklock));
|
||||
/* -------------
|
||||
* give favor to read processes
|
||||
* -------------
|
||||
*/
|
||||
slckP->flag = NOLOCK;
|
||||
if (slckP->nshlocks > 0)
|
||||
{
|
||||
while (slckP->nshlocks > 0)
|
||||
{
|
||||
S_UNLOCK(&(slckP->shlock));
|
||||
S_LOCK(&(slckP->comlock));
|
||||
}
|
||||
S_UNLOCK(&(slckP->shlock));
|
||||
}
|
||||
else
|
||||
S_UNLOCK(&(slckP->shlock));
|
||||
S_UNLOCK(&(slckP->exlock));
|
||||
S_UNLOCK(&(slckP->locklock));
|
||||
PRINT_SLDEBUG("SpinRelease/released", lockid, slckP);
|
||||
S_UNLOCK(&(slckP->shlock));
|
||||
PRINT_SLDEBUG("SpinRelease/done", lockid, slckP);
|
||||
}
|
||||
|
||||
#else /* !HAS_TEST_AND_SET */
|
||||
/* Spinlocks are implemented using SysV semaphores */
|
||||
|
||||
static bool AttachSpinLocks(IPCKey key);
|
||||
static bool SpinIsLocked(SPINLOCK lock);
|
||||
|
||||
/*
|
||||
* SpinAcquire -- try to grab a spinlock
|
||||
* No TAS, so spinlocks are implemented using SysV semaphores.
|
||||
*
|
||||
* We support two slightly different APIs here: SpinAcquire/SpinRelease
|
||||
* work with SPINLOCK integer indexes for the permanent spinlocks, which
|
||||
* are all assumed to live in the first spinlock semaphore set. There
|
||||
* is also an emulation of the s_lock.h TAS-spinlock macros; for that case,
|
||||
* typedef slock_t stores the semId and sem number of the sema to use.
|
||||
* The semas needed are created by CreateSpinlocks and doled out by
|
||||
* s_init_lock_sema.
|
||||
*
|
||||
* Since many systems have a rather small SEMMSL limit on semas per set,
|
||||
* we allocate the semaphores required in sets of SPINLOCKS_PER_SET semas.
|
||||
* This value is deliberately made equal to PROC_NSEMS_PER_SET so that all
|
||||
* sema sets allocated by Postgres will be the same size; that eases the
|
||||
* semaphore-recycling logic in IpcSemaphoreCreate().
|
||||
*
|
||||
* Note that the SpinLockIds array is not in shared memory; it is filled
|
||||
* by the postmaster and then inherited through fork() by backends. This
|
||||
* is OK because its contents do not change after system startup.
|
||||
*/
|
||||
|
||||
#define SPINLOCKS_PER_SET PROC_NSEMS_PER_SET
|
||||
|
||||
static IpcSemaphoreId *SpinLockIds = NULL;
|
||||
|
||||
static int numSpinSets = 0; /* number of sema sets used */
|
||||
static int numSpinLocks = 0; /* total number of semas allocated */
|
||||
static int nextSpinLock = 0; /* next free spinlock index */
|
||||
|
||||
static void SpinFreeAllSemaphores(void);
|
||||
|
||||
/*
|
||||
* SLockShmemSize --- return shared-memory space needed
|
||||
*/
|
||||
int
|
||||
SLockShmemSize(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* CreateSpinlocks --- create and initialize spinlocks during startup
|
||||
*/
|
||||
void
|
||||
CreateSpinlocks(PGShmemHeader *seghdr)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (SpinLockIds == NULL)
|
||||
{
|
||||
/*
|
||||
* Compute number of spinlocks needed. If this logic gets any more
|
||||
* complicated, it should be distributed into the affected modules,
|
||||
* similar to the way shmem space estimation is handled.
|
||||
*
|
||||
* For now, though, we just need the fixed spinlocks (MAX_SPINS),
|
||||
* two spinlocks per shared disk buffer, and four spinlocks for XLOG.
|
||||
*/
|
||||
numSpinLocks = (int) MAX_SPINS + 2 * NBuffers + 4;
|
||||
|
||||
/* might as well round up to a multiple of SPINLOCKS_PER_SET */
|
||||
numSpinSets = (numSpinLocks - 1) / SPINLOCKS_PER_SET + 1;
|
||||
numSpinLocks = numSpinSets * SPINLOCKS_PER_SET;
|
||||
|
||||
SpinLockIds = (IpcSemaphoreId *)
|
||||
malloc(numSpinSets * sizeof(IpcSemaphoreId));
|
||||
Assert(SpinLockIds != NULL);
|
||||
}
|
||||
|
||||
for (i = 0; i < numSpinSets; i++)
|
||||
SpinLockIds[i] = -1;
|
||||
|
||||
/*
|
||||
* Arrange to delete semas on exit --- set this up now so that we
|
||||
* will clean up if 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.
|
||||
*/
|
||||
on_shmem_exit(SpinFreeAllSemaphores, 0);
|
||||
|
||||
/* Create sema sets and set all semas to count 1 */
|
||||
for (i = 0; i < numSpinSets; i++)
|
||||
{
|
||||
SpinLockIds[i] = IpcSemaphoreCreate(SPINLOCKS_PER_SET,
|
||||
IPCProtection,
|
||||
1,
|
||||
false);
|
||||
}
|
||||
|
||||
/*
|
||||
* Assign indexes for fixed spinlocks
|
||||
*/
|
||||
Assert(MAX_SPINS <= SPINLOCKS_PER_SET);
|
||||
InitSpinLockIDs();
|
||||
|
||||
/* Init counter for allocating dynamic spinlocks */
|
||||
nextSpinLock = MAX_SPINS;
|
||||
}
|
||||
|
||||
/*
|
||||
* SpinFreeAllSemaphores -
|
||||
* 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 for spinlocks.
|
||||
*/
|
||||
static void
|
||||
SpinFreeAllSemaphores(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < numSpinSets; i++)
|
||||
{
|
||||
if (SpinLockIds[i] >= 0)
|
||||
IpcSemaphoreKill(SpinLockIds[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* SpinAcquire -- grab a fixed spinlock
|
||||
*
|
||||
* FAILS if the semaphore is corrupted.
|
||||
*/
|
||||
void
|
||||
SpinAcquire(SPINLOCK lock)
|
||||
{
|
||||
IpcSemaphoreLock(SpinLockId, lock, IpcExclusiveLock);
|
||||
IpcSemaphoreLock(SpinLockIds[0], lock);
|
||||
PROC_INCR_SLOCK(lock);
|
||||
}
|
||||
|
||||
/*
|
||||
* SpinRelease -- release a spin lock
|
||||
* SpinRelease -- release a fixed spin lock
|
||||
*
|
||||
* FAILS if the semaphore is corrupted
|
||||
*/
|
||||
void
|
||||
SpinRelease(SPINLOCK lock)
|
||||
{
|
||||
Assert(SpinIsLocked(lock));
|
||||
PROC_DECR_SLOCK(lock);
|
||||
IpcSemaphoreUnlock(SpinLockId, lock, IpcExclusiveLock);
|
||||
}
|
||||
|
||||
static bool
|
||||
SpinIsLocked(SPINLOCK lock)
|
||||
{
|
||||
#ifdef USE_ASSERT_CHECKING
|
||||
/* Check it's locked */
|
||||
int semval;
|
||||
|
||||
semval = IpcSemaphoreGetValue(SpinLockId, lock);
|
||||
return semval < IpcSemaphoreDefaultStartValue;
|
||||
semval = IpcSemaphoreGetValue(SpinLockIds[0], lock);
|
||||
Assert(semval < 1);
|
||||
#endif
|
||||
PROC_DECR_SLOCK(lock);
|
||||
IpcSemaphoreUnlock(SpinLockIds[0], lock);
|
||||
}
|
||||
|
||||
/*
|
||||
* CreateSpinlocks -- Create a sysV semaphore array for
|
||||
* the spinlocks
|
||||
*
|
||||
* s_lock.h hardware-spinlock emulation
|
||||
*/
|
||||
|
||||
void
|
||||
CreateSpinlocks(IPCKey key)
|
||||
s_init_lock_sema(volatile slock_t *lock)
|
||||
{
|
||||
|
||||
SpinLockId = IpcSemaphoreCreate(key, MAX_SPINS, IPCProtection,
|
||||
IpcSemaphoreDefaultStartValue, 1);
|
||||
|
||||
if (SpinLockId <= 0)
|
||||
elog(STOP, "CreateSpinlocks: cannot create spin locks");
|
||||
|
||||
return;
|
||||
if (nextSpinLock >= numSpinLocks)
|
||||
elog(FATAL, "s_init_lock_sema: not enough semaphores");
|
||||
lock->semId = SpinLockIds[nextSpinLock / SPINLOCKS_PER_SET];
|
||||
lock->sem = nextSpinLock % SPINLOCKS_PER_SET;
|
||||
nextSpinLock++;
|
||||
}
|
||||
|
||||
/*
|
||||
* InitSpinLocks -- Spinlock bootstrapping
|
||||
*
|
||||
* We need several spinlocks for bootstrapping:
|
||||
* ShmemIndexLock (for the shmem index table) and
|
||||
* ShmemLock (for the shmem allocator), BufMgrLock (for buffer
|
||||
* pool exclusive access), LockMgrLock (for the lock table), and
|
||||
* ProcStructLock (a spin lock for the shared process structure).
|
||||
* If there's a Sony WORM drive attached, we also have a spinlock
|
||||
* (SJCacheLock) for it. Same story for the main memory storage mgr.
|
||||
*
|
||||
*/
|
||||
void
|
||||
InitSpinLocks(void)
|
||||
s_unlock_sema(volatile slock_t *lock)
|
||||
{
|
||||
extern SPINLOCK ShmemLock;
|
||||
extern SPINLOCK ShmemIndexLock;
|
||||
extern SPINLOCK BufMgrLock;
|
||||
extern SPINLOCK LockMgrLock;
|
||||
extern SPINLOCK ProcStructLock;
|
||||
extern SPINLOCK SInvalLock;
|
||||
extern SPINLOCK OidGenLockId;
|
||||
extern SPINLOCK XidGenLockId;
|
||||
extern SPINLOCK ControlFileLockId;
|
||||
IpcSemaphoreUnlock(lock->semId, lock->sem);
|
||||
}
|
||||
|
||||
#ifdef STABLE_MEMORY_STORAGE
|
||||
extern SPINLOCK MMCacheLock;
|
||||
bool
|
||||
s_lock_free_sema(volatile slock_t *lock)
|
||||
{
|
||||
return IpcSemaphoreGetValue(lock->semId, lock->sem) > 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* These five (or six) spinlocks have fixed location is shmem */
|
||||
ShmemLock = (SPINLOCK) SHMEMLOCKID;
|
||||
ShmemIndexLock = (SPINLOCK) SHMEMINDEXLOCKID;
|
||||
BufMgrLock = (SPINLOCK) BUFMGRLOCKID;
|
||||
LockMgrLock = (SPINLOCK) LOCKMGRLOCKID;
|
||||
ProcStructLock = (SPINLOCK) PROCSTRUCTLOCKID;
|
||||
SInvalLock = (SPINLOCK) SINVALLOCKID;
|
||||
OidGenLockId = (SPINLOCK) OIDGENLOCKID;
|
||||
XidGenLockId = (SPINLOCK) XIDGENLOCKID;
|
||||
ControlFileLockId = (SPINLOCK) CNTLFILELOCKID;
|
||||
|
||||
#ifdef STABLE_MEMORY_STORAGE
|
||||
MMCacheLock = (SPINLOCK) MMCACHELOCKID;
|
||||
#endif
|
||||
|
||||
return;
|
||||
int
|
||||
tas_sema(volatile slock_t *lock)
|
||||
{
|
||||
/* Note that TAS macros return 0 if *success* */
|
||||
return ! IpcSemaphoreTryLock(lock->semId, lock->sem);
|
||||
}
|
||||
|
||||
#endif /* !HAS_TEST_AND_SET */
|
||||
|
||||
Reference in New Issue
Block a user