LCOV - code coverage report
Current view: top level - src/backend/storage/ipc - waiteventset.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 88.3 % 247 218
Test Date: 2026-03-01 18:15:11 Functions: 100.0 % 17 17
Legend: Lines:     hit not hit

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

Generated by: LCOV version 2.0-1