Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 83709a0

Browse files
committed
Use SIGURG rather than SIGUSR1 for latches.
Traditionally, SIGUSR1 has been overloaded for ad-hoc signals, procsignal.c signals and latch.c wakeups. Move that last use over to a new dedicated signal. SIGURG is normally used to report out-of-band socket data, but PostgreSQL doesn't use that facility. The signal handler is now installed in all postmaster children by InitializeLatchSupport(). Those wishing to disconnect from it should call ShutdownLatchSupport(). Future patches will use this separation of signals to avoid the need for a signal handler on some operating systems. Discussion: https://postgr.es/m/CA+hUKGJjxPDpzBE0a3hyUywBvaZuC89yx3jK9RFZgfv_KHU7gg@mail.gmail.com
1 parent c8f3bc2 commit 83709a0

File tree

5 files changed

+40
-46
lines changed

5 files changed

+40
-46
lines changed

src/backend/postmaster/bgworker.c

+2-17
Original file line numberDiff line numberDiff line change
@@ -713,22 +713,6 @@ bgworker_die(SIGNAL_ARGS)
713713
MyBgworkerEntry->bgw_type)));
714714
}
715715

716-
/*
717-
* Standard SIGUSR1 handler for unconnected workers
718-
*
719-
* Here, we want to make sure an unconnected worker will at least heed
720-
* latch activity.
721-
*/
722-
static void
723-
bgworker_sigusr1_handler(SIGNAL_ARGS)
724-
{
725-
int save_errno = errno;
726-
727-
latch_sigusr1_handler();
728-
729-
errno = save_errno;
730-
}
731-
732716
/*
733717
* Start a new background worker
734718
*
@@ -759,6 +743,7 @@ StartBackgroundWorker(void)
759743
*/
760744
if ((worker->bgw_flags & BGWORKER_SHMEM_ACCESS) == 0)
761745
{
746+
ShutdownLatchSupport();
762747
dsm_detach_all();
763748
PGSharedMemoryDetach();
764749
}
@@ -786,7 +771,7 @@ StartBackgroundWorker(void)
786771
else
787772
{
788773
pqsignal(SIGINT, SIG_IGN);
789-
pqsignal(SIGUSR1, bgworker_sigusr1_handler);
774+
pqsignal(SIGUSR1, SIG_IGN);
790775
pqsignal(SIGFPE, SIG_IGN);
791776
}
792777
pqsignal(SIGTERM, bgworker_die);

src/backend/postmaster/postmaster.c

+4
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,10 @@ PostmasterMain(int argc, char *argv[])
656656
pqsignal_pm(SIGUSR2, dummy_handler); /* unused, reserve for children */
657657
pqsignal_pm(SIGCHLD, reaper); /* handle child termination */
658658

659+
#ifdef SIGURG
660+
pqsignal_pm(SIGURG, SIG_IGN); /* ignored */
661+
#endif
662+
659663
/*
660664
* No other place in Postgres should touch SIGTTIN/SIGTTOU handling. We
661665
* ignore those signals in a postmaster environment, so that there is no

src/backend/storage/ipc/latch.c

+33-17
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* When SetLatch is called from the same process that owns the latch,
1818
* SetLatch writes the byte directly to the pipe. If it's owned by another
19-
* process, SIGUSR1 is sent and the signal handler in the waiting process
19+
* process, SIGURG is sent and the signal handler in the waiting process
2020
* writes the byte to the pipe on behalf of the signaling process.
2121
*
2222
* The Windows implementation uses Windows events that are inherited by all
@@ -148,6 +148,7 @@ static int selfpipe_writefd = -1;
148148
static int selfpipe_owner_pid = 0;
149149

150150
/* Private function prototypes */
151+
static void latch_sigurg_handler(SIGNAL_ARGS);
151152
static void sendSelfPipeByte(void);
152153
static void drainSelfPipe(void);
153154
#endif /* WIN32 */
@@ -244,6 +245,8 @@ InitializeLatchSupport(void)
244245
/* Tell fd.c about these two long-lived FDs */
245246
ReserveExternalFD();
246247
ReserveExternalFD();
248+
249+
pqsignal(SIGURG, latch_sigurg_handler);
247250
#else
248251
/* currently, nothing to do here for Windows */
249252
#endif
@@ -267,6 +270,24 @@ InitializeLatchWaitSet(void)
267270
Assert(latch_pos == LatchWaitSetLatchPos);
268271
}
269272

273+
void
274+
ShutdownLatchSupport(void)
275+
{
276+
pqsignal(SIGURG, SIG_IGN);
277+
278+
if (LatchWaitSet)
279+
{
280+
FreeWaitEventSet(LatchWaitSet);
281+
LatchWaitSet = NULL;
282+
}
283+
284+
close(selfpipe_readfd);
285+
close(selfpipe_writefd);
286+
selfpipe_readfd = -1;
287+
selfpipe_writefd = -1;
288+
selfpipe_owner_pid = InvalidPid;
289+
}
290+
270291
/*
271292
* Initialize a process-local latch.
272293
*/
@@ -335,10 +356,6 @@ InitSharedLatch(Latch *latch)
335356
* any sort of locking here, meaning that we could fail to detect the error
336357
* if two processes try to own the same latch at about the same time. If
337358
* there is any risk of that, caller must provide an interlock to prevent it.
338-
*
339-
* In any process that calls OwnLatch(), make sure that
340-
* latch_sigusr1_handler() is called from the SIGUSR1 signal handler,
341-
* as shared latches use SIGUSR1 for inter-process communication.
342359
*/
343360
void
344361
OwnLatch(Latch *latch)
@@ -562,7 +579,7 @@ SetLatch(Latch *latch)
562579
sendSelfPipeByte();
563580
}
564581
else
565-
kill(owner_pid, SIGUSR1);
582+
kill(owner_pid, SIGURG);
566583
#else
567584

568585
/*
@@ -1266,7 +1283,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
12661283
* the pipe-buffer fill up we're still ok, because the pipe is in
12671284
* nonblocking mode. It's unlikely for that to happen, because the
12681285
* self pipe isn't filled unless we're blocking (waiting = true), or
1269-
* from inside a signal handler in latch_sigusr1_handler().
1286+
* from inside a signal handler in latch_sigurg_handler().
12701287
*
12711288
* On windows, we'll also notice if there's a pending event for the
12721289
* latch when blocking, but there's no danger of anything filling up,
@@ -1934,22 +1951,21 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
19341951
}
19351952
#endif
19361953

1954+
#ifndef WIN32
19371955
/*
1938-
* SetLatch uses SIGUSR1 to wake up the process waiting on the latch.
1939-
*
1940-
* Wake up WaitLatch, if we're waiting. (We might not be, since SIGUSR1 is
1941-
* overloaded for multiple purposes; or we might not have reached WaitLatch
1942-
* yet, in which case we don't need to fill the pipe either.)
1956+
* SetLatch uses SIGURG to wake up the process waiting on the latch.
19431957
*
1944-
* NB: when calling this in a signal handler, be sure to save and restore
1945-
* errno around it.
1958+
* Wake up WaitLatch, if we're waiting.
19461959
*/
1947-
#ifndef WIN32
1948-
void
1949-
latch_sigusr1_handler(void)
1960+
static void
1961+
latch_sigurg_handler(SIGNAL_ARGS)
19501962
{
1963+
int save_errno = errno;
1964+
19511965
if (waiting)
19521966
sendSelfPipeByte();
1967+
1968+
errno = save_errno;
19531969
}
19541970
#endif /* !WIN32 */
19551971

src/backend/storage/ipc/procsignal.c

-2
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,5 @@ procsignal_sigusr1_handler(SIGNAL_ARGS)
688688

689689
SetLatch(MyLatch);
690690

691-
latch_sigusr1_handler();
692-
693691
errno = save_errno;
694692
}

src/include/storage/latch.h

+1-10
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ extern void OwnLatch(Latch *latch);
163163
extern void DisownLatch(Latch *latch);
164164
extern void SetLatch(Latch *latch);
165165
extern void ResetLatch(Latch *latch);
166+
extern void ShutdownLatchSupport(void);
166167

167168
extern WaitEventSet *CreateWaitEventSet(MemoryContext context, int nevents);
168169
extern void FreeWaitEventSet(WaitEventSet *set);
@@ -179,14 +180,4 @@ extern int WaitLatchOrSocket(Latch *latch, int wakeEvents,
179180
pgsocket sock, long timeout, uint32 wait_event_info);
180181
extern void InitializeLatchWaitSet(void);
181182

182-
/*
183-
* Unix implementation uses SIGUSR1 for inter-process signaling.
184-
* Win32 doesn't need this.
185-
*/
186-
#ifndef WIN32
187-
extern void latch_sigusr1_handler(void);
188-
#else
189-
#define latch_sigusr1_handler() ((void) 0)
190-
#endif
191-
192183
#endif /* LATCH_H */

0 commit comments

Comments
 (0)