Commit 59f71a0d authored by Andres Freund's avatar Andres Freund

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
parent 85a2a890
...@@ -589,11 +589,11 @@ AutoVacLauncherMain(int argc, char *argv[]) ...@@ -589,11 +589,11 @@ AutoVacLauncherMain(int argc, char *argv[])
* Wait until naptime expires or we get some type of signal (all the * Wait until naptime expires or we get some type of signal (all the
* signal handlers will wake us by calling SetLatch). * signal handlers will wake us by calling SetLatch).
*/ */
rc = WaitLatch(&MyProc->procLatch, rc = WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
(nap.tv_sec * 1000L) + (nap.tv_usec / 1000L)); (nap.tv_sec * 1000L) + (nap.tv_usec / 1000L));
ResetLatch(&MyProc->procLatch); ResetLatch(MyLatch);
DisableCatchupInterrupt(); DisableCatchupInterrupt();
...@@ -1341,8 +1341,7 @@ avl_sighup_handler(SIGNAL_ARGS) ...@@ -1341,8 +1341,7 @@ avl_sighup_handler(SIGNAL_ARGS)
int save_errno = errno; int save_errno = errno;
got_SIGHUP = true; got_SIGHUP = true;
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -1354,8 +1353,7 @@ avl_sigusr2_handler(SIGNAL_ARGS) ...@@ -1354,8 +1353,7 @@ avl_sigusr2_handler(SIGNAL_ARGS)
int save_errno = errno; int save_errno = errno;
got_SIGUSR2 = true; got_SIGUSR2 = true;
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -1367,8 +1365,7 @@ avl_sigterm_handler(SIGNAL_ARGS) ...@@ -1367,8 +1365,7 @@ avl_sigterm_handler(SIGNAL_ARGS)
int save_errno = errno; int save_errno = errno;
got_SIGTERM = true; got_SIGTERM = true;
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
errno = save_errno; errno = save_errno;
} }
......
...@@ -942,7 +942,7 @@ WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pidp) ...@@ -942,7 +942,7 @@ WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pidp)
if (status != BGWH_NOT_YET_STARTED) if (status != BGWH_NOT_YET_STARTED)
break; break;
rc = WaitLatch(&MyProc->procLatch, rc = WaitLatch(MyLatch,
WL_LATCH_SET | WL_POSTMASTER_DEATH, 0); WL_LATCH_SET | WL_POSTMASTER_DEATH, 0);
if (rc & WL_POSTMASTER_DEATH) if (rc & WL_POSTMASTER_DEATH)
...@@ -951,7 +951,7 @@ WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pidp) ...@@ -951,7 +951,7 @@ WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pidp)
break; break;
} }
ResetLatch(&MyProc->procLatch); ResetLatch(MyLatch);
} }
} }
PG_CATCH(); PG_CATCH();
......
...@@ -248,7 +248,7 @@ BackgroundWriterMain(void) ...@@ -248,7 +248,7 @@ BackgroundWriterMain(void)
int rc; int rc;
/* Clear any already-pending wakeups */ /* Clear any already-pending wakeups */
ResetLatch(&MyProc->procLatch); ResetLatch(MyLatch);
if (got_SIGHUP) if (got_SIGHUP)
{ {
...@@ -336,7 +336,7 @@ BackgroundWriterMain(void) ...@@ -336,7 +336,7 @@ BackgroundWriterMain(void)
* down with latch events that are likely to happen frequently during * down with latch events that are likely to happen frequently during
* normal operation. * normal operation.
*/ */
rc = WaitLatch(&MyProc->procLatch, rc = WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
BgWriterDelay /* ms */ ); BgWriterDelay /* ms */ );
...@@ -363,7 +363,7 @@ BackgroundWriterMain(void) ...@@ -363,7 +363,7 @@ BackgroundWriterMain(void)
/* Ask for notification at next buffer allocation */ /* Ask for notification at next buffer allocation */
StrategyNotifyBgWriter(MyProc->pgprocno); StrategyNotifyBgWriter(MyProc->pgprocno);
/* Sleep ... */ /* Sleep ... */
rc = WaitLatch(&MyProc->procLatch, rc = WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
BgWriterDelay * HIBERNATE_FACTOR); BgWriterDelay * HIBERNATE_FACTOR);
/* Reset the notification request in case we timed out */ /* Reset the notification request in case we timed out */
...@@ -426,8 +426,7 @@ BgSigHupHandler(SIGNAL_ARGS) ...@@ -426,8 +426,7 @@ BgSigHupHandler(SIGNAL_ARGS)
int save_errno = errno; int save_errno = errno;
got_SIGHUP = true; got_SIGHUP = true;
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -439,8 +438,7 @@ ReqShutdownHandler(SIGNAL_ARGS) ...@@ -439,8 +438,7 @@ ReqShutdownHandler(SIGNAL_ARGS)
int save_errno = errno; int save_errno = errno;
shutdown_requested = true; shutdown_requested = true;
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
errno = save_errno; errno = save_errno;
} }
......
...@@ -360,7 +360,7 @@ CheckpointerMain(void) ...@@ -360,7 +360,7 @@ CheckpointerMain(void)
int rc; int rc;
/* Clear any already-pending wakeups */ /* Clear any already-pending wakeups */
ResetLatch(&MyProc->procLatch); ResetLatch(MyLatch);
/* /*
* Process any requests or signals received recently. * Process any requests or signals received recently.
...@@ -559,7 +559,7 @@ CheckpointerMain(void) ...@@ -559,7 +559,7 @@ CheckpointerMain(void)
cur_timeout = Min(cur_timeout, XLogArchiveTimeout - elapsed_secs); cur_timeout = Min(cur_timeout, XLogArchiveTimeout - elapsed_secs);
} }
rc = WaitLatch(&MyProc->procLatch, rc = WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
cur_timeout * 1000L /* convert to ms */ ); cur_timeout * 1000L /* convert to ms */ );
...@@ -832,8 +832,7 @@ ChkptSigHupHandler(SIGNAL_ARGS) ...@@ -832,8 +832,7 @@ ChkptSigHupHandler(SIGNAL_ARGS)
int save_errno = errno; int save_errno = errno;
got_SIGHUP = true; got_SIGHUP = true;
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -845,8 +844,7 @@ ReqCheckpointHandler(SIGNAL_ARGS) ...@@ -845,8 +844,7 @@ ReqCheckpointHandler(SIGNAL_ARGS)
int save_errno = errno; int save_errno = errno;
checkpoint_requested = true; checkpoint_requested = true;
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -869,8 +867,7 @@ ReqShutdownHandler(SIGNAL_ARGS) ...@@ -869,8 +867,7 @@ ReqShutdownHandler(SIGNAL_ARGS)
int save_errno = errno; int save_errno = errno;
shutdown_requested = true; shutdown_requested = true;
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
errno = save_errno; errno = save_errno;
} }
......
...@@ -78,11 +78,6 @@ static volatile sig_atomic_t got_SIGTERM = false; ...@@ -78,11 +78,6 @@ static volatile sig_atomic_t got_SIGTERM = false;
static volatile sig_atomic_t wakened = false; static volatile sig_atomic_t wakened = false;
static volatile sig_atomic_t ready_to_stop = 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 * Local function forward declarations
* ---------- * ----------
...@@ -220,10 +215,6 @@ pgarch_forkexec(void) ...@@ -220,10 +215,6 @@ pgarch_forkexec(void)
NON_EXEC_STATIC void NON_EXEC_STATIC void
PgArchiverMain(int argc, char *argv[]) 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, * Ignore all signals usually bound to some action in the postmaster,
* except for SIGHUP, SIGTERM, SIGUSR1, SIGUSR2, and SIGQUIT. * except for SIGHUP, SIGTERM, SIGUSR1, SIGUSR2, and SIGQUIT.
...@@ -269,7 +260,7 @@ ArchSigHupHandler(SIGNAL_ARGS) ...@@ -269,7 +260,7 @@ ArchSigHupHandler(SIGNAL_ARGS)
/* set flag to re-read config file at next convenient time */ /* set flag to re-read config file at next convenient time */
got_SIGHUP = true; got_SIGHUP = true;
SetLatch(&mainloop_latch); SetLatch(MyLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -287,7 +278,7 @@ ArchSigTermHandler(SIGNAL_ARGS) ...@@ -287,7 +278,7 @@ ArchSigTermHandler(SIGNAL_ARGS)
* archive commands. * archive commands.
*/ */
got_SIGTERM = true; got_SIGTERM = true;
SetLatch(&mainloop_latch); SetLatch(MyLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -300,7 +291,7 @@ pgarch_waken(SIGNAL_ARGS) ...@@ -300,7 +291,7 @@ pgarch_waken(SIGNAL_ARGS)
/* set flag that there is work to be done */ /* set flag that there is work to be done */
wakened = true; wakened = true;
SetLatch(&mainloop_latch); SetLatch(MyLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -313,7 +304,7 @@ pgarch_waken_stop(SIGNAL_ARGS) ...@@ -313,7 +304,7 @@ pgarch_waken_stop(SIGNAL_ARGS)
/* set flag to do a final cycle and shut down afterwards */ /* set flag to do a final cycle and shut down afterwards */
ready_to_stop = true; ready_to_stop = true;
SetLatch(&mainloop_latch); SetLatch(MyLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -344,7 +335,7 @@ pgarch_MainLoop(void) ...@@ -344,7 +335,7 @@ pgarch_MainLoop(void)
*/ */
do do
{ {
ResetLatch(&mainloop_latch); ResetLatch(MyLatch);
/* When we get SIGUSR2, we do one more archive cycle, then exit */ /* When we get SIGUSR2, we do one more archive cycle, then exit */
time_to_stop = ready_to_stop; time_to_stop = ready_to_stop;
...@@ -397,7 +388,7 @@ pgarch_MainLoop(void) ...@@ -397,7 +388,7 @@ pgarch_MainLoop(void)
{ {
int rc; int rc;
rc = WaitLatch(&mainloop_latch, rc = WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
timeout * 1000L); timeout * 1000L);
if (rc & WL_TIMEOUT) if (rc & WL_TIMEOUT)
......
...@@ -130,8 +130,6 @@ PgStat_MsgBgWriter BgWriterStats; ...@@ -130,8 +130,6 @@ PgStat_MsgBgWriter BgWriterStats;
*/ */
NON_EXEC_STATIC pgsocket pgStatSock = PGINVALID_SOCKET; NON_EXEC_STATIC pgsocket pgStatSock = PGINVALID_SOCKET;
static Latch pgStatLatch;
static struct sockaddr_storage pgStatAddr; static struct sockaddr_storage pgStatAddr;
static time_t last_pgstat_start_time; static time_t last_pgstat_start_time;
...@@ -3151,15 +3149,10 @@ PgstatCollectorMain(int argc, char *argv[]) ...@@ -3151,15 +3149,10 @@ PgstatCollectorMain(int argc, char *argv[])
PgStat_Msg msg; PgStat_Msg msg;
int wr; 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, * Ignore all signals usually bound to some action in the postmaster,
* except SIGHUP and SIGQUIT. Note we don't need a SIGUSR1 handler to * 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(SIGHUP, pgstat_sighup_handler);
pqsignal(SIGINT, SIG_IGN); pqsignal(SIGINT, SIG_IGN);
...@@ -3205,7 +3198,7 @@ PgstatCollectorMain(int argc, char *argv[]) ...@@ -3205,7 +3198,7 @@ PgstatCollectorMain(int argc, char *argv[])
for (;;) for (;;)
{ {
/* Clear any already-pending wakeups */ /* Clear any already-pending wakeups */
ResetLatch(&pgStatLatch); ResetLatch(MyLatch);
/* /*
* Quit if we get SIGQUIT from the postmaster. * Quit if we get SIGQUIT from the postmaster.
...@@ -3363,7 +3356,7 @@ PgstatCollectorMain(int argc, char *argv[]) ...@@ -3363,7 +3356,7 @@ PgstatCollectorMain(int argc, char *argv[])
/* Sleep until there's something to do */ /* Sleep until there's something to do */
#ifndef WIN32 #ifndef WIN32
wr = WaitLatchOrSocket(&pgStatLatch, wr = WaitLatchOrSocket(MyLatch,
WL_LATCH_SET | WL_POSTMASTER_DEATH | WL_SOCKET_READABLE, WL_LATCH_SET | WL_POSTMASTER_DEATH | WL_SOCKET_READABLE,
pgStatSock, pgStatSock,
-1L); -1L);
...@@ -3379,7 +3372,7 @@ PgstatCollectorMain(int argc, char *argv[]) ...@@ -3379,7 +3372,7 @@ PgstatCollectorMain(int argc, char *argv[])
* to not provoke "pgstat wait timeout" complaints from * to not provoke "pgstat wait timeout" complaints from
* backend_read_statsfile. * backend_read_statsfile.
*/ */
wr = WaitLatchOrSocket(&pgStatLatch, wr = WaitLatchOrSocket(MyLatch,
WL_LATCH_SET | WL_POSTMASTER_DEATH | WL_SOCKET_READABLE | WL_TIMEOUT, WL_LATCH_SET | WL_POSTMASTER_DEATH | WL_SOCKET_READABLE | WL_TIMEOUT,
pgStatSock, pgStatSock,
2 * 1000L /* msec */ ); 2 * 1000L /* msec */ );
...@@ -3409,7 +3402,7 @@ pgstat_exit(SIGNAL_ARGS) ...@@ -3409,7 +3402,7 @@ pgstat_exit(SIGNAL_ARGS)
int save_errno = errno; int save_errno = errno;
need_exit = true; need_exit = true;
SetLatch(&pgStatLatch); SetLatch(MyLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -3421,7 +3414,7 @@ pgstat_sighup_handler(SIGNAL_ARGS) ...@@ -3421,7 +3414,7 @@ pgstat_sighup_handler(SIGNAL_ARGS)
int save_errno = errno; int save_errno = errno;
got_SIGHUP = true; got_SIGHUP = true;
SetLatch(&pgStatLatch); SetLatch(MyLatch);
errno = save_errno; errno = save_errno;
} }
......
...@@ -85,7 +85,6 @@ static FILE *csvlogFile = NULL; ...@@ -85,7 +85,6 @@ static FILE *csvlogFile = NULL;
NON_EXEC_STATIC pg_time_t first_syslogger_file_time = 0; NON_EXEC_STATIC pg_time_t first_syslogger_file_time = 0;
static char *last_file_name = NULL; static char *last_file_name = NULL;
static char *last_csv_file_name = NULL; static char *last_csv_file_name = NULL;
static Latch sysLoggerLatch;
/* /*
* Buffers for saving partial messages from different backends. * Buffers for saving partial messages from different backends.
...@@ -231,12 +230,6 @@ SysLoggerMain(int argc, char *argv[]) ...@@ -231,12 +230,6 @@ SysLoggerMain(int argc, char *argv[])
syslogPipe[1] = 0; syslogPipe[1] = 0;
#endif #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 * Properly accept or ignore signals the postmaster might send us
* *
...@@ -302,7 +295,7 @@ SysLoggerMain(int argc, char *argv[]) ...@@ -302,7 +295,7 @@ SysLoggerMain(int argc, char *argv[])
#endif #endif
/* Clear any already-pending wakeups */ /* Clear any already-pending wakeups */
ResetLatch(&sysLoggerLatch); ResetLatch(MyLatch);
/* /*
* Process any requests or signals received recently. * Process any requests or signals received recently.
...@@ -428,7 +421,7 @@ SysLoggerMain(int argc, char *argv[]) ...@@ -428,7 +421,7 @@ SysLoggerMain(int argc, char *argv[])
* Sleep until there's something to do * Sleep until there's something to do
*/ */
#ifndef WIN32 #ifndef WIN32
rc = WaitLatchOrSocket(&sysLoggerLatch, rc = WaitLatchOrSocket(MyLatch,
WL_LATCH_SET | WL_SOCKET_READABLE | cur_flags, WL_LATCH_SET | WL_SOCKET_READABLE | cur_flags,
syslogPipe[0], syslogPipe[0],
cur_timeout); cur_timeout);
...@@ -480,7 +473,7 @@ SysLoggerMain(int argc, char *argv[]) ...@@ -480,7 +473,7 @@ SysLoggerMain(int argc, char *argv[])
*/ */
LeaveCriticalSection(&sysloggerSection); LeaveCriticalSection(&sysloggerSection);
(void) WaitLatch(&sysLoggerLatch, (void) WaitLatch(MyLatch,
WL_LATCH_SET | cur_flags, WL_LATCH_SET | cur_flags,
cur_timeout); cur_timeout);
...@@ -1061,7 +1054,7 @@ pipeThread(void *arg) ...@@ -1061,7 +1054,7 @@ pipeThread(void *arg)
{ {
if (ftell(syslogFile) >= Log_RotationSize * 1024L || if (ftell(syslogFile) >= Log_RotationSize * 1024L ||
(csvlogFile != NULL && ftell(csvlogFile) >= Log_RotationSize * 1024L)) (csvlogFile != NULL && ftell(csvlogFile) >= Log_RotationSize * 1024L))
SetLatch(&sysLoggerLatch); SetLatch(MyLatch);
} }
LeaveCriticalSection(&sysloggerSection); LeaveCriticalSection(&sysloggerSection);
} }
...@@ -1073,7 +1066,7 @@ pipeThread(void *arg) ...@@ -1073,7 +1066,7 @@ pipeThread(void *arg)
flush_pipe_input(logbuffer, &bytes_in_logbuffer); flush_pipe_input(logbuffer, &bytes_in_logbuffer);
/* set the latch to waken the main thread, which will quit */ /* set the latch to waken the main thread, which will quit */
SetLatch(&sysLoggerLatch); SetLatch(MyLatch);
LeaveCriticalSection(&sysloggerSection); LeaveCriticalSection(&sysloggerSection);
_endthread(); _endthread();
...@@ -1353,7 +1346,7 @@ sigHupHandler(SIGNAL_ARGS) ...@@ -1353,7 +1346,7 @@ sigHupHandler(SIGNAL_ARGS)
int save_errno = errno; int save_errno = errno;
got_SIGHUP = true; got_SIGHUP = true;
SetLatch(&sysLoggerLatch); SetLatch(MyLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -1365,7 +1358,7 @@ sigUsr1Handler(SIGNAL_ARGS) ...@@ -1365,7 +1358,7 @@ sigUsr1Handler(SIGNAL_ARGS)
int save_errno = errno; int save_errno = errno;
rotation_requested = true; rotation_requested = true;
SetLatch(&sysLoggerLatch); SetLatch(MyLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -252,7 +252,7 @@ WalWriterMain(void) ...@@ -252,7 +252,7 @@ WalWriterMain(void)
} }
/* Clear any already-pending wakeups */ /* Clear any already-pending wakeups */
ResetLatch(&MyProc->procLatch); ResetLatch(MyLatch);
/* /*
* Process any requests or signals received recently. * Process any requests or signals received recently.
...@@ -287,7 +287,7 @@ WalWriterMain(void) ...@@ -287,7 +287,7 @@ WalWriterMain(void)
else else
cur_timeout = WalWriterDelay * HIBERNATE_FACTOR; cur_timeout = WalWriterDelay * HIBERNATE_FACTOR;
rc = WaitLatch(&MyProc->procLatch, rc = WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
cur_timeout); cur_timeout);
...@@ -345,8 +345,7 @@ WalSigHupHandler(SIGNAL_ARGS) ...@@ -345,8 +345,7 @@ WalSigHupHandler(SIGNAL_ARGS)
int save_errno = errno; int save_errno = errno;
got_SIGHUP = true; got_SIGHUP = true;
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -358,8 +357,7 @@ WalShutdownHandler(SIGNAL_ARGS) ...@@ -358,8 +357,7 @@ WalShutdownHandler(SIGNAL_ARGS)
int save_errno = errno; int save_errno = errno;
shutdown_requested = true; shutdown_requested = true;
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
errno = save_errno; errno = save_errno;
} }
......
...@@ -162,7 +162,7 @@ SyncRepWaitForLSN(XLogRecPtr XactCommitLSN) ...@@ -162,7 +162,7 @@ SyncRepWaitForLSN(XLogRecPtr XactCommitLSN)
int syncRepState; int syncRepState;
/* Must reset the latch before testing state. */ /* Must reset the latch before testing state. */
ResetLatch(&MyProc->procLatch); ResetLatch(MyLatch);
/* /*
* Try checking the state without the lock first. There's no * Try checking the state without the lock first. There's no
...@@ -235,7 +235,7 @@ SyncRepWaitForLSN(XLogRecPtr XactCommitLSN) ...@@ -235,7 +235,7 @@ SyncRepWaitForLSN(XLogRecPtr XactCommitLSN)
* Wait on latch. Any condition that should wake us up will set the * Wait on latch. Any condition that should wake us up will set the
* latch, so no need for timeout. * 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);
} }
/* /*
......
...@@ -292,8 +292,8 @@ procsignal_sigusr1_handler(SIGNAL_ARGS) ...@@ -292,8 +292,8 @@ procsignal_sigusr1_handler(SIGNAL_ARGS)
if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN)) if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN))
RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN); RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN);
if (set_latch_on_sigusr1 && MyProc != NULL) if (set_latch_on_sigusr1)
SetLatch(&MyProc->procLatch); SetLatch(MyLatch);
latch_sigusr1_handler(); latch_sigusr1_handler();
......
...@@ -827,13 +827,13 @@ shm_mq_send_bytes(shm_mq_handle *mqh, Size nbytes, const void *data, ...@@ -827,13 +827,13 @@ shm_mq_send_bytes(shm_mq_handle *mqh, Size nbytes, const void *data,
* at top of loop, because setting an already-set latch is much * at top of loop, because setting an already-set latch is much
* cheaper than setting one that has been reset. * 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. */ /* An interrupt may have occurred while we were waiting. */
CHECK_FOR_INTERRUPTS(); CHECK_FOR_INTERRUPTS();
/* Reset the latch so we don't spin. */ /* Reset the latch so we don't spin. */
ResetLatch(&MyProc->procLatch); ResetLatch(MyLatch);
} }
else else
{ {
...@@ -924,13 +924,13 @@ shm_mq_receive_bytes(shm_mq *mq, Size bytes_needed, bool nowait, ...@@ -924,13 +924,13 @@ shm_mq_receive_bytes(shm_mq *mq, Size bytes_needed, bool nowait,
* loop, because setting an already-set latch is much cheaper than * loop, because setting an already-set latch is much cheaper than
* setting one that has been reset. * 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. */ /* An interrupt may have occurred while we were waiting. */
CHECK_FOR_INTERRUPTS(); CHECK_FOR_INTERRUPTS();
/* Reset the latch so we don't spin. */ /* Reset the latch so we don't spin. */
ResetLatch(&MyProc->procLatch); ResetLatch(MyLatch);
} }
} }
...@@ -991,13 +991,13 @@ shm_mq_wait_internal(volatile shm_mq *mq, PGPROC *volatile * ptr, ...@@ -991,13 +991,13 @@ shm_mq_wait_internal(volatile shm_mq *mq, PGPROC *volatile * ptr,
} }
/* Wait to be signalled. */ /* 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. */ /* An interrupt may have occurred while we were waiting. */
CHECK_FOR_INTERRUPTS(); CHECK_FOR_INTERRUPTS();
/* Reset the latch so we don't spin. */ /* Reset the latch so we don't spin. */
ResetLatch(&MyProc->procLatch); ResetLatch(MyLatch);
} }
} }
PG_CATCH(); PG_CATCH();
......
...@@ -290,13 +290,6 @@ InitProcess(void) ...@@ -290,13 +290,6 @@ InitProcess(void)
if (MyProc != NULL) if (MyProc != NULL)
elog(ERROR, "you already exist"); 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 * Try to get a proc struct from the free list. If this fails, we must be
* out of PGPROC structures (not to mention semaphores). * out of PGPROC structures (not to mention semaphores).
...@@ -391,10 +384,12 @@ InitProcess(void) ...@@ -391,10 +384,12 @@ InitProcess(void)
SHMQueueElemInit(&(MyProc->syncRepLinks)); SHMQueueElemInit(&(MyProc->syncRepLinks));
/* /*
* Acquire ownership of the PGPROC's latch, so that we can use WaitLatch. * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch
* Note that there's no particular need to do ResetLatch here. * 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); OwnLatch(&MyProc->procLatch);
SwitchToSharedLatch();
/* /*
* We might be reusing a semaphore that belonged to a failed process. So * We might be reusing a semaphore that belonged to a failed process. So
...@@ -474,13 +469,6 @@ InitAuxiliaryProcess(void) ...@@ -474,13 +469,6 @@ InitAuxiliaryProcess(void)
if (MyProc != NULL) if (MyProc != NULL)
elog(ERROR, "you already exist"); 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 * We use the ProcStructLock to protect assignment and releasing of
* AuxiliaryProcs entries. * AuxiliaryProcs entries.
...@@ -547,10 +535,12 @@ InitAuxiliaryProcess(void) ...@@ -547,10 +535,12 @@ InitAuxiliaryProcess(void)
#endif #endif
/* /*
* Acquire ownership of the PGPROC's latch, so that we can use WaitLatch. * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch
* Note that there's no particular need to do ResetLatch here. * 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); OwnLatch(&MyProc->procLatch);
SwitchToSharedLatch();
/* /*
* We might be reusing a semaphore that belonged to a failed process. So * We might be reusing a semaphore that belonged to a failed process. So
...@@ -800,10 +790,12 @@ ProcKill(int code, Datum arg) ...@@ -800,10 +790,12 @@ ProcKill(int code, Datum arg)
ReplicationSlotRelease(); ReplicationSlotRelease();
/* /*
* Clear MyProc first; then disown the process latch. This is so that * Reset MyLatch to the process local one. This is so that signal
* signal handlers won't try to clear the process latch after it's no * handlers et al can continue using the latch after the shared latch
* longer ours. * isn't ours anymore. After that clear MyProc and disown the shared
* latch.
*/ */
SwitchBackToLocalLatch();
proc = MyProc; proc = MyProc;
MyProc = NULL; MyProc = NULL;
DisownLatch(&proc->procLatch); DisownLatch(&proc->procLatch);
...@@ -867,10 +859,12 @@ AuxiliaryProcKill(int code, Datum arg) ...@@ -867,10 +859,12 @@ AuxiliaryProcKill(int code, Datum arg)
LWLockReleaseAll(); LWLockReleaseAll();
/* /*
* Clear MyProc first; then disown the process latch. This is so that * Reset MyLatch to the process local one. This is so that signal
* signal handlers won't try to clear the process latch after it's no * handlers et al can continue using the latch after the shared latch
* longer ours. * isn't ours anymore. After that clear MyProc and disown the shared
* latch.
*/ */
SwitchBackToLocalLatch();
proc = MyProc; proc = MyProc;
MyProc = NULL; MyProc = NULL;
DisownLatch(&proc->procLatch); DisownLatch(&proc->procLatch);
......
...@@ -2607,8 +2607,7 @@ die(SIGNAL_ARGS) ...@@ -2607,8 +2607,7 @@ die(SIGNAL_ARGS)
} }
/* If we're still here, waken anything waiting on the process latch */ /* If we're still here, waken anything waiting on the process latch */
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -2649,8 +2648,7 @@ StatementCancelHandler(SIGNAL_ARGS) ...@@ -2649,8 +2648,7 @@ StatementCancelHandler(SIGNAL_ARGS)
} }
/* If we're still here, waken anything waiting on the process latch */ /* If we're still here, waken anything waiting on the process latch */
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -2675,8 +2673,7 @@ SigHupHandler(SIGNAL_ARGS) ...@@ -2675,8 +2673,7 @@ SigHupHandler(SIGNAL_ARGS)
int save_errno = errno; int save_errno = errno;
got_SIGHUP = true; got_SIGHUP = true;
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -2814,8 +2811,7 @@ RecoveryConflictInterrupt(ProcSignalReason reason) ...@@ -2814,8 +2811,7 @@ RecoveryConflictInterrupt(ProcSignalReason reason)
* waiting on that latch, expecting to get interrupted by query cancels et * waiting on that latch, expecting to get interrupted by query cancels et
* al., would also need to set set_latch_on_sigusr1. * al., would also need to set set_latch_on_sigusr1.
*/ */
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
errno = save_errno; errno = save_errno;
} }
......
...@@ -413,10 +413,10 @@ pg_sleep(PG_FUNCTION_ARGS) ...@@ -413,10 +413,10 @@ pg_sleep(PG_FUNCTION_ARGS)
else else
break; break;
(void) WaitLatch(&MyProc->procLatch, (void) WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT, WL_LATCH_SET | WL_TIMEOUT,
delay_ms); delay_ms);
ResetLatch(&MyProc->procLatch); ResetLatch(MyLatch);
} }
PG_RETURN_VOID(); PG_RETURN_VOID();
......
...@@ -40,6 +40,15 @@ struct Port *MyProcPort; ...@@ -40,6 +40,15 @@ struct Port *MyProcPort;
long MyCancelKey; long MyCancelKey;
int MyPMChildSlot; 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. * 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; * Except during early startup, this is also the server's working directory;
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include "postmaster/postmaster.h" #include "postmaster/postmaster.h"
#include "storage/fd.h" #include "storage/fd.h"
#include "storage/ipc.h" #include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/pg_shmem.h" #include "storage/pg_shmem.h"
#include "storage/proc.h" #include "storage/proc.h"
#include "storage/procarray.h" #include "storage/procarray.h"
...@@ -55,6 +56,7 @@ ProcessingMode Mode = InitProcessing; ...@@ -55,6 +56,7 @@ ProcessingMode Mode = InitProcessing;
/* List of lock files to be removed at proc exit */ /* List of lock files to be removed at proc exit */
static List *lock_files = NIL; static List *lock_files = NIL;
static Latch LocalLatchData;
/* ---------------------------------------------------------------- /* ----------------------------------------------------------------
* ignoring system indexes support stuff * ignoring system indexes support stuff
...@@ -189,6 +191,11 @@ InitPostmasterChild(void) ...@@ -189,6 +191,11 @@ InitPostmasterChild(void)
/* We don't want the postmaster's proc_exit() handlers */ /* We don't want the postmaster's proc_exit() handlers */
on_exit_reset(); 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 * If possible, make this process a group leader, so that the postmaster
* can signal any child processes too. Not all processes will have * can signal any child processes too. Not all processes will have
...@@ -215,6 +222,11 @@ InitStandaloneProcess(const char *argv0) ...@@ -215,6 +222,11 @@ InitStandaloneProcess(const char *argv0)
MyStartTime = time(NULL); /* set our start time in case we call elog */ 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 */ /* Compute paths, no postmaster to inherit from */
if (my_exec_path[0] == '\0') if (my_exec_path[0] == '\0')
{ {
...@@ -227,6 +239,31 @@ InitStandaloneProcess(const char *argv0) ...@@ -227,6 +239,31 @@ InitStandaloneProcess(const char *argv0)
get_pkglib_path(my_exec_path, pkglib_path); 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. * GetUserId - get the current effective user ID.
* *
......
...@@ -284,11 +284,9 @@ handle_sig_alarm(SIGNAL_ARGS) ...@@ -284,11 +284,9 @@ handle_sig_alarm(SIGNAL_ARGS)
/* /*
* SIGALRM is always cause for waking anything waiting on the process * SIGALRM is always cause for waking anything waiting on the process
* latch. Cope with MyProc not being there, as the startup process also * latch.
* uses this signal handler.
*/ */
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
/* /*
* Fire any pending timeouts, but only if we're enabled to do so. * Fire any pending timeouts, but only if we're enabled to do so.
......
...@@ -148,6 +148,7 @@ extern int max_worker_processes; ...@@ -148,6 +148,7 @@ extern int max_worker_processes;
extern PGDLLIMPORT int MyProcPid; extern PGDLLIMPORT int MyProcPid;
extern PGDLLIMPORT pg_time_t MyStartTime; extern PGDLLIMPORT pg_time_t MyStartTime;
extern PGDLLIMPORT struct Port *MyProcPort; extern PGDLLIMPORT struct Port *MyProcPort;
extern PGDLLIMPORT struct Latch *MyLatch;
extern long MyCancelKey; extern long MyCancelKey;
extern int MyPMChildSlot; extern int MyPMChildSlot;
...@@ -302,6 +303,9 @@ extern void SetCurrentRoleId(Oid roleid, bool is_superuser); ...@@ -302,6 +303,9 @@ extern void SetCurrentRoleId(Oid roleid, bool is_superuser);
extern void SetDataDir(const char *dir); extern void SetDataDir(const char *dir);
extern void ChangeToDataDir(void); extern void ChangeToDataDir(void);
extern void SwitchToSharedLatch(void);
extern void SwitchBackToLocalLatch(void);
/* in utils/misc/superuser.c */ /* in utils/misc/superuser.c */
extern bool superuser(void); /* current user is superuser */ extern bool superuser(void); /* current user is superuser */
extern bool superuser_arg(Oid roleid); /* given user is superuser */ extern bool superuser_arg(Oid roleid); /* given user is superuser */
......
...@@ -85,7 +85,7 @@ ...@@ -85,7 +85,7 @@
* the public functions. It is defined here to allow embedding Latches as * the public functions. It is defined here to allow embedding Latches as
* part of bigger structs. * part of bigger structs.
*/ */
typedef struct typedef struct Latch
{ {
sig_atomic_t is_set; sig_atomic_t is_set;
bool is_shared; bool is_shared;
......
...@@ -285,13 +285,13 @@ wait_for_workers_to_become_ready(worker_state *wstate, ...@@ -285,13 +285,13 @@ wait_for_workers_to_become_ready(worker_state *wstate,
} }
/* Wait to be signalled. */ /* 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. */ /* An interrupt may have occurred while we were waiting. */
CHECK_FOR_INTERRUPTS(); CHECK_FOR_INTERRUPTS();
/* Reset the latch so we don't spin. */ /* Reset the latch so we don't spin. */
ResetLatch(&MyProc->procLatch); ResetLatch(MyLatch);
} }
} }
PG_CATCH(); PG_CATCH();
......
...@@ -230,9 +230,9 @@ test_shm_mq_pipelined(PG_FUNCTION_ARGS) ...@@ -230,9 +230,9 @@ test_shm_mq_pipelined(PG_FUNCTION_ARGS)
* have read or written data and therefore there may now be work * have read or written data and therefore there may now be work
* for us to do. * for us to do.
*/ */
WaitLatch(&MyProc->procLatch, WL_LATCH_SET, 0); WaitLatch(MyLatch, WL_LATCH_SET, 0);
CHECK_FOR_INTERRUPTS(); CHECK_FOR_INTERRUPTS();
ResetLatch(&MyProc->procLatch); ResetLatch(MyLatch);
} }
} }
......
...@@ -211,8 +211,7 @@ handle_sigterm(SIGNAL_ARGS) ...@@ -211,8 +211,7 @@ handle_sigterm(SIGNAL_ARGS)
{ {
int save_errno = errno; int save_errno = errno;
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
if (!proc_exit_inprogress) if (!proc_exit_inprogress)
{ {
......
...@@ -74,8 +74,7 @@ worker_spi_sigterm(SIGNAL_ARGS) ...@@ -74,8 +74,7 @@ worker_spi_sigterm(SIGNAL_ARGS)
int save_errno = errno; int save_errno = errno;
got_sigterm = true; got_sigterm = true;
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -91,8 +90,7 @@ worker_spi_sighup(SIGNAL_ARGS) ...@@ -91,8 +90,7 @@ worker_spi_sighup(SIGNAL_ARGS)
int save_errno = errno; int save_errno = errno;
got_sighup = true; got_sighup = true;
if (MyProc) SetLatch(MyLatch);
SetLatch(&MyProc->procLatch);
errno = save_errno; errno = save_errno;
} }
...@@ -227,10 +225,10 @@ worker_spi_main(Datum main_arg) ...@@ -227,10 +225,10 @@ worker_spi_main(Datum main_arg)
* necessary, but is awakened if postmaster dies. That way the * necessary, but is awakened if postmaster dies. That way the
* background process goes away immediately in an emergency. * background process goes away immediately in an emergency.
*/ */
rc = WaitLatch(&MyProc->procLatch, rc = WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
worker_spi_naptime * 1000L); worker_spi_naptime * 1000L);
ResetLatch(&MyProc->procLatch); ResetLatch(MyLatch);
/* emergency bailout if postmaster has died */ /* emergency bailout if postmaster has died */
if (rc & WL_POSTMASTER_DEATH) if (rc & WL_POSTMASTER_DEATH)
......
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