5656#include "storage/pmsignal.h"
5757#include "storage/shmem.h"
5858
59+ /*
60+ * Select the fd readiness primitive to use. Normally the "most modern"
61+ * primitive supported by the OS will be used, but for testing it can be
62+ * useful to manually specify the used primitive. If desired, just add a
63+ * define somewhere before this block.
64+ */
65+ #if defined(LATCH_USE_POLL )|| defined(LATCH_USE_SELECT )
66+ /* don't overwrite manual choice */
67+ #elif defined(HAVE_POLL )
68+ #define LATCH_USE_POLL
69+ #elif HAVE_SYS_SELECT_H
70+ #define LATCH_USE_SELECT
71+ #else
72+ #error "no latch implementation available"
73+ #endif
74+
5975/* Are we currently in WaitLatch? The signal handler would like to know. */
6076static volatile sig_atomic_t waiting = false;
6177
@@ -215,10 +231,10 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
215231cur_time ;
216232long cur_timeout ;
217233
218- #ifdef HAVE_POLL
234+ #if defined( LATCH_USE_POLL )
219235struct pollfd pfds [3 ];
220236int nfds ;
221- #else
237+ #elif defined( LATCH_USE_SELECT )
222238struct timeval tv ,
223239* tvp ;
224240fd_set input_mask ;
@@ -248,7 +264,7 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
248264Assert (timeout >=0 && timeout <=INT_MAX );
249265cur_timeout = timeout ;
250266
251- #ifndef HAVE_POLL
267+ #ifdef LATCH_USE_SELECT
252268tv .tv_sec = cur_timeout /1000L ;
253269tv .tv_usec = (cur_timeout %1000L )* 1000L ;
254270tvp = & tv ;
@@ -258,7 +274,7 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
258274{
259275cur_timeout = -1 ;
260276
261- #ifndef HAVE_POLL
277+ #ifdef LATCH_USE_SELECT
262278tvp = NULL ;
263279#endif
264280}
@@ -292,16 +308,10 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
292308}
293309
294310/*
295- * Must wait ... we use poll(2) if available, otherwise select(2).
296- *
297- * On at least older linux kernels select(), in violation of POSIX,
298- * doesn't reliably return a socket as writable if closed - but we
299- * rely on that. So far all the known cases of this problem are on
300- * platforms that also provide a poll() implementation without that
301- * bug. If we find one where that's not the case, we'll need to add a
302- * workaround.
311+ * Must wait ... we use the polling interface determined at the top of
312+ * this file to do so.
303313 */
304- #ifdef HAVE_POLL
314+ #if defined( LATCH_USE_POLL )
305315nfds = 0 ;
306316if (wakeEvents & (WL_SOCKET_READABLE |WL_SOCKET_WRITEABLE ))
307317{
@@ -397,8 +407,16 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
397407result |=WL_POSTMASTER_DEATH ;
398408}
399409}
400- #else /* !HAVE_POLL */
410+ #elif defined( LATCH_USE_SELECT )
401411
412+ /*
413+ * On at least older linux kernels select(), in violation of POSIX,
414+ * doesn't reliably return a socket as writable if closed - but we
415+ * rely on that. So far all the known cases of this problem are on
416+ * platforms that also provide a poll() implementation without that
417+ * bug. If we find one where that's not the case, we'll need to add a
418+ * workaround.
419+ */
402420FD_ZERO (& input_mask );
403421FD_ZERO (& output_mask );
404422
@@ -478,7 +496,7 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
478496result |=WL_POSTMASTER_DEATH ;
479497}
480498}
481- #endif /*HAVE_POLL */
499+ #endif /*LATCH_USE_SELECT */
482500
483501/* If we're not done, update cur_timeout for next iteration */
484502if (result == 0 && (wakeEvents & WL_TIMEOUT ))
@@ -491,7 +509,7 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
491509/* Timeout has expired, no need to continue looping */
492510result |=WL_TIMEOUT ;
493511}
494- #ifndef HAVE_POLL
512+ #ifdef LATCH_USE_SELECT
495513else
496514{
497515tv .tv_sec = cur_timeout /1000L ;