Commit c715fdea authored by Tom Lane's avatar Tom Lane

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.
parent 91482271
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/postmaster.sgml,v 1.16 2000/11/22 01:41:13 momjian Exp $
$Header: /cvsroot/pgsql/doc/src/sgml/ref/postmaster.sgml,v 1.17 2000/11/28 23:27:54 tgl Exp $
Postgres documentation
-->
......@@ -400,32 +400,6 @@ $ ps -e | grep postmast
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><computeroutput>
IpcMemoryAttach: shmat() failed: Permission denied
</computeroutput></term>
<listitem>
<para>
A likely explanation is that another user attempted to start a
<application>postmaster</application>
process on the same port which acquired shared resources and then
died. Since Postgres shared memory keys are based on the port number
assigned to the
<application>postmaster</application>,
such conflicts are likely if there is more than one installation on
a single host. If there are no other
<application>postmaster</application>
processes currently running (see above), run
<application>ipcclean</application>
and try again. If other <application>postmaster</application>
images
are running, you will have to find the owners of those processes to
coordinate the assignment of port numbers and/or removal of unused
shared memory segments.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect2>
......
......@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Header: /cvsroot/pgsql/src/backend/access/transam/xlog.c,v 1.35 2000/11/27 05:36:12 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/access/transam/xlog.c,v 1.36 2000/11/28 23:27:54 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -85,12 +85,6 @@ typedef struct XLogCtlWrite
} XLogCtlWrite;
#ifndef HAS_TEST_AND_SET
#define TAS(lck) 0
#define S_UNLOCK(lck)
#define S_INIT_LOCK(lck)
#endif
typedef struct XLogCtlData
{
XLogCtlInsert Insert;
......@@ -102,12 +96,10 @@ typedef struct XLogCtlData
uint32 XLogCacheByte;
uint32 XLogCacheBlck;
StartUpID ThisStartUpID;
#ifdef HAS_TEST_AND_SET
slock_t insert_lck;
slock_t info_lck;
slock_t lgwr_lck;
slock_t chkp_lck; /* checkpoint lock */
#endif
} XLogCtlData;
static XLogCtlData *XLogCtl = NULL;
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/async.c,v 1.72 2000/11/21 21:15:59 petere Exp $
* $Header: /cvsroot/pgsql/src/backend/commands/async.c,v 1.73 2000/11/28 23:27:54 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -119,8 +119,8 @@ static Dllist *pendingNotifies = NULL;
static volatile int notifyInterruptEnabled = 0;
static volatile int notifyInterruptOccurred = 0;
/* True if we've registered an on_shmem_exit cleanup (or at least tried to). */
static int unlistenExitRegistered = 0;
/* True if we've registered an on_shmem_exit cleanup */
static bool unlistenExitRegistered = false;
static void Async_UnlistenAll(void);
......@@ -253,9 +253,8 @@ Async_Listen(char *relname, int pid)
*/
if (!unlistenExitRegistered)
{
if (on_shmem_exit(Async_UnlistenOnExit, 0) < 0)
elog(NOTICE, "Async_Listen: out of shmem_exit slots");
unlistenExitRegistered = 1;
on_shmem_exit(Async_UnlistenOnExit, 0);
unlistenExitRegistered = true;
}
}
......
......@@ -11,7 +11,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.193 2000/11/27 04:03:20 inoue Exp $
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.194 2000/11/28 23:27:55 tgl Exp $
*
* NOTES
*
......@@ -117,26 +117,6 @@ int PostPortNumber;
char * UnixSocketDir;
char * Virtual_host;
/*
* This is a sequence number that indicates how many times we've had to
* throw away the shared memory and start over because we doubted its
* integrity. It starts off at zero and is incremented every time we
* start over. We use this to ensure that we use a new IPC shared memory
* key for the new shared memory segment in case the old segment isn't
* entirely gone yet.
*
* The sequence actually cycles back to 0 after 9, so pathologically there
* could be an IPC failure if 10 sets of backends are all stuck and won't
* release IPC resources.
*/
static short shmem_seq = 0;
/*
* This is the base IPC shared memory key. Other keys are generated by
* adding to this.
*/
static IpcMemoryKey ipc_key;
/*
* MaxBackends is the actual limit on the number of backends we will
* start. The default is established by configure, but it can be
......@@ -1292,39 +1272,6 @@ ConnFree(Port *conn)
free(conn);
}
/*
* get_host_port -- return a pseudo port number (16 bits)
* derived from the primary IP address of Virtual_host.
*/
static unsigned short
get_host_port(void)
{
static unsigned short hostPort = 0;
if (hostPort == 0)
{
SockAddr saddr;
struct hostent *hp;
hp = gethostbyname(Virtual_host);
if ((hp == NULL) || (hp->h_addrtype != AF_INET))
{
char msg[1024];
snprintf(msg, sizeof(msg),
"FATAL: get_host_port: gethostbyname(%s) failed\n",
Virtual_host);
fputs(msg, stderr);
pqdebug("%s", msg);
exit(1);
}
memmove((char *) &(saddr.in.sin_addr),
(char *) hp->h_addr,
hp->h_length);
hostPort = ntohl(saddr.in.sin_addr.s_addr) & 0xFFFF;
}
return hostPort;
}
/*
* reset_shared -- reset shared memory and semaphores
......@@ -1333,40 +1280,16 @@ static void
reset_shared(unsigned short port)
{
/*
* A typical ipc_key is 5432001, which is port 5432, sequence
* number 0, and 01 as the index in IPCKeyGetBufferMemoryKey().
* The 32-bit INT_MAX is 2147483 6 47.
*
* The default algorithm for calculating the IPC keys assumes that all
* instances of postmaster on a given host are listening on different
* ports. In order to work (prevent shared memory collisions) if you
* run multiple PostgreSQL instances on the same port and different IP
* addresses on a host, we change the algorithm if you give postmaster
* the -h option, or set PGHOST, to a value other than the internal
* default.
*
* If Virtual_host is set, then we generate the IPC keys using the
* last two octets of the IP address instead of the port number.
* This algorithm assumes that no one will run multiple PostgreSQL
* instances on one host using two IP addresses that have the same two
* last octets in different class C networks. If anyone does, it
* would be rare.
*
* So, if you use -h or PGHOST, don't try to run two instances of
* PostgreSQL on the same IP address but different ports. If you
* don't use them, then you must use different ports (via -p or
* PGPORT). And, of course, don't try to use both approaches on one
* host.
* Reset assignment of shared mem and semaphore IPC keys.
* Doing this means that in normal cases we'll assign the same keys
* on each "cycle of life", and thereby avoid leaving dead IPC objects
* floating around if the postmaster crashes and is restarted.
*/
if (Virtual_host[0] != '\0')
port = get_host_port();
ipc_key = port * 1000 + shmem_seq * 100;
CreateSharedMemoryAndSemaphores(ipc_key, MaxBackends);
shmem_seq += 1;
if (shmem_seq >= 10)
shmem_seq -= 10;
IpcInitKeyAssignment(port);
/*
* Create or re-create shared memory and semaphores.
*/
CreateSharedMemoryAndSemaphores(false, MaxBackends);
}
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/buf_init.c,v 1.37 2000/10/23 04:10:06 vadim Exp $
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/buf_init.c,v 1.38 2000/11/28 23:27:55 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -56,13 +56,6 @@ int Num_Descriptors;
BufferDesc *BufferDescriptors;
BufferBlock BufferBlocks;
#ifndef HAS_TEST_AND_SET
long *NWaitIOBackendP;
#endif
extern IpcSemaphoreId WaitIOSemId;
long *PrivateRefCount; /* also used in freelist.c */
bits8 *BufferLocks; /* flag bits showing locks I have set */
BufferTag *BufferTagLastDirtied; /* tag buffer had when last
......@@ -139,7 +132,7 @@ long int LocalBufferFlushCount;
* amount of available memory.
*/
void
InitBufferPool(IPCKey key)
InitBufferPool(void)
{
bool foundBufs,
foundDescs;
......@@ -170,18 +163,6 @@ InitBufferPool(IPCKey key)
ShmemInitStruct("Buffer Blocks",
NBuffers * BLCKSZ, &foundBufs);
#ifndef HAS_TEST_AND_SET
{
bool foundNWaitIO;
NWaitIOBackendP = (long *) ShmemInitStruct("#Backends Waiting IO",
sizeof(long),
&foundNWaitIO);
if (!foundNWaitIO)
*NWaitIOBackendP = 0;
}
#endif
if (foundDescs || foundBufs)
{
......@@ -214,10 +195,8 @@ InitBufferPool(IPCKey key)
buf->flags = (BM_DELETED | BM_FREE | BM_VALID);
buf->refcount = 0;
buf->buf_id = i;
#ifdef HAS_TEST_AND_SET
S_INIT_LOCK(&(buf->io_in_progress_lock));
S_INIT_LOCK(&(buf->cntx_lock));
#endif
}
/* close the circular queue */
......@@ -231,22 +210,6 @@ InitBufferPool(IPCKey key)
SpinRelease(BufMgrLock);
#ifndef HAS_TEST_AND_SET
{
extern IpcSemaphoreId WaitIOSemId;
extern IpcSemaphoreId WaitCLSemId;
WaitIOSemId = IpcSemaphoreCreate(IPCKeyGetWaitIOSemaphoreKey(key),
1, IPCProtection, 0, 1);
if (WaitIOSemId < 0)
elog(FATAL, "InitBufferPool: IpcSemaphoreCreate(WaitIOSemId) failed");
WaitCLSemId = IpcSemaphoreCreate(IPCKeyGetWaitCLSemaphoreKey(key),
1, IPCProtection,
IpcSemaphoreDefaultStartValue, 1);
if (WaitCLSemId < 0)
elog(FATAL, "InitBufferPool: IpcSemaphoreCreate(WaitCLSemId) failed");
}
#endif
PrivateRefCount = (long *) calloc(NBuffers, sizeof(long));
BufferLocks = (bits8 *) calloc(NBuffers, sizeof(bits8));
BufferTagLastDirtied = (BufferTag *) calloc(NBuffers, sizeof(BufferTag));
......@@ -262,7 +225,7 @@ InitBufferPool(IPCKey key)
* ----------------------------------------------------
*/
int
BufferShmemSize()
BufferShmemSize(void)
{
int size = 0;
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v 1.94 2000/11/20 16:47:31 petere Exp $
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v 1.95 2000/11/28 23:27:55 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -93,12 +93,6 @@ extern void AbortBufferIO(void);
*/
#define BUFFER_IS_BROKEN(buf) ((buf->flags & BM_IO_ERROR) && !(buf->flags & BM_DIRTY))
#ifndef HAS_TEST_AND_SET
static void SignalIO(BufferDesc *buf);
extern long *NWaitIOBackendP; /* defined in buf_init.c */
#endif /* HAS_TEST_AND_SET */
static Buffer ReadBufferWithBufferLock(Relation relation, BlockNumber blockNum,
bool bufferLockHeld);
static BufferDesc *BufferAlloc(Relation reln, BlockNumber blockNum,
......@@ -1187,27 +1181,7 @@ BufferSync()
*
* Should be entered with buffer manager spinlock held; releases it before
* waiting and re-acquires it afterwards.
*
* OLD NOTES:
* Because IO_IN_PROGRESS conflicts are
* expected to be rare, there is only one BufferIO
* lock in the entire system. All processes block
* on this semaphore when they try to use a buffer
* that someone else is faulting in. Whenever a
* process finishes an IO and someone is waiting for
* the buffer, BufferIO is signaled (SignalIO). All
* waiting processes then wake up and check to see
* if their buffer is now ready. This implementation
* is simple, but efficient enough if WaitIO is
* rarely called by multiple processes simultaneously.
*
* NEW NOTES:
* The above is true only on machines without test-and-set
* semaphores (which we hope are few, these days). On better
* hardware, each buffer has a spinlock that we can wait on.
*/
#ifdef HAS_TEST_AND_SET
*/
static void
WaitIO(BufferDesc *buf, SPINLOCK spinlock)
{
......@@ -1224,43 +1198,6 @@ WaitIO(BufferDesc *buf, SPINLOCK spinlock)
}
}
#else /* !HAS_TEST_AND_SET */
IpcSemaphoreId WaitIOSemId;
IpcSemaphoreId WaitCLSemId;
static void
WaitIO(BufferDesc *buf, SPINLOCK spinlock)
{
bool inProgress;
for (;;)
{
/* wait until someone releases IO lock */
(*NWaitIOBackendP)++;
SpinRelease(spinlock);
IpcSemaphoreLock(WaitIOSemId, 0, 1);
SpinAcquire(spinlock);
inProgress = (buf->flags & BM_IO_IN_PROGRESS);
if (!inProgress)
break;
}
}
/*
* SignalIO
*/
static void
SignalIO(BufferDesc *buf)
{
/* somebody better be waiting. */
Assert(buf->refcount > 1);
IpcSemaphoreUnlock(WaitIOSemId, 0, *NWaitIOBackendP);
*NWaitIOBackendP = 0;
}
#endif /* HAS_TEST_AND_SET */
long NDirectFileRead; /* some I/O's are direct file access.
* bypass bufmgr */
......@@ -2297,11 +2234,7 @@ UnlockBuffers()
Assert(BufferIsValid(i + 1));
buf = &(BufferDescriptors[i]);
#ifdef HAS_TEST_AND_SET
S_LOCK(&(buf->cntx_lock));
#else
IpcSemaphoreLock(WaitCLSemId, 0, IpcExclusiveLock);
#endif
if (BufferLocks[i] & BL_R_LOCK)
{
......@@ -2324,11 +2257,9 @@ UnlockBuffers()
Assert(buf->w_lock);
buf->w_lock = false;
}
#ifdef HAS_TEST_AND_SET
S_UNLOCK(&(buf->cntx_lock));
#else
IpcSemaphoreUnlock(WaitCLSemId, 0, IpcExclusiveLock);
#endif
BufferLocks[i] = 0;
}
}
......@@ -2346,11 +2277,7 @@ LockBuffer(Buffer buffer, int mode)
buf = &(BufferDescriptors[buffer - 1]);
buflock = &(BufferLocks[buffer - 1]);
#ifdef HAS_TEST_AND_SET
S_LOCK(&(buf->cntx_lock));
#else
IpcSemaphoreLock(WaitCLSemId, 0, IpcExclusiveLock);
#endif
if (mode == BUFFER_LOCK_UNLOCK)
{
......@@ -2380,15 +2307,9 @@ LockBuffer(Buffer buffer, int mode)
Assert(!(*buflock & (BL_R_LOCK | BL_W_LOCK | BL_RI_LOCK)));
while (buf->ri_lock || buf->w_lock)
{
#ifdef HAS_TEST_AND_SET
S_UNLOCK(&(buf->cntx_lock));
s_lock_sleep(i++);
S_LOCK(&(buf->cntx_lock));
#else
IpcSemaphoreUnlock(WaitCLSemId, 0, IpcExclusiveLock);
s_lock_sleep(i++);
IpcSemaphoreLock(WaitCLSemId, 0, IpcExclusiveLock);
#endif
}
(buf->r_locks)++;
*buflock |= BL_R_LOCK;
......@@ -2412,15 +2333,9 @@ LockBuffer(Buffer buffer, int mode)
*buflock |= BL_RI_LOCK;
buf->ri_lock = true;
}
#ifdef HAS_TEST_AND_SET
S_UNLOCK(&(buf->cntx_lock));
s_lock_sleep(i++);
S_LOCK(&(buf->cntx_lock));
#else
IpcSemaphoreUnlock(WaitCLSemId, 0, IpcExclusiveLock);
s_lock_sleep(i++);
IpcSemaphoreLock(WaitCLSemId, 0, IpcExclusiveLock);
#endif
}
buf->w_lock = true;
*buflock |= BL_W_LOCK;
......@@ -2438,12 +2353,7 @@ LockBuffer(Buffer buffer, int mode)
else
elog(ERROR, "LockBuffer: unknown lock mode %d", mode);
#ifdef HAS_TEST_AND_SET
S_UNLOCK(&(buf->cntx_lock));
#else
IpcSemaphoreUnlock(WaitCLSemId, 0, IpcExclusiveLock);
#endif
}
/*
......@@ -2471,7 +2381,6 @@ StartBufferIO(BufferDesc *buf, bool forInput)
Assert(!InProgressBuf);
Assert(!(buf->flags & BM_IO_IN_PROGRESS));
buf->flags |= BM_IO_IN_PROGRESS;
#ifdef HAS_TEST_AND_SET
/*
* There used to be
......@@ -2485,7 +2394,7 @@ StartBufferIO(BufferDesc *buf, bool forInput)
* happen -- tgl
*/
S_LOCK(&(buf->io_in_progress_lock));
#endif /* HAS_TEST_AND_SET */
InProgressBuf = buf;
IsForInput = forInput;
}
......@@ -2502,12 +2411,7 @@ static void
TerminateBufferIO(BufferDesc *buf)
{
Assert(buf == InProgressBuf);
#ifdef HAS_TEST_AND_SET
S_UNLOCK(&(buf->io_in_progress_lock));
#else
if (buf->refcount > 1)
SignalIO(buf);
#endif /* HAS_TEST_AND_SET */
InProgressBuf = (BufferDesc *) 0;
}
......
/*-------------------------------------------------------------------------
*
* s_lock.c
* buffer manager interface routines
* Spinlock support routines
*
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/Attic/s_lock.c,v 1.25 2000/11/16 05:51:01 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/Attic/s_lock.c,v 1.26 2000/11/28 23:27:55 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include <sys/time.h>
#include <unistd.h>
#include "postgres.h"
#include "storage/s_lock.h"
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/Attic/xlog_bufmgr.c,v 1.4 2000/11/22 02:19:14 inoue Exp $
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/Attic/xlog_bufmgr.c,v 1.5 2000/11/28 23:27:55 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -88,12 +88,6 @@ extern void AbortBufferIO(void);
*/
#define BUFFER_IS_BROKEN(buf) ((buf->flags & BM_IO_ERROR) && !(buf->flags & BM_DIRTY))
#ifndef HAS_TEST_AND_SET
static void SignalIO(BufferDesc *buf);
extern long *NWaitIOBackendP; /* defined in buf_init.c */
#endif /* HAS_TEST_AND_SET */
static Buffer ReadBufferWithBufferLock(Relation relation, BlockNumber blockNum,
bool bufferLockHeld);
static BufferDesc *BufferAlloc(Relation reln, BlockNumber blockNum,
......@@ -853,27 +847,7 @@ BufferSync()
*
* Should be entered with buffer manager spinlock held; releases it before
* waiting and re-acquires it afterwards.
*
* OLD NOTES:
* Because IO_IN_PROGRESS conflicts are
* expected to be rare, there is only one BufferIO
* lock in the entire system. All processes block
* on this semaphore when they try to use a buffer
* that someone else is faulting in. Whenever a
* process finishes an IO and someone is waiting for
* the buffer, BufferIO is signaled (SignalIO). All
* waiting processes then wake up and check to see
* if their buffer is now ready. This implementation
* is simple, but efficient enough if WaitIO is
* rarely called by multiple processes simultaneously.
*
* NEW NOTES:
* The above is true only on machines without test-and-set
* semaphores (which we hope are few, these days). On better
* hardware, each buffer has a spinlock that we can wait on.
*/
#ifdef HAS_TEST_AND_SET
*/
static void
WaitIO(BufferDesc *buf, SPINLOCK spinlock)
{
......@@ -890,43 +864,6 @@ WaitIO(BufferDesc *buf, SPINLOCK spinlock)
}
}
#else /* !HAS_TEST_AND_SET */
IpcSemaphoreId WaitIOSemId;
IpcSemaphoreId WaitCLSemId;
static void
WaitIO(BufferDesc *buf, SPINLOCK spinlock)
{
bool inProgress;
for (;;)
{
/* wait until someone releases IO lock */
(*NWaitIOBackendP)++;
SpinRelease(spinlock);
IpcSemaphoreLock(WaitIOSemId, 0, 1);
SpinAcquire(spinlock);
inProgress = (buf->flags & BM_IO_IN_PROGRESS);
if (!inProgress)
break;
}
}
/*
* SignalIO
*/
static void
SignalIO(BufferDesc *buf)
{
/* somebody better be waiting. */
Assert(buf->refcount > 1);
IpcSemaphoreUnlock(WaitIOSemId, 0, *NWaitIOBackendP);
*NWaitIOBackendP = 0;
}
#endif /* HAS_TEST_AND_SET */
long NDirectFileRead; /* some I/O's are direct file access.
* bypass bufmgr */
......@@ -1965,11 +1902,7 @@ UnlockBuffers()
Assert(BufferIsValid(i + 1));
buf = &(BufferDescriptors[i]);
#ifdef HAS_TEST_AND_SET
S_LOCK(&(buf->cntx_lock));
#else
IpcSemaphoreLock(WaitCLSemId, 0, IpcExclusiveLock);
#endif
if (BufferLocks[i] & BL_R_LOCK)
{
......@@ -1992,11 +1925,9 @@ UnlockBuffers()
Assert(buf->w_lock);
buf->w_lock = false;
}
#ifdef HAS_TEST_AND_SET
S_UNLOCK(&(buf->cntx_lock));
#else
IpcSemaphoreUnlock(WaitCLSemId, 0, IpcExclusiveLock);
#endif
BufferLocks[i] = 0;
}
}
......@@ -2014,11 +1945,7 @@ LockBuffer(Buffer buffer, int mode)
buf = &(BufferDescriptors[buffer - 1]);
buflock = &(BufferLocks[buffer - 1]);
#ifdef HAS_TEST_AND_SET
S_LOCK(&(buf->cntx_lock));
#else
IpcSemaphoreLock(WaitCLSemId, 0, IpcExclusiveLock);
#endif
if (mode == BUFFER_LOCK_UNLOCK)
{
......@@ -2048,15 +1975,9 @@ LockBuffer(Buffer buffer, int mode)
Assert(!(*buflock & (BL_R_LOCK | BL_W_LOCK | BL_RI_LOCK)));
while (buf->ri_lock || buf->w_lock)
{
#ifdef HAS_TEST_AND_SET
S_UNLOCK(&(buf->cntx_lock));
s_lock_sleep(i++);
S_LOCK(&(buf->cntx_lock));
#else
IpcSemaphoreUnlock(WaitCLSemId, 0, IpcExclusiveLock);
s_lock_sleep(i++);
IpcSemaphoreLock(WaitCLSemId, 0, IpcExclusiveLock);
#endif
}
(buf->r_locks)++;
*buflock |= BL_R_LOCK;
......@@ -2080,15 +2001,9 @@ LockBuffer(Buffer buffer, int mode)
*buflock |= BL_RI_LOCK;
buf->ri_lock = true;
}
#ifdef HAS_TEST_AND_SET
S_UNLOCK(&(buf->cntx_lock));
s_lock_sleep(i++);
S_LOCK(&(buf->cntx_lock));
#else
IpcSemaphoreUnlock(WaitCLSemId, 0, IpcExclusiveLock);
s_lock_sleep(i++);
IpcSemaphoreLock(WaitCLSemId, 0, IpcExclusiveLock);
#endif
}
buf->w_lock = true;
*buflock |= BL_W_LOCK;
......@@ -2109,12 +2024,7 @@ LockBuffer(Buffer buffer, int mode)
else
elog(ERROR, "LockBuffer: unknown lock mode %d", mode);
#ifdef HAS_TEST_AND_SET
S_UNLOCK(&(buf->cntx_lock));
#else
IpcSemaphoreUnlock(WaitCLSemId, 0, IpcExclusiveLock);
#endif
}
/*
......@@ -2142,7 +2052,6 @@ StartBufferIO(BufferDesc *buf, bool forInput)
Assert(!InProgressBuf);
Assert(!(buf->flags & BM_IO_IN_PROGRESS));
buf->flags |= BM_IO_IN_PROGRESS;
#ifdef HAS_TEST_AND_SET
/*
* There used to be
......@@ -2156,7 +2065,7 @@ StartBufferIO(BufferDesc *buf, bool forInput)
* happen -- tgl
*/
S_LOCK(&(buf->io_in_progress_lock));
#endif /* HAS_TEST_AND_SET */
InProgressBuf = buf;
IsForInput = forInput;
}
......@@ -2173,12 +2082,7 @@ static void
TerminateBufferIO(BufferDesc *buf)
{
Assert(buf == InProgressBuf);
#ifdef HAS_TEST_AND_SET
S_UNLOCK(&(buf->io_in_progress_lock));
#else
if (buf->refcount > 1)
SignalIO(buf);
#endif /* HAS_TEST_AND_SET */
InProgressBuf = (BufferDesc *) 0;
}
......
This diff is collapsed.
......@@ -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
/*
* Create shared memory for slocks
*/
CreateAndInitSLockMemory(IPCKeyGetSLockSharedMemoryKey(key));
#endif
/*
* Kill and create the buffer manager buffer pool (and semaphore)
*/
CreateSpinlocks(IPCKeyGetSpinLockSemaphoreKey(key));
PGShmemHeader *seghdr;
/*
* 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
*/
InitLocks();
if (InitLockTable() == INVALID_TABLEID)
elog(FATAL, "Couldn't create the lock table");
seghdr = IpcMemoryCreate(size, private, IPCProtection);
/* ----------------
* do process table stuff
* ----------------
/*
* First initialize spinlocks --- needed by InitShmemAllocation()
*/
InitProcGlobal(key, maxBackends);
CreateSharedInvalidationState(key, maxBackends);
}
CreateSpinlocks(seghdr);
/*
* AttachSharedMemoryAndSemaphores
* Attachs existant shared memory and semaphores.
*/
void
AttachSharedMemoryAndSemaphores(IPCKey key)
{
/* ----------------
* create rather than attach if using private key
* ----------------
/*
* Set up shmem.c hashtable
*/
if (key == PrivateIPCKey)
{
CreateSharedMemoryAndSemaphores(key, 16);
return;
}
InitShmemAllocation(seghdr);
#ifdef HAS_TEST_AND_SET
/* ----------------
* attach the slock shared memory
* ----------------
*/
AttachSLockMemory(IPCKeyGetSLockSharedMemoryKey(key));
#endif
/* ----------------
* attach the buffer manager buffer pool (and semaphore)
* ----------------
/*
* Set up xlog and buffers
*/
InitShmem(key, 0);
InitBufferPool(key);
XLOGShmemInit();
InitBufferPool();
/* ----------------
* initialize lock table stuff
* ----------------
/*
* Set up lock manager
*/
InitLocks();
if (InitLockTable() == INVALID_TABLEID)
elog(FATAL, "Couldn't attach to the lock table");
elog(FATAL, "Couldn't create the lock table");
/*
* Set up process table
*/
InitProcGlobal(maxBackends);
AttachSharedInvalidationState(key);
/*
* Set up shared-inval messaging
*/
CreateSharedInvalidationState(maxBackends);
}
This diff is collapsed.
......@@ -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)
{
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");
}
return sizeof(SISeg) + sizeof(ProcState) * (maxBackends - 1);
}
/*
* SISegInit
* Initialize contents of a new shared memory sinval segment
* SIBufferInit
* Create and initialize a new SI message buffer
*/
static void
SISegInit(SISeg *segP, int maxBackends)
void
SIBufferInit(int maxBackends)
{
int segSize;
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;
......
This diff is collapsed.
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lock.c,v 1.72 2000/11/08 22:10:00 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lock.c,v 1.73 2000/11/28 23:27:56 tgl Exp $
*
* NOTES
* Outside modules can create a lock table and acquire/release
......@@ -56,6 +56,7 @@ static char *lock_types[] =
"AccessExclusiveLock"
};
static char *DeadLockMessage = "Deadlock detected.\n\tSee the lock(l) manual page for a possible cause.";
#ifdef LOCK_DEBUG
......@@ -943,8 +944,7 @@ WaitOnLock(LOCKMETHOD lockmethod, LOCK *lock, LOCKMODE lockmode)
lock) != NO_ERROR)
{
/* -------------------
* This could have happend as a result of a deadlock,
* see HandleDeadLock().
* We failed as a result of a deadlock, see HandleDeadLock().
* Decrement the lock nHolding and holders fields as
* we are no longer waiting on this lock.
* -------------------
......@@ -957,8 +957,7 @@ WaitOnLock(LOCKMETHOD lockmethod, LOCK *lock, LOCKMODE lockmode)
if (lock->activeHolders[lockmode] == lock->holders[lockmode])
lock->waitMask &= BITS_OFF[lockmode];
SpinRelease(lockMethodTable->ctl->masterLock);
elog(ERROR, "WaitOnLock: error on wakeup - Aborting this transaction");
elog(ERROR, DeadLockMessage);
/* not reached */
}
......
This diff is collapsed.
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.72 2000/11/16 22:30:39 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.73 2000/11/28 23:27:57 tgl Exp $
*
*
*-------------------------------------------------------------------------
......@@ -45,7 +45,6 @@
static void ReverifyMyDatabase(const char *name);
static void InitCommunication(void);
static IPCKey PostgresIpcKey;
/*** InitPostgres support ***/
......@@ -141,7 +140,7 @@ ReverifyMyDatabase(const char *name)
* --------------------------------
*/
static void
InitCommunication()
InitCommunication(void)
{
/* ----------------
* initialize shared memory and semaphores appropriately.
......@@ -151,26 +150,11 @@ InitCommunication()
{
/* ----------------
* we're running a postgres backend by itself with
* no front end or postmaster.
* no front end or postmaster. Create private "shmem"
* and semaphores. Setting MaxBackends = 16 is arbitrary.
* ----------------
*/
char *ipc_key; /* value of environment variable */
IPCKey key;
ipc_key = getenv("IPC_KEY");
if (!PointerIsValid(ipc_key))
{
/* Normal standalone backend */
key = PrivateIPCKey;
}
else
{
/* Allow standalone's IPC key to be set */
key = atoi(ipc_key);
}
PostgresIpcKey = key;
AttachSharedMemoryAndSemaphores(key);
CreateSharedMemoryAndSemaphores(true, 16);
}
}
......@@ -295,7 +279,7 @@ InitPostgres(const char *dbname, const char *username)
/*
* Set up my per-backend PROC struct in shared memory.
*/
InitProcess(PostgresIpcKey);
InitProcess();
/*
* Initialize my entry in the shared-invalidation manager's array of
......@@ -307,7 +291,7 @@ InitPostgres(const char *dbname, const char *username)
*/
MyBackendId = InvalidBackendId;
InitSharedInvalidationState();
InitBackendSharedInvalidationState();
if (MyBackendId > MAXBACKENDS || MyBackendId <= 0)
elog(FATAL, "cinit2: bad backend id %d", MyBackendId);
......@@ -365,11 +349,11 @@ BaseInit(void)
*/
InitCommunication();
DebugFileOpen();
smgrinit();
EnablePortalManager(); /* memory for portal/transaction stuff */
/* initialize the local buffer manager */
InitLocalBuffer();
}
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: buf_internals.h,v 1.43 2000/11/08 22:10:02 tgl Exp $
* $Id: buf_internals.h,v 1.44 2000/11/28 23:27:57 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -16,6 +16,7 @@
#include "storage/buf.h"
#include "storage/lmgr.h"
#include "storage/s_lock.h"
/* Buf Mgr constants */
/* in bufmgr.c */
......@@ -100,11 +101,9 @@ typedef struct sbufdesc
BufFlags flags; /* see bit definitions above */
unsigned refcount; /* # of times buffer is pinned */
#ifdef HAS_TEST_AND_SET
/* can afford a dedicated lock if test-and-set locks are available */
slock_t io_in_progress_lock;
slock_t io_in_progress_lock; /* to block for I/O to complete */
slock_t cntx_lock; /* to lock access to page context */
#endif /* HAS_TEST_AND_SET */
unsigned r_locks; /* # of shared locks */
bool ri_lock; /* read-intent lock */
bool w_lock; /* context exclusively locked */
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: bufmgr.h,v 1.43 2000/11/08 22:10:02 tgl Exp $
* $Id: bufmgr.h,v 1.44 2000/11/28 23:27:57 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -154,7 +154,7 @@ extern Buffer ReleaseAndReadBuffer(Buffer buffer, Relation relation,
BlockNumber blockNum);
extern int FlushBuffer(Buffer buffer, bool sync, bool release);
extern void InitBufferPool(IPCKey key);
extern void InitBufferPool(void);
extern void PrintBufferUsage(FILE *statfp);
extern void ResetBufferUsage(void);
extern void ResetBufferPool(bool isCommit);
......
......@@ -7,14 +7,10 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: ipc.h,v 1.42 2000/10/07 14:39:17 momjian Exp $
*
* NOTES
* This file is very architecture-specific. This stuff should actually
* be factored into the port/ directories.
* $Id: ipc.h,v 1.43 2000/11/28 23:27:57 tgl Exp $
*
* Some files that would normally need to include only sys/ipc.h must
* instead included this file because on Ultrix, sys/ipc.h is not designed
* instead include this file because on Ultrix, sys/ipc.h is not designed
* to be included multiple times. This file (by virtue of the ifndef IPC_H)
* is.
*-------------------------------------------------------------------------
......@@ -26,11 +22,9 @@
#include <sys/types.h>
#ifdef HAVE_SYS_IPC_H
#include <sys/ipc.h> /* For IPC_PRIVATE */
#include <sys/ipc.h>
#endif /* HAVE_SYS_IPC_H */
#include "config.h"
#ifndef HAVE_UNION_SEMUN
union semun
{
......@@ -38,79 +32,41 @@ union semun
struct semid_ds *buf;
unsigned short *array;
};
#endif
typedef uint16 SystemPortAddress;
/* semaphore definitions */
/* generic IPC definitions */
#define IPCProtection (0600) /* access/modify by user only */
#define IPC_NMAXSEM 25 /* maximum number of semaphores */
#define IpcSemaphoreDefaultStartValue 255
#define IpcSharedLock (-1)
#define IpcExclusiveLock (-255)
#define IpcUnknownStatus (-1)
#define IpcInvalidArgument (-2)
#define IpcSemIdExist (-3)
#define IpcSemIdNotExist (-4)
typedef uint32 IpcSemaphoreKey; /* semaphore key */
typedef int IpcSemaphoreId;
/* shared memory definitions */
#define IpcMemCreationFailed (-1)
#define IpcMemIdGetFailed (-2)
#define IpcMemAttachFailed 0
typedef uint32 IPCKey;
/* semaphore definitions */
#define PrivateIPCKey IPC_PRIVATE
#define DefaultIPCKey 17317
typedef uint32 IpcSemaphoreKey; /* semaphore key passed to semget(2) */
typedef int IpcSemaphoreId; /* semaphore ID returned by semget(2) */
typedef uint32 IpcMemoryKey; /* shared memory key */
typedef int IpcMemoryId;
#define IPC_NMAXSEM 32 /* maximum number of semaphores per semID */
#define PGSemaMagic 537 /* must be less than SEMVMX */
/* ipc.c */
extern bool proc_exit_inprogress;
extern void proc_exit(int code);
extern void shmem_exit(int code);
extern int on_shmem_exit(void (*function) (), Datum arg);
extern int on_proc_exit(void (*function) (), Datum arg);
extern void on_exit_reset(void);
/* shared memory definitions */
extern IpcSemaphoreId IpcSemaphoreCreate(IpcSemaphoreKey semKey,
int semNum, int permission, int semStartValue,
int removeOnExit);
extern void IpcSemaphoreKill(IpcSemaphoreKey key);
extern void IpcSemaphoreLock(IpcSemaphoreId semId, int sem, int lock);
extern void IpcSemaphoreUnlock(IpcSemaphoreId semId, int sem, int lock);
extern int IpcSemaphoreGetCount(IpcSemaphoreId semId, int sem);
extern int IpcSemaphoreGetValue(IpcSemaphoreId semId, int sem);
extern IpcMemoryId IpcMemoryCreate(IpcMemoryKey memKey, uint32 size,
int permission);
extern IpcMemoryId IpcMemoryIdGet(IpcMemoryKey memKey, uint32 size);
extern char *IpcMemoryAttach(IpcMemoryId memId);
extern void IpcMemoryKill(IpcMemoryKey memKey);
extern void CreateAndInitSLockMemory(IPCKey key);
extern void AttachSLockMemory(IPCKey key);
typedef uint32 IpcMemoryKey; /* shared memory key passed to shmget(2) */
typedef int IpcMemoryId; /* shared memory ID returned by shmget(2) */
typedef struct /* standard header for all Postgres shmem */
{
int32 magic; /* magic # to identify Postgres segments */
#define PGShmemMagic 679834892
pid_t creatorPID; /* PID of creating process */
uint32 totalsize; /* total size of segment */
uint32 freeoffset; /* offset to first free space */
} PGShmemHeader;
#ifdef HAS_TEST_AND_SET
#define NOLOCK 0
#define SHAREDLOCK 1
#define EXCLUSIVELOCK 2
/* spinlock definitions */
typedef enum _LockId_
{
BUFMGRLOCKID,
LOCKLOCKID,
OIDGENLOCKID,
XIDGENLOCKID,
CNTLFILELOCKID,
......@@ -118,100 +74,40 @@ typedef enum _LockId_
SHMEMINDEXLOCKID,
LOCKMGRLOCKID,
SINVALLOCKID,
#ifdef STABLE_MEMORY_STORAGE
MMCACHELOCKID,
#endif
PROCSTRUCTLOCKID,
FIRSTFREELOCKID
} _LockId_;
#define MAX_SPINS FIRSTFREELOCKID
typedef struct slock
{
slock_t locklock;
unsigned char flag;
short nshlocks;
slock_t shlock;
slock_t exlock;
slock_t comlock;
struct slock *next;
} SLock;
#else /* HAS_TEST_AND_SET */
typedef enum _LockId_
{
SHMEMLOCKID,
SHMEMINDEXLOCKID,
BUFMGRLOCKID,
LOCKMGRLOCKID,
SINVALLOCKID,
#ifdef STABLE_MEMORY_STORAGE
MMCACHELOCKID,
#endif
PROCSTRUCTLOCKID,
OIDGENLOCKID,
XIDGENLOCKID,
CNTLFILELOCKID,
FIRSTFREELOCKID
MAX_SPINS /* must be last item! */
} _LockId_;
#define MAX_SPINS FIRSTFREELOCKID
#endif /* HAS_TEST_AND_SET */
/* ipc.c */
extern bool proc_exit_inprogress;
/*
* the following are originally in ipci.h but the prototypes have circular
* dependencies and most files include both ipci.h and ipc.h anyway, hence
* combined.
*
*/
extern void proc_exit(int code);
extern void shmem_exit(int code);
extern void on_proc_exit(void (*function) (), Datum arg);
extern void on_shmem_exit(void (*function) (), Datum arg);
extern void on_exit_reset(void);
/*
* Note:
* These must not hash to DefaultIPCKey or PrivateIPCKey.
*/
#define SystemPortAddressGetIPCKey(address) \
(28597 * (address) + 17491)
extern void IpcInitKeyAssignment(int port);
/*
* these keys are originally numbered from 1 to 12 consecutively but not
* all are used. The unused ones are removed. - ay 4/95.
*/
#define IPCKeyGetBufferMemoryKey(key) \
((key == PrivateIPCKey) ? key : 1 + (key))
#define IPCKeyGetSIBufferMemoryBlock(key) \
((key == PrivateIPCKey) ? key : 7 + (key))
#define IPCKeyGetSLockSharedMemoryKey(key) \
((key == PrivateIPCKey) ? key : 10 + (key))
#define IPCKeyGetSpinLockSemaphoreKey(key) \
((key == PrivateIPCKey) ? key : 11 + (key))
#define IPCKeyGetWaitIOSemaphoreKey(key) \
((key == PrivateIPCKey) ? key : 12 + (key))
#define IPCKeyGetWaitCLSemaphoreKey(key) \
((key == PrivateIPCKey) ? key : 13 + (key))
/* --------------------------
* NOTE: This macro must always give the highest numbered key as every backend
* process forked off by the postmaster will be trying to acquire a semaphore
* with a unique key value starting at key+14 and incrementing up. Each
* backend uses the current key value then increments it by one.
* --------------------------
*/
#define IPCGetProcessSemaphoreInitKey(key) \
((key == PrivateIPCKey) ? key : 14 + (key))
extern IpcSemaphoreId IpcSemaphoreCreate(int numSems, int permission,
int semStartValue,
bool removeOnExit);
extern void IpcSemaphoreKill(IpcSemaphoreId semId);
extern void IpcSemaphoreLock(IpcSemaphoreId semId, int sem);
extern void IpcSemaphoreUnlock(IpcSemaphoreId semId, int sem);
extern bool IpcSemaphoreTryLock(IpcSemaphoreId semId, int sem);
extern int IpcSemaphoreGetValue(IpcSemaphoreId semId, int sem);
extern PGShmemHeader *IpcMemoryCreate(uint32 size, bool private,
int permission);
/* ipci.c */
extern IPCKey SystemPortAddressCreateIPCKey(SystemPortAddress address);
extern void CreateSharedMemoryAndSemaphores(IPCKey key, int maxBackends);
extern void AttachSharedMemoryAndSemaphores(IPCKey key);
extern void CreateSharedMemoryAndSemaphores(bool private, int maxBackends);
#endif /* IPC_H */
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: lmgr.h,v 1.25 2000/06/08 22:37:54 momjian Exp $
* $Id: lmgr.h,v 1.26 2000/11/28 23:27:57 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -47,7 +47,4 @@ extern void UnlockPage(Relation relation, BlockNumber blkno, LOCKMODE lockmode);
extern void XactLockTableInsert(TransactionId xid);
extern void XactLockTableWait(TransactionId xid);
/* proc.c */
extern void InitProcGlobal(IPCKey key, int maxBackends);
#endif /* LMGR_H */
/*-------------------------------------------------------------------------
*
* proc.h
*
* per-process shared memory data structures
*
*
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: proc.h,v 1.31 2000/05/31 00:28:38 petere Exp $
* $Id: proc.h,v 1.32 2000/11/28 23:27:57 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -23,9 +23,8 @@ extern int DeadlockTimeout;
typedef struct
{
int sleeplock;
int semNum;
IpcSemaphoreId semId;
IpcSemaphoreKey semKey;
int semNum;
} SEMA;
/*
......@@ -33,7 +32,6 @@ typedef struct
*/
typedef struct proc
{
/* proc->links MUST BE THE FIRST ELEMENT OF STRUCT (see ProcWakeup()) */
SHM_QUEUE links; /* proc can be waiting for one event(lock) */
......@@ -63,34 +61,6 @@ typedef struct proc
* transaction */
} PROC;
/*
* PROC_NSEMS_PER_SET is the number of semaphores in each sys-V semaphore set
* we allocate. It must be *less than* 32 (or however many bits in an int
* on your machine), or our free-semaphores bitmap won't work. You also must
* not set it higher than your kernel's SEMMSL (max semaphores per set)
* parameter, which is often around 25.
*
* MAX_PROC_SEMS is the maximum number of per-process semaphores (those used
* by the lock mgr) we can keep track of. It must be a multiple of
* PROC_NSEMS_PER_SET.
*/
#define PROC_NSEMS_PER_SET 16
#define MAX_PROC_SEMS (((MAXBACKENDS-1)/PROC_NSEMS_PER_SET+1)*PROC_NSEMS_PER_SET)
typedef struct procglobal
{
SHMEM_OFFSET freeProcs;
IPCKey currKey;
int32 freeSemMap[MAX_PROC_SEMS / PROC_NSEMS_PER_SET];
/*
* In each freeSemMap entry, the PROC_NSEMS_PER_SET least-significant
* bits flag whether individual semaphores are in use, and the next
* higher bit is set to show that the entire set is allocated.
*/
} PROC_HDR;
extern PROC *MyProc;
#define PROC_INCR_SLOCK(lock) \
......@@ -115,16 +85,46 @@ do { \
extern SPINLOCK ProcStructLock;
/*
* There is one ProcGlobal struct for the whole installation.
*
* PROC_NSEMS_PER_SET is the number of semaphores in each sys-V semaphore set
* we allocate. It must be no more than 32 (or however many bits in an int
* on your machine), or our free-semaphores bitmap won't work. It also must
* be *less than* your kernel's SEMMSL (max semaphores per set) parameter,
* which is often around 25. (Less than, because we allocate one extra sema
* in each set for identification purposes.)
*
* PROC_SEM_MAP_ENTRIES is the number of semaphore sets we need to allocate
* to keep track of up to MAXBACKENDS backends.
*/
#define PROC_NSEMS_PER_SET 16
#define PROC_SEM_MAP_ENTRIES ((MAXBACKENDS-1)/PROC_NSEMS_PER_SET+1)
typedef struct procglobal
{
/* Head of list of free PROC structures */
SHMEM_OFFSET freeProcs;
/* Info about semaphore sets used for per-process semaphores */
IpcSemaphoreId procSemIds[PROC_SEM_MAP_ENTRIES];
int32 freeSemMap[PROC_SEM_MAP_ENTRIES];
/*
* In each freeSemMap entry, bit i is set if the i'th semaphore of the
* set is allocated to a process. (i counts from 0 at the LSB)
*/
} PROC_HDR;
/*
* Function Prototypes
*/
extern void InitProcess(IPCKey key);
extern void InitProcGlobal(int maxBackends);
extern void InitProcess(void);
extern void ProcReleaseLocks(void);
extern bool ProcRemove(int pid);
/* extern bool ProcKill(int exitStatus, int pid); */
/* make static in storage/lmgr/proc.c -- jolly */
extern void ProcQueueInit(PROC_QUEUE *queue);
extern int ProcSleep(PROC_QUEUE *queue, LOCKMETHODCTL *lockctl, int token,
LOCK *lock);
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/include/storage/s_lock.h,v 1.73 2000/10/22 22:15:03 petere Exp $
* $Header: /cvsroot/pgsql/src/include/storage/s_lock.h,v 1.74 2000/11/28 23:27:57 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -64,7 +64,7 @@
* manual for POWER in any case.
*
*/
#if !defined(S_LOCK_H)
#ifndef S_LOCK_H
#define S_LOCK_H
#include "storage/ipc.h"
......@@ -403,8 +403,8 @@ extern void s_lock(volatile slock_t *lock, const char *file, const int line);
#define S_LOCK(lock) \
do { \
if (TAS((volatile slock_t *) lock)) \
s_lock((volatile slock_t *) lock, __FILE__, __LINE__); \
if (TAS((volatile slock_t *) (lock))) \
s_lock((volatile slock_t *) (lock), __FILE__, __LINE__); \
} while (0)
#endif /* S_LOCK */
......@@ -421,12 +421,46 @@ extern void s_lock(volatile slock_t *lock, const char *file, const int line);
#endif /* S_INIT_LOCK */
#if !defined(TAS)
int tas(volatile slock_t *lock); /* port/.../tas.s, or
extern int tas(volatile slock_t *lock); /* port/.../tas.s, or
* s_lock.c */
#define TAS(lock) tas((volatile slock_t *) lock)
#define TAS(lock) tas((volatile slock_t *) (lock))
#endif /* TAS */
#else /* !HAS_TEST_AND_SET */
/*
* Fake spinlock implementation using SysV semaphores --- slow and prone
* to fall foul of kernel limits on number of semaphores, so don't use this
* unless you must!
*/
typedef struct
{
/* reference to semaphore used to implement this spinlock */
IpcSemaphoreId semId;
int sem;
} slock_t;
extern bool s_lock_free_sema(volatile slock_t *lock);
extern void s_unlock_sema(volatile slock_t *lock);
extern void s_init_lock_sema(volatile slock_t *lock);
extern int tas_sema(volatile slock_t *lock);
extern void s_lock(volatile slock_t *lock, const char *file, const int line);
#define S_LOCK(lock) \
do { \
if (TAS((volatile slock_t *) (lock))) \
s_lock((volatile slock_t *) (lock), __FILE__, __LINE__); \
} while (0)
#define S_LOCK_FREE(lock) s_lock_free_sema(lock)
#define S_UNLOCK(lock) s_unlock_sema(lock)
#define S_INIT_LOCK(lock) s_init_lock_sema(lock)
#define TAS(lock) tas_sema(lock)
#endif /* HAS_TEST_AND_SET */
#endif /* S_LOCK_H */
#endif /* S_LOCK_H */
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: shmem.h,v 1.23 2000/06/28 03:33:27 tgl Exp $
* $Id: shmem.h,v 1.24 2000/11/28 23:27:57 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -18,17 +18,23 @@
#include "utils/hsearch.h"
/* The shared memory region can start at a different address
/*
* The shared memory region can start at a different address
* in every process. Shared memory "pointers" are actually
* offsets relative to the start of the shared memory region(s).
*
* In current usage, this is not actually a problem, but we keep
* the code that used to handle it...
*/
typedef unsigned long SHMEM_OFFSET;
#define INVALID_OFFSET (-1)
#define BAD_LOCATION (-1)
/* start of the lowest shared memory region. For now, assume that
* there is only one shared memory region
/*
* Start of the primary shared memory region, in this process' address space.
* The macros in this header file can only cope with offsets into this
* shared memory region!
*/
extern SHMEM_OFFSET ShmemBase;
......@@ -39,14 +45,14 @@ extern SHMEM_OFFSET ShmemBase;
/* coerce a pointer into a shmem offset */
#define MAKE_OFFSET(xx_ptr)\
(SHMEM_OFFSET) (((unsigned long)(xx_ptr))-ShmemBase)
((SHMEM_OFFSET) (((unsigned long)(xx_ptr))-ShmemBase))
#define SHM_PTR_VALID(xx_ptr)\
(((unsigned long)xx_ptr) > ShmemBase)
(((unsigned long)(xx_ptr)) > ShmemBase)
/* cannot have an offset to ShmemFreeStart (offset 0) */
#define SHM_OFFSET_VALID(xx_offs)\
((xx_offs != 0) && (xx_offs != INVALID_OFFSET))
(((xx_offs) != 0) && ((xx_offs) != INVALID_OFFSET))
extern SPINLOCK ShmemLock;
......@@ -60,11 +66,9 @@ typedef struct SHM_QUEUE
} SHM_QUEUE;
/* shmem.c */
extern void ShmemIndexReset(void);
extern void ShmemCreate(unsigned int key, unsigned int size);
extern int InitShmem(unsigned int key, unsigned int size);
extern void InitShmemAllocation(PGShmemHeader *seghdr);
extern void *ShmemAlloc(Size size);
extern int ShmemIsValid(unsigned long addr);
extern bool ShmemIsValid(unsigned long addr);
extern HTAB *ShmemInitHash(char *name, long init_size, long max_size,
HASHCTL *infoP, int hash_flags);
extern bool ShmemPIDLookup(int pid, SHMEM_OFFSET *locationPtr);
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: sinval.h,v 1.15 2000/11/12 20:51:52 tgl Exp $
* $Id: sinval.h,v 1.16 2000/11/28 23:27:57 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -19,9 +19,9 @@
extern SPINLOCK SInvalLock;
extern void CreateSharedInvalidationState(IPCKey key, int maxBackends);
extern void AttachSharedInvalidationState(IPCKey key);
extern void InitSharedInvalidationState(void);
extern int SInvalShmemSize(int maxBackends);
extern void CreateSharedInvalidationState(int maxBackends);
extern void InitBackendSharedInvalidationState(void);
extern void RegisterSharedInvalid(int cacheId, Index hashIndex,
ItemPointer pointer);
extern void InvalidateSharedInvalid(void (*invalFunction) (),
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: sinvaladt.h,v 1.23 2000/11/12 20:51:52 tgl Exp $
* $Id: sinvaladt.h,v 1.24 2000/11/28 23:27:57 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -107,15 +107,13 @@ typedef struct SISeg
} SISeg;
extern SISeg *shmInvalBuffer; /* pointer to the shared buffer segment,
* set by SISegmentAttach() */
extern SISeg *shmInvalBuffer; /* pointer to the shared inval buffer */
/*
* prototypes for functions in sinvaladt.c
*/
extern int SISegmentInit(bool createNewSegment, IPCKey key,
int maxBackends);
extern void SIBufferInit(int maxBackends);
extern int SIBackendInit(SISeg *segP);
extern bool SIInsertDataEntry(SISeg *segP, SharedInvalidData *data);
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: spin.h,v 1.12 2000/05/31 00:28:38 petere Exp $
* $Id: spin.h,v 1.13 2000/11/28 23:27:57 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -19,11 +19,10 @@
/*
* two implementations of spin locks
*
* sequent, sparc, sun3: real spin locks. uses a TAS instruction; see
* src/storage/ipc/s_lock.c for details.
*
* default: fake spin locks using semaphores. see spin.c
* Where TAS instruction is available: real spin locks.
* See src/storage/ipc/s_lock.c for details.
*
* Otherwise: fake spin locks using semaphores. see spin.c
*/
typedef int SPINLOCK;
......@@ -32,8 +31,10 @@ typedef int SPINLOCK;
extern bool Trace_spinlocks;
#endif
extern void CreateSpinlocks(IPCKey key);
extern void InitSpinLocks(void);
extern int SLockShmemSize(void);
extern void CreateSpinlocks(PGShmemHeader *seghdr);
extern void SpinAcquire(SPINLOCK lockid);
extern void SpinRelease(SPINLOCK lockid);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment