@@ -283,27 +283,31 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
283
283
do
284
284
{
285
285
/*
286
- * Clear the pipe, then check if the latch is set already. If someone
287
- * sets the latch between this and the poll()/select() below, the
288
- * setter will write a byte to the pipe (or signal us and the signal
289
- * handler will do that), and the poll()/select() will return
290
- * immediately.
286
+ * Check if the latch is set already. If so, leave loop immediately,
287
+ * avoid blocking again. We don't attempt to report any other events
288
+ * that might also be satisfied.
289
+ *
290
+ * If someone sets the latch between this and the poll()/select()
291
+ * below, the setter will write a byte to the pipe (or signal us and
292
+ * the signal handler will do that), and the poll()/select() will
293
+ * return immediately.
294
+ *
295
+ * If there's a pending byte in the self pipe, we'll notice whenever
296
+ * blocking. Only clearing the pipe in that case avoids having to
297
+ * drain it every time WaitLatchOrSocket() is used. Should the
298
+ * pipe-buffer fill up we're still ok, because the pipe is in
299
+ * nonblocking mode. It's unlikely for that to happen, because the
300
+ * self pipe isn't filled unless we're blocking (waiting = true), or
301
+ * from inside a signal handler in latch_sigusr1_handler().
291
302
*
292
303
* Note: we assume that the kernel calls involved in drainSelfPipe()
293
304
* and SetLatch() will provide adequate synchronization on machines
294
305
* with weak memory ordering, so that we cannot miss seeing is_set if
295
306
* the signal byte is already in the pipe when we drain it.
296
307
*/
297
- drainSelfPipe ();
298
-
299
308
if ((wakeEvents & WL_LATCH_SET ) && latch -> is_set )
300
309
{
301
310
result |= WL_LATCH_SET ;
302
-
303
- /*
304
- * Leave loop immediately, avoid blocking again. We don't attempt
305
- * to report any other events that might also be satisfied.
306
- */
307
311
break ;
308
312
}
309
313
@@ -313,24 +317,26 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
313
317
*/
314
318
#if defined(LATCH_USE_POLL )
315
319
nfds = 0 ;
320
+
321
+ /* selfpipe is always in pfds[0] */
322
+ pfds [0 ].fd = selfpipe_readfd ;
323
+ pfds [0 ].events = POLLIN ;
324
+ pfds [0 ].revents = 0 ;
325
+ nfds ++ ;
326
+
316
327
if (wakeEvents & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE ))
317
328
{
318
- /* socket, if used, is always in pfds[0 ] */
319
- pfds [0 ].fd = sock ;
320
- pfds [0 ].events = 0 ;
329
+ /* socket, if used, is always in pfds[1 ] */
330
+ pfds [1 ].fd = sock ;
331
+ pfds [1 ].events = 0 ;
321
332
if (wakeEvents & WL_SOCKET_READABLE )
322
- pfds [0 ].events |= POLLIN ;
333
+ pfds [1 ].events |= POLLIN ;
323
334
if (wakeEvents & WL_SOCKET_WRITEABLE )
324
- pfds [0 ].events |= POLLOUT ;
325
- pfds [0 ].revents = 0 ;
335
+ pfds [1 ].events |= POLLOUT ;
336
+ pfds [1 ].revents = 0 ;
326
337
nfds ++ ;
327
338
}
328
339
329
- pfds [nfds ].fd = selfpipe_readfd ;
330
- pfds [nfds ].events = POLLIN ;
331
- pfds [nfds ].revents = 0 ;
332
- nfds ++ ;
333
-
334
340
if (wakeEvents & WL_POSTMASTER_DEATH )
335
341
{
336
342
/* postmaster fd, if used, is always in pfds[nfds - 1] */
@@ -364,19 +370,27 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
364
370
else
365
371
{
366
372
/* at least one event occurred, so check revents values */
373
+
374
+ if (pfds [0 ].revents & POLLIN )
375
+ {
376
+ /* There's data in the self-pipe, clear it. */
377
+ drainSelfPipe ();
378
+ }
379
+
367
380
if ((wakeEvents & WL_SOCKET_READABLE ) &&
368
- (pfds [0 ].revents & POLLIN ))
381
+ (pfds [1 ].revents & POLLIN ))
369
382
{
370
383
/* data available in socket, or EOF/error condition */
371
384
result |= WL_SOCKET_READABLE ;
372
385
}
373
386
if ((wakeEvents & WL_SOCKET_WRITEABLE ) &&
374
- (pfds [0 ].revents & POLLOUT ))
387
+ (pfds [1 ].revents & POLLOUT ))
375
388
{
376
389
/* socket is writable */
377
390
result |= WL_SOCKET_WRITEABLE ;
378
391
}
379
- if (pfds [0 ].revents & (POLLHUP | POLLERR | POLLNVAL ))
392
+ if ((wakeEvents & WL_SOCKET_WRITEABLE ) &&
393
+ (pfds [1 ].revents & (POLLHUP | POLLERR | POLLNVAL )))
380
394
{
381
395
/* EOF/error condition */
382
396
if (wakeEvents & WL_SOCKET_READABLE )
@@ -468,6 +482,11 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
468
482
else
469
483
{
470
484
/* at least one event occurred, so check masks */
485
+ if (FD_ISSET (selfpipe_readfd , & input_mask ))
486
+ {
487
+ /* There's data in the self-pipe, clear it. */
488
+ drainSelfPipe ();
489
+ }
471
490
if ((wakeEvents & WL_SOCKET_READABLE ) && FD_ISSET (sock , & input_mask ))
472
491
{
473
492
/* data available in socket, or EOF */
@@ -498,6 +517,16 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
498
517
}
499
518
#endif /* LATCH_USE_SELECT */
500
519
520
+ /*
521
+ * Check again whether latch is set, the arrival of a signal/self-byte
522
+ * might be what stopped our sleep. It's not required for correctness
523
+ * to signal the latch as being set (we'd just loop if there's no
524
+ * other event), but it seems good to report an arrived latch asap.
525
+ * This way we also don't have to compute the current timestamp again.
526
+ */
527
+ if ((wakeEvents & WL_LATCH_SET ) && latch -> is_set )
528
+ result |= WL_LATCH_SET ;
529
+
501
530
/* If we're not done, update cur_timeout for next iteration */
502
531
if (result == 0 && (wakeEvents & WL_TIMEOUT ))
503
532
{
0 commit comments