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