* Wait until naptime expires or we get some type of signal (all the
* signal handlers will wake us by calling SetLatch).
*/
- rc = WaitLatch(&MyProc->procLatch,
+ rc = WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
(nap.tv_sec * 1000L) + (nap.tv_usec / 1000L));
- ResetLatch(&MyProc->procLatch);
+ ResetLatch(MyLatch);
DisableCatchupInterrupt();
int save_errno = errno;
got_SIGHUP = true;
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
int save_errno = errno;
got_SIGUSR2 = true;
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
int save_errno = errno;
got_SIGTERM = true;
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
if (status != BGWH_NOT_YET_STARTED)
break;
- rc = WaitLatch(&MyProc->procLatch,
+ rc = WaitLatch(MyLatch,
WL_LATCH_SET | WL_POSTMASTER_DEATH, 0);
if (rc & WL_POSTMASTER_DEATH)
break;
}
- ResetLatch(&MyProc->procLatch);
+ ResetLatch(MyLatch);
}
}
PG_CATCH();
int rc;
/* Clear any already-pending wakeups */
- ResetLatch(&MyProc->procLatch);
+ ResetLatch(MyLatch);
if (got_SIGHUP)
{
* down with latch events that are likely to happen frequently during
* normal operation.
*/
- rc = WaitLatch(&MyProc->procLatch,
+ rc = WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
BgWriterDelay /* ms */ );
/* Ask for notification at next buffer allocation */
StrategyNotifyBgWriter(MyProc->pgprocno);
/* Sleep ... */
- rc = WaitLatch(&MyProc->procLatch,
+ rc = WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
BgWriterDelay * HIBERNATE_FACTOR);
/* Reset the notification request in case we timed out */
int save_errno = errno;
got_SIGHUP = true;
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
int save_errno = errno;
shutdown_requested = true;
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
int rc;
/* Clear any already-pending wakeups */
- ResetLatch(&MyProc->procLatch);
+ ResetLatch(MyLatch);
/*
* Process any requests or signals received recently.
cur_timeout = Min(cur_timeout, XLogArchiveTimeout - elapsed_secs);
}
- rc = WaitLatch(&MyProc->procLatch,
+ rc = WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
cur_timeout * 1000L /* convert to ms */ );
int save_errno = errno;
got_SIGHUP = true;
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
int save_errno = errno;
checkpoint_requested = true;
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
int save_errno = errno;
shutdown_requested = true;
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
static volatile sig_atomic_t wakened = false;
static volatile sig_atomic_t ready_to_stop = false;
-/*
- * Latch used by signal handlers to wake up the sleep in the main loop.
- */
-static Latch mainloop_latch;
-
/* ----------
* Local function forward declarations
* ----------
NON_EXEC_STATIC void
PgArchiverMain(int argc, char *argv[])
{
- InitializeLatchSupport(); /* needed for latch waits */
-
- InitLatch(&mainloop_latch); /* initialize latch used in main loop */
-
/*
* Ignore all signals usually bound to some action in the postmaster,
* except for SIGHUP, SIGTERM, SIGUSR1, SIGUSR2, and SIGQUIT.
/* set flag to re-read config file at next convenient time */
got_SIGHUP = true;
- SetLatch(&mainloop_latch);
+ SetLatch(MyLatch);
errno = save_errno;
}
* archive commands.
*/
got_SIGTERM = true;
- SetLatch(&mainloop_latch);
+ SetLatch(MyLatch);
errno = save_errno;
}
/* set flag that there is work to be done */
wakened = true;
- SetLatch(&mainloop_latch);
+ SetLatch(MyLatch);
errno = save_errno;
}
/* set flag to do a final cycle and shut down afterwards */
ready_to_stop = true;
- SetLatch(&mainloop_latch);
+ SetLatch(MyLatch);
errno = save_errno;
}
*/
do
{
- ResetLatch(&mainloop_latch);
+ ResetLatch(MyLatch);
/* When we get SIGUSR2, we do one more archive cycle, then exit */
time_to_stop = ready_to_stop;
{
int rc;
- rc = WaitLatch(&mainloop_latch,
+ rc = WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
timeout * 1000L);
if (rc & WL_TIMEOUT)
*/
NON_EXEC_STATIC pgsocket pgStatSock = PGINVALID_SOCKET;
-static Latch pgStatLatch;
-
static struct sockaddr_storage pgStatAddr;
static time_t last_pgstat_start_time;
PgStat_Msg msg;
int wr;
- InitializeLatchSupport(); /* needed for latch waits */
-
- /* Initialize private latch for use by signal handlers */
- InitLatch(&pgStatLatch);
-
/*
* Ignore all signals usually bound to some action in the postmaster,
* except SIGHUP and SIGQUIT. Note we don't need a SIGUSR1 handler to
- * support latch operations, because pgStatLatch is local not shared.
+ * support latch operations, because we only use a local latch.
*/
pqsignal(SIGHUP, pgstat_sighup_handler);
pqsignal(SIGINT, SIG_IGN);
for (;;)
{
/* Clear any already-pending wakeups */
- ResetLatch(&pgStatLatch);
+ ResetLatch(MyLatch);
/*
* Quit if we get SIGQUIT from the postmaster.
/* Sleep until there's something to do */
#ifndef WIN32
- wr = WaitLatchOrSocket(&pgStatLatch,
+ wr = WaitLatchOrSocket(MyLatch,
WL_LATCH_SET | WL_POSTMASTER_DEATH | WL_SOCKET_READABLE,
pgStatSock,
-1L);
* to not provoke "pgstat wait timeout" complaints from
* backend_read_statsfile.
*/
- wr = WaitLatchOrSocket(&pgStatLatch,
+ wr = WaitLatchOrSocket(MyLatch,
WL_LATCH_SET | WL_POSTMASTER_DEATH | WL_SOCKET_READABLE | WL_TIMEOUT,
pgStatSock,
2 * 1000L /* msec */ );
int save_errno = errno;
need_exit = true;
- SetLatch(&pgStatLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
int save_errno = errno;
got_SIGHUP = true;
- SetLatch(&pgStatLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
NON_EXEC_STATIC pg_time_t first_syslogger_file_time = 0;
static char *last_file_name = NULL;
static char *last_csv_file_name = NULL;
-static Latch sysLoggerLatch;
/*
* Buffers for saving partial messages from different backends.
syslogPipe[1] = 0;
#endif
- InitializeLatchSupport(); /* needed for latch waits */
-
-
- /* Initialize private latch for use by signal handlers */
- InitLatch(&sysLoggerLatch);
-
/*
* Properly accept or ignore signals the postmaster might send us
*
#endif
/* Clear any already-pending wakeups */
- ResetLatch(&sysLoggerLatch);
+ ResetLatch(MyLatch);
/*
* Process any requests or signals received recently.
* Sleep until there's something to do
*/
#ifndef WIN32
- rc = WaitLatchOrSocket(&sysLoggerLatch,
+ rc = WaitLatchOrSocket(MyLatch,
WL_LATCH_SET | WL_SOCKET_READABLE | cur_flags,
syslogPipe[0],
cur_timeout);
*/
LeaveCriticalSection(&sysloggerSection);
- (void) WaitLatch(&sysLoggerLatch,
+ (void) WaitLatch(MyLatch,
WL_LATCH_SET | cur_flags,
cur_timeout);
{
if (ftell(syslogFile) >= Log_RotationSize * 1024L ||
(csvlogFile != NULL && ftell(csvlogFile) >= Log_RotationSize * 1024L))
- SetLatch(&sysLoggerLatch);
+ SetLatch(MyLatch);
}
LeaveCriticalSection(&sysloggerSection);
}
flush_pipe_input(logbuffer, &bytes_in_logbuffer);
/* set the latch to waken the main thread, which will quit */
- SetLatch(&sysLoggerLatch);
+ SetLatch(MyLatch);
LeaveCriticalSection(&sysloggerSection);
_endthread();
int save_errno = errno;
got_SIGHUP = true;
- SetLatch(&sysLoggerLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
int save_errno = errno;
rotation_requested = true;
- SetLatch(&sysLoggerLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
}
/* Clear any already-pending wakeups */
- ResetLatch(&MyProc->procLatch);
+ ResetLatch(MyLatch);
/*
* Process any requests or signals received recently.
else
cur_timeout = WalWriterDelay * HIBERNATE_FACTOR;
- rc = WaitLatch(&MyProc->procLatch,
+ rc = WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
cur_timeout);
int save_errno = errno;
got_SIGHUP = true;
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
int save_errno = errno;
shutdown_requested = true;
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
int syncRepState;
/* Must reset the latch before testing state. */
- ResetLatch(&MyProc->procLatch);
+ ResetLatch(MyLatch);
/*
* Try checking the state without the lock first. There's no
* Wait on latch. Any condition that should wake us up will set the
* latch, so no need for timeout.
*/
- WaitLatch(&MyProc->procLatch, WL_LATCH_SET | WL_POSTMASTER_DEATH, -1);
+ WaitLatch(MyLatch, WL_LATCH_SET | WL_POSTMASTER_DEATH, -1);
}
/*
if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN))
RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN);
- if (set_latch_on_sigusr1 && MyProc != NULL)
- SetLatch(&MyProc->procLatch);
+ if (set_latch_on_sigusr1)
+ SetLatch(MyLatch);
latch_sigusr1_handler();
* at top of loop, because setting an already-set latch is much
* cheaper than setting one that has been reset.
*/
- WaitLatch(&MyProc->procLatch, WL_LATCH_SET, 0);
+ WaitLatch(MyLatch, WL_LATCH_SET, 0);
/* An interrupt may have occurred while we were waiting. */
CHECK_FOR_INTERRUPTS();
/* Reset the latch so we don't spin. */
- ResetLatch(&MyProc->procLatch);
+ ResetLatch(MyLatch);
}
else
{
* loop, because setting an already-set latch is much cheaper than
* setting one that has been reset.
*/
- WaitLatch(&MyProc->procLatch, WL_LATCH_SET, 0);
+ WaitLatch(MyLatch, WL_LATCH_SET, 0);
/* An interrupt may have occurred while we were waiting. */
CHECK_FOR_INTERRUPTS();
/* Reset the latch so we don't spin. */
- ResetLatch(&MyProc->procLatch);
+ ResetLatch(MyLatch);
}
}
}
/* Wait to be signalled. */
- WaitLatch(&MyProc->procLatch, WL_LATCH_SET, 0);
+ WaitLatch(MyLatch, WL_LATCH_SET, 0);
/* An interrupt may have occurred while we were waiting. */
CHECK_FOR_INTERRUPTS();
/* Reset the latch so we don't spin. */
- ResetLatch(&MyProc->procLatch);
+ ResetLatch(MyLatch);
}
}
PG_CATCH();
if (MyProc != NULL)
elog(ERROR, "you already exist");
- /*
- * Initialize process-local latch support. This could fail if the kernel
- * is low on resources, and if so we want to exit cleanly before acquiring
- * any shared-memory resources.
- */
- InitializeLatchSupport();
-
/*
* Try to get a proc struct from the free list. If this fails, we must be
* out of PGPROC structures (not to mention semaphores).
SHMQueueElemInit(&(MyProc->syncRepLinks));
/*
- * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch.
- * Note that there's no particular need to do ResetLatch here.
+ * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch
+ * on it. That allows us to repoint the process latch, which so far
+ * points to process local one, to the shared one.
*/
OwnLatch(&MyProc->procLatch);
+ SwitchToSharedLatch();
/*
* We might be reusing a semaphore that belonged to a failed process. So
if (MyProc != NULL)
elog(ERROR, "you already exist");
- /*
- * Initialize process-local latch support. This could fail if the kernel
- * is low on resources, and if so we want to exit cleanly before acquiring
- * any shared-memory resources.
- */
- InitializeLatchSupport();
-
/*
* We use the ProcStructLock to protect assignment and releasing of
* AuxiliaryProcs entries.
#endif
/*
- * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch.
- * Note that there's no particular need to do ResetLatch here.
+ * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch
+ * on it. That allows us to repoint the process latch, which so far
+ * points to process local one, to the shared one.
*/
OwnLatch(&MyProc->procLatch);
+ SwitchToSharedLatch();
/*
* We might be reusing a semaphore that belonged to a failed process. So
ReplicationSlotRelease();
/*
- * Clear MyProc first; then disown the process latch. This is so that
- * signal handlers won't try to clear the process latch after it's no
- * longer ours.
+ * Reset MyLatch to the process local one. This is so that signal
+ * handlers et al can continue using the latch after the shared latch
+ * isn't ours anymore. After that clear MyProc and disown the shared
+ * latch.
*/
+ SwitchBackToLocalLatch();
proc = MyProc;
MyProc = NULL;
DisownLatch(&proc->procLatch);
LWLockReleaseAll();
/*
- * Clear MyProc first; then disown the process latch. This is so that
- * signal handlers won't try to clear the process latch after it's no
- * longer ours.
+ * Reset MyLatch to the process local one. This is so that signal
+ * handlers et al can continue using the latch after the shared latch
+ * isn't ours anymore. After that clear MyProc and disown the shared
+ * latch.
*/
+ SwitchBackToLocalLatch();
proc = MyProc;
MyProc = NULL;
DisownLatch(&proc->procLatch);
}
/* If we're still here, waken anything waiting on the process latch */
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
}
/* If we're still here, waken anything waiting on the process latch */
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
int save_errno = errno;
got_SIGHUP = true;
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
* waiting on that latch, expecting to get interrupted by query cancels et
* al., would also need to set set_latch_on_sigusr1.
*/
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
else
break;
- (void) WaitLatch(&MyProc->procLatch,
+ (void) WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT,
delay_ms);
- ResetLatch(&MyProc->procLatch);
+ ResetLatch(MyLatch);
}
PG_RETURN_VOID();
long MyCancelKey;
int MyPMChildSlot;
+/*
+ * MyLatch points to the latch that should be used for signal handling by the
+ * current process. It will either point to a process local latch if the
+ * current process does not have a PGPROC entry in that moment, or to
+ * PGPROC->procLatch if it has. Thus it can always be used in signal handlers,
+ * without checking for its existence.
+ */
+struct Latch *MyLatch;
+
/*
* DataDir is the absolute path to the top level of the PGDATA directory tree.
* Except during early startup, this is also the server's working directory;
#include "postmaster/postmaster.h"
#include "storage/fd.h"
#include "storage/ipc.h"
+#include "storage/latch.h"
#include "storage/pg_shmem.h"
#include "storage/proc.h"
#include "storage/procarray.h"
/* List of lock files to be removed at proc exit */
static List *lock_files = NIL;
+static Latch LocalLatchData;
/* ----------------------------------------------------------------
* ignoring system indexes support stuff
/* We don't want the postmaster's proc_exit() handlers */
on_exit_reset();
+ /* Initialize process-local latch support */
+ InitializeLatchSupport();
+ MyLatch = &LocalLatchData;
+ InitLatch(MyLatch);
+
/*
* If possible, make this process a group leader, so that the postmaster
* can signal any child processes too. Not all processes will have
MyStartTime = time(NULL); /* set our start time in case we call elog */
+ /* Initialize process-local latch support */
+ InitializeLatchSupport();
+ MyLatch = &LocalLatchData;
+ InitLatch(MyLatch);
+
/* Compute paths, no postmaster to inherit from */
if (my_exec_path[0] == '\0')
{
get_pkglib_path(my_exec_path, pkglib_path);
}
+void
+SwitchToSharedLatch(void)
+{
+ Assert(MyLatch == &LocalLatchData);
+ Assert(MyProc != NULL);
+
+ MyLatch = &MyProc->procLatch;
+ /*
+ * Set the shared latch as the local one might have been set. This
+ * shouldn't normally be necessary as code is supposed to check the
+ * condition before waiting for the latch, but a bit care can't hurt.
+ */
+ SetLatch(MyLatch);
+}
+
+void
+SwitchBackToLocalLatch(void)
+{
+ Assert(MyLatch != &LocalLatchData);
+ Assert(MyProc != NULL && MyLatch == &MyProc->procLatch);
+
+ MyLatch = &LocalLatchData;
+ SetLatch(MyLatch);
+}
+
/*
* GetUserId - get the current effective user ID.
*
/*
* SIGALRM is always cause for waking anything waiting on the process
- * latch. Cope with MyProc not being there, as the startup process also
- * uses this signal handler.
+ * latch.
*/
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
/*
* Fire any pending timeouts, but only if we're enabled to do so.
extern PGDLLIMPORT int MyProcPid;
extern PGDLLIMPORT pg_time_t MyStartTime;
extern PGDLLIMPORT struct Port *MyProcPort;
+extern PGDLLIMPORT struct Latch *MyLatch;
extern long MyCancelKey;
extern int MyPMChildSlot;
extern void SetDataDir(const char *dir);
extern void ChangeToDataDir(void);
+extern void SwitchToSharedLatch(void);
+extern void SwitchBackToLocalLatch(void);
+
/* in utils/misc/superuser.c */
extern bool superuser(void); /* current user is superuser */
extern bool superuser_arg(Oid roleid); /* given user is superuser */
* the public functions. It is defined here to allow embedding Latches as
* part of bigger structs.
*/
-typedef struct
+typedef struct Latch
{
sig_atomic_t is_set;
bool is_shared;
}
/* Wait to be signalled. */
- WaitLatch(&MyProc->procLatch, WL_LATCH_SET, 0);
+ WaitLatch(MyLatch, WL_LATCH_SET, 0);
/* An interrupt may have occurred while we were waiting. */
CHECK_FOR_INTERRUPTS();
/* Reset the latch so we don't spin. */
- ResetLatch(&MyProc->procLatch);
+ ResetLatch(MyLatch);
}
}
PG_CATCH();
* have read or written data and therefore there may now be work
* for us to do.
*/
- WaitLatch(&MyProc->procLatch, WL_LATCH_SET, 0);
+ WaitLatch(MyLatch, WL_LATCH_SET, 0);
CHECK_FOR_INTERRUPTS();
- ResetLatch(&MyProc->procLatch);
+ ResetLatch(MyLatch);
}
}
{
int save_errno = errno;
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
if (!proc_exit_inprogress)
{
int save_errno = errno;
got_sigterm = true;
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
int save_errno = errno;
got_sighup = true;
- if (MyProc)
- SetLatch(&MyProc->procLatch);
+ SetLatch(MyLatch);
errno = save_errno;
}
* necessary, but is awakened if postmaster dies. That way the
* background process goes away immediately in an emergency.
*/
- rc = WaitLatch(&MyProc->procLatch,
+ rc = WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
worker_spi_naptime * 1000L);
- ResetLatch(&MyProc->procLatch);
+ ResetLatch(MyLatch);
/* emergency bailout if postmaster has died */
if (rc & WL_POSTMASTER_DEATH)