@@ -135,9 +135,12 @@ DisownLatch(volatile Latch *latch)
135
135
* If the latch is already set, the function returns immediately.
136
136
*
137
137
* The 'timeout' is given in milliseconds, and -1 means wait forever.
138
- * On some platforms, signals cause the timeout to be restarted, so beware
139
- * that the function can sleep for several times longer than the specified
140
- * timeout.
138
+ * On some platforms, signals do not interrupt the wait, or even
139
+ * cause the timeout to be restarted, so beware that the function can sleep
140
+ * for several times longer than the requested timeout. However, this
141
+ * difficulty is not so great as it seems, because the signal handlers for any
142
+ * signals that the caller should respond to ought to be programmed to end the
143
+ * wait by calling SetLatch. Ideally, the timeout parameter is vestigial.
141
144
*
142
145
* The latch must be owned by the current process, ie. it must be a
143
146
* backend-local latch initialized with InitLatch, or a shared latch
@@ -228,6 +231,7 @@ WaitLatchOrSocket(volatile Latch *latch, pgsocket sock, bool forRead,
228
231
{
229
232
if (errno == EINTR )
230
233
continue ;
234
+ waiting = false;
231
235
ereport (ERROR ,
232
236
(errcode_for_socket_access (),
233
237
errmsg ("select() failed: %m" )));
@@ -255,6 +259,10 @@ WaitLatchOrSocket(volatile Latch *latch, pgsocket sock, bool forRead,
255
259
* Sets a latch and wakes up anyone waiting on it.
256
260
*
257
261
* This is cheap if the latch is already set, otherwise not so much.
262
+ *
263
+ * NB: when calling this in a signal handler, be sure to save and restore
264
+ * errno around it. (That's standard practice in most signal handlers, of
265
+ * course, but we used to omit it in handlers that only set a flag.)
258
266
*/
259
267
void
260
268
SetLatch (volatile Latch * latch )
@@ -300,7 +308,10 @@ SetLatch(volatile Latch *latch)
300
308
if (owner_pid == 0 )
301
309
return ;
302
310
else if (owner_pid == MyProcPid )
303
- sendSelfPipeByte ();
311
+ {
312
+ if (waiting )
313
+ sendSelfPipeByte ();
314
+ }
304
315
else
305
316
kill (owner_pid , SIGUSR1 );
306
317
}
@@ -332,7 +343,11 @@ ResetLatch(volatile Latch *latch)
332
343
* SetLatch uses SIGUSR1 to wake up the process waiting on the latch.
333
344
*
334
345
* Wake up WaitLatch, if we're waiting. (We might not be, since SIGUSR1 is
335
- * overloaded for multiple purposes.)
346
+ * overloaded for multiple purposes; or we might not have reached WaitLatch
347
+ * yet, in which case we don't need to fill the pipe either.)
348
+ *
349
+ * NB: when calling this in a signal handler, be sure to save and restore
350
+ * errno around it.
336
351
*/
337
352
void
338
353
latch_sigusr1_handler (void )
@@ -396,13 +411,19 @@ sendSelfPipeByte(void)
396
411
}
397
412
}
398
413
399
- /* Read all available data from the self-pipe */
414
+ /*
415
+ * Read all available data from the self-pipe
416
+ *
417
+ * Note: this is only called when waiting = true. If it fails and doesn't
418
+ * return, it must reset that flag first (though ideally, this will never
419
+ * happen).
420
+ */
400
421
static void
401
422
drainSelfPipe (void )
402
423
{
403
424
/*
404
425
* There shouldn't normally be more than one byte in the pipe, or maybe a
405
- * few more if multiple processes run SetLatch at the same instant.
426
+ * few bytes if multiple processes run SetLatch at the same instant.
406
427
*/
407
428
char buf [16 ];
408
429
int rc ;
@@ -417,9 +438,21 @@ drainSelfPipe(void)
417
438
else if (errno == EINTR )
418
439
continue ; /* retry */
419
440
else
441
+ {
442
+ waiting = false;
420
443
elog (ERROR , "read() on self-pipe failed: %m" );
444
+ }
421
445
}
422
446
else if (rc == 0 )
447
+ {
448
+ waiting = false;
423
449
elog (ERROR , "unexpected EOF on self-pipe" );
450
+ }
451
+ else if (rc < sizeof (buf ))
452
+ {
453
+ /* we successfully drained the pipe; no need to read() again */
454
+ break ;
455
+ }
456
+ /* else buffer wasn't big enough, so read again */
424
457
}
425
458
}
0 commit comments