LCOV - code coverage report
Current view: top level - src/backend/storage/ipc - latch.c (source / functions) Hit Total Coverage
Test: PostgreSQL 14devel Lines: 247 287 86.1 %
Date: 2020-11-27 11:06:40 Functions: 20 20 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * latch.c
       4             :  *    Routines for inter-process latches
       5             :  *
       6             :  * The Unix implementation uses the so-called self-pipe trick to overcome the
       7             :  * race condition involved with poll() (or epoll_wait() on linux) and setting
       8             :  * a global flag in the signal handler. When a latch is set and the current
       9             :  * process is waiting for it, the signal handler wakes up the poll() in
      10             :  * WaitLatch by writing a byte to a pipe. A signal by itself doesn't interrupt
      11             :  * poll() on all platforms, and even on platforms where it does, a signal that
      12             :  * arrives just before the poll() call does not prevent poll() from entering
      13             :  * sleep. An incoming byte on a pipe however reliably interrupts the sleep,
      14             :  * and causes poll() to return immediately even if the signal arrives before
      15             :  * poll() begins.
      16             :  *
      17             :  * When SetLatch is called from the same process that owns the latch,
      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
      20             :  * writes the byte to the pipe on behalf of the signaling process.
      21             :  *
      22             :  * The Windows implementation uses Windows events that are inherited by all
      23             :  * postmaster child processes. There's no need for the self-pipe trick there.
      24             :  *
      25             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
      26             :  * Portions Copyright (c) 1994, Regents of the University of California
      27             :  *
      28             :  * IDENTIFICATION
      29             :  *    src/backend/storage/ipc/latch.c
      30             :  *
      31             :  *-------------------------------------------------------------------------
      32             :  */
      33             : #include "postgres.h"
      34             : 
      35             : #include <fcntl.h>
      36             : #include <limits.h>
      37             : #include <signal.h>
      38             : #include <unistd.h>
      39             : #ifdef HAVE_SYS_EPOLL_H
      40             : #include <sys/epoll.h>
      41             : #endif
      42             : #ifdef HAVE_SYS_EVENT_H
      43             : #include <sys/event.h>
      44             : #endif
      45             : #ifdef HAVE_POLL_H
      46             : #include <poll.h>
      47             : #endif
      48             : 
      49             : #include "miscadmin.h"
      50             : #include "pgstat.h"
      51             : #include "port/atomics.h"
      52             : #include "portability/instr_time.h"
      53             : #include "postmaster/postmaster.h"
      54             : #include "storage/fd.h"
      55             : #include "storage/ipc.h"
      56             : #include "storage/latch.h"
      57             : #include "storage/pmsignal.h"
      58             : #include "storage/shmem.h"
      59             : #include "utils/memutils.h"
      60             : 
      61             : /*
      62             :  * Select the fd readiness primitive to use. Normally the "most modern"
      63             :  * primitive supported by the OS will be used, but for testing it can be
      64             :  * useful to manually specify the used primitive.  If desired, just add a
      65             :  * define somewhere before this block.
      66             :  */
      67             : #if defined(WAIT_USE_EPOLL) || defined(WAIT_USE_POLL) || \
      68             :     defined(WAIT_USE_KQUEUE) || defined(WAIT_USE_WIN32)
      69             : /* don't overwrite manual choice */
      70             : #elif defined(HAVE_SYS_EPOLL_H)
      71             : #define WAIT_USE_EPOLL
      72             : #elif defined(HAVE_KQUEUE)
      73             : #define WAIT_USE_KQUEUE
      74             : #elif defined(HAVE_POLL)
      75             : #define WAIT_USE_POLL
      76             : #elif WIN32
      77             : #define WAIT_USE_WIN32
      78             : #else
      79             : #error "no wait set implementation available"
      80             : #endif
      81             : 
      82             : /* typedef in latch.h */
      83             : struct WaitEventSet
      84             : {
      85             :     int         nevents;        /* number of registered events */
      86             :     int         nevents_space;  /* maximum number of events in this set */
      87             : 
      88             :     /*
      89             :      * Array, of nevents_space length, storing the definition of events this
      90             :      * set is waiting for.
      91             :      */
      92             :     WaitEvent  *events;
      93             : 
      94             :     /*
      95             :      * If WL_LATCH_SET is specified in any wait event, latch is a pointer to
      96             :      * said latch, and latch_pos the offset in the ->events array. This is
      97             :      * useful because we check the state of the latch before performing doing
      98             :      * syscalls related to waiting.
      99             :      */
     100             :     Latch      *latch;
     101             :     int         latch_pos;
     102             : 
     103             :     /*
     104             :      * WL_EXIT_ON_PM_DEATH is converted to WL_POSTMASTER_DEATH, but this flag
     105             :      * is set so that we'll exit immediately if postmaster death is detected,
     106             :      * instead of returning.
     107             :      */
     108             :     bool        exit_on_postmaster_death;
     109             : 
     110             : #if defined(WAIT_USE_EPOLL)
     111             :     int         epoll_fd;
     112             :     /* epoll_wait returns events in a user provided arrays, allocate once */
     113             :     struct epoll_event *epoll_ret_events;
     114             : #elif defined(WAIT_USE_KQUEUE)
     115             :     int         kqueue_fd;
     116             :     /* kevent returns events in a user provided arrays, allocate once */
     117             :     struct kevent *kqueue_ret_events;
     118             :     bool        report_postmaster_not_running;
     119             : #elif defined(WAIT_USE_POLL)
     120             :     /* poll expects events to be waited on every poll() call, prepare once */
     121             :     struct pollfd *pollfds;
     122             : #elif defined(WAIT_USE_WIN32)
     123             : 
     124             :     /*
     125             :      * Array of windows events. The first element always contains
     126             :      * pgwin32_signal_event, so the remaining elements are offset by one (i.e.
     127             :      * event->pos + 1).
     128             :      */
     129             :     HANDLE     *handles;
     130             : #endif
     131             : };
     132             : 
     133             : /* A common WaitEventSet used to implement WatchLatch() */
     134             : static WaitEventSet *LatchWaitSet;
     135             : 
     136             : /* The position of the latch in LatchWaitSet. */
     137             : #define LatchWaitSetLatchPos 0
     138             : 
     139             : #ifndef WIN32
     140             : /* Are we currently in WaitLatch? The signal handler would like to know. */
     141             : static volatile sig_atomic_t waiting = false;
     142             : 
     143             : /* Read and write ends of the self-pipe */
     144             : static int  selfpipe_readfd = -1;
     145             : static int  selfpipe_writefd = -1;
     146             : 
     147             : /* Process owning the self-pipe --- needed for checking purposes */
     148             : static int  selfpipe_owner_pid = 0;
     149             : 
     150             : /* Private function prototypes */
     151             : static void sendSelfPipeByte(void);
     152             : static void drainSelfPipe(void);
     153             : #endif                          /* WIN32 */
     154             : 
     155             : #if defined(WAIT_USE_EPOLL)
     156             : static void WaitEventAdjustEpoll(WaitEventSet *set, WaitEvent *event, int action);
     157             : #elif defined(WAIT_USE_KQUEUE)
     158             : static void WaitEventAdjustKqueue(WaitEventSet *set, WaitEvent *event, int old_events);
     159             : #elif defined(WAIT_USE_POLL)
     160             : static void WaitEventAdjustPoll(WaitEventSet *set, WaitEvent *event);
     161             : #elif defined(WAIT_USE_WIN32)
     162             : static void WaitEventAdjustWin32(WaitEventSet *set, WaitEvent *event);
     163             : #endif
     164             : 
     165             : static inline int WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
     166             :                                         WaitEvent *occurred_events, int nevents);
     167             : 
     168             : /*
     169             :  * Initialize the process-local latch infrastructure.
     170             :  *
     171             :  * This must be called once during startup of any process that can wait on
     172             :  * latches, before it issues any InitLatch() or OwnLatch() calls.
     173             :  */
     174             : void
     175       15804 : InitializeLatchSupport(void)
     176             : {
     177             : #ifndef WIN32
     178             :     int         pipefd[2];
     179             : 
     180       15804 :     if (IsUnderPostmaster)
     181             :     {
     182             :         /*
     183             :          * We might have inherited connections to a self-pipe created by the
     184             :          * postmaster.  It's critical that child processes create their own
     185             :          * self-pipes, of course, and we really want them to close the
     186             :          * inherited FDs for safety's sake.
     187             :          */
     188       14198 :         if (selfpipe_owner_pid != 0)
     189             :         {
     190             :             /* Assert we go through here but once in a child process */
     191             :             Assert(selfpipe_owner_pid != MyProcPid);
     192             :             /* Release postmaster's pipe FDs; ignore any error */
     193           0 :             (void) close(selfpipe_readfd);
     194           0 :             (void) close(selfpipe_writefd);
     195             :             /* Clean up, just for safety's sake; we'll set these below */
     196           0 :             selfpipe_readfd = selfpipe_writefd = -1;
     197           0 :             selfpipe_owner_pid = 0;
     198             :             /* Keep fd.c's accounting straight */
     199           0 :             ReleaseExternalFD();
     200           0 :             ReleaseExternalFD();
     201             :         }
     202             :         else
     203             :         {
     204             :             /*
     205             :              * Postmaster didn't create a self-pipe ... or else we're in an
     206             :              * EXEC_BACKEND build, in which case it doesn't matter since the
     207             :              * postmaster's pipe FDs were closed by the action of FD_CLOEXEC.
     208             :              * fd.c won't have state to clean up, either.
     209             :              */
     210             :             Assert(selfpipe_readfd == -1);
     211             :         }
     212             :     }
     213             :     else
     214             :     {
     215             :         /* In postmaster or standalone backend, assert we do this but once */
     216             :         Assert(selfpipe_readfd == -1);
     217             :         Assert(selfpipe_owner_pid == 0);
     218             :     }
     219             : 
     220             :     /*
     221             :      * Set up the self-pipe that allows a signal handler to wake up the
     222             :      * poll()/epoll_wait() in WaitLatch. Make the write-end non-blocking, so
     223             :      * that SetLatch won't block if the event has already been set many times
     224             :      * filling the kernel buffer. Make the read-end non-blocking too, so that
     225             :      * we can easily clear the pipe by reading until EAGAIN or EWOULDBLOCK.
     226             :      * Also, make both FDs close-on-exec, since we surely do not want any
     227             :      * child processes messing with them.
     228             :      */
     229       15804 :     if (pipe(pipefd) < 0)
     230           0 :         elog(FATAL, "pipe() failed: %m");
     231       15804 :     if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) == -1)
     232           0 :         elog(FATAL, "fcntl(F_SETFL) failed on read-end of self-pipe: %m");
     233       15804 :     if (fcntl(pipefd[1], F_SETFL, O_NONBLOCK) == -1)
     234           0 :         elog(FATAL, "fcntl(F_SETFL) failed on write-end of self-pipe: %m");
     235       15804 :     if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) == -1)
     236           0 :         elog(FATAL, "fcntl(F_SETFD) failed on read-end of self-pipe: %m");
     237       15804 :     if (fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) == -1)
     238           0 :         elog(FATAL, "fcntl(F_SETFD) failed on write-end of self-pipe: %m");
     239             : 
     240       15804 :     selfpipe_readfd = pipefd[0];
     241       15804 :     selfpipe_writefd = pipefd[1];
     242       15804 :     selfpipe_owner_pid = MyProcPid;
     243             : 
     244             :     /* Tell fd.c about these two long-lived FDs */
     245       15804 :     ReserveExternalFD();
     246       15804 :     ReserveExternalFD();
     247             : #else
     248             :     /* currently, nothing to do here for Windows */
     249             : #endif
     250       15804 : }
     251             : 
     252             : void
     253       15804 : InitializeLatchWaitSet(void)
     254             : {
     255             :     int         latch_pos PG_USED_FOR_ASSERTS_ONLY;
     256             : 
     257             :     Assert(LatchWaitSet == NULL);
     258             : 
     259             :     /* Set up the WaitEventSet used by WaitLatch(). */
     260       15804 :     LatchWaitSet = CreateWaitEventSet(TopMemoryContext, 2);
     261       15804 :     latch_pos = AddWaitEventToSet(LatchWaitSet, WL_LATCH_SET, PGINVALID_SOCKET,
     262             :                                   MyLatch, NULL);
     263       15804 :     if (IsUnderPostmaster)
     264       14198 :         AddWaitEventToSet(LatchWaitSet, WL_EXIT_ON_PM_DEATH,
     265             :                           PGINVALID_SOCKET, NULL, NULL);
     266             : 
     267             :     Assert(latch_pos == LatchWaitSetLatchPos);
     268       15804 : }
     269             : 
     270             : /*
     271             :  * Initialize a process-local latch.
     272             :  */
     273             : void
     274       15804 : InitLatch(Latch *latch)
     275             : {
     276       15804 :     latch->is_set = false;
     277       15804 :     latch->owner_pid = MyProcPid;
     278       15804 :     latch->is_shared = false;
     279             : 
     280             : #ifndef WIN32
     281             :     /* Assert InitializeLatchSupport has been called in this process */
     282             :     Assert(selfpipe_readfd >= 0 && selfpipe_owner_pid == MyProcPid);
     283             : #else
     284             :     latch->event = CreateEvent(NULL, TRUE, FALSE, NULL);
     285             :     if (latch->event == NULL)
     286             :         elog(ERROR, "CreateEvent failed: error code %lu", GetLastError());
     287             : #endif                          /* WIN32 */
     288       15804 : }
     289             : 
     290             : /*
     291             :  * Initialize a shared latch that can be set from other processes. The latch
     292             :  * is initially owned by no-one; use OwnLatch to associate it with the
     293             :  * current process.
     294             :  *
     295             :  * InitSharedLatch needs to be called in postmaster before forking child
     296             :  * processes, usually right after allocating the shared memory block
     297             :  * containing the latch with ShmemInitStruct. (The Unix implementation
     298             :  * doesn't actually require that, but the Windows one does.) Because of
     299             :  * this restriction, we have no concurrency issues to worry about here.
     300             :  *
     301             :  * Note that other handles created in this module are never marked as
     302             :  * inheritable.  Thus we do not need to worry about cleaning up child
     303             :  * process references to postmaster-private latches or WaitEventSets.
     304             :  */
     305             : void
     306      260538 : InitSharedLatch(Latch *latch)
     307             : {
     308             : #ifdef WIN32
     309             :     SECURITY_ATTRIBUTES sa;
     310             : 
     311             :     /*
     312             :      * Set up security attributes to specify that the events are inherited.
     313             :      */
     314             :     ZeroMemory(&sa, sizeof(sa));
     315             :     sa.nLength = sizeof(sa);
     316             :     sa.bInheritHandle = TRUE;
     317             : 
     318             :     latch->event = CreateEvent(&sa, TRUE, FALSE, NULL);
     319             :     if (latch->event == NULL)
     320             :         elog(ERROR, "CreateEvent failed: error code %lu", GetLastError());
     321             : #endif
     322             : 
     323      260538 :     latch->is_set = false;
     324      260538 :     latch->owner_pid = 0;
     325      260538 :     latch->is_shared = true;
     326      260538 : }
     327             : 
     328             : /*
     329             :  * Associate a shared latch with the current process, allowing it to
     330             :  * wait on the latch.
     331             :  *
     332             :  * Although there is a sanity check for latch-already-owned, we don't do
     333             :  * any sort of locking here, meaning that we could fail to detect the error
     334             :  * if two processes try to own the same latch at about the same time.  If
     335             :  * there is any risk of that, caller must provide an interlock to prevent it.
     336             :  *
     337             :  * In any process that calls OwnLatch(), make sure that
     338             :  * latch_sigusr1_handler() is called from the SIGUSR1 signal handler,
     339             :  * as shared latches use SIGUSR1 for inter-process communication.
     340             :  */
     341             : void
     342       14154 : OwnLatch(Latch *latch)
     343             : {
     344             :     /* Sanity checks */
     345             :     Assert(latch->is_shared);
     346             : 
     347             : #ifndef WIN32
     348             :     /* Assert InitializeLatchSupport has been called in this process */
     349             :     Assert(selfpipe_readfd >= 0 && selfpipe_owner_pid == MyProcPid);
     350             : #endif
     351             : 
     352       14154 :     if (latch->owner_pid != 0)
     353           0 :         elog(ERROR, "latch already owned");
     354             : 
     355       14154 :     latch->owner_pid = MyProcPid;
     356       14154 : }
     357             : 
     358             : /*
     359             :  * Disown a shared latch currently owned by the current process.
     360             :  */
     361             : void
     362       14154 : DisownLatch(Latch *latch)
     363             : {
     364             :     Assert(latch->is_shared);
     365             :     Assert(latch->owner_pid == MyProcPid);
     366             : 
     367       14154 :     latch->owner_pid = 0;
     368       14154 : }
     369             : 
     370             : /*
     371             :  * Wait for a given latch to be set, or for postmaster death, or until timeout
     372             :  * is exceeded. 'wakeEvents' is a bitmask that specifies which of those events
     373             :  * to wait for. If the latch is already set (and WL_LATCH_SET is given), the
     374             :  * function returns immediately.
     375             :  *
     376             :  * The "timeout" is given in milliseconds. It must be >= 0 if WL_TIMEOUT flag
     377             :  * is given.  Although it is declared as "long", we don't actually support
     378             :  * timeouts longer than INT_MAX milliseconds.  Note that some extra overhead
     379             :  * is incurred when WL_TIMEOUT is given, so avoid using a timeout if possible.
     380             :  *
     381             :  * The latch must be owned by the current process, ie. it must be a
     382             :  * process-local latch initialized with InitLatch, or a shared latch
     383             :  * associated with the current process by calling OwnLatch.
     384             :  *
     385             :  * Returns bit mask indicating which condition(s) caused the wake-up. Note
     386             :  * that if multiple wake-up conditions are true, there is no guarantee that
     387             :  * we return all of them in one call, but we will return at least one.
     388             :  */
     389             : int
     390      652410 : WaitLatch(Latch *latch, int wakeEvents, long timeout,
     391             :           uint32 wait_event_info)
     392             : {
     393             :     WaitEvent   event;
     394             : 
     395             :     /* Postmaster-managed callers must handle postmaster death somehow. */
     396             :     Assert(!IsUnderPostmaster ||
     397             :            (wakeEvents & WL_EXIT_ON_PM_DEATH) ||
     398             :            (wakeEvents & WL_POSTMASTER_DEATH));
     399             : 
     400             :     /*
     401             :      * Some callers may have a latch other than MyLatch, or no latch at all,
     402             :      * or want to handle postmaster death differently.  It's cheap to assign
     403             :      * those, so just do it every time.
     404             :      */
     405      652410 :     if (!(wakeEvents & WL_LATCH_SET))
     406           0 :         latch = NULL;
     407      652410 :     ModifyWaitEvent(LatchWaitSet, LatchWaitSetLatchPos, WL_LATCH_SET, latch);
     408     1304820 :     LatchWaitSet->exit_on_postmaster_death =
     409     1304820 :         ((wakeEvents & WL_EXIT_ON_PM_DEATH) != 0);
     410             : 
     411      652410 :     if (WaitEventSetWait(LatchWaitSet,
     412      652410 :                          (wakeEvents & WL_TIMEOUT) ? timeout : -1,
     413             :                          &event, 1,
     414             :                          wait_event_info) == 0)
     415       16758 :         return WL_TIMEOUT;
     416             :     else
     417      635622 :         return event.events;
     418             : }
     419             : 
     420             : /*
     421             :  * Like WaitLatch, but with an extra socket argument for WL_SOCKET_*
     422             :  * conditions.
     423             :  *
     424             :  * When waiting on a socket, EOF and error conditions always cause the socket
     425             :  * to be reported as readable/writable/connected, so that the caller can deal
     426             :  * with the condition.
     427             :  *
     428             :  * wakeEvents must include either WL_EXIT_ON_PM_DEATH for automatic exit
     429             :  * if the postmaster dies or WL_POSTMASTER_DEATH for a flag set in the
     430             :  * return value if the postmaster dies.  The latter is useful for rare cases
     431             :  * where some behavior other than immediate exit is needed.
     432             :  *
     433             :  * NB: These days this is just a wrapper around the WaitEventSet API. When
     434             :  * using a latch very frequently, consider creating a longer living
     435             :  * WaitEventSet instead; that's more efficient.
     436             :  */
     437             : int
     438       44044 : WaitLatchOrSocket(Latch *latch, int wakeEvents, pgsocket sock,
     439             :                   long timeout, uint32 wait_event_info)
     440             : {
     441       44044 :     int         ret = 0;
     442             :     int         rc;
     443             :     WaitEvent   event;
     444       44044 :     WaitEventSet *set = CreateWaitEventSet(CurrentMemoryContext, 3);
     445             : 
     446       44044 :     if (wakeEvents & WL_TIMEOUT)
     447             :         Assert(timeout >= 0);
     448             :     else
     449       13594 :         timeout = -1;
     450             : 
     451       44044 :     if (wakeEvents & WL_LATCH_SET)
     452       43756 :         AddWaitEventToSet(set, WL_LATCH_SET, PGINVALID_SOCKET,
     453             :                           latch, NULL);
     454             : 
     455             :     /* Postmaster-managed callers must handle postmaster death somehow. */
     456             :     Assert(!IsUnderPostmaster ||
     457             :            (wakeEvents & WL_EXIT_ON_PM_DEATH) ||
     458             :            (wakeEvents & WL_POSTMASTER_DEATH));
     459             : 
     460       44044 :     if ((wakeEvents & WL_POSTMASTER_DEATH) && IsUnderPostmaster)
     461           0 :         AddWaitEventToSet(set, WL_POSTMASTER_DEATH, PGINVALID_SOCKET,
     462             :                           NULL, NULL);
     463             : 
     464       44044 :     if ((wakeEvents & WL_EXIT_ON_PM_DEATH) && IsUnderPostmaster)
     465       44044 :         AddWaitEventToSet(set, WL_EXIT_ON_PM_DEATH, PGINVALID_SOCKET,
     466             :                           NULL, NULL);
     467             : 
     468       44044 :     if (wakeEvents & WL_SOCKET_MASK)
     469             :     {
     470             :         int         ev;
     471             : 
     472       44044 :         ev = wakeEvents & WL_SOCKET_MASK;
     473       44044 :         AddWaitEventToSet(set, ev, sock, NULL, NULL);
     474             :     }
     475             : 
     476       44044 :     rc = WaitEventSetWait(set, timeout, &event, 1, wait_event_info);
     477             : 
     478       44044 :     if (rc == 0)
     479         958 :         ret |= WL_TIMEOUT;
     480             :     else
     481             :     {
     482       43086 :         ret |= event.events & (WL_LATCH_SET |
     483             :                                WL_POSTMASTER_DEATH |
     484             :                                WL_SOCKET_MASK);
     485             :     }
     486             : 
     487       44044 :     FreeWaitEventSet(set);
     488             : 
     489       44044 :     return ret;
     490             : }
     491             : 
     492             : /*
     493             :  * Sets a latch and wakes up anyone waiting on it.
     494             :  *
     495             :  * This is cheap if the latch is already set, otherwise not so much.
     496             :  *
     497             :  * NB: when calling this in a signal handler, be sure to save and restore
     498             :  * errno around it.  (That's standard practice in most signal handlers, of
     499             :  * course, but we used to omit it in handlers that only set a flag.)
     500             :  *
     501             :  * NB: this function is called from critical sections and signal handlers so
     502             :  * throwing an error is not a good idea.
     503             :  */
     504             : void
     505     2142716 : SetLatch(Latch *latch)
     506             : {
     507             : #ifndef WIN32
     508             :     pid_t       owner_pid;
     509             : #else
     510             :     HANDLE      handle;
     511             : #endif
     512             : 
     513             :     /*
     514             :      * The memory barrier has to be placed here to ensure that any flag
     515             :      * variables possibly changed by this process have been flushed to main
     516             :      * memory, before we check/set is_set.
     517             :      */
     518     2142716 :     pg_memory_barrier();
     519             : 
     520             :     /* Quick exit if already set */
     521     2142710 :     if (latch->is_set)
     522     1467232 :         return;
     523             : 
     524      675478 :     latch->is_set = true;
     525             : 
     526             : #ifndef WIN32
     527             : 
     528             :     /*
     529             :      * See if anyone's waiting for the latch. It can be the current process if
     530             :      * we're in a signal handler. We use the self-pipe to wake up the
     531             :      * poll()/epoll_wait() in that case. If it's another process, send a
     532             :      * signal.
     533             :      *
     534             :      * Fetch owner_pid only once, in case the latch is concurrently getting
     535             :      * owned or disowned. XXX: This assumes that pid_t is atomic, which isn't
     536             :      * guaranteed to be true! In practice, the effective range of pid_t fits
     537             :      * in a 32 bit integer, and so should be atomic. In the worst case, we
     538             :      * might end up signaling the wrong process. Even then, you're very
     539             :      * unlucky if a process with that bogus pid exists and belongs to
     540             :      * Postgres; and PG database processes should handle excess SIGUSR1
     541             :      * interrupts without a problem anyhow.
     542             :      *
     543             :      * Another sort of race condition that's possible here is for a new
     544             :      * process to own the latch immediately after we look, so we don't signal
     545             :      * it. This is okay so long as all callers of ResetLatch/WaitLatch follow
     546             :      * the standard coding convention of waiting at the bottom of their loops,
     547             :      * not the top, so that they'll correctly process latch-setting events
     548             :      * that happen before they enter the loop.
     549             :      */
     550      675478 :     owner_pid = latch->owner_pid;
     551      675478 :     if (owner_pid == 0)
     552          66 :         return;
     553      675412 :     else if (owner_pid == MyProcPid)
     554             :     {
     555       52486 :         if (waiting)
     556       17384 :             sendSelfPipeByte();
     557             :     }
     558             :     else
     559      622926 :         kill(owner_pid, SIGUSR1);
     560             : #else
     561             : 
     562             :     /*
     563             :      * See if anyone's waiting for the latch. It can be the current process if
     564             :      * we're in a signal handler.
     565             :      *
     566             :      * Use a local variable here just in case somebody changes the event field
     567             :      * concurrently (which really should not happen).
     568             :      */
     569             :     handle = latch->event;
     570             :     if (handle)
     571             :     {
     572             :         SetEvent(handle);
     573             : 
     574             :         /*
     575             :          * Note that we silently ignore any errors. We might be in a signal
     576             :          * handler or other critical path where it's not safe to call elog().
     577             :          */
     578             :     }
     579             : #endif
     580             : 
     581             : }
     582             : 
     583             : /*
     584             :  * Clear the latch. Calling WaitLatch after this will sleep, unless
     585             :  * the latch is set again before the WaitLatch call.
     586             :  */
     587             : void
     588     1036642 : ResetLatch(Latch *latch)
     589             : {
     590             :     /* Only the owner should reset the latch */
     591             :     Assert(latch->owner_pid == MyProcPid);
     592             : 
     593     1036642 :     latch->is_set = false;
     594             : 
     595             :     /*
     596             :      * Ensure that the write to is_set gets flushed to main memory before we
     597             :      * examine any flag variables.  Otherwise a concurrent SetLatch might
     598             :      * falsely conclude that it needn't signal us, even though we have missed
     599             :      * seeing some flag updates that SetLatch was supposed to inform us of.
     600             :      */
     601     1036642 :     pg_memory_barrier();
     602     1036642 : }
     603             : 
     604             : /*
     605             :  * Create a WaitEventSet with space for nevents different events to wait for.
     606             :  *
     607             :  * These events can then be efficiently waited upon together, using
     608             :  * WaitEventSetWait().
     609             :  */
     610             : WaitEventSet *
     611       69176 : CreateWaitEventSet(MemoryContext context, int nevents)
     612             : {
     613             :     WaitEventSet *set;
     614             :     char       *data;
     615       69176 :     Size        sz = 0;
     616             : 
     617             :     /*
     618             :      * Use MAXALIGN size/alignment to guarantee that later uses of memory are
     619             :      * aligned correctly. E.g. epoll_event might need 8 byte alignment on some
     620             :      * platforms, but earlier allocations like WaitEventSet and WaitEvent
     621             :      * might not sized to guarantee that when purely using sizeof().
     622             :      */
     623       69176 :     sz += MAXALIGN(sizeof(WaitEventSet));
     624       69176 :     sz += MAXALIGN(sizeof(WaitEvent) * nevents);
     625             : 
     626             : #if defined(WAIT_USE_EPOLL)
     627       69176 :     sz += MAXALIGN(sizeof(struct epoll_event) * nevents);
     628             : #elif defined(WAIT_USE_KQUEUE)
     629             :     sz += MAXALIGN(sizeof(struct kevent) * nevents);
     630             : #elif defined(WAIT_USE_POLL)
     631             :     sz += MAXALIGN(sizeof(struct pollfd) * nevents);
     632             : #elif defined(WAIT_USE_WIN32)
     633             :     /* need space for the pgwin32_signal_event */
     634             :     sz += MAXALIGN(sizeof(HANDLE) * (nevents + 1));
     635             : #endif
     636             : 
     637       69176 :     data = (char *) MemoryContextAllocZero(context, sz);
     638             : 
     639       69176 :     set = (WaitEventSet *) data;
     640       69176 :     data += MAXALIGN(sizeof(WaitEventSet));
     641             : 
     642       69176 :     set->events = (WaitEvent *) data;
     643       69176 :     data += MAXALIGN(sizeof(WaitEvent) * nevents);
     644             : 
     645             : #if defined(WAIT_USE_EPOLL)
     646       69176 :     set->epoll_ret_events = (struct epoll_event *) data;
     647       69176 :     data += MAXALIGN(sizeof(struct epoll_event) * nevents);
     648             : #elif defined(WAIT_USE_KQUEUE)
     649             :     set->kqueue_ret_events = (struct kevent *) data;
     650             :     data += MAXALIGN(sizeof(struct kevent) * nevents);
     651             : #elif defined(WAIT_USE_POLL)
     652             :     set->pollfds = (struct pollfd *) data;
     653             :     data += MAXALIGN(sizeof(struct pollfd) * nevents);
     654             : #elif defined(WAIT_USE_WIN32)
     655             :     set->handles = (HANDLE) data;
     656             :     data += MAXALIGN(sizeof(HANDLE) * nevents);
     657             : #endif
     658             : 
     659       69176 :     set->latch = NULL;
     660       69176 :     set->nevents_space = nevents;
     661       69176 :     set->exit_on_postmaster_death = false;
     662             : 
     663             : #if defined(WAIT_USE_EPOLL)
     664       69176 :     if (!AcquireExternalFD())
     665             :     {
     666             :         /* treat this as though epoll_create1 itself returned EMFILE */
     667           0 :         elog(ERROR, "epoll_create1 failed: %m");
     668             :     }
     669             : #ifdef EPOLL_CLOEXEC
     670       69176 :     set->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
     671       69176 :     if (set->epoll_fd < 0)
     672             :     {
     673           0 :         ReleaseExternalFD();
     674           0 :         elog(ERROR, "epoll_create1 failed: %m");
     675             :     }
     676             : #else
     677             :     /* cope with ancient glibc lacking epoll_create1 (e.g., RHEL5) */
     678             :     set->epoll_fd = epoll_create(nevents);
     679             :     if (set->epoll_fd < 0)
     680             :     {
     681             :         ReleaseExternalFD();
     682             :         elog(ERROR, "epoll_create failed: %m");
     683             :     }
     684             :     if (fcntl(set->epoll_fd, F_SETFD, FD_CLOEXEC) == -1)
     685             :     {
     686             :         int         save_errno = errno;
     687             : 
     688             :         close(set->epoll_fd);
     689             :         ReleaseExternalFD();
     690             :         errno = save_errno;
     691             :         elog(ERROR, "fcntl(F_SETFD) failed on epoll descriptor: %m");
     692             :     }
     693             : #endif                          /* EPOLL_CLOEXEC */
     694             : #elif defined(WAIT_USE_KQUEUE)
     695             :     if (!AcquireExternalFD())
     696             :     {
     697             :         /* treat this as though kqueue itself returned EMFILE */
     698             :         elog(ERROR, "kqueue failed: %m");
     699             :     }
     700             :     set->kqueue_fd = kqueue();
     701             :     if (set->kqueue_fd < 0)
     702             :     {
     703             :         ReleaseExternalFD();
     704             :         elog(ERROR, "kqueue failed: %m");
     705             :     }
     706             :     if (fcntl(set->kqueue_fd, F_SETFD, FD_CLOEXEC) == -1)
     707             :     {
     708             :         int         save_errno = errno;
     709             : 
     710             :         close(set->kqueue_fd);
     711             :         ReleaseExternalFD();
     712             :         errno = save_errno;
     713             :         elog(ERROR, "fcntl(F_SETFD) failed on kqueue descriptor: %m");
     714             :     }
     715             :     set->report_postmaster_not_running = false;
     716             : #elif defined(WAIT_USE_WIN32)
     717             : 
     718             :     /*
     719             :      * To handle signals while waiting, we need to add a win32 specific event.
     720             :      * We accounted for the additional event at the top of this routine. See
     721             :      * port/win32/signal.c for more details.
     722             :      *
     723             :      * Note: pgwin32_signal_event should be first to ensure that it will be
     724             :      * reported when multiple events are set.  We want to guarantee that
     725             :      * pending signals are serviced.
     726             :      */
     727             :     set->handles[0] = pgwin32_signal_event;
     728             :     StaticAssertStmt(WSA_INVALID_EVENT == NULL, "");
     729             : #endif
     730             : 
     731       69176 :     return set;
     732             : }
     733             : 
     734             : /*
     735             :  * Free a previously created WaitEventSet.
     736             :  *
     737             :  * Note: preferably, this shouldn't have to free any resources that could be
     738             :  * inherited across an exec().  If it did, we'd likely leak those resources in
     739             :  * many scenarios.  For the epoll case, we ensure that by setting FD_CLOEXEC
     740             :  * when the FD is created.  For the Windows case, we assume that the handles
     741             :  * involved are non-inheritable.
     742             :  */
     743             : void
     744       44812 : FreeWaitEventSet(WaitEventSet *set)
     745             : {
     746             : #if defined(WAIT_USE_EPOLL)
     747       44812 :     close(set->epoll_fd);
     748       44812 :     ReleaseExternalFD();
     749             : #elif defined(WAIT_USE_KQUEUE)
     750             :     close(set->kqueue_fd);
     751             :     ReleaseExternalFD();
     752             : #elif defined(WAIT_USE_WIN32)
     753             :     WaitEvent  *cur_event;
     754             : 
     755             :     for (cur_event = set->events;
     756             :          cur_event < (set->events + set->nevents);
     757             :          cur_event++)
     758             :     {
     759             :         if (cur_event->events & WL_LATCH_SET)
     760             :         {
     761             :             /* uses the latch's HANDLE */
     762             :         }
     763             :         else if (cur_event->events & WL_POSTMASTER_DEATH)
     764             :         {
     765             :             /* uses PostmasterHandle */
     766             :         }
     767             :         else
     768             :         {
     769             :             /* Clean up the event object we created for the socket */
     770             :             WSAEventSelect(cur_event->fd, NULL, 0);
     771             :             WSACloseEvent(set->handles[cur_event->pos + 1]);
     772             :         }
     773             :     }
     774             : #endif
     775             : 
     776       44812 :     pfree(set);
     777       44812 : }
     778             : 
     779             : /* ---
     780             :  * Add an event to the set. Possible events are:
     781             :  * - WL_LATCH_SET: Wait for the latch to be set
     782             :  * - WL_POSTMASTER_DEATH: Wait for postmaster to die
     783             :  * - WL_SOCKET_READABLE: Wait for socket to become readable,
     784             :  *   can be combined in one event with other WL_SOCKET_* events
     785             :  * - WL_SOCKET_WRITEABLE: Wait for socket to become writeable,
     786             :  *   can be combined with other WL_SOCKET_* events
     787             :  * - WL_SOCKET_CONNECTED: Wait for socket connection to be established,
     788             :  *   can be combined with other WL_SOCKET_* events (on non-Windows
     789             :  *   platforms, this is the same as WL_SOCKET_WRITEABLE)
     790             :  * - WL_EXIT_ON_PM_DEATH: Exit immediately if the postmaster dies
     791             :  *
     792             :  * Returns the offset in WaitEventSet->events (starting from 0), which can be
     793             :  * used to modify previously added wait events using ModifyWaitEvent().
     794             :  *
     795             :  * In the WL_LATCH_SET case the latch must be owned by the current process,
     796             :  * i.e. it must be a process-local latch initialized with InitLatch, or a
     797             :  * shared latch associated with the current process by calling OwnLatch.
     798             :  *
     799             :  * In the WL_SOCKET_READABLE/WRITEABLE/CONNECTED cases, EOF and error
     800             :  * conditions cause the socket to be reported as readable/writable/connected,
     801             :  * so that the caller can deal with the condition.
     802             :  *
     803             :  * The user_data pointer specified here will be set for the events returned
     804             :  * by WaitEventSetWait(), allowing to easily associate additional data with
     805             :  * events.
     806             :  */
     807             : int
     808      189828 : AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch,
     809             :                   void *user_data)
     810             : {
     811             :     WaitEvent  *event;
     812             : 
     813             :     /* not enough space */
     814             :     Assert(set->nevents < set->nevents_space);
     815             : 
     816      189828 :     if (events == WL_EXIT_ON_PM_DEATH)
     817             :     {
     818       58242 :         events = WL_POSTMASTER_DEATH;
     819       58242 :         set->exit_on_postmaster_death = true;
     820             :     }
     821             : 
     822      189828 :     if (latch)
     823             :     {
     824       68888 :         if (latch->owner_pid != MyProcPid)
     825           0 :             elog(ERROR, "cannot wait on a latch owned by another process");
     826       68888 :         if (set->latch)
     827           0 :             elog(ERROR, "cannot wait on more than one latch");
     828       68888 :         if ((events & WL_LATCH_SET) != WL_LATCH_SET)
     829           0 :             elog(ERROR, "latch events only support being set");
     830             :     }
     831             :     else
     832             :     {
     833      120940 :         if (events & WL_LATCH_SET)
     834           0 :             elog(ERROR, "cannot wait on latch without a specified latch");
     835             :     }
     836             : 
     837             :     /* waiting for socket readiness without a socket indicates a bug */
     838      189828 :     if (fd == PGINVALID_SOCKET && (events & WL_SOCKET_MASK))
     839           0 :         elog(ERROR, "cannot wait on socket event without a socket");
     840             : 
     841      189828 :     event = &set->events[set->nevents];
     842      189828 :     event->pos = set->nevents++;
     843      189828 :     event->fd = fd;
     844      189828 :     event->events = events;
     845      189828 :     event->user_data = user_data;
     846             : #ifdef WIN32
     847             :     event->reset = false;
     848             : #endif
     849             : 
     850      189828 :     if (events == WL_LATCH_SET)
     851             :     {
     852       68888 :         set->latch = latch;
     853       68888 :         set->latch_pos = event->pos;
     854             : #ifndef WIN32
     855       68888 :         event->fd = selfpipe_readfd;
     856             : #endif
     857             :     }
     858      120940 :     else if (events == WL_POSTMASTER_DEATH)
     859             :     {
     860             : #ifndef WIN32
     861       67568 :         event->fd = postmaster_alive_fds[POSTMASTER_FD_WATCH];
     862             : #endif
     863             :     }
     864             : 
     865             :     /* perform wait primitive specific initialization, if needed */
     866             : #if defined(WAIT_USE_EPOLL)
     867      189828 :     WaitEventAdjustEpoll(set, event, EPOLL_CTL_ADD);
     868             : #elif defined(WAIT_USE_KQUEUE)
     869             :     WaitEventAdjustKqueue(set, event, 0);
     870             : #elif defined(WAIT_USE_POLL)
     871             :     WaitEventAdjustPoll(set, event);
     872             : #elif defined(WAIT_USE_WIN32)
     873             :     WaitEventAdjustWin32(set, event);
     874             : #endif
     875             : 
     876      189828 :     return event->pos;
     877             : }
     878             : 
     879             : /*
     880             :  * Change the event mask and, in the WL_LATCH_SET case, the latch associated
     881             :  * with the WaitEvent.  The latch may be changed to NULL to disable the latch
     882             :  * temporarily, and then set back to a latch later.
     883             :  *
     884             :  * 'pos' is the id returned by AddWaitEventToSet.
     885             :  */
     886             : void
     887      813104 : ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
     888             : {
     889             :     WaitEvent  *event;
     890             : #if defined(WAIT_USE_KQUEUE)
     891             :     int         old_events;
     892             : #endif
     893             : 
     894             :     Assert(pos < set->nevents);
     895             : 
     896      813104 :     event = &set->events[pos];
     897             : #if defined(WAIT_USE_KQUEUE)
     898             :     old_events = event->events;
     899             : #endif
     900             : 
     901             :     /*
     902             :      * If neither the event mask nor the associated latch changes, return
     903             :      * early. That's an important optimization for some sockets, where
     904             :      * ModifyWaitEvent is frequently used to switch from waiting for reads to
     905             :      * waiting on writes.
     906             :      */
     907      813104 :     if (events == event->events &&
     908      806034 :         (!(event->events & WL_LATCH_SET) || set->latch == latch))
     909      785738 :         return;
     910             : 
     911       27366 :     if (event->events & WL_LATCH_SET &&
     912       20296 :         events != event->events)
     913             :     {
     914           0 :         elog(ERROR, "cannot modify latch event");
     915             :     }
     916             : 
     917       27366 :     if (event->events & WL_POSTMASTER_DEATH)
     918             :     {
     919           0 :         elog(ERROR, "cannot modify postmaster death event");
     920             :     }
     921             : 
     922             :     /* FIXME: validate event mask */
     923       27366 :     event->events = events;
     924             : 
     925       27366 :     if (events == WL_LATCH_SET)
     926             :     {
     927       20296 :         if (latch && latch->owner_pid != MyProcPid)
     928           0 :             elog(ERROR, "cannot wait on a latch owned by another process");
     929       20296 :         set->latch = latch;
     930             :         /*
     931             :          * On Unix, we don't need to modify the kernel object because the
     932             :          * underlying pipe is the same for all latches so we can return
     933             :          * immediately.  On Windows, we need to update our array of handles,
     934             :          * but we leave the old one in place and tolerate spurious wakeups if
     935             :          * the latch is disabled.
     936             :          */
     937             : #if defined(WAIT_USE_WIN32)
     938             :         if (!latch)
     939             :             return;
     940             : #else
     941       20296 :         return;
     942             : #endif
     943             :     }
     944             : 
     945             : #if defined(WAIT_USE_EPOLL)
     946        7070 :     WaitEventAdjustEpoll(set, event, EPOLL_CTL_MOD);
     947             : #elif defined(WAIT_USE_KQUEUE)
     948             :     WaitEventAdjustKqueue(set, event, old_events);
     949             : #elif defined(WAIT_USE_POLL)
     950             :     WaitEventAdjustPoll(set, event);
     951             : #elif defined(WAIT_USE_WIN32)
     952             :     WaitEventAdjustWin32(set, event);
     953             : #endif
     954             : }
     955             : 
     956             : #if defined(WAIT_USE_EPOLL)
     957             : /*
     958             :  * action can be one of EPOLL_CTL_ADD | EPOLL_CTL_MOD | EPOLL_CTL_DEL
     959             :  */
     960             : static void
     961      196898 : WaitEventAdjustEpoll(WaitEventSet *set, WaitEvent *event, int action)
     962             : {
     963             :     struct epoll_event epoll_ev;
     964             :     int         rc;
     965             : 
     966             :     /* pointer to our event, returned by epoll_wait */
     967      196898 :     epoll_ev.data.ptr = event;
     968             :     /* always wait for errors */
     969      196898 :     epoll_ev.events = EPOLLERR | EPOLLHUP;
     970             : 
     971             :     /* prepare pollfd entry once */
     972      196898 :     if (event->events == WL_LATCH_SET)
     973             :     {
     974             :         Assert(set->latch != NULL);
     975       68888 :         epoll_ev.events |= EPOLLIN;
     976             :     }
     977      128010 :     else if (event->events == WL_POSTMASTER_DEATH)
     978             :     {
     979       67568 :         epoll_ev.events |= EPOLLIN;
     980             :     }
     981             :     else
     982             :     {
     983             :         Assert(event->fd != PGINVALID_SOCKET);
     984             :         Assert(event->events & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE));
     985             : 
     986       60442 :         if (event->events & WL_SOCKET_READABLE)
     987       51036 :             epoll_ev.events |= EPOLLIN;
     988       60442 :         if (event->events & WL_SOCKET_WRITEABLE)
     989       14080 :             epoll_ev.events |= EPOLLOUT;
     990             :     }
     991             : 
     992             :     /*
     993             :      * Even though unused, we also pass epoll_ev as the data argument if
     994             :      * EPOLL_CTL_DEL is passed as action.  There used to be an epoll bug
     995             :      * requiring that, and actually it makes the code simpler...
     996             :      */
     997      196898 :     rc = epoll_ctl(set->epoll_fd, action, event->fd, &epoll_ev);
     998             : 
     999      196898 :     if (rc < 0)
    1000           0 :         ereport(ERROR,
    1001             :                 (errcode_for_socket_access(),
    1002             :         /* translator: %s is a syscall name, such as "poll()" */
    1003             :                  errmsg("%s failed: %m",
    1004             :                         "epoll_ctl()")));
    1005      196898 : }
    1006             : #endif
    1007             : 
    1008             : #if defined(WAIT_USE_POLL)
    1009             : static void
    1010             : WaitEventAdjustPoll(WaitEventSet *set, WaitEvent *event)
    1011             : {
    1012             :     struct pollfd *pollfd = &set->pollfds[event->pos];
    1013             : 
    1014             :     pollfd->revents = 0;
    1015             :     pollfd->fd = event->fd;
    1016             : 
    1017             :     /* prepare pollfd entry once */
    1018             :     if (event->events == WL_LATCH_SET)
    1019             :     {
    1020             :         Assert(set->latch != NULL);
    1021             :         pollfd->events = POLLIN;
    1022             :     }
    1023             :     else if (event->events == WL_POSTMASTER_DEATH)
    1024             :     {
    1025             :         pollfd->events = POLLIN;
    1026             :     }
    1027             :     else
    1028             :     {
    1029             :         Assert(event->events & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE));
    1030             :         pollfd->events = 0;
    1031             :         if (event->events & WL_SOCKET_READABLE)
    1032             :             pollfd->events |= POLLIN;
    1033             :         if (event->events & WL_SOCKET_WRITEABLE)
    1034             :             pollfd->events |= POLLOUT;
    1035             :     }
    1036             : 
    1037             :     Assert(event->fd != PGINVALID_SOCKET);
    1038             : }
    1039             : #endif
    1040             : 
    1041             : #if defined(WAIT_USE_KQUEUE)
    1042             : 
    1043             : /*
    1044             :  * On most BSD family systems, the udata member of struct kevent is of type
    1045             :  * void *, so we could directly convert to/from WaitEvent *.  Unfortunately,
    1046             :  * NetBSD has it as intptr_t, so here we wallpaper over that difference with
    1047             :  * an lvalue cast.
    1048             :  */
    1049             : #define AccessWaitEvent(k_ev) (*((WaitEvent **)(&(k_ev)->udata)))
    1050             : 
    1051             : static inline void
    1052             : WaitEventAdjustKqueueAdd(struct kevent *k_ev, int filter, int action,
    1053             :                          WaitEvent *event)
    1054             : {
    1055             :     k_ev->ident = event->fd;
    1056             :     k_ev->filter = filter;
    1057             :     k_ev->flags = action;
    1058             :     k_ev->fflags = 0;
    1059             :     k_ev->data = 0;
    1060             :     AccessWaitEvent(k_ev) = event;
    1061             : }
    1062             : 
    1063             : static inline void
    1064             : WaitEventAdjustKqueueAddPostmaster(struct kevent *k_ev, WaitEvent *event)
    1065             : {
    1066             :     /* For now postmaster death can only be added, not removed. */
    1067             :     k_ev->ident = PostmasterPid;
    1068             :     k_ev->filter = EVFILT_PROC;
    1069             :     k_ev->flags = EV_ADD;
    1070             :     k_ev->fflags = NOTE_EXIT;
    1071             :     k_ev->data = 0;
    1072             :     AccessWaitEvent(k_ev) = event;
    1073             : }
    1074             : 
    1075             : /*
    1076             :  * old_events is the previous event mask, used to compute what has changed.
    1077             :  */
    1078             : static void
    1079             : WaitEventAdjustKqueue(WaitEventSet *set, WaitEvent *event, int old_events)
    1080             : {
    1081             :     int         rc;
    1082             :     struct kevent k_ev[2];
    1083             :     int         count = 0;
    1084             :     bool        new_filt_read = false;
    1085             :     bool        old_filt_read = false;
    1086             :     bool        new_filt_write = false;
    1087             :     bool        old_filt_write = false;
    1088             : 
    1089             :     if (old_events == event->events)
    1090             :         return;
    1091             : 
    1092             :     Assert(event->events != WL_LATCH_SET || set->latch != NULL);
    1093             :     Assert(event->events == WL_LATCH_SET ||
    1094             :            event->events == WL_POSTMASTER_DEATH ||
    1095             :            (event->events & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE)));
    1096             : 
    1097             :     if (event->events == WL_POSTMASTER_DEATH)
    1098             :     {
    1099             :         /*
    1100             :          * Unlike all the other implementations, we detect postmaster death
    1101             :          * using process notification instead of waiting on the postmaster
    1102             :          * alive pipe.
    1103             :          */
    1104             :         WaitEventAdjustKqueueAddPostmaster(&k_ev[count++], event);
    1105             :     }
    1106             :     else
    1107             :     {
    1108             :         /*
    1109             :          * We need to compute the adds and deletes required to get from the
    1110             :          * old event mask to the new event mask, since kevent treats readable
    1111             :          * and writable as separate events.
    1112             :          */
    1113             :         if (old_events == WL_LATCH_SET ||
    1114             :             (old_events & WL_SOCKET_READABLE))
    1115             :             old_filt_read = true;
    1116             :         if (event->events == WL_LATCH_SET ||
    1117             :             (event->events & WL_SOCKET_READABLE))
    1118             :             new_filt_read = true;
    1119             :         if (old_events & WL_SOCKET_WRITEABLE)
    1120             :             old_filt_write = true;
    1121             :         if (event->events & WL_SOCKET_WRITEABLE)
    1122             :             new_filt_write = true;
    1123             :         if (old_filt_read && !new_filt_read)
    1124             :             WaitEventAdjustKqueueAdd(&k_ev[count++], EVFILT_READ, EV_DELETE,
    1125             :                                      event);
    1126             :         else if (!old_filt_read && new_filt_read)
    1127             :             WaitEventAdjustKqueueAdd(&k_ev[count++], EVFILT_READ, EV_ADD,
    1128             :                                      event);
    1129             :         if (old_filt_write && !new_filt_write)
    1130             :             WaitEventAdjustKqueueAdd(&k_ev[count++], EVFILT_WRITE, EV_DELETE,
    1131             :                                      event);
    1132             :         else if (!old_filt_write && new_filt_write)
    1133             :             WaitEventAdjustKqueueAdd(&k_ev[count++], EVFILT_WRITE, EV_ADD,
    1134             :                                      event);
    1135             :     }
    1136             : 
    1137             :     Assert(count > 0);
    1138             :     Assert(count <= 2);
    1139             : 
    1140             :     rc = kevent(set->kqueue_fd, &k_ev[0], count, NULL, 0, NULL);
    1141             : 
    1142             :     /*
    1143             :      * When adding the postmaster's pid, we have to consider that it might
    1144             :      * already have exited and perhaps even been replaced by another process
    1145             :      * with the same pid.  If so, we have to defer reporting this as an event
    1146             :      * until the next call to WaitEventSetWaitBlock().
    1147             :      */
    1148             : 
    1149             :     if (rc < 0)
    1150             :     {
    1151             :         if (event->events == WL_POSTMASTER_DEATH &&
    1152             :             (errno == ESRCH || errno == EACCES))
    1153             :             set->report_postmaster_not_running = true;
    1154             :         else
    1155             :             ereport(ERROR,
    1156             :                     (errcode_for_socket_access(),
    1157             :             /* translator: %s is a syscall name, such as "poll()" */
    1158             :                      errmsg("%s failed: %m",
    1159             :                             "kevent()")));
    1160             :     }
    1161             :     else if (event->events == WL_POSTMASTER_DEATH &&
    1162             :              PostmasterPid != getppid() &&
    1163             :              !PostmasterIsAlive())
    1164             :     {
    1165             :         /*
    1166             :          * The extra PostmasterIsAliveInternal() check prevents false alarms
    1167             :          * on systems that give a different value for getppid() while being
    1168             :          * traced by a debugger.
    1169             :          */
    1170             :         set->report_postmaster_not_running = true;
    1171             :     }
    1172             : }
    1173             : 
    1174             : #endif
    1175             : 
    1176             : #if defined(WAIT_USE_WIN32)
    1177             : static void
    1178             : WaitEventAdjustWin32(WaitEventSet *set, WaitEvent *event)
    1179             : {
    1180             :     HANDLE     *handle = &set->handles[event->pos + 1];
    1181             : 
    1182             :     if (event->events == WL_LATCH_SET)
    1183             :     {
    1184             :         Assert(set->latch != NULL);
    1185             :         *handle = set->latch->event;
    1186             :     }
    1187             :     else if (event->events == WL_POSTMASTER_DEATH)
    1188             :     {
    1189             :         *handle = PostmasterHandle;
    1190             :     }
    1191             :     else
    1192             :     {
    1193             :         int         flags = FD_CLOSE;   /* always check for errors/EOF */
    1194             : 
    1195             :         if (event->events & WL_SOCKET_READABLE)
    1196             :             flags |= FD_READ;
    1197             :         if (event->events & WL_SOCKET_WRITEABLE)
    1198             :             flags |= FD_WRITE;
    1199             :         if (event->events & WL_SOCKET_CONNECTED)
    1200             :             flags |= FD_CONNECT;
    1201             : 
    1202             :         if (*handle == WSA_INVALID_EVENT)
    1203             :         {
    1204             :             *handle = WSACreateEvent();
    1205             :             if (*handle == WSA_INVALID_EVENT)
    1206             :                 elog(ERROR, "failed to create event for socket: error code %u",
    1207             :                      WSAGetLastError());
    1208             :         }
    1209             :         if (WSAEventSelect(event->fd, *handle, flags) != 0)
    1210             :             elog(ERROR, "failed to set up event for socket: error code %u",
    1211             :                  WSAGetLastError());
    1212             : 
    1213             :         Assert(event->fd != PGINVALID_SOCKET);
    1214             :     }
    1215             : }
    1216             : #endif
    1217             : 
    1218             : /*
    1219             :  * Wait for events added to the set to happen, or until the timeout is
    1220             :  * reached.  At most nevents occurred events are returned.
    1221             :  *
    1222             :  * If timeout = -1, block until an event occurs; if 0, check sockets for
    1223             :  * readiness, but don't block; if > 0, block for at most timeout milliseconds.
    1224             :  *
    1225             :  * Returns the number of events occurred, or 0 if the timeout was reached.
    1226             :  *
    1227             :  * Returned events will have the fd, pos, user_data fields set to the
    1228             :  * values associated with the registered event.
    1229             :  */
    1230             : int
    1231      916050 : WaitEventSetWait(WaitEventSet *set, long timeout,
    1232             :                  WaitEvent *occurred_events, int nevents,
    1233             :                  uint32 wait_event_info)
    1234             : {
    1235      916050 :     int         returned_events = 0;
    1236             :     instr_time  start_time;
    1237             :     instr_time  cur_time;
    1238      916050 :     long        cur_timeout = -1;
    1239             : 
    1240             :     Assert(nevents > 0);
    1241             : 
    1242             :     /*
    1243             :      * Initialize timeout if requested.  We must record the current time so
    1244             :      * that we can determine the remaining timeout if interrupted.
    1245             :      */
    1246      916050 :     if (timeout >= 0)
    1247             :     {
    1248       63740 :         INSTR_TIME_SET_CURRENT(start_time);
    1249             :         Assert(timeout >= 0 && timeout <= INT_MAX);
    1250       63740 :         cur_timeout = timeout;
    1251             :     }
    1252             : 
    1253      916050 :     pgstat_report_wait_start(wait_event_info);
    1254             : 
    1255             : #ifndef WIN32
    1256      916050 :     waiting = true;
    1257             : #else
    1258             :     /* Ensure that signals are serviced even if latch is already set */
    1259             :     pgwin32_dispatch_queued_signals();
    1260             : #endif
    1261     2312144 :     while (returned_events == 0)
    1262             :     {
    1263             :         int         rc;
    1264             : 
    1265             :         /*
    1266             :          * Check if the latch is set already. If so, leave the loop
    1267             :          * immediately, avoid blocking again. We don't attempt to report any
    1268             :          * other events that might also be satisfied.
    1269             :          *
    1270             :          * If someone sets the latch between this and the
    1271             :          * WaitEventSetWaitBlock() below, the setter will write a byte to the
    1272             :          * pipe (or signal us and the signal handler will do that), and the
    1273             :          * readiness routine will return immediately.
    1274             :          *
    1275             :          * On unix, If there's a pending byte in the self pipe, we'll notice
    1276             :          * whenever blocking. Only clearing the pipe in that case avoids
    1277             :          * having to drain it every time WaitLatchOrSocket() is used. Should
    1278             :          * the pipe-buffer fill up we're still ok, because the pipe is in
    1279             :          * nonblocking mode. It's unlikely for that to happen, because the
    1280             :          * self pipe isn't filled unless we're blocking (waiting = true), or
    1281             :          * from inside a signal handler in latch_sigusr1_handler().
    1282             :          *
    1283             :          * On windows, we'll also notice if there's a pending event for the
    1284             :          * latch when blocking, but there's no danger of anything filling up,
    1285             :          * as "Setting an event that is already set has no effect.".
    1286             :          *
    1287             :          * Note: we assume that the kernel calls involved in latch management
    1288             :          * will provide adequate synchronization on machines with weak memory
    1289             :          * ordering, so that we cannot miss seeing is_set if a notification
    1290             :          * has already been queued.
    1291             :          */
    1292     2049114 :         if (set->latch && set->latch->is_set)
    1293             :         {
    1294      635274 :             occurred_events->fd = PGINVALID_SOCKET;
    1295      635274 :             occurred_events->pos = set->latch_pos;
    1296      635274 :             occurred_events->user_data =
    1297      635274 :                 set->events[set->latch_pos].user_data;
    1298      635274 :             occurred_events->events = WL_LATCH_SET;
    1299      635274 :             occurred_events++;
    1300      635274 :             returned_events++;
    1301             : 
    1302      635274 :             break;
    1303             :         }
    1304             : 
    1305             :         /*
    1306             :          * Wait for events using the readiness primitive chosen at the top of
    1307             :          * this file. If -1 is returned, a timeout has occurred, if 0 we have
    1308             :          * to retry, everything >= 1 is the number of returned events.
    1309             :          */
    1310     1413840 :         rc = WaitEventSetWaitBlock(set, cur_timeout,
    1311             :                                    occurred_events, nevents);
    1312             : 
    1313     1413810 :         if (rc == -1)
    1314       17714 :             break;              /* timeout occurred */
    1315             :         else
    1316     1396096 :             returned_events = rc;
    1317             : 
    1318             :         /* If we're not done, update cur_timeout for next iteration */
    1319     1396096 :         if (returned_events == 0 && timeout >= 0)
    1320             :         {
    1321       32090 :             INSTR_TIME_SET_CURRENT(cur_time);
    1322       33670 :             INSTR_TIME_SUBTRACT(cur_time, start_time);
    1323       32090 :             cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
    1324       32090 :             if (cur_timeout <= 0)
    1325           2 :                 break;
    1326             :         }
    1327             :     }
    1328             : #ifndef WIN32
    1329      916020 :     waiting = false;
    1330             : #endif
    1331             : 
    1332      916020 :     pgstat_report_wait_end();
    1333             : 
    1334      916020 :     return returned_events;
    1335             : }
    1336             : 
    1337             : 
    1338             : #if defined(WAIT_USE_EPOLL)
    1339             : 
    1340             : /*
    1341             :  * Wait using linux's epoll_wait(2).
    1342             :  *
    1343             :  * This is the preferable wait method, as several readiness notifications are
    1344             :  * delivered, without having to iterate through all of set->events. The return
    1345             :  * epoll_event struct contain a pointer to our events, making association
    1346             :  * easy.
    1347             :  */
    1348             : static inline int
    1349     1413840 : WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
    1350             :                       WaitEvent *occurred_events, int nevents)
    1351             : {
    1352     1413840 :     int         returned_events = 0;
    1353             :     int         rc;
    1354             :     WaitEvent  *cur_event;
    1355             :     struct epoll_event *cur_epoll_event;
    1356             : 
    1357             :     /* Sleep */
    1358     1413840 :     rc = epoll_wait(set->epoll_fd, set->epoll_ret_events,
    1359             :                     nevents, cur_timeout);
    1360             : 
    1361             :     /* Check return code */
    1362     1413840 :     if (rc < 0)
    1363             :     {
    1364             :         /* EINTR is okay, otherwise complain */
    1365      573514 :         if (errno != EINTR)
    1366             :         {
    1367           0 :             waiting = false;
    1368           0 :             ereport(ERROR,
    1369             :                     (errcode_for_socket_access(),
    1370             :             /* translator: %s is a syscall name, such as "poll()" */
    1371             :                      errmsg("%s failed: %m",
    1372             :                             "epoll_wait()")));
    1373             :         }
    1374      573514 :         return 0;
    1375             :     }
    1376      840326 :     else if (rc == 0)
    1377             :     {
    1378             :         /* timeout exceeded */
    1379       17714 :         return -1;
    1380             :     }
    1381             : 
    1382             :     /*
    1383             :      * At least one event occurred, iterate over the returned epoll events
    1384             :      * until they're either all processed, or we've returned all the events
    1385             :      * the caller desired.
    1386             :      */
    1387      822612 :     for (cur_epoll_event = set->epoll_ret_events;
    1388     1645194 :          cur_epoll_event < (set->epoll_ret_events + rc) &&
    1389             :          returned_events < nevents;
    1390      822582 :          cur_epoll_event++)
    1391             :     {
    1392             :         /* epoll's data pointer is set to the associated WaitEvent */
    1393      822612 :         cur_event = (WaitEvent *) cur_epoll_event->data.ptr;
    1394             : 
    1395      822612 :         occurred_events->pos = cur_event->pos;
    1396      822612 :         occurred_events->user_data = cur_event->user_data;
    1397      822612 :         occurred_events->events = 0;
    1398             : 
    1399      822612 :         if (cur_event->events == WL_LATCH_SET &&
    1400      572234 :             cur_epoll_event->events & (EPOLLIN | EPOLLERR | EPOLLHUP))
    1401             :         {
    1402             :             /* There's data in the self-pipe, clear it. */
    1403      572234 :             drainSelfPipe();
    1404             : 
    1405      584916 :             if (set->latch && set->latch->is_set)
    1406             :             {
    1407       12682 :                 occurred_events->fd = PGINVALID_SOCKET;
    1408       12682 :                 occurred_events->events = WL_LATCH_SET;
    1409       12682 :                 occurred_events++;
    1410       12682 :                 returned_events++;
    1411             :             }
    1412             :         }
    1413      250378 :         else if (cur_event->events == WL_POSTMASTER_DEATH &&
    1414          36 :                  cur_epoll_event->events & (EPOLLIN | EPOLLERR | EPOLLHUP))
    1415             :         {
    1416             :             /*
    1417             :              * We expect an EPOLLHUP when the remote end is closed, but
    1418             :              * because we don't expect the pipe to become readable or to have
    1419             :              * any errors either, treat those cases as postmaster death, too.
    1420             :              *
    1421             :              * Be paranoid about a spurious event signaling the postmaster as
    1422             :              * being dead.  There have been reports about that happening with
    1423             :              * older primitives (select(2) to be specific), and a spurious
    1424             :              * WL_POSTMASTER_DEATH event would be painful. Re-checking doesn't
    1425             :              * cost much.
    1426             :              */
    1427          42 :             if (!PostmasterIsAliveInternal())
    1428             :             {
    1429          36 :                 if (set->exit_on_postmaster_death)
    1430          30 :                     proc_exit(1);
    1431           6 :                 occurred_events->fd = PGINVALID_SOCKET;
    1432           6 :                 occurred_events->events = WL_POSTMASTER_DEATH;
    1433           6 :                 occurred_events++;
    1434           6 :                 returned_events++;
    1435             :             }
    1436             :         }
    1437      250342 :         else if (cur_event->events & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE))
    1438             :         {
    1439             :             Assert(cur_event->fd != PGINVALID_SOCKET);
    1440             : 
    1441      250342 :             if ((cur_event->events & WL_SOCKET_READABLE) &&
    1442      237570 :                 (cur_epoll_event->events & (EPOLLIN | EPOLLERR | EPOLLHUP)))
    1443             :             {
    1444             :                 /* data available in socket, or EOF */
    1445      235290 :                 occurred_events->events |= WL_SOCKET_READABLE;
    1446             :             }
    1447             : 
    1448      250342 :             if ((cur_event->events & WL_SOCKET_WRITEABLE) &&
    1449       15334 :                 (cur_epoll_event->events & (EPOLLOUT | EPOLLERR | EPOLLHUP)))
    1450             :             {
    1451             :                 /* writable, or EOF */
    1452       15274 :                 occurred_events->events |= WL_SOCKET_WRITEABLE;
    1453             :             }
    1454             : 
    1455      250342 :             if (occurred_events->events != 0)
    1456             :             {
    1457      250342 :                 occurred_events->fd = cur_event->fd;
    1458      250342 :                 occurred_events++;
    1459      250342 :                 returned_events++;
    1460             :             }
    1461             :         }
    1462             :     }
    1463             : 
    1464      822582 :     return returned_events;
    1465             : }
    1466             : 
    1467             : #elif defined(WAIT_USE_KQUEUE)
    1468             : 
    1469             : /*
    1470             :  * Wait using kevent(2) on BSD-family systems and macOS.
    1471             :  *
    1472             :  * For now this mirrors the epoll code, but in future it could modify the fd
    1473             :  * set in the same call to kevent as it uses for waiting instead of doing that
    1474             :  * with separate system calls.
    1475             :  */
    1476             : static int
    1477             : WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
    1478             :                       WaitEvent *occurred_events, int nevents)
    1479             : {
    1480             :     int         returned_events = 0;
    1481             :     int         rc;
    1482             :     WaitEvent  *cur_event;
    1483             :     struct kevent *cur_kqueue_event;
    1484             :     struct timespec timeout;
    1485             :     struct timespec *timeout_p;
    1486             : 
    1487             :     if (cur_timeout < 0)
    1488             :         timeout_p = NULL;
    1489             :     else
    1490             :     {
    1491             :         timeout.tv_sec = cur_timeout / 1000;
    1492             :         timeout.tv_nsec = (cur_timeout % 1000) * 1000000;
    1493             :         timeout_p = &timeout;
    1494             :     }
    1495             : 
    1496             :     /*
    1497             :      * Report postmaster events discovered by WaitEventAdjustKqueue() or an
    1498             :      * earlier call to WaitEventSetWait().
    1499             :      */
    1500             :     if (unlikely(set->report_postmaster_not_running))
    1501             :     {
    1502             :         if (set->exit_on_postmaster_death)
    1503             :             proc_exit(1);
    1504             :         occurred_events->fd = PGINVALID_SOCKET;
    1505             :         occurred_events->events = WL_POSTMASTER_DEATH;
    1506             :         return 1;
    1507             :     }
    1508             : 
    1509             :     /* Sleep */
    1510             :     rc = kevent(set->kqueue_fd, NULL, 0,
    1511             :                 set->kqueue_ret_events, nevents,
    1512             :                 timeout_p);
    1513             : 
    1514             :     /* Check return code */
    1515             :     if (rc < 0)
    1516             :     {
    1517             :         /* EINTR is okay, otherwise complain */
    1518             :         if (errno != EINTR)
    1519             :         {
    1520             :             waiting = false;
    1521             :             ereport(ERROR,
    1522             :                     (errcode_for_socket_access(),
    1523             :             /* translator: %s is a syscall name, such as "poll()" */
    1524             :                      errmsg("%s failed: %m",
    1525             :                             "kevent()")));
    1526             :         }
    1527             :         return 0;
    1528             :     }
    1529             :     else if (rc == 0)
    1530             :     {
    1531             :         /* timeout exceeded */
    1532             :         return -1;
    1533             :     }
    1534             : 
    1535             :     /*
    1536             :      * At least one event occurred, iterate over the returned kqueue events
    1537             :      * until they're either all processed, or we've returned all the events
    1538             :      * the caller desired.
    1539             :      */
    1540             :     for (cur_kqueue_event = set->kqueue_ret_events;
    1541             :          cur_kqueue_event < (set->kqueue_ret_events + rc) &&
    1542             :          returned_events < nevents;
    1543             :          cur_kqueue_event++)
    1544             :     {
    1545             :         /* kevent's udata points to the associated WaitEvent */
    1546             :         cur_event = AccessWaitEvent(cur_kqueue_event);
    1547             : 
    1548             :         occurred_events->pos = cur_event->pos;
    1549             :         occurred_events->user_data = cur_event->user_data;
    1550             :         occurred_events->events = 0;
    1551             : 
    1552             :         if (cur_event->events == WL_LATCH_SET &&
    1553             :             cur_kqueue_event->filter == EVFILT_READ)
    1554             :         {
    1555             :             /* There's data in the self-pipe, clear it. */
    1556             :             drainSelfPipe();
    1557             : 
    1558             :             if (set->latch && set->latch->is_set)
    1559             :             {
    1560             :                 occurred_events->fd = PGINVALID_SOCKET;
    1561             :                 occurred_events->events = WL_LATCH_SET;
    1562             :                 occurred_events++;
    1563             :                 returned_events++;
    1564             :             }
    1565             :         }
    1566             :         else if (cur_event->events == WL_POSTMASTER_DEATH &&
    1567             :                  cur_kqueue_event->filter == EVFILT_PROC &&
    1568             :                  (cur_kqueue_event->fflags & NOTE_EXIT) != 0)
    1569             :         {
    1570             :             /*
    1571             :              * The kernel will tell this kqueue object only once about the exit
    1572             :              * of the postmaster, so let's remember that for next time so that
    1573             :              * we provide level-triggered semantics.
    1574             :              */
    1575             :             set->report_postmaster_not_running = true;
    1576             : 
    1577             :             if (set->exit_on_postmaster_death)
    1578             :                 proc_exit(1);
    1579             :             occurred_events->fd = PGINVALID_SOCKET;
    1580             :             occurred_events->events = WL_POSTMASTER_DEATH;
    1581             :             occurred_events++;
    1582             :             returned_events++;
    1583             :         }
    1584             :         else if (cur_event->events & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE))
    1585             :         {
    1586             :             Assert(cur_event->fd >= 0);
    1587             : 
    1588             :             if ((cur_event->events & WL_SOCKET_READABLE) &&
    1589             :                 (cur_kqueue_event->filter == EVFILT_READ))
    1590             :             {
    1591             :                 /* readable, or EOF */
    1592             :                 occurred_events->events |= WL_SOCKET_READABLE;
    1593             :             }
    1594             : 
    1595             :             if ((cur_event->events & WL_SOCKET_WRITEABLE) &&
    1596             :                 (cur_kqueue_event->filter == EVFILT_WRITE))
    1597             :             {
    1598             :                 /* writable, or EOF */
    1599             :                 occurred_events->events |= WL_SOCKET_WRITEABLE;
    1600             :             }
    1601             : 
    1602             :             if (occurred_events->events != 0)
    1603             :             {
    1604             :                 occurred_events->fd = cur_event->fd;
    1605             :                 occurred_events++;
    1606             :                 returned_events++;
    1607             :             }
    1608             :         }
    1609             :     }
    1610             : 
    1611             :     return returned_events;
    1612             : }
    1613             : 
    1614             : #elif defined(WAIT_USE_POLL)
    1615             : 
    1616             : /*
    1617             :  * Wait using poll(2).
    1618             :  *
    1619             :  * This allows to receive readiness notifications for several events at once,
    1620             :  * but requires iterating through all of set->pollfds.
    1621             :  */
    1622             : static inline int
    1623             : WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
    1624             :                       WaitEvent *occurred_events, int nevents)
    1625             : {
    1626             :     int         returned_events = 0;
    1627             :     int         rc;
    1628             :     WaitEvent  *cur_event;
    1629             :     struct pollfd *cur_pollfd;
    1630             : 
    1631             :     /* Sleep */
    1632             :     rc = poll(set->pollfds, set->nevents, (int) cur_timeout);
    1633             : 
    1634             :     /* Check return code */
    1635             :     if (rc < 0)
    1636             :     {
    1637             :         /* EINTR is okay, otherwise complain */
    1638             :         if (errno != EINTR)
    1639             :         {
    1640             :             waiting = false;
    1641             :             ereport(ERROR,
    1642             :                     (errcode_for_socket_access(),
    1643             :             /* translator: %s is a syscall name, such as "poll()" */
    1644             :                      errmsg("%s failed: %m",
    1645             :                             "poll()")));
    1646             :         }
    1647             :         return 0;
    1648             :     }
    1649             :     else if (rc == 0)
    1650             :     {
    1651             :         /* timeout exceeded */
    1652             :         return -1;
    1653             :     }
    1654             : 
    1655             :     for (cur_event = set->events, cur_pollfd = set->pollfds;
    1656             :          cur_event < (set->events + set->nevents) &&
    1657             :          returned_events < nevents;
    1658             :          cur_event++, cur_pollfd++)
    1659             :     {
    1660             :         /* no activity on this FD, skip */
    1661             :         if (cur_pollfd->revents == 0)
    1662             :             continue;
    1663             : 
    1664             :         occurred_events->pos = cur_event->pos;
    1665             :         occurred_events->user_data = cur_event->user_data;
    1666             :         occurred_events->events = 0;
    1667             : 
    1668             :         if (cur_event->events == WL_LATCH_SET &&
    1669             :             (cur_pollfd->revents & (POLLIN | POLLHUP | POLLERR | POLLNVAL)))
    1670             :         {
    1671             :             /* There's data in the self-pipe, clear it. */
    1672             :             drainSelfPipe();
    1673             : 
    1674             :             if (set->latch && set->latch->is_set)
    1675             :             {
    1676             :                 occurred_events->fd = PGINVALID_SOCKET;
    1677             :                 occurred_events->events = WL_LATCH_SET;
    1678             :                 occurred_events++;
    1679             :                 returned_events++;
    1680             :             }
    1681             :         }
    1682             :         else if (cur_event->events == WL_POSTMASTER_DEATH &&
    1683             :                  (cur_pollfd->revents & (POLLIN | POLLHUP | POLLERR | POLLNVAL)))
    1684             :         {
    1685             :             /*
    1686             :              * We expect an POLLHUP when the remote end is closed, but because
    1687             :              * we don't expect the pipe to become readable or to have any
    1688             :              * errors either, treat those cases as postmaster death, too.
    1689             :              *
    1690             :              * Be paranoid about a spurious event signaling the postmaster as
    1691             :              * being dead.  There have been reports about that happening with
    1692             :              * older primitives (select(2) to be specific), and a spurious
    1693             :              * WL_POSTMASTER_DEATH event would be painful. Re-checking doesn't
    1694             :              * cost much.
    1695             :              */
    1696             :             if (!PostmasterIsAliveInternal())
    1697             :             {
    1698             :                 if (set->exit_on_postmaster_death)
    1699             :                     proc_exit(1);
    1700             :                 occurred_events->fd = PGINVALID_SOCKET;
    1701             :                 occurred_events->events = WL_POSTMASTER_DEATH;
    1702             :                 occurred_events++;
    1703             :                 returned_events++;
    1704             :             }
    1705             :         }
    1706             :         else if (cur_event->events & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE))
    1707             :         {
    1708             :             int         errflags = POLLHUP | POLLERR | POLLNVAL;
    1709             : 
    1710             :             Assert(cur_event->fd >= PGINVALID_SOCKET);
    1711             : 
    1712             :             if ((cur_event->events & WL_SOCKET_READABLE) &&
    1713             :                 (cur_pollfd->revents & (POLLIN | errflags)))
    1714             :             {
    1715             :                 /* data available in socket, or EOF */
    1716             :                 occurred_events->events |= WL_SOCKET_READABLE;
    1717             :             }
    1718             : 
    1719             :             if ((cur_event->events & WL_SOCKET_WRITEABLE) &&
    1720             :                 (cur_pollfd->revents & (POLLOUT | errflags)))
    1721             :             {
    1722             :                 /* writeable, or EOF */
    1723             :                 occurred_events->events |= WL_SOCKET_WRITEABLE;
    1724             :             }
    1725             : 
    1726             :             if (occurred_events->events != 0)
    1727             :             {
    1728             :                 occurred_events->fd = cur_event->fd;
    1729             :                 occurred_events++;
    1730             :                 returned_events++;
    1731             :             }
    1732             :         }
    1733             :     }
    1734             :     return returned_events;
    1735             : }
    1736             : 
    1737             : #elif defined(WAIT_USE_WIN32)
    1738             : 
    1739             : /*
    1740             :  * Wait using Windows' WaitForMultipleObjects().
    1741             :  *
    1742             :  * Unfortunately this will only ever return a single readiness notification at
    1743             :  * a time.  Note that while the official documentation for
    1744             :  * WaitForMultipleObjects is ambiguous about multiple events being "consumed"
    1745             :  * with a single bWaitAll = FALSE call,
    1746             :  * https://blogs.msdn.microsoft.com/oldnewthing/20150409-00/?p=44273 confirms
    1747             :  * that only one event is "consumed".
    1748             :  */
    1749             : static inline int
    1750             : WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
    1751             :                       WaitEvent *occurred_events, int nevents)
    1752             : {
    1753             :     int         returned_events = 0;
    1754             :     DWORD       rc;
    1755             :     WaitEvent  *cur_event;
    1756             : 
    1757             :     /* Reset any wait events that need it */
    1758             :     for (cur_event = set->events;
    1759             :          cur_event < (set->events + set->nevents);
    1760             :          cur_event++)
    1761             :     {
    1762             :         if (cur_event->reset)
    1763             :         {
    1764             :             WaitEventAdjustWin32(set, cur_event);
    1765             :             cur_event->reset = false;
    1766             :         }
    1767             : 
    1768             :         /*
    1769             :          * Windows does not guarantee to log an FD_WRITE network event
    1770             :          * indicating that more data can be sent unless the previous send()
    1771             :          * failed with WSAEWOULDBLOCK.  While our caller might well have made
    1772             :          * such a call, we cannot assume that here.  Therefore, if waiting for
    1773             :          * write-ready, force the issue by doing a dummy send().  If the dummy
    1774             :          * send() succeeds, assume that the socket is in fact write-ready, and
    1775             :          * return immediately.  Also, if it fails with something other than
    1776             :          * WSAEWOULDBLOCK, return a write-ready indication to let our caller
    1777             :          * deal with the error condition.
    1778             :          */
    1779             :         if (cur_event->events & WL_SOCKET_WRITEABLE)
    1780             :         {
    1781             :             char        c;
    1782             :             WSABUF      buf;
    1783             :             DWORD       sent;
    1784             :             int         r;
    1785             : 
    1786             :             buf.buf = &c;
    1787             :             buf.len = 0;
    1788             : 
    1789             :             r = WSASend(cur_event->fd, &buf, 1, &sent, 0, NULL, NULL);
    1790             :             if (r == 0 || WSAGetLastError() != WSAEWOULDBLOCK)
    1791             :             {
    1792             :                 occurred_events->pos = cur_event->pos;
    1793             :                 occurred_events->user_data = cur_event->user_data;
    1794             :                 occurred_events->events = WL_SOCKET_WRITEABLE;
    1795             :                 occurred_events->fd = cur_event->fd;
    1796             :                 return 1;
    1797             :             }
    1798             :         }
    1799             :     }
    1800             : 
    1801             :     /*
    1802             :      * Sleep.
    1803             :      *
    1804             :      * Need to wait for ->nevents + 1, because signal handle is in [0].
    1805             :      */
    1806             :     rc = WaitForMultipleObjects(set->nevents + 1, set->handles, FALSE,
    1807             :                                 cur_timeout);
    1808             : 
    1809             :     /* Check return code */
    1810             :     if (rc == WAIT_FAILED)
    1811             :         elog(ERROR, "WaitForMultipleObjects() failed: error code %lu",
    1812             :              GetLastError());
    1813             :     else if (rc == WAIT_TIMEOUT)
    1814             :     {
    1815             :         /* timeout exceeded */
    1816             :         return -1;
    1817             :     }
    1818             : 
    1819             :     if (rc == WAIT_OBJECT_0)
    1820             :     {
    1821             :         /* Service newly-arrived signals */
    1822             :         pgwin32_dispatch_queued_signals();
    1823             :         return 0;               /* retry */
    1824             :     }
    1825             : 
    1826             :     /*
    1827             :      * With an offset of one, due to the always present pgwin32_signal_event,
    1828             :      * the handle offset directly corresponds to a wait event.
    1829             :      */
    1830             :     cur_event = (WaitEvent *) &set->events[rc - WAIT_OBJECT_0 - 1];
    1831             : 
    1832             :     occurred_events->pos = cur_event->pos;
    1833             :     occurred_events->user_data = cur_event->user_data;
    1834             :     occurred_events->events = 0;
    1835             : 
    1836             :     if (cur_event->events == WL_LATCH_SET)
    1837             :     {
    1838             :         /*
    1839             :          * We cannot use set->latch->event to reset the fired event if we
    1840             :          * aren't waiting on this latch now.
    1841             :          */
    1842             :         if (!ResetEvent(set->handles[cur_event->pos + 1]))
    1843             :             elog(ERROR, "ResetEvent failed: error code %lu", GetLastError());
    1844             : 
    1845             :         if (set->latch && set->latch->is_set)
    1846             :         {
    1847             :             occurred_events->fd = PGINVALID_SOCKET;
    1848             :             occurred_events->events = WL_LATCH_SET;
    1849             :             occurred_events++;
    1850             :             returned_events++;
    1851             :         }
    1852             :     }
    1853             :     else if (cur_event->events == WL_POSTMASTER_DEATH)
    1854             :     {
    1855             :         /*
    1856             :          * Postmaster apparently died.  Since the consequences of falsely
    1857             :          * returning WL_POSTMASTER_DEATH could be pretty unpleasant, we take
    1858             :          * the trouble to positively verify this with PostmasterIsAlive(),
    1859             :          * even though there is no known reason to think that the event could
    1860             :          * be falsely set on Windows.
    1861             :          */
    1862             :         if (!PostmasterIsAliveInternal())
    1863             :         {
    1864             :             if (set->exit_on_postmaster_death)
    1865             :                 proc_exit(1);
    1866             :             occurred_events->fd = PGINVALID_SOCKET;
    1867             :             occurred_events->events = WL_POSTMASTER_DEATH;
    1868             :             occurred_events++;
    1869             :             returned_events++;
    1870             :         }
    1871             :     }
    1872             :     else if (cur_event->events & WL_SOCKET_MASK)
    1873             :     {
    1874             :         WSANETWORKEVENTS resEvents;
    1875             :         HANDLE      handle = set->handles[cur_event->pos + 1];
    1876             : 
    1877             :         Assert(cur_event->fd);
    1878             : 
    1879             :         occurred_events->fd = cur_event->fd;
    1880             : 
    1881             :         ZeroMemory(&resEvents, sizeof(resEvents));
    1882             :         if (WSAEnumNetworkEvents(cur_event->fd, handle, &resEvents) != 0)
    1883             :             elog(ERROR, "failed to enumerate network events: error code %u",
    1884             :                  WSAGetLastError());
    1885             :         if ((cur_event->events & WL_SOCKET_READABLE) &&
    1886             :             (resEvents.lNetworkEvents & FD_READ))
    1887             :         {
    1888             :             /* data available in socket */
    1889             :             occurred_events->events |= WL_SOCKET_READABLE;
    1890             : 
    1891             :             /*------
    1892             :              * WaitForMultipleObjects doesn't guarantee that a read event will
    1893             :              * be returned if the latch is set at the same time.  Even if it
    1894             :              * did, the caller might drop that event expecting it to reoccur
    1895             :              * on next call.  So, we must force the event to be reset if this
    1896             :              * WaitEventSet is used again in order to avoid an indefinite
    1897             :              * hang.  Refer https://msdn.microsoft.com/en-us/library/windows/desktop/ms741576(v=vs.85).aspx
    1898             :              * for the behavior of socket events.
    1899             :              *------
    1900             :              */
    1901             :             cur_event->reset = true;
    1902             :         }
    1903             :         if ((cur_event->events & WL_SOCKET_WRITEABLE) &&
    1904             :             (resEvents.lNetworkEvents & FD_WRITE))
    1905             :         {
    1906             :             /* writeable */
    1907             :             occurred_events->events |= WL_SOCKET_WRITEABLE;
    1908             :         }
    1909             :         if ((cur_event->events & WL_SOCKET_CONNECTED) &&
    1910             :             (resEvents.lNetworkEvents & FD_CONNECT))
    1911             :         {
    1912             :             /* connected */
    1913             :             occurred_events->events |= WL_SOCKET_CONNECTED;
    1914             :         }
    1915             :         if (resEvents.lNetworkEvents & FD_CLOSE)
    1916             :         {
    1917             :             /* EOF/error, so signal all caller-requested socket flags */
    1918             :             occurred_events->events |= (cur_event->events & WL_SOCKET_MASK);
    1919             :         }
    1920             : 
    1921             :         if (occurred_events->events != 0)
    1922             :         {
    1923             :             occurred_events++;
    1924             :             returned_events++;
    1925             :         }
    1926             :     }
    1927             : 
    1928             :     return returned_events;
    1929             : }
    1930             : #endif
    1931             : 
    1932             : /*
    1933             :  * SetLatch uses SIGUSR1 to wake up the process waiting on the latch.
    1934             :  *
    1935             :  * Wake up WaitLatch, if we're waiting.  (We might not be, since SIGUSR1 is
    1936             :  * overloaded for multiple purposes; or we might not have reached WaitLatch
    1937             :  * yet, in which case we don't need to fill the pipe either.)
    1938             :  *
    1939             :  * NB: when calling this in a signal handler, be sure to save and restore
    1940             :  * errno around it.
    1941             :  */
    1942             : #ifndef WIN32
    1943             : void
    1944      628368 : latch_sigusr1_handler(void)
    1945             : {
    1946      628368 :     if (waiting)
    1947      595796 :         sendSelfPipeByte();
    1948      628368 : }
    1949             : #endif                          /* !WIN32 */
    1950             : 
    1951             : /* Send one byte to the self-pipe, to wake up WaitLatch */
    1952             : #ifndef WIN32
    1953             : static void
    1954      613180 : sendSelfPipeByte(void)
    1955             : {
    1956             :     int         rc;
    1957      613180 :     char        dummy = 0;
    1958             : 
    1959      613180 : retry:
    1960      613180 :     rc = write(selfpipe_writefd, &dummy, 1);
    1961      613180 :     if (rc < 0)
    1962             :     {
    1963             :         /* If interrupted by signal, just retry */
    1964           0 :         if (errno == EINTR)
    1965           0 :             goto retry;
    1966             : 
    1967             :         /*
    1968             :          * If the pipe is full, we don't need to retry, the data that's there
    1969             :          * already is enough to wake up WaitLatch.
    1970             :          */
    1971           0 :         if (errno == EAGAIN || errno == EWOULDBLOCK)
    1972           0 :             return;
    1973             : 
    1974             :         /*
    1975             :          * Oops, the write() failed for some other reason. We might be in a
    1976             :          * signal handler, so it's not safe to elog(). We have no choice but
    1977             :          * silently ignore the error.
    1978             :          */
    1979           0 :         return;
    1980             :     }
    1981             : }
    1982             : #endif                          /* !WIN32 */
    1983             : 
    1984             : /*
    1985             :  * Read all available data from the self-pipe
    1986             :  *
    1987             :  * Note: this is only called when waiting = true.  If it fails and doesn't
    1988             :  * return, it must reset that flag first (though ideally, this will never
    1989             :  * happen).
    1990             :  */
    1991             : #ifndef WIN32
    1992             : static void
    1993      572234 : drainSelfPipe(void)
    1994             : {
    1995             :     /*
    1996             :      * There shouldn't normally be more than one byte in the pipe, or maybe a
    1997             :      * few bytes if multiple processes run SetLatch at the same instant.
    1998             :      */
    1999             :     char        buf[16];
    2000             :     int         rc;
    2001             : 
    2002             :     for (;;)
    2003             :     {
    2004      572234 :         rc = read(selfpipe_readfd, buf, sizeof(buf));
    2005      572234 :         if (rc < 0)
    2006             :         {
    2007           0 :             if (errno == EAGAIN || errno == EWOULDBLOCK)
    2008             :                 break;          /* the pipe is empty */
    2009           0 :             else if (errno == EINTR)
    2010           0 :                 continue;       /* retry */
    2011             :             else
    2012             :             {
    2013           0 :                 waiting = false;
    2014           0 :                 elog(ERROR, "read() on self-pipe failed: %m");
    2015             :             }
    2016             :         }
    2017      572234 :         else if (rc == 0)
    2018             :         {
    2019           0 :             waiting = false;
    2020           0 :             elog(ERROR, "unexpected EOF on self-pipe");
    2021             :         }
    2022      572234 :         else if (rc < sizeof(buf))
    2023             :         {
    2024             :             /* we successfully drained the pipe; no need to read() again */
    2025      572234 :             break;
    2026             :         }
    2027             :         /* else buffer wasn't big enough, so read again */
    2028             :     }
    2029      572234 : }
    2030             : #endif                          /* !WIN32 */

Generated by: LCOV version 1.13