Commit 72a3902a authored by Tom Lane's avatar Tom Lane

Create an internal semaphore API that is not tied to SysV semaphores.

As proof of concept, provide an alternate implementation based on POSIX
semaphores.  Also push the SysV shared-memory implementation into a
separate file so that it can be replaced conveniently.
parent 91fc10fd
......@@ -15664,6 +15664,42 @@ HAVE_POSIX_SIGNALS=$pgac_cv_func_posix_signals
# Select semaphore implementation type.
if test x"$USE_NAMED_POSIX_SEMAPHORES" = x"1" ; then
cat >>confdefs.h <<\_ACEOF
#define USE_NAMED_POSIX_SEMAPHORES 1
_ACEOF
SEMA_IMPLEMENTATION="src/backend/port/posix_sema.c"
else
if test x"$USE_UNNAMED_POSIX_SEMAPHORES" = x"1" ; then
cat >>confdefs.h <<\_ACEOF
#define USE_UNNAMED_POSIX_SEMAPHORES 1
_ACEOF
SEMA_IMPLEMENTATION="src/backend/port/posix_sema.c"
else
cat >>confdefs.h <<\_ACEOF
#define USE_SYSV_SEMAPHORES 1
_ACEOF
SEMA_IMPLEMENTATION="src/backend/port/sysv_sema.c"
fi
fi
# Select shared-memory implementation type.
cat >>confdefs.h <<\_ACEOF
#define USE_SYSV_SHARED_MEMORY 1
_ACEOF
SHMEM_IMPLEMENTATION="src/backend/port/sysv_shmem.c"
if test "$enable_nls" = yes ; then
echo "$as_me:$LINENO: checking for library containing gettext" >&5
......@@ -16724,7 +16760,7 @@ fi
ac_config_files="$ac_config_files GNUmakefile src/Makefile.global"
ac_config_links="$ac_config_links src/backend/port/dynloader.c:src/backend/port/dynloader/${template}.c src/include/dynloader.h:src/backend/port/dynloader/${template}.h src/include/pg_config_os.h:src/include/port/${template}.h src/Makefile.port:src/makefiles/Makefile.${template}"
ac_config_links="$ac_config_links src/backend/port/dynloader.c:src/backend/port/dynloader/${template}.c src/backend/port/pg_sema.c:${SEMA_IMPLEMENTATION} src/backend/port/pg_shmem.c:${SHMEM_IMPLEMENTATION} src/include/dynloader.h:src/backend/port/dynloader/${template}.h src/include/pg_config_os.h:src/include/port/${template}.h src/Makefile.port:src/makefiles/Makefile.${template}"
ac_config_headers="$ac_config_headers src/include/pg_config.h"
......@@ -17207,6 +17243,8 @@ do
"src/Makefile.global" ) CONFIG_FILES="$CONFIG_FILES src/Makefile.global" ;;
"src/backend/port/tas.s" ) CONFIG_LINKS="$CONFIG_LINKS src/backend/port/tas.s:src/backend/port/tas/${tas_file}" ;;
"src/backend/port/dynloader.c" ) CONFIG_LINKS="$CONFIG_LINKS src/backend/port/dynloader.c:src/backend/port/dynloader/${template}.c" ;;
"src/backend/port/pg_sema.c" ) CONFIG_LINKS="$CONFIG_LINKS src/backend/port/pg_sema.c:${SEMA_IMPLEMENTATION}" ;;
"src/backend/port/pg_shmem.c" ) CONFIG_LINKS="$CONFIG_LINKS src/backend/port/pg_shmem.c:${SHMEM_IMPLEMENTATION}" ;;
"src/include/dynloader.h" ) CONFIG_LINKS="$CONFIG_LINKS src/include/dynloader.h:src/backend/port/dynloader/${template}.h" ;;
"src/include/pg_config_os.h" ) CONFIG_LINKS="$CONFIG_LINKS src/include/pg_config_os.h:src/include/port/${template}.h" ;;
"src/Makefile.port" ) CONFIG_LINKS="$CONFIG_LINKS src/Makefile.port:src/makefiles/Makefile.${template}" ;;
......
dnl Process this file with autoconf to produce a configure script.
dnl $Header: /cvsroot/pgsql/configure.in,v 1.183 2002/04/26 19:47:35 tgl Exp $
dnl $Header: /cvsroot/pgsql/configure.in,v 1.184 2002/05/05 00:03:28 tgl Exp $
dnl Developers, please strive to achieve this order:
dnl
......@@ -1168,6 +1168,26 @@ AC_CHECK_TYPES([sig_atomic_t], [], [], [#include <signal.h>])
PGAC_FUNC_POSIX_SIGNALS
# Select semaphore implementation type.
if test x"$USE_NAMED_POSIX_SEMAPHORES" = x"1" ; then
AC_DEFINE(USE_NAMED_POSIX_SEMAPHORES, 1, [Define to select named POSIX semaphores])
SEMA_IMPLEMENTATION="src/backend/port/posix_sema.c"
else
if test x"$USE_UNNAMED_POSIX_SEMAPHORES" = x"1" ; then
AC_DEFINE(USE_UNNAMED_POSIX_SEMAPHORES, 1, [Define to select unnamed POSIX semaphores])
SEMA_IMPLEMENTATION="src/backend/port/posix_sema.c"
else
AC_DEFINE(USE_SYSV_SEMAPHORES, 1, [Define to select SysV-style semaphores])
SEMA_IMPLEMENTATION="src/backend/port/sysv_sema.c"
fi
fi
# Select shared-memory implementation type.
AC_DEFINE(USE_SYSV_SHARED_MEMORY, 1, [Define to select SysV-style shared memory])
SHMEM_IMPLEMENTATION="src/backend/port/sysv_shmem.c"
if test "$enable_nls" = yes ; then
PGAC_CHECK_GETTEXT
fi
......@@ -1222,6 +1242,8 @@ AC_CONFIG_FILES([GNUmakefile src/Makefile.global])
AC_CONFIG_LINKS([
src/backend/port/dynloader.c:src/backend/port/dynloader/${template}.c
src/backend/port/pg_sema.c:${SEMA_IMPLEMENTATION}
src/backend/port/pg_shmem.c:${SHMEM_IMPLEMENTATION}
src/include/dynloader.h:src/backend/port/dynloader/${template}.h
src/include/pg_config_os.h:src/include/port/${template}.h
src/Makefile.port:src/makefiles/Makefile.${template}
......
......@@ -4,7 +4,7 @@
#
# Copyright (c) 1994, Regents of the University of California
#
# $Header: /cvsroot/pgsql/src/backend/Makefile,v 1.77 2002/03/13 00:05:05 petere Exp $
# $Header: /cvsroot/pgsql/src/backend/Makefile,v 1.78 2002/05/05 00:03:28 tgl Exp $
#
#-------------------------------------------------------------------------
......@@ -194,7 +194,7 @@ ifeq ($(enable_nls), yes)
endif
distclean: clean
rm -f port/tas.s port/dynloader.c
rm -f port/tas.s port/dynloader.c port/pg_sema.c port/pg_shmem.c
maintainer-clean: distclean
rm -f $(srcdir)/bootstrap/bootparse.c \
......
......@@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.127 2002/04/27 21:24:33 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.128 2002/05/05 00:03:28 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -33,6 +33,7 @@
#include "catalog/pg_type.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "storage/ipc.h"
#include "storage/proc.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
......
......@@ -13,7 +13,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.17 2002/05/01 23:06:41 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.18 2002/05/05 00:03:28 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -36,6 +36,7 @@
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "storage/backendid.h"
#include "storage/ipc.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/catcache.h"
......
......@@ -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.83 2002/03/06 06:09:29 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/commands/async.c,v 1.84 2002/05/05 00:03:28 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -85,6 +85,7 @@
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "storage/ipc.h"
#include "tcop/tcopprot.h"
#include "utils/fmgroids.h"
#include "utils/ps_status.h"
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/libpq/auth.c,v 1.80 2002/04/04 04:25:47 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/libpq/auth.c,v 1.81 2002/05/05 00:03:28 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -25,6 +25,7 @@
#endif
#include <netinet/in.h>
#include <arpa/inet.h>
#include "libpq/auth.h"
#include "libpq/crypt.h"
#include "libpq/hba.h"
......@@ -32,6 +33,8 @@
#include "libpq/password.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "storage/ipc.h"
static void sendAuthRequest(Port *port, AuthRequest areq);
static int old_be_recvauth(Port *port);
......
......@@ -29,7 +29,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: pqcomm.c,v 1.132 2002/04/21 01:03:33 tgl Exp $
* $Id: pqcomm.c,v 1.133 2002/05/05 00:03:28 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -79,6 +79,7 @@
#include "libpq/libpq.h"
#include "miscadmin.h"
#include "storage/ipc.h"
static void pq_close(void);
......
......@@ -13,7 +13,7 @@
# be converted to Method 2.
#
# IDENTIFICATION
# $Header: /cvsroot/pgsql/src/backend/port/Makefile,v 1.11 2002/03/13 00:05:06 petere Exp $
# $Header: /cvsroot/pgsql/src/backend/port/Makefile,v 1.12 2002/05/05 00:03:28 tgl Exp $
#
#-------------------------------------------------------------------------
......@@ -21,7 +21,7 @@ subdir = src/backend/port
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
OBJS = dynloader.o
OBJS = dynloader.o pg_sema.o pg_shmem.o
OBJS += $(GETHOSTNAME) $(GETRUSAGE) $(INET_ATON) $(ISINF) $(MEMCMP) \
$(MISSING_RANDOM) $(SNPRINTF) $(SRANDOM) $(STRCASECMP) $(STRERROR) \
......
/*-------------------------------------------------------------------------
*
* posix_sema.c
* Implement PGSemaphores using POSIX semaphore facilities
*
* We prefer the unnamed style of POSIX semaphore (the kind made with
* sem_init). We can cope with the kind made with sem_open, however.
*
*
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/port/posix_sema.c,v 1.1 2002/05/05 00:03:28 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include "storage/pg_sema.h"
#ifdef USE_NAMED_POSIX_SEMAPHORES
/* PGSemaphore is pointer to pointer to sem_t */
#define PG_SEM_REF(x) (*(x))
#else
/* PGSemaphore is pointer to sem_t */
#define PG_SEM_REF(x) (x)
#endif
#define IPCProtection (0600) /* access/modify by user only */
static sem_t **mySemPointers; /* keep track of created semaphores */
static int numSems; /* number of semas acquired so far */
static int maxSems; /* allocated size of mySemaPointers array */
static int nextSemKey; /* next name to try */
static void ReleaseSemaphores(int status, Datum arg);
#ifdef USE_NAMED_POSIX_SEMAPHORES
/*
* PosixSemaphoreCreate
*
* Attempt to create a new named semaphore.
*
* If we fail with a failure code other than collision-with-existing-sema,
* print out an error and abort. Other types of errors suggest nonrecoverable
* problems.
*/
static sem_t *
PosixSemaphoreCreate(void)
{
int semKey;
char semname[64];
sem_t *mySem;
for (;;)
{
semKey = nextSemKey++;
snprintf(semname, sizeof(semname), "/pgsql-%d", semKey);
mySem = sem_open(semname, O_CREAT | O_EXCL,
(mode_t) IPCProtection, (unsigned) 1);
if (mySem != SEM_FAILED)
break;
/* Loop if error indicates a collision */
if (errno == EEXIST || errno == EACCES || errno == EINTR)
continue;
/*
* Else complain and abort
*/
fprintf(stderr, "PosixSemaphoreCreate: sem_open(%s) failed: %s\n",
semname, strerror(errno));
proc_exit(1);
}
/*
* Unlink the semaphore immediately, so it can't be accessed externally.
* This also ensures that it will go away if we crash.
*/
sem_unlink(semname);
return mySem;
}
#else /* !USE_NAMED_POSIX_SEMAPHORES */
/*
* PosixSemaphoreCreate
*
* Attempt to create a new unnamed semaphore.
*/
static void
PosixSemaphoreCreate(sem_t *sem)
{
if (sem_init(sem, 1, 1) < 0)
{
fprintf(stderr, "PosixSemaphoreCreate: sem_init failed: %s\n",
strerror(errno));
proc_exit(1);
}
}
#endif /* USE_NAMED_POSIX_SEMAPHORES */
/*
* PosixSemaphoreKill - removes a semaphore
*/
static void
PosixSemaphoreKill(sem_t *sem)
{
#ifdef USE_NAMED_POSIX_SEMAPHORES
/* Got to use sem_close for named semaphores */
if (sem_close(sem) < 0)
fprintf(stderr, "PosixSemaphoreKill: sem_close failed: %s\n",
strerror(errno));
#else
/* Got to use sem_destroy for unnamed semaphores */
if (sem_destroy(sem) < 0)
fprintf(stderr, "PosixSemaphoreKill: sem_destroy failed: %s\n",
strerror(errno));
#endif
}
/*
* PGReserveSemaphores --- initialize semaphore support
*
* This is called during postmaster start or shared memory reinitialization.
* It should do whatever is needed to be able to support up to maxSemas
* subsequent PGSemaphoreCreate calls. Also, if any system resources
* are acquired here or in PGSemaphoreCreate, register an on_shmem_exit
* callback to release them.
*
* The port number is passed for possible use as a key (for Posix, we use
* it to generate the starting semaphore name). In a standalone backend,
* zero will be passed.
*
* In the Posix implementation, we acquire semaphores on-demand; the
* maxSemas parameter is just used to size the array that keeps track of
* acquired semas for subsequent releasing.
*/
void
PGReserveSemaphores(int maxSemas, int port)
{
mySemPointers = (sem_t **) malloc(maxSemas * sizeof(sem_t *));
if (mySemPointers == NULL)
elog(PANIC, "Out of memory in PGReserveSemaphores");
numSems = 0;
maxSems = maxSemas;
nextSemKey = port * 1000;
on_shmem_exit(ReleaseSemaphores, 0);
}
/*
* Release semaphores at shutdown or shmem reinitialization
*
* (called as an on_shmem_exit callback, hence funny argument list)
*/
static void
ReleaseSemaphores(int status, Datum arg)
{
int i;
for (i = 0; i < numSems; i++)
PosixSemaphoreKill(mySemPointers[i]);
free(mySemPointers);
}
/*
* PGSemaphoreCreate
*
* Initialize a PGSemaphore structure to represent a sema with count 1
*/
void
PGSemaphoreCreate(PGSemaphore sema)
{
sem_t *newsem;
/* Can't do this in a backend, because static state is postmaster's */
Assert(!IsUnderPostmaster);
if (numSems >= maxSems)
elog(PANIC, "PGSemaphoreCreate: too many semaphores created");
#ifdef USE_NAMED_POSIX_SEMAPHORES
*sema = newsem = PosixSemaphoreCreate();
#else
PosixSemaphoreCreate(sema);
newsem = sema;
#endif
/* Remember new sema for ReleaseSemaphores */
mySemPointers[numSems++] = newsem;
}
/*
* PGSemaphoreReset
*
* Reset a previously-initialized PGSemaphore to have count 0
*/
void
PGSemaphoreReset(PGSemaphore sema)
{
/*
* There's no direct API for this in POSIX, so we have to ratchet the
* semaphore down to 0 with repeated trywait's.
*/
for (;;)
{
if (sem_trywait(PG_SEM_REF(sema)) < 0)
{
if (errno == EAGAIN || errno == EDEADLK)
break; /* got it down to 0 */
if (errno == EINTR)
continue; /* can this happen? */
fprintf(stderr, "PGSemaphoreReset: sem_trywait failed: %s\n",
strerror(errno));
proc_exit(1);
}
}
}
/*
* PGSemaphoreLock
*
* Lock a semaphore (decrement count), blocking if count would be < 0
*/
void
PGSemaphoreLock(PGSemaphore sema, bool interruptOK)
{
int errStatus;
/*
* Note: if errStatus is -1 and errno == EINTR then it means we
* returned from the operation prematurely because we were sent a
* signal. So we try and lock the semaphore again.
*
* Each time around the loop, we check for a cancel/die interrupt. We
* assume that if such an interrupt comes in while we are waiting, it
* will cause the sem_wait() call to exit with errno == EINTR, so that we
* will be able to service the interrupt (if not in a critical section
* already).
*
* Once we acquire the lock, we do NOT check for an interrupt before
* returning. The caller needs to be able to record ownership of the
* lock before any interrupt can be accepted.
*
* There is a window of a few instructions between CHECK_FOR_INTERRUPTS
* and entering the sem_wait() call. If a cancel/die interrupt occurs in
* that window, we would fail to notice it until after we acquire the
* lock (or get another interrupt to escape the sem_wait()). We can
* avoid this problem by temporarily setting ImmediateInterruptOK to
* true before we do CHECK_FOR_INTERRUPTS; then, a die() interrupt in
* this interval will execute directly. However, there is a huge
* pitfall: there is another window of a few instructions after the
* sem_wait() before we are able to reset ImmediateInterruptOK. If an
* interrupt occurs then, we'll lose control, which means that the
* lock has been acquired but our caller did not get a chance to
* record the fact. Therefore, we only set ImmediateInterruptOK if the
* caller tells us it's OK to do so, ie, the caller does not need to
* record acquiring the lock. (This is currently true for lockmanager
* locks, since the process that granted us the lock did all the
* necessary state updates. It's not true for Posix semaphores used to
* implement LW locks or emulate spinlocks --- but the wait time for
* such locks should not be very long, anyway.)
*/
do
{
ImmediateInterruptOK = interruptOK;
CHECK_FOR_INTERRUPTS();
errStatus = sem_wait(PG_SEM_REF(sema));
ImmediateInterruptOK = false;
} while (errStatus < 0 && errno == EINTR);
if (errStatus < 0)
{
fprintf(stderr, "PGSemaphoreLock: sem_wait failed: %s\n",
strerror(errno));
proc_exit(255);
}
}
/*
* PGSemaphoreUnlock
*
* Unlock a semaphore (increment count)
*/
void
PGSemaphoreUnlock(PGSemaphore sema)
{
int errStatus;
/*
* Note: if errStatus is -1 and errno == EINTR then it means we
* returned from the operation prematurely because we were sent a
* signal. So we try and unlock the semaphore again. Not clear this
* can really happen, but might as well cope.
*/
do
{
errStatus = sem_post(PG_SEM_REF(sema));
} while (errStatus < 0 && errno == EINTR);
if (errStatus < 0)
{
fprintf(stderr, "PGSemaphoreUnlock: sem_post failed: %s\n",
strerror(errno));
proc_exit(255);
}
}
/*
* PGSemaphoreTryLock
*
* Lock a semaphore only if able to do so without blocking
*/
bool
PGSemaphoreTryLock(PGSemaphore sema)
{
int errStatus;
/*
* Note: if errStatus is -1 and errno == EINTR then it means we
* returned from the operation prematurely because we were sent a
* signal. So we try and lock the semaphore again.
*/
do
{
errStatus = sem_trywait(PG_SEM_REF(sema));
} while (errStatus < 0 && errno == EINTR);
if (errStatus < 0)
{
if (errno == EAGAIN || errno == EDEADLK)
return false; /* failed to lock it */
/* Otherwise we got trouble */
fprintf(stderr, "PGSemaphoreTryLock: sem_trywait failed: %s\n",
strerror(errno));
proc_exit(255);
}
return true;
}
This diff is collapsed.
This diff is collapsed.
......@@ -16,7 +16,7 @@
*
* Copyright (c) 2001, PostgreSQL Global Development Group
*
* $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.19 2002/04/03 00:27:25 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.20 2002/05/05 00:03:28 tgl Exp $
* ----------
*/
#include "postgres.h"
......@@ -44,6 +44,7 @@
#include "miscadmin.h"
#include "utils/memutils.h"
#include "storage/backendid.h"
#include "storage/ipc.h"
#include "utils/rel.h"
#include "utils/hsearch.h"
#include "utils/ps_status.h"
......
......@@ -37,7 +37,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.272 2002/04/04 04:25:48 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.273 2002/05/05 00:03:28 tgl Exp $
*
* NOTES
*
......@@ -1361,18 +1361,15 @@ ClosePostmasterPorts(bool pgstat_too)
static void
reset_shared(unsigned short port)
{
/*
* 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.
*/
IpcInitKeyAssignment(port);
/*
* Create or re-create shared memory and semaphores.
*
* Note: in each "cycle of life" we will normally assign the same IPC
* keys (if using SysV shmem and/or semas), since the port number is
* used to determine IPC keys. This helps ensure that we will clean up
* dead IPC objects if the postmaster crashes and is restarted.
*/
CreateSharedMemoryAndSemaphores(false, MaxBackends);
CreateSharedMemoryAndSemaphores(false, MaxBackends, port);
}
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/file/fd.c,v 1.90 2002/03/06 06:10:03 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/storage/file/fd.c,v 1.91 2002/05/05 00:03:28 tgl Exp $
*
* NOTES:
*
......@@ -51,6 +51,7 @@
#include "miscadmin.h"
#include "storage/fd.h"
#include "storage/ipc.h"
/* Filename components for OpenTemporaryFile */
......
This diff is collapsed.
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/ipci.c,v 1.46 2002/03/02 21:39:29 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/ipci.c,v 1.47 2002/05/05 00:03:28 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -21,8 +21,11 @@
#include "access/xlog.h"
#include "storage/bufmgr.h"
#include "storage/freespace.h"
#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/lwlock.h"
#include "storage/pg_sema.h"
#include "storage/pg_shmem.h"
#include "storage/pmsignal.h"
#include "storage/proc.h"
#include "storage/sinval.h"
......@@ -41,9 +44,12 @@
* memory. This is true for a standalone backend, false for a postmaster.
*/
void
CreateSharedMemoryAndSemaphores(bool makePrivate, int maxBackends)
CreateSharedMemoryAndSemaphores(bool makePrivate,
int maxBackends,
int port)
{
int size;
int numSemas;
PGShmemHeader *seghdr;
/*
......@@ -70,12 +76,14 @@ CreateSharedMemoryAndSemaphores(bool makePrivate, int maxBackends)
/*
* Create the shmem segment
*/
seghdr = IpcMemoryCreate(size, makePrivate, IPCProtection);
seghdr = PGSharedMemoryCreate(size, makePrivate, port);
/*
* First initialize spinlocks --- needed by InitShmemAllocation()
* Create semaphores
*/
CreateSpinlocks();
numSemas = ProcGlobalSemas(maxBackends);
numSemas += SpinlockSemas();
PGReserveSemaphores(numSemas, port);
/*
* Set up shared memory allocation mechanism
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmem.c,v 1.64 2002/03/06 06:10:05 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmem.c,v 1.65 2002/05/05 00:03:28 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -61,6 +61,7 @@
#include "postgres.h"
#include "access/transam.h"
#include "storage/pg_shmem.h"
#include "storage/spin.h"
#include "utils/tqual.h"
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/sinvaladt.c,v 1.45 2002/03/02 23:35:57 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/sinvaladt.c,v 1.46 2002/05/05 00:03:28 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -16,6 +16,7 @@
#include "miscadmin.h"
#include "storage/backendid.h"
#include "storage/ipc.h"
#include "storage/pmsignal.h"
#include "storage/proc.h"
#include "storage/sinvaladt.h"
......
......@@ -15,7 +15,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lwlock.c,v 1.9 2002/03/02 21:39:29 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lwlock.c,v 1.10 2002/05/05 00:03:28 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -302,7 +302,7 @@ LWLockAcquire(LWLockId lockid, LWLockMode mode)
for (;;)
{
/* "false" means cannot accept cancel/die interrupt here. */
IpcSemaphoreLock(proc->sem.semId, proc->sem.semNum, false);
PGSemaphoreLock(&proc->sem, false);
if (!proc->lwWaiting)
break;
extraWaits++;
......@@ -325,7 +325,7 @@ LWLockAcquire(LWLockId lockid, LWLockMode mode)
* Fix the process wait semaphore's count for any absorbed wakeups.
*/
while (extraWaits-- > 0)
IpcSemaphoreUnlock(proc->sem.semId, proc->sem.semNum);
PGSemaphoreUnlock(&proc->sem);
}
/*
......@@ -485,7 +485,7 @@ LWLockRelease(LWLockId lockid)
head = proc->lwWaitLink;
proc->lwWaitLink = NULL;
proc->lwWaiting = false;
IpcSemaphoreUnlock(proc->sem.semId, proc->sem.semNum);
PGSemaphoreUnlock(&proc->sem);
}
/*
......
This diff is collapsed.
......@@ -6,7 +6,8 @@
*
* For machines that have test-and-set (TAS) instructions, s_lock.h/.c
* define the spinlock implementation. This file contains only a stub
* implementation for spinlocks using SysV semaphores. The semaphore method
* implementation for spinlocks using PGSemaphores. Unless semaphores
* are implemented in a way that doesn't involve a kernel call, this
* is too slow to be very useful :-(
*
*
......@@ -15,143 +16,49 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/spin.c,v 1.7 2001/11/05 17:46:28 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/spin.c,v 1.8 2002/05/05 00:03:28 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include <errno.h>
#include "storage/ipc.h"
/* In Ultrix, sem.h and shm.h must be included AFTER ipc.h */
#ifdef HAVE_SYS_SEM_H
#include <sys/sem.h>
#endif
#if defined(__darwin__)
#include "port/darwin/sem.h"
#endif
#include "storage/lwlock.h"
#include "storage/proc.h"
#include "storage/pg_sema.h"
#include "storage/spin.h"
#ifdef HAS_TEST_AND_SET
/*
* CreateSpinlocks --- create and initialize spinlocks during startup
* Report number of semaphores needed to support spinlocks.
*/
void
CreateSpinlocks(void)
int
SpinlockSemas(void)
{
/* no-op when we have TAS spinlocks */
return 0;
}
#else /* !HAS_TEST_AND_SET */
/*
* No TAS, so spinlocks are implemented using SysV semaphores.
*
* 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 shmem initialization.
* No TAS, so spinlocks are implemented as PGSemaphores.
*/
#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);
/*
* CreateSpinlocks --- create and initialize spinlocks during startup
* Report number of semaphores needed to support spinlocks.
*/
void
CreateSpinlocks(void)
int
SpinlockSemas(void)
{
int i;
if (SpinLockIds == NULL)
{
/*
* Compute number of spinlocks needed. It would be cleaner to
* distribute this logic into the affected modules, similar to the
* way shmem space estimation is handled.
*
* For now, though, we just need a few spinlocks (10 should be
* plenty) plus one for each LWLock.
*/
numSpinLocks = NumLWLocks() + 10;
/* 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.
* It would be cleaner to distribute this logic into the affected modules,
* similar to the way shmem space estimation is handled.
*
* For now, though, we just need a few spinlocks (10 should be
* plenty) plus one for each LWLock.
*/
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);
}
/* Init counter for allocating dynamic spinlocks */
nextSpinLock = 0;
}
/*
* 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]);
}
free(SpinLockIds);
SpinLockIds = NULL;
return NumLWLocks() + 10;
}
/*
......@@ -161,30 +68,28 @@ SpinFreeAllSemaphores(void)
void
s_init_lock_sema(volatile slock_t *lock)
{
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++;
PGSemaphoreCreate((PGSemaphore) lock);
}
void
s_unlock_sema(volatile slock_t *lock)
{
IpcSemaphoreUnlock(lock->semId, lock->sem);
PGSemaphoreUnlock((PGSemaphore) lock);
}
bool
s_lock_free_sema(volatile slock_t *lock)
{
return IpcSemaphoreGetValue(lock->semId, lock->sem) > 0;
/* We don't currently use S_LOCK_FREE anyway */
elog(ERROR, "spin.c does not support S_LOCK_FREE()");
return false;
}
int
tas_sema(volatile slock_t *lock)
{
/* Note that TAS macros return 0 if *success* */
return !IpcSemaphoreTryLock(lock->semId, lock->sem);
return !PGSemaphoreTryLock((PGSemaphore) lock);
}
#endif /* !HAS_TEST_AND_SET */
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.262 2002/04/20 21:56:15 petere Exp $
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.263 2002/05/05 00:03:28 tgl Exp $
*
* NOTES
* this is the "main" module of the postgres backend and
......@@ -49,11 +49,12 @@
#include "parser/parse.h"
#include "parser/parser.h"
#include "rewrite/rewriteHandler.h"
#include "storage/ipc.h"
#include "storage/proc.h"
#include "tcop/fastpath.h"
#include "tcop/pquery.h"
#include "tcop/tcopprot.h"
#include "tcop/utility.h"
#include "storage/proc.h"
#include "utils/exc.h"
#include "utils/guc.h"
#include "utils/memutils.h"
......@@ -1692,7 +1693,7 @@ PostgresMain(int argc, char *argv[], const char *username)
if (!IsUnderPostmaster)
{
puts("\nPOSTGRES backend interactive interface ");
puts("$Revision: 1.262 $ $Date: 2002/04/20 21:56:15 $\n");
puts("$Revision: 1.263 $ $Date: 2002/05/05 00:03:28 $\n");
}
/*
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/error/elog.c,v 1.96 2002/04/21 00:22:52 ishii Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/error/elog.c,v 1.97 2002/05/05 00:03:29 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -30,6 +30,7 @@
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "storage/ipc.h"
#include "storage/proc.h"
#include "tcop/tcopprot.h"
#include "utils/memutils.h"
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.88 2002/05/03 20:43:30 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.89 2002/05/05 00:03:29 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -32,6 +32,8 @@
#include "catalog/pg_shadow.h"
#include "libpq/libpq-be.h"
#include "miscadmin.h"
#include "storage/ipc.h"
#include "storage/pg_shmem.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
......@@ -819,25 +821,24 @@ CreateLockFile(const char *filename, bool amPostmaster,
if (isDDLock)
{
char *ptr;
unsigned long shmKey,
shmId;
unsigned long id1,
id2;
ptr = strchr(buffer, '\n');
if (ptr != NULL &&
(ptr = strchr(ptr + 1, '\n')) != NULL)
{
ptr++;
if (sscanf(ptr, "%lu %lu", &shmKey, &shmId) == 2)
if (sscanf(ptr, "%lu %lu", &id1, &id2) == 2)
{
if (SharedMemoryIsInUse((IpcMemoryKey) shmKey,
(IpcMemoryId) shmId))
if (PGSharedMemoryIsInUse(id1, id2))
{
fprintf(stderr,
"Found a pre-existing shared memory block (ID %d) still in use.\n"
"Found a pre-existing shared memory block (key %lu, id %lu) still in use.\n"
"If you're sure there are no old backends still running,\n"
"remove the shared memory block with ipcrm(1), or just\n"
"delete \"%s\".\n",
(int) shmId, filename);
id1, id2, filename);
return false;
}
}
......@@ -941,11 +942,11 @@ TouchSocketLockFile(void)
* This may be called multiple times in the life of a postmaster, if we
* delete and recreate shmem due to backend crash. Therefore, be prepared
* to overwrite existing information. (As of 7.1, a postmaster only creates
* one shm seg anyway; but for the purposes here, if we did have more than
* one shm seg at a time; but for the purposes here, if we did have more than
* one then any one of them would do anyway.)
*/
void
RecordSharedMemoryInLockFile(IpcMemoryKey shmKey, IpcMemoryId shmId)
RecordSharedMemoryInLockFile(unsigned long id1, unsigned long id2)
{
int fd;
int len;
......@@ -988,11 +989,10 @@ RecordSharedMemoryInLockFile(IpcMemoryKey shmKey, IpcMemoryId shmId)
ptr++;
/*
* Append shm key and ID. Format to try to keep it the same length
* Append key information. Format to try to keep it the same length
* always (trailing junk won't hurt, but might confuse humans).
*/
sprintf(ptr, "%9lu %9lu\n",
(unsigned long) shmKey, (unsigned long) shmId);
sprintf(ptr, "%9lu %9lu\n", id1, id2);
/*
* And rewrite the data. Since we write in a single kernel call, this
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.103 2002/04/27 21:24:34 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.104 2002/05/05 00:03:29 tgl Exp $
*
*
*-------------------------------------------------------------------------
......@@ -32,6 +32,7 @@
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "storage/backendid.h"
#include "storage/ipc.h"
#include "storage/proc.h"
#include "storage/sinval.h"
#include "storage/smgr.h"
......@@ -181,7 +182,7 @@ InitCommunication(void)
* postmaster. Create private "shmem" and semaphores. Setting
* MaxBackends = 16 is arbitrary.
*/
CreateSharedMemoryAndSemaphores(true, 16);
CreateSharedMemoryAndSemaphores(true, 16, 0);
}
}
......
......@@ -12,7 +12,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: miscadmin.h,v 1.102 2002/04/04 04:25:51 momjian Exp $
* $Id: miscadmin.h,v 1.103 2002/05/05 00:03:29 tgl Exp $
*
* NOTES
* some of the information in this file should be moved to
......@@ -23,7 +23,8 @@
#ifndef MISCADMIN_H
#define MISCADMIN_H
#include "storage/ipc.h"
#include <sys/types.h>
/*****************************************************************************
* System interrupt and critical section handling
......@@ -291,8 +292,8 @@ extern void BaseInit(void);
extern bool CreateDataDirLockFile(const char *datadir, bool amPostmaster);
extern bool CreateSocketLockFile(const char *socketfile, bool amPostmaster);
extern void TouchSocketLockFile(void);
extern void RecordSharedMemoryInLockFile(IpcMemoryKey shmKey,
IpcMemoryId shmId);
extern void RecordSharedMemoryInLockFile(unsigned long id1,
unsigned long id2);
extern void ValidatePgVersion(const char *path);
......
......@@ -8,7 +8,7 @@
* or in pg_config.h afterwards. Of course, if you edit pg_config.h, then your
* changes will be overwritten the next time you run configure.
*
* $Id: pg_config.h.in,v 1.23 2002/04/21 19:48:19 thomas Exp $
* $Id: pg_config.h.in,v 1.24 2002/05/05 00:03:29 tgl Exp $
*/
#ifndef PG_CONFIG_H
......@@ -711,6 +711,15 @@ extern int fdatasync(int fildes);
/* Define if you have syslog() */
#undef HAVE_SYSLOG
/* Define exactly one of these symbols to select semaphore implementation */
#undef USE_NAMED_POSIX_SEMAPHORES
#undef USE_UNNAMED_POSIX_SEMAPHORES
#undef USE_SYSV_SEMAPHORES
/* Define exactly one of these symbols to select shared-mem implementation */
#undef USE_SYSV_SHARED_MEMORY
/*
*------------------------------------------------------------------------
* Part 4: pull in system-specific declarations.
......
......@@ -3,63 +3,21 @@
* ipc.h
* POSTGRES inter-process communication definitions.
*
* This file is misnamed, as it no longer has much of anything directly
* to do with IPC. The functionality here is concerned with managing
* exit-time cleanup for either a postmaster or a backend.
*
*
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: ipc.h,v 1.54 2001/11/05 17:46:35 momjian Exp $
* $Id: ipc.h,v 1.55 2002/05/05 00:03:29 tgl Exp $
*
* Some files that would normally need to include only sys/ipc.h must
* 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.
*-------------------------------------------------------------------------
*/
#ifndef IPC_H
#define IPC_H
#include <sys/types.h>
#ifdef HAVE_SYS_IPC_H
#include <sys/ipc.h>
#endif /* HAVE_SYS_IPC_H */
#ifndef HAVE_UNION_SEMUN
union semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
};
#endif
/* generic IPC definitions */
#define IPCProtection (0600) /* access/modify by user only */
/* semaphore definitions */
typedef uint32 IpcSemaphoreKey; /* semaphore key passed to semget(2) */
typedef int IpcSemaphoreId; /* semaphore ID returned by semget(2) */
#define IPC_NMAXSEM 32 /* maximum number of semaphores per semID */
#define PGSemaMagic 537 /* must be less than SEMVMX */
/* shared memory definitions */
typedef uint32 IpcMemoryKey; /* shared memory key passed to shmget(2) */
typedef int IpcMemoryId; /* shared memory ID returned by shmget(2) */
typedef struct PGShmemHeader /* 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;
/* ipc.c */
extern bool proc_exit_inprogress;
......@@ -70,24 +28,9 @@ extern void on_proc_exit(void (*function) (), Datum arg);
extern void on_shmem_exit(void (*function) (), Datum arg);
extern void on_exit_reset(void);
extern void IpcInitKeyAssignment(int port);
extern IpcSemaphoreId IpcSemaphoreCreate(int numSems, int permission,
int semStartValue,
bool removeOnExit);
extern void IpcSemaphoreKill(IpcSemaphoreId semId);
extern void IpcSemaphoreLock(IpcSemaphoreId semId, int sem, bool interruptOK);
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 makePrivate,
int permission);
extern bool SharedMemoryIsInUse(IpcMemoryKey shmKey, IpcMemoryId shmId);
/* ipci.c */
extern void CreateSharedMemoryAndSemaphores(bool makePrivate,
int maxBackends);
int maxBackends,
int port);
#endif /* IPC_H */
/*-------------------------------------------------------------------------
*
* pg_sema.h
* Platform-independent API for semaphores.
*
* PostgreSQL requires counting semaphores (the kind that keep track of
* multiple unlock operations, and will allow an equal number of subsequent
* lock operations before blocking). The underlying implementation is
* not the same on every platform. This file defines the API that must
* be provided by each port.
*
*
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: pg_sema.h,v 1.1 2002/05/05 00:03:29 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#ifndef PG_SEMA_H
#define PG_SEMA_H
/*
* PGSemaphoreData and pointer type PGSemaphore are the data structure
* representing an individual semaphore. The contents of PGSemaphoreData
* vary across implementations and must never be touched by platform-
* independent code. PGSemaphoreData structures are always allocated
* in shared memory (to support implementations where the data changes during
* lock/unlock).
*
* pg_config.h must define exactly one of the USE_xxx_SEMAPHORES symbols.
*/
#ifdef USE_NAMED_POSIX_SEMAPHORES
#include <semaphore.h>
typedef sem_t *PGSemaphoreData;
#endif
#ifdef USE_UNNAMED_POSIX_SEMAPHORES
#include <semaphore.h>
typedef sem_t PGSemaphoreData;
#endif
#ifdef USE_SYSV_SEMAPHORES
typedef struct PGSemaphoreData
{
int semId; /* semaphore set identifier */
int semNum; /* semaphore number within set */
} PGSemaphoreData;
#endif
typedef PGSemaphoreData *PGSemaphore;
/* Module initialization (called during postmaster start or shmem reinit) */
extern void PGReserveSemaphores(int maxSemas, int port);
/* Initialize a PGSemaphore structure to represent a sema with count 1 */
extern void PGSemaphoreCreate(PGSemaphore sema);
/* Reset a previously-initialized PGSemaphore to have count 0 */
extern void PGSemaphoreReset(PGSemaphore sema);
/* Lock a semaphore (decrement count), blocking if count would be < 0 */
extern void PGSemaphoreLock(PGSemaphore sema, bool interruptOK);
/* Unlock a semaphore (increment count) */
extern void PGSemaphoreUnlock(PGSemaphore sema);
/* Lock a semaphore only if able to do so without blocking */
extern bool PGSemaphoreTryLock(PGSemaphore sema);
#endif /* PG_SEMA_H */
/*-------------------------------------------------------------------------
*
* pg_shmem.h
* Platform-independent API for shared memory support.
*
* Every port is expected to support shared memory with approximately
* SysV-ish semantics; in particular, a memory block is not anonymous
* but has an ID, and we must be able to tell whether there are any
* remaining processes attached to a block of a specified ID.
*
* To simplify life for the SysV implementation, the ID is assumed to
* consist of two unsigned long values (these are key and ID in SysV
* terms). Other platforms may ignore the second value if they need
* only one ID number.
*
*
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: pg_shmem.h,v 1.1 2002/05/05 00:03:29 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#ifndef PG_SHMEM_H
#define PG_SHMEM_H
#include <sys/types.h>
typedef struct PGShmemHeader /* 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;
extern PGShmemHeader *PGSharedMemoryCreate(uint32 size, bool makePrivate,
int port);
extern bool PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2);
#endif /* PG_SHMEM_H */
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: proc.h,v 1.54 2001/11/06 00:38:26 tgl Exp $
* $Id: proc.h,v 1.55 2002/05/05 00:03:29 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -16,16 +16,10 @@
#include "access/xlog.h"
#include "storage/backendid.h"
#include "storage/ipc.h"
#include "storage/lock.h"
#include "storage/pg_sema.h"
typedef struct
{
IpcSemaphoreId semId; /* SysV semaphore set ID */
int semNum; /* semaphore number within set */
} SEMA;
/*
* Each backend has a PROC struct in shared memory. There is also a list of
* currently-unused PROC structs that will be reallocated to new backends.
......@@ -39,7 +33,7 @@ struct PROC
/* proc->links MUST BE FIRST IN STRUCT (see ProcSleep,ProcWakeup,etc) */
SHM_QUEUE links; /* list link if process is in a list */
SEMA sem; /* ONE semaphore to sleep on */
PGSemaphoreData sem; /* ONE semaphore to sleep on */
int errType; /* STATUS_OK or STATUS_ERROR after wakeup */
TransactionId xid; /* transaction currently being executed by
......@@ -84,47 +78,11 @@ extern PROC *MyProc;
/*
* 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. Note that we need one extra
* semaphore (see storage/lmgr/proc.c), so the computation may look wrong,
* but it's right.
*/
#define PROC_NSEMS_PER_SET 16
#define PROC_SEM_MAP_ENTRIES(maxBackends) ((maxBackends)/PROC_NSEMS_PER_SET+1)
typedef struct
{
/* info about a single set of per-process semaphores */
IpcSemaphoreId procSemId;
int32 freeSemMap;
/*
* In freeSemMap, bit i is set if the i'th semaphore of this sema set
* is allocated to a process. (i counts from 0 at the LSB)
*/
} SEM_MAP_ENTRY;
typedef struct PROC_HDR
{
/* Head of list of free PROC structures */
SHMEM_OFFSET freeProcs;
/* Info about semaphore sets used for per-process semaphores */
int semMapEntries;
/*
* VARIABLE LENGTH ARRAY: actual length is semMapEntries. THIS MUST BE
* LAST IN THE STRUCT DECLARATION.
*/
SEM_MAP_ENTRY procSemMap[1];
} PROC_HDR;
......@@ -135,6 +93,7 @@ extern int DeadlockTimeout;
/*
* Function Prototypes
*/
extern int ProcGlobalSemas(int maxBackends);
extern void InitProcGlobal(int maxBackends);
extern void InitProcess(void);
extern void InitDummyProcess(void);
......
......@@ -63,14 +63,14 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: s_lock.h,v 1.97 2002/01/29 15:44:42 tgl Exp $
* $Id: s_lock.h,v 1.98 2002/05/05 00:03:29 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#ifndef S_LOCK_H
#define S_LOCK_H
#include "storage/ipc.h"
#include "storage/pg_sema.h"
#if defined(HAS_TEST_AND_SET)
......@@ -438,7 +438,6 @@ extern slock_t wc_tas(volatile slock_t *lock);
* AIX (POWER)
*
* Note that slock_t on POWER/POWER2/PowerPC is int instead of char
* (see storage/ipc.h).
*/
#define TAS(lock) cs((int *) (lock), 0, 1)
#endif /* _AIX */
......@@ -462,17 +461,11 @@ extern slock_t wc_tas(volatile slock_t *lock);
#else /* !HAS_TEST_AND_SET */
/*
* Fake spinlock implementation using SysV semaphores --- slow and prone
* Fake spinlock implementation using semaphores --- slow and prone
* to fall foul of kernel limits on number of semaphores, so don't use this
* unless you must! The subroutines appear in spin.c.
*/
typedef struct
{
/* reference to semaphore used to implement this spinlock */
IpcSemaphoreId semId;
int sem;
} slock_t;
typedef PGSemaphoreData slock_t;
extern bool s_lock_free_sema(volatile slock_t *lock);
extern void s_unlock_sema(volatile slock_t *lock);
......
......@@ -40,7 +40,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: spin.h,v 1.19 2001/11/05 17:46:35 momjian Exp $
* $Id: spin.h,v 1.20 2002/05/05 00:03:29 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -72,6 +72,6 @@
#define SpinLockFree(lock) S_LOCK_FREE(lock)
extern void CreateSpinlocks(void);
extern int SpinlockSemas(void);
#endif /* SPIN_H */
......@@ -21,22 +21,6 @@
#include "catalog/pg_type.h"
/* perl stuff */
/*
* Evil Code Alert
*
* both posgreSQL and perl try to do 'the right thing'
* and provide union semun if the platform doesn't define
* it in a system header.
* psql uses HAVE_UNION_SEMUN
* perl uses HAS_UNION_SEMUN
* together, they cause compile errors.
* If we need it, the psql headers above will provide it.
* So we tell perl that we have it.
*/
#ifndef HAS_UNION_SEMUN
#define HAS_UNION_SEMUN
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
......
......@@ -33,7 +33,7 @@
* ENHANCEMENTS, OR MODIFICATIONS.
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/pl/plperl/plperl.c,v 1.29 2002/03/06 18:50:26 momjian Exp $
* $Header: /cvsroot/pgsql/src/pl/plperl/plperl.c,v 1.30 2002/05/05 00:03:29 tgl Exp $
*
**********************************************************************/
......@@ -62,22 +62,6 @@
#include "catalog/pg_type.h"
/* perl stuff */
/*
* Evil Code Alert
*
* both posgreSQL and perl try to do 'the right thing'
* and provide union semun if the platform doesn't define
* it in a system header.
* psql uses HAVE_UNION_SEMUN
* perl uses HAS_UNION_SEMUN
* together, they cause compile errors.
* If we need it, the psql headers above will provide it.
* So we tell perl that we have it.
*/
#ifndef HAS_UNION_SEMUN
#define HAS_UNION_SEMUN
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
......
# -traditional-cpp means "don't use apple's cpp-precomp" on darwin
# this should change to -no-cpp-precomp when that flag is implemented
CC="$CC -traditional-cpp"
# Select appropriate semaphore support
USE_NAMED_POSIX_SEMAPHORES=1
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