+ /* Must wait ... we use poll(2) if available, otherwise select(2) */
+#ifdef HAVE_POLL
+ nfds = 0;
+ if (wakeEvents & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE))
+ {
+ /* socket, if used, is always in pfds[0] */
+ pfds[0].fd = sock;
+ pfds[0].events = 0;
+ if (wakeEvents & WL_SOCKET_READABLE)
+ pfds[0].events |= POLLIN;
+ if (wakeEvents & WL_SOCKET_WRITEABLE)
+ pfds[0].events |= POLLOUT;
+ pfds[0].revents = 0;
+ nfds++;
+ }
+
+ pfds[nfds].fd = selfpipe_readfd;
+ pfds[nfds].events = POLLIN;
+ pfds[nfds].revents = 0;
+ nfds++;
+
+ if (wakeEvents & WL_POSTMASTER_DEATH)
+ {
+ /* postmaster fd, if used, is always in pfds[nfds - 1] */
+ pfds[nfds].fd = postmaster_alive_fds[POSTMASTER_FD_WATCH];
+ pfds[nfds].events = POLLIN;
+ pfds[nfds].revents = 0;
+ nfds++;
+ }
+
+ /* Sleep */
+ rc = poll(pfds, nfds, (int) timeout);
+
+ /* Check return code */
+ if (rc < 0)
+ {
+ if (errno == EINTR)
+ continue;
+ waiting = false;
+ ereport(ERROR,
+ (errcode_for_socket_access(),
+ errmsg("poll() failed: %m")));
+ }
+ if (rc == 0 && (wakeEvents & WL_TIMEOUT))
+ {
+ /* timeout exceeded */
+ result |= WL_TIMEOUT;
+ }
+ if ((wakeEvents & WL_SOCKET_READABLE) &&
+ (pfds[0].revents & POLLIN))
+ {
+ /* data available in socket */
+ result |= WL_SOCKET_READABLE;
+ }
+ if ((wakeEvents & WL_SOCKET_WRITEABLE) &&
+ (pfds[0].revents & POLLOUT))
+ {
+ result |= WL_SOCKET_WRITEABLE;
+ }
+ if ((wakeEvents & WL_POSTMASTER_DEATH) &&
+ (pfds[nfds - 1].revents & POLLIN))
+ {
+ result |= WL_POSTMASTER_DEATH;
+ }
+
+#else /* !HAVE_POLL */
+