16
16
*
17
17
* When SetLatch is called from the same process that owns the latch,
18
18
* 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
20
20
* writes the byte to the pipe on behalf of the signaling process.
21
21
*
22
22
* The Windows implementation uses Windows events that are inherited by all
@@ -148,6 +148,7 @@ static int selfpipe_writefd = -1;
148
148
static int selfpipe_owner_pid = 0 ;
149
149
150
150
/* Private function prototypes */
151
+ static void latch_sigurg_handler (SIGNAL_ARGS );
151
152
static void sendSelfPipeByte (void );
152
153
static void drainSelfPipe (void );
153
154
#endif /* WIN32 */
@@ -244,6 +245,8 @@ InitializeLatchSupport(void)
244
245
/* Tell fd.c about these two long-lived FDs */
245
246
ReserveExternalFD ();
246
247
ReserveExternalFD ();
248
+
249
+ pqsignal (SIGURG , latch_sigurg_handler );
247
250
#else
248
251
/* currently, nothing to do here for Windows */
249
252
#endif
@@ -267,6 +270,24 @@ InitializeLatchWaitSet(void)
267
270
Assert (latch_pos == LatchWaitSetLatchPos );
268
271
}
269
272
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
+
270
291
/*
271
292
* Initialize a process-local latch.
272
293
*/
@@ -335,10 +356,6 @@ InitSharedLatch(Latch *latch)
335
356
* any sort of locking here, meaning that we could fail to detect the error
336
357
* if two processes try to own the same latch at about the same time. If
337
358
* 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.
342
359
*/
343
360
void
344
361
OwnLatch (Latch * latch )
@@ -562,7 +579,7 @@ SetLatch(Latch *latch)
562
579
sendSelfPipeByte ();
563
580
}
564
581
else
565
- kill (owner_pid , SIGUSR1 );
582
+ kill (owner_pid , SIGURG );
566
583
#else
567
584
568
585
/*
@@ -1266,7 +1283,7 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
1266
1283
* the pipe-buffer fill up we're still ok, because the pipe is in
1267
1284
* nonblocking mode. It's unlikely for that to happen, because the
1268
1285
* 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 ().
1270
1287
*
1271
1288
* On windows, we'll also notice if there's a pending event for the
1272
1289
* latch when blocking, but there's no danger of anything filling up,
@@ -1934,22 +1951,21 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
1934
1951
}
1935
1952
#endif
1936
1953
1954
+ #ifndef WIN32
1937
1955
/*
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.
1943
1957
*
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.
1946
1959
*/
1947
- #ifndef WIN32
1948
- void
1949
- latch_sigusr1_handler (void )
1960
+ static void
1961
+ latch_sigurg_handler (SIGNAL_ARGS )
1950
1962
{
1963
+ int save_errno = errno ;
1964
+
1951
1965
if (waiting )
1952
1966
sendSelfPipeByte ();
1967
+
1968
+ errno = save_errno ;
1953
1969
}
1954
1970
#endif /* !WIN32 */
1955
1971
0 commit comments