mirror of
https://github.com/postgres/postgres.git
synced 2025-11-22 12:22:45 +03:00
Add a default local latch for use in signal handlers.
To do so, move InitializeLatchSupport() into the new common process initialization functions, and add a new global variable MyLatch. MyLatch is usable as soon InitPostmasterChild() has been called (i.e. very early during startup). Initially it points to a process local latch that exists in all processes. InitProcess/InitAuxiliaryProcess then replaces that local latch with PGPROC->procLatch. During shutdown the reverse happens. This is primarily advantageous for two reasons: For one it simplifies dealing with the shared process latch, especially in signal handlers, because instead of having to check for MyProc, MyLatch can be used unconditionally. For another, a later patch that makes FEs/BE communication use latches, now can rely on the existence of a latch, even before having gone through InitProcess. Discussion: 20140927191243.GD5423@alap3.anarazel.de
This commit is contained in:
@@ -589,11 +589,11 @@ AutoVacLauncherMain(int argc, char *argv[])
|
||||
* 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();
|
||||
|
||||
@@ -1341,8 +1341,7 @@ avl_sighup_handler(SIGNAL_ARGS)
|
||||
int save_errno = errno;
|
||||
|
||||
got_SIGHUP = true;
|
||||
if (MyProc)
|
||||
SetLatch(&MyProc->procLatch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
@@ -1354,8 +1353,7 @@ avl_sigusr2_handler(SIGNAL_ARGS)
|
||||
int save_errno = errno;
|
||||
|
||||
got_SIGUSR2 = true;
|
||||
if (MyProc)
|
||||
SetLatch(&MyProc->procLatch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
@@ -1367,8 +1365,7 @@ avl_sigterm_handler(SIGNAL_ARGS)
|
||||
int save_errno = errno;
|
||||
|
||||
got_SIGTERM = true;
|
||||
if (MyProc)
|
||||
SetLatch(&MyProc->procLatch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
|
||||
@@ -942,7 +942,7 @@ WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pidp)
|
||||
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)
|
||||
@@ -951,7 +951,7 @@ WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pidp)
|
||||
break;
|
||||
}
|
||||
|
||||
ResetLatch(&MyProc->procLatch);
|
||||
ResetLatch(MyLatch);
|
||||
}
|
||||
}
|
||||
PG_CATCH();
|
||||
|
||||
@@ -248,7 +248,7 @@ BackgroundWriterMain(void)
|
||||
int rc;
|
||||
|
||||
/* Clear any already-pending wakeups */
|
||||
ResetLatch(&MyProc->procLatch);
|
||||
ResetLatch(MyLatch);
|
||||
|
||||
if (got_SIGHUP)
|
||||
{
|
||||
@@ -336,7 +336,7 @@ BackgroundWriterMain(void)
|
||||
* 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 */ );
|
||||
|
||||
@@ -363,7 +363,7 @@ BackgroundWriterMain(void)
|
||||
/* 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 */
|
||||
@@ -426,8 +426,7 @@ BgSigHupHandler(SIGNAL_ARGS)
|
||||
int save_errno = errno;
|
||||
|
||||
got_SIGHUP = true;
|
||||
if (MyProc)
|
||||
SetLatch(&MyProc->procLatch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
@@ -439,8 +438,7 @@ ReqShutdownHandler(SIGNAL_ARGS)
|
||||
int save_errno = errno;
|
||||
|
||||
shutdown_requested = true;
|
||||
if (MyProc)
|
||||
SetLatch(&MyProc->procLatch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
|
||||
@@ -360,7 +360,7 @@ CheckpointerMain(void)
|
||||
int rc;
|
||||
|
||||
/* Clear any already-pending wakeups */
|
||||
ResetLatch(&MyProc->procLatch);
|
||||
ResetLatch(MyLatch);
|
||||
|
||||
/*
|
||||
* Process any requests or signals received recently.
|
||||
@@ -559,7 +559,7 @@ CheckpointerMain(void)
|
||||
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 */ );
|
||||
|
||||
@@ -832,8 +832,7 @@ ChkptSigHupHandler(SIGNAL_ARGS)
|
||||
int save_errno = errno;
|
||||
|
||||
got_SIGHUP = true;
|
||||
if (MyProc)
|
||||
SetLatch(&MyProc->procLatch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
@@ -845,8 +844,7 @@ ReqCheckpointHandler(SIGNAL_ARGS)
|
||||
int save_errno = errno;
|
||||
|
||||
checkpoint_requested = true;
|
||||
if (MyProc)
|
||||
SetLatch(&MyProc->procLatch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
@@ -869,8 +867,7 @@ ReqShutdownHandler(SIGNAL_ARGS)
|
||||
int save_errno = errno;
|
||||
|
||||
shutdown_requested = true;
|
||||
if (MyProc)
|
||||
SetLatch(&MyProc->procLatch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
|
||||
@@ -78,11 +78,6 @@ static volatile sig_atomic_t got_SIGTERM = false;
|
||||
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
|
||||
* ----------
|
||||
@@ -220,10 +215,6 @@ pgarch_forkexec(void)
|
||||
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.
|
||||
@@ -269,7 +260,7 @@ ArchSigHupHandler(SIGNAL_ARGS)
|
||||
|
||||
/* set flag to re-read config file at next convenient time */
|
||||
got_SIGHUP = true;
|
||||
SetLatch(&mainloop_latch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
@@ -287,7 +278,7 @@ ArchSigTermHandler(SIGNAL_ARGS)
|
||||
* archive commands.
|
||||
*/
|
||||
got_SIGTERM = true;
|
||||
SetLatch(&mainloop_latch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
@@ -300,7 +291,7 @@ pgarch_waken(SIGNAL_ARGS)
|
||||
|
||||
/* set flag that there is work to be done */
|
||||
wakened = true;
|
||||
SetLatch(&mainloop_latch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
@@ -313,7 +304,7 @@ pgarch_waken_stop(SIGNAL_ARGS)
|
||||
|
||||
/* set flag to do a final cycle and shut down afterwards */
|
||||
ready_to_stop = true;
|
||||
SetLatch(&mainloop_latch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
@@ -344,7 +335,7 @@ pgarch_MainLoop(void)
|
||||
*/
|
||||
do
|
||||
{
|
||||
ResetLatch(&mainloop_latch);
|
||||
ResetLatch(MyLatch);
|
||||
|
||||
/* When we get SIGUSR2, we do one more archive cycle, then exit */
|
||||
time_to_stop = ready_to_stop;
|
||||
@@ -397,7 +388,7 @@ pgarch_MainLoop(void)
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = WaitLatch(&mainloop_latch,
|
||||
rc = WaitLatch(MyLatch,
|
||||
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
|
||||
timeout * 1000L);
|
||||
if (rc & WL_TIMEOUT)
|
||||
|
||||
@@ -130,8 +130,6 @@ PgStat_MsgBgWriter BgWriterStats;
|
||||
*/
|
||||
NON_EXEC_STATIC pgsocket pgStatSock = PGINVALID_SOCKET;
|
||||
|
||||
static Latch pgStatLatch;
|
||||
|
||||
static struct sockaddr_storage pgStatAddr;
|
||||
|
||||
static time_t last_pgstat_start_time;
|
||||
@@ -3151,15 +3149,10 @@ PgstatCollectorMain(int argc, char *argv[])
|
||||
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);
|
||||
@@ -3205,7 +3198,7 @@ PgstatCollectorMain(int argc, char *argv[])
|
||||
for (;;)
|
||||
{
|
||||
/* Clear any already-pending wakeups */
|
||||
ResetLatch(&pgStatLatch);
|
||||
ResetLatch(MyLatch);
|
||||
|
||||
/*
|
||||
* Quit if we get SIGQUIT from the postmaster.
|
||||
@@ -3363,7 +3356,7 @@ PgstatCollectorMain(int argc, char *argv[])
|
||||
|
||||
/* 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);
|
||||
@@ -3379,7 +3372,7 @@ PgstatCollectorMain(int argc, char *argv[])
|
||||
* 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 */ );
|
||||
@@ -3409,7 +3402,7 @@ pgstat_exit(SIGNAL_ARGS)
|
||||
int save_errno = errno;
|
||||
|
||||
need_exit = true;
|
||||
SetLatch(&pgStatLatch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
@@ -3421,7 +3414,7 @@ pgstat_sighup_handler(SIGNAL_ARGS)
|
||||
int save_errno = errno;
|
||||
|
||||
got_SIGHUP = true;
|
||||
SetLatch(&pgStatLatch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
|
||||
@@ -85,7 +85,6 @@ static FILE *csvlogFile = NULL;
|
||||
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.
|
||||
@@ -231,12 +230,6 @@ SysLoggerMain(int argc, char *argv[])
|
||||
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
|
||||
*
|
||||
@@ -302,7 +295,7 @@ SysLoggerMain(int argc, char *argv[])
|
||||
#endif
|
||||
|
||||
/* Clear any already-pending wakeups */
|
||||
ResetLatch(&sysLoggerLatch);
|
||||
ResetLatch(MyLatch);
|
||||
|
||||
/*
|
||||
* Process any requests or signals received recently.
|
||||
@@ -428,7 +421,7 @@ SysLoggerMain(int argc, char *argv[])
|
||||
* 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);
|
||||
@@ -480,7 +473,7 @@ SysLoggerMain(int argc, char *argv[])
|
||||
*/
|
||||
LeaveCriticalSection(&sysloggerSection);
|
||||
|
||||
(void) WaitLatch(&sysLoggerLatch,
|
||||
(void) WaitLatch(MyLatch,
|
||||
WL_LATCH_SET | cur_flags,
|
||||
cur_timeout);
|
||||
|
||||
@@ -1061,7 +1054,7 @@ pipeThread(void *arg)
|
||||
{
|
||||
if (ftell(syslogFile) >= Log_RotationSize * 1024L ||
|
||||
(csvlogFile != NULL && ftell(csvlogFile) >= Log_RotationSize * 1024L))
|
||||
SetLatch(&sysLoggerLatch);
|
||||
SetLatch(MyLatch);
|
||||
}
|
||||
LeaveCriticalSection(&sysloggerSection);
|
||||
}
|
||||
@@ -1073,7 +1066,7 @@ pipeThread(void *arg)
|
||||
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();
|
||||
@@ -1353,7 +1346,7 @@ sigHupHandler(SIGNAL_ARGS)
|
||||
int save_errno = errno;
|
||||
|
||||
got_SIGHUP = true;
|
||||
SetLatch(&sysLoggerLatch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
@@ -1365,7 +1358,7 @@ sigUsr1Handler(SIGNAL_ARGS)
|
||||
int save_errno = errno;
|
||||
|
||||
rotation_requested = true;
|
||||
SetLatch(&sysLoggerLatch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
|
||||
@@ -252,7 +252,7 @@ WalWriterMain(void)
|
||||
}
|
||||
|
||||
/* Clear any already-pending wakeups */
|
||||
ResetLatch(&MyProc->procLatch);
|
||||
ResetLatch(MyLatch);
|
||||
|
||||
/*
|
||||
* Process any requests or signals received recently.
|
||||
@@ -287,7 +287,7 @@ WalWriterMain(void)
|
||||
else
|
||||
cur_timeout = WalWriterDelay * HIBERNATE_FACTOR;
|
||||
|
||||
rc = WaitLatch(&MyProc->procLatch,
|
||||
rc = WaitLatch(MyLatch,
|
||||
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
|
||||
cur_timeout);
|
||||
|
||||
@@ -345,8 +345,7 @@ WalSigHupHandler(SIGNAL_ARGS)
|
||||
int save_errno = errno;
|
||||
|
||||
got_SIGHUP = true;
|
||||
if (MyProc)
|
||||
SetLatch(&MyProc->procLatch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
@@ -358,8 +357,7 @@ WalShutdownHandler(SIGNAL_ARGS)
|
||||
int save_errno = errno;
|
||||
|
||||
shutdown_requested = true;
|
||||
if (MyProc)
|
||||
SetLatch(&MyProc->procLatch);
|
||||
SetLatch(MyLatch);
|
||||
|
||||
errno = save_errno;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user