PostgreSQL Source Code  git master
latch.h File Reference
#include <signal.h>
#include "utils/resowner.h"
Include dependency graph for latch.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  Latch
 
struct  WaitEvent
 

Macros

#define WL_LATCH_SET   (1 << 0)
 
#define WL_SOCKET_READABLE   (1 << 1)
 
#define WL_SOCKET_WRITEABLE   (1 << 2)
 
#define WL_TIMEOUT   (1 << 3) /* not for WaitEventSetWait() */
 
#define WL_POSTMASTER_DEATH   (1 << 4)
 
#define WL_EXIT_ON_PM_DEATH   (1 << 5)
 
#define WL_SOCKET_CONNECTED   WL_SOCKET_WRITEABLE
 
#define WL_SOCKET_CLOSED   (1 << 7)
 
#define WL_SOCKET_ACCEPT   WL_SOCKET_READABLE
 
#define WL_SOCKET_MASK
 

Typedefs

typedef struct Latch Latch
 
typedef struct WaitEvent WaitEvent
 
typedef struct WaitEventSet WaitEventSet
 

Functions

void InitializeLatchSupport (void)
 
void InitLatch (Latch *latch)
 
void InitSharedLatch (Latch *latch)
 
void OwnLatch (Latch *latch)
 
void DisownLatch (Latch *latch)
 
void SetLatch (Latch *latch)
 
void ResetLatch (Latch *latch)
 
void ShutdownLatchSupport (void)
 
WaitEventSetCreateWaitEventSet (ResourceOwner resowner, int nevents)
 
void FreeWaitEventSet (WaitEventSet *set)
 
void FreeWaitEventSetAfterFork (WaitEventSet *set)
 
int AddWaitEventToSet (WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
 
void ModifyWaitEvent (WaitEventSet *set, int pos, uint32 events, Latch *latch)
 
int WaitEventSetWait (WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
 
int WaitLatch (Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
 
int WaitLatchOrSocket (Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
 
void InitializeLatchWaitSet (void)
 
int GetNumRegisteredWaitEvents (WaitEventSet *set)
 
bool WaitEventSetCanReportClosed (void)
 

Macro Definition Documentation

◆ WL_EXIT_ON_PM_DEATH

#define WL_EXIT_ON_PM_DEATH   (1 << 5)

Definition at line 132 of file latch.h.

◆ WL_LATCH_SET

#define WL_LATCH_SET   (1 << 0)

Definition at line 127 of file latch.h.

◆ WL_POSTMASTER_DEATH

#define WL_POSTMASTER_DEATH   (1 << 4)

Definition at line 131 of file latch.h.

◆ WL_SOCKET_ACCEPT

#define WL_SOCKET_ACCEPT   WL_SOCKET_READABLE

Definition at line 144 of file latch.h.

◆ WL_SOCKET_CLOSED

#define WL_SOCKET_CLOSED   (1 << 7)

Definition at line 139 of file latch.h.

◆ WL_SOCKET_CONNECTED

#define WL_SOCKET_CONNECTED   WL_SOCKET_WRITEABLE

Definition at line 137 of file latch.h.

◆ WL_SOCKET_MASK

#define WL_SOCKET_MASK
Value:
WL_SOCKET_WRITEABLE | \
WL_SOCKET_CONNECTED | \
WL_SOCKET_ACCEPT | \
WL_SOCKET_CLOSED)
#define WL_SOCKET_READABLE
Definition: latch.h:128

Definition at line 146 of file latch.h.

◆ WL_SOCKET_READABLE

#define WL_SOCKET_READABLE   (1 << 1)

Definition at line 128 of file latch.h.

◆ WL_SOCKET_WRITEABLE

#define WL_SOCKET_WRITEABLE   (1 << 2)

Definition at line 129 of file latch.h.

◆ WL_TIMEOUT

#define WL_TIMEOUT   (1 << 3) /* not for WaitEventSetWait() */

Definition at line 130 of file latch.h.

Typedef Documentation

◆ Latch

typedef struct Latch Latch

◆ WaitEvent

typedef struct WaitEvent WaitEvent

◆ WaitEventSet

typedef struct WaitEventSet WaitEventSet

Definition at line 1 of file latch.h.

Function Documentation

◆ AddWaitEventToSet()

int AddWaitEventToSet ( WaitEventSet set,
uint32  events,
pgsocket  fd,
Latch latch,
void *  user_data 
)

Definition at line 964 of file latch.c.

966 {
967  WaitEvent *event;
968 
969  /* not enough space */
970  Assert(set->nevents < set->nevents_space);
971 
972  if (events == WL_EXIT_ON_PM_DEATH)
973  {
974  events = WL_POSTMASTER_DEATH;
975  set->exit_on_postmaster_death = true;
976  }
977 
978  if (latch)
979  {
980  if (latch->owner_pid != MyProcPid)
981  elog(ERROR, "cannot wait on a latch owned by another process");
982  if (set->latch)
983  elog(ERROR, "cannot wait on more than one latch");
984  if ((events & WL_LATCH_SET) != WL_LATCH_SET)
985  elog(ERROR, "latch events only support being set");
986  }
987  else
988  {
989  if (events & WL_LATCH_SET)
990  elog(ERROR, "cannot wait on latch without a specified latch");
991  }
992 
993  /* waiting for socket readiness without a socket indicates a bug */
994  if (fd == PGINVALID_SOCKET && (events & WL_SOCKET_MASK))
995  elog(ERROR, "cannot wait on socket event without a socket");
996 
997  event = &set->events[set->nevents];
998  event->pos = set->nevents++;
999  event->fd = fd;
1000  event->events = events;
1001  event->user_data = user_data;
1002 #ifdef WIN32
1003  event->reset = false;
1004 #endif
1005 
1006  if (events == WL_LATCH_SET)
1007  {
1008  set->latch = latch;
1009  set->latch_pos = event->pos;
1010 #if defined(WAIT_USE_SELF_PIPE)
1011  event->fd = selfpipe_readfd;
1012 #elif defined(WAIT_USE_SIGNALFD)
1013  event->fd = signal_fd;
1014 #else
1015  event->fd = PGINVALID_SOCKET;
1016 #ifdef WAIT_USE_EPOLL
1017  return event->pos;
1018 #endif
1019 #endif
1020  }
1021  else if (events == WL_POSTMASTER_DEATH)
1022  {
1023 #ifndef WIN32
1025 #endif
1026  }
1027 
1028  /* perform wait primitive specific initialization, if needed */
1029 #if defined(WAIT_USE_EPOLL)
1030  WaitEventAdjustEpoll(set, event, EPOLL_CTL_ADD);
1031 #elif defined(WAIT_USE_KQUEUE)
1032  WaitEventAdjustKqueue(set, event, 0);
1033 #elif defined(WAIT_USE_POLL)
1034  WaitEventAdjustPoll(set, event);
1035 #elif defined(WAIT_USE_WIN32)
1036  WaitEventAdjustWin32(set, event);
1037 #endif
1038 
1039  return event->pos;
1040 }
#define ERROR
Definition: elog.h:39
int MyProcPid
Definition: globals.c:45
static int selfpipe_readfd
Definition: latch.c:173
static void WaitEventAdjustPoll(WaitEventSet *set, WaitEvent *event)
Definition: latch.c:1177
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:132
#define WL_LATCH_SET
Definition: latch.h:127
#define WL_POSTMASTER_DEATH
Definition: latch.h:131
#define WL_SOCKET_MASK
Definition: latch.h:146
Assert(fmt[strlen(fmt) - 1] !='\n')
#define PGINVALID_SOCKET
Definition: port.h:31
int postmaster_alive_fds[2]
Definition: postmaster.c:577
#define POSTMASTER_FD_WATCH
Definition: postmaster.h:46
static int fd(const char *x, int i)
Definition: preproc-init.c:105
int owner_pid
Definition: latch.h:117
Latch * latch
Definition: latch.c:122
bool exit_on_postmaster_death
Definition: latch.c:130
int nevents
Definition: latch.c:107
int latch_pos
Definition: latch.c:123
int nevents_space
Definition: latch.c:108
WaitEvent * events
Definition: latch.c:114
int pos
Definition: latch.h:154

References Assert(), elog(), ERROR, WaitEventSet::events, WaitEventSet::exit_on_postmaster_death, fd(), WaitEventSet::latch, WaitEventSet::latch_pos, MyProcPid, WaitEventSet::nevents, WaitEventSet::nevents_space, Latch::owner_pid, PGINVALID_SOCKET, WaitEvent::pos, postmaster_alive_fds, POSTMASTER_FD_WATCH, selfpipe_readfd, WaitEventAdjustPoll(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_SOCKET_MASK.

Referenced by ConfigurePostmasterWaitSet(), ExecAppendAsyncEventWait(), InitializeLatchWaitSet(), postgresForeignAsyncConfigureWait(), pq_init(), SysLoggerMain(), and WaitLatchOrSocket().

◆ CreateWaitEventSet()

WaitEventSet* CreateWaitEventSet ( ResourceOwner  resowner,
int  nevents 
)

Definition at line 752 of file latch.c.

753 {
754  WaitEventSet *set;
755  char *data;
756  Size sz = 0;
757 
758  /*
759  * Use MAXALIGN size/alignment to guarantee that later uses of memory are
760  * aligned correctly. E.g. epoll_event might need 8 byte alignment on some
761  * platforms, but earlier allocations like WaitEventSet and WaitEvent
762  * might not be sized to guarantee that when purely using sizeof().
763  */
764  sz += MAXALIGN(sizeof(WaitEventSet));
765  sz += MAXALIGN(sizeof(WaitEvent) * nevents);
766 
767 #if defined(WAIT_USE_EPOLL)
768  sz += MAXALIGN(sizeof(struct epoll_event) * nevents);
769 #elif defined(WAIT_USE_KQUEUE)
770  sz += MAXALIGN(sizeof(struct kevent) * nevents);
771 #elif defined(WAIT_USE_POLL)
772  sz += MAXALIGN(sizeof(struct pollfd) * nevents);
773 #elif defined(WAIT_USE_WIN32)
774  /* need space for the pgwin32_signal_event */
775  sz += MAXALIGN(sizeof(HANDLE) * (nevents + 1));
776 #endif
777 
778  if (resowner != NULL)
779  ResourceOwnerEnlarge(resowner);
780 
782 
783  set = (WaitEventSet *) data;
784  data += MAXALIGN(sizeof(WaitEventSet));
785 
786  set->events = (WaitEvent *) data;
787  data += MAXALIGN(sizeof(WaitEvent) * nevents);
788 
789 #if defined(WAIT_USE_EPOLL)
790  set->epoll_ret_events = (struct epoll_event *) data;
791  data += MAXALIGN(sizeof(struct epoll_event) * nevents);
792 #elif defined(WAIT_USE_KQUEUE)
793  set->kqueue_ret_events = (struct kevent *) data;
794  data += MAXALIGN(sizeof(struct kevent) * nevents);
795 #elif defined(WAIT_USE_POLL)
796  set->pollfds = (struct pollfd *) data;
797  data += MAXALIGN(sizeof(struct pollfd) * nevents);
798 #elif defined(WAIT_USE_WIN32)
799  set->handles = (HANDLE) data;
800  data += MAXALIGN(sizeof(HANDLE) * nevents);
801 #endif
802 
803  set->latch = NULL;
804  set->nevents_space = nevents;
805  set->exit_on_postmaster_death = false;
806 
807  if (resowner != NULL)
808  {
809  ResourceOwnerRememberWaitEventSet(resowner, set);
810  set->owner = resowner;
811  }
812 
813 #if defined(WAIT_USE_EPOLL)
814  if (!AcquireExternalFD())
815  {
816  /* treat this as though epoll_create1 itself returned EMFILE */
817  elog(ERROR, "epoll_create1 failed: %m");
818  }
819  set->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
820  if (set->epoll_fd < 0)
821  {
823  elog(ERROR, "epoll_create1 failed: %m");
824  }
825 #elif defined(WAIT_USE_KQUEUE)
826  if (!AcquireExternalFD())
827  {
828  /* treat this as though kqueue itself returned EMFILE */
829  elog(ERROR, "kqueue failed: %m");
830  }
831  set->kqueue_fd = kqueue();
832  if (set->kqueue_fd < 0)
833  {
835  elog(ERROR, "kqueue failed: %m");
836  }
837  if (fcntl(set->kqueue_fd, F_SETFD, FD_CLOEXEC) == -1)
838  {
839  int save_errno = errno;
840 
841  close(set->kqueue_fd);
843  errno = save_errno;
844  elog(ERROR, "fcntl(F_SETFD) failed on kqueue descriptor: %m");
845  }
846  set->report_postmaster_not_running = false;
847 #elif defined(WAIT_USE_WIN32)
848 
849  /*
850  * To handle signals while waiting, we need to add a win32 specific event.
851  * We accounted for the additional event at the top of this routine. See
852  * port/win32/signal.c for more details.
853  *
854  * Note: pgwin32_signal_event should be first to ensure that it will be
855  * reported when multiple events are set. We want to guarantee that
856  * pending signals are serviced.
857  */
858  set->handles[0] = pgwin32_signal_event;
859  StaticAssertStmt(WSA_INVALID_EVENT == NULL, "");
860 #endif
861 
862  return set;
863 }
#define MAXALIGN(LEN)
Definition: c.h:800
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:927
size_t Size
Definition: c.h:594
void ReleaseExternalFD(void)
Definition: fd.c:1239
bool AcquireExternalFD(void)
Definition: fd.c:1186
#define close(a)
Definition: win32.h:12
static void ResourceOwnerRememberWaitEventSet(ResourceOwner owner, WaitEventSet *set)
Definition: latch.c:215
MemoryContext TopMemoryContext
Definition: mcxt.c:141
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1077
const void * data
void ResourceOwnerEnlarge(ResourceOwner owner)
Definition: resowner.c:442
HANDLE pgwin32_signal_event
Definition: signal.c:27
ResourceOwner owner
Definition: latch.c:105
struct pollfd * pollfds
Definition: latch.c:143

References AcquireExternalFD(), close, data, elog(), ERROR, WaitEventSet::events, WaitEventSet::exit_on_postmaster_death, WaitEventSet::latch, MAXALIGN, MemoryContextAllocZero(), WaitEventSet::nevents_space, WaitEventSet::owner, pgwin32_signal_event, WaitEventSet::pollfds, ReleaseExternalFD(), ResourceOwnerEnlarge(), ResourceOwnerRememberWaitEventSet(), StaticAssertStmt, and TopMemoryContext.

Referenced by ConfigurePostmasterWaitSet(), ExecAppendAsyncEventWait(), InitializeLatchWaitSet(), pq_init(), SysLoggerMain(), and WaitLatchOrSocket().

◆ DisownLatch()

void DisownLatch ( Latch latch)

Definition at line 490 of file latch.c.

491 {
492  Assert(latch->is_shared);
493  Assert(latch->owner_pid == MyProcPid);
494 
495  latch->owner_pid = 0;
496 }
bool is_shared
Definition: latch.h:116

References Assert(), Latch::is_shared, MyProcPid, and Latch::owner_pid.

Referenced by AuxiliaryProcKill(), ProcKill(), and ShutdownWalRecovery().

◆ FreeWaitEventSet()

void FreeWaitEventSet ( WaitEventSet set)

Definition at line 875 of file latch.c.

876 {
877  if (set->owner)
878  {
880  set->owner = NULL;
881  }
882 
883 #if defined(WAIT_USE_EPOLL)
884  close(set->epoll_fd);
886 #elif defined(WAIT_USE_KQUEUE)
887  close(set->kqueue_fd);
889 #elif defined(WAIT_USE_WIN32)
890  for (WaitEvent *cur_event = set->events;
891  cur_event < (set->events + set->nevents);
892  cur_event++)
893  {
894  if (cur_event->events & WL_LATCH_SET)
895  {
896  /* uses the latch's HANDLE */
897  }
898  else if (cur_event->events & WL_POSTMASTER_DEATH)
899  {
900  /* uses PostmasterHandle */
901  }
902  else
903  {
904  /* Clean up the event object we created for the socket */
905  WSAEventSelect(cur_event->fd, NULL, 0);
906  WSACloseEvent(set->handles[cur_event->pos + 1]);
907  }
908  }
909 #endif
910 
911  pfree(set);
912 }
static void ResourceOwnerForgetWaitEventSet(ResourceOwner owner, WaitEventSet *set)
Definition: latch.c:220
void pfree(void *pointer)
Definition: mcxt.c:1431

References close, WaitEventSet::events, WaitEventSet::nevents, WaitEventSet::owner, pfree(), ReleaseExternalFD(), ResourceOwnerForgetWaitEventSet(), WL_LATCH_SET, and WL_POSTMASTER_DEATH.

Referenced by ConfigurePostmasterWaitSet(), ExecAppendAsyncEventWait(), ResOwnerReleaseWaitEventSet(), ShutdownLatchSupport(), and WaitLatchOrSocket().

◆ FreeWaitEventSetAfterFork()

void FreeWaitEventSetAfterFork ( WaitEventSet set)

Definition at line 918 of file latch.c.

919 {
920 #if defined(WAIT_USE_EPOLL)
921  close(set->epoll_fd);
923 #elif defined(WAIT_USE_KQUEUE)
924  /* kqueues are not normally inherited by child processes */
926 #endif
927 
928  pfree(set);
929 }

References close, pfree(), and ReleaseExternalFD().

Referenced by ClosePostmasterPorts().

◆ GetNumRegisteredWaitEvents()

int GetNumRegisteredWaitEvents ( WaitEventSet set)

Definition at line 2231 of file latch.c.

2232 {
2233  return set->nevents;
2234 }

References WaitEventSet::nevents.

Referenced by ExecAppendAsyncEventWait(), and postgresForeignAsyncConfigureWait().

◆ InitializeLatchSupport()

void InitializeLatchSupport ( void  )

Definition at line 233 of file latch.c.

234 {
235 #if defined(WAIT_USE_SELF_PIPE)
236  int pipefd[2];
237 
238  if (IsUnderPostmaster)
239  {
240  /*
241  * We might have inherited connections to a self-pipe created by the
242  * postmaster. It's critical that child processes create their own
243  * self-pipes, of course, and we really want them to close the
244  * inherited FDs for safety's sake.
245  */
246  if (selfpipe_owner_pid != 0)
247  {
248  /* Assert we go through here but once in a child process */
250  /* Release postmaster's pipe FDs; ignore any error */
251  (void) close(selfpipe_readfd);
252  (void) close(selfpipe_writefd);
253  /* Clean up, just for safety's sake; we'll set these below */
255  selfpipe_owner_pid = 0;
256  /* Keep fd.c's accounting straight */
259  }
260  else
261  {
262  /*
263  * Postmaster didn't create a self-pipe ... or else we're in an
264  * EXEC_BACKEND build, in which case it doesn't matter since the
265  * postmaster's pipe FDs were closed by the action of FD_CLOEXEC.
266  * fd.c won't have state to clean up, either.
267  */
268  Assert(selfpipe_readfd == -1);
269  }
270  }
271  else
272  {
273  /* In postmaster or standalone backend, assert we do this but once */
274  Assert(selfpipe_readfd == -1);
276  }
277 
278  /*
279  * Set up the self-pipe that allows a signal handler to wake up the
280  * poll()/epoll_wait() in WaitLatch. Make the write-end non-blocking, so
281  * that SetLatch won't block if the event has already been set many times
282  * filling the kernel buffer. Make the read-end non-blocking too, so that
283  * we can easily clear the pipe by reading until EAGAIN or EWOULDBLOCK.
284  * Also, make both FDs close-on-exec, since we surely do not want any
285  * child processes messing with them.
286  */
287  if (pipe(pipefd) < 0)
288  elog(FATAL, "pipe() failed: %m");
289  if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) == -1)
290  elog(FATAL, "fcntl(F_SETFL) failed on read-end of self-pipe: %m");
291  if (fcntl(pipefd[1], F_SETFL, O_NONBLOCK) == -1)
292  elog(FATAL, "fcntl(F_SETFL) failed on write-end of self-pipe: %m");
293  if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) == -1)
294  elog(FATAL, "fcntl(F_SETFD) failed on read-end of self-pipe: %m");
295  if (fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) == -1)
296  elog(FATAL, "fcntl(F_SETFD) failed on write-end of self-pipe: %m");
297 
298  selfpipe_readfd = pipefd[0];
299  selfpipe_writefd = pipefd[1];
301 
302  /* Tell fd.c about these two long-lived FDs */
305 
307 #endif
308 
309 #ifdef WAIT_USE_SIGNALFD
310  sigset_t signalfd_mask;
311 
312  if (IsUnderPostmaster)
313  {
314  /*
315  * It would probably be safe to re-use the inherited signalfd since
316  * signalfds only see the current process's pending signals, but it
317  * seems less surprising to close it and create our own.
318  */
319  if (signal_fd != -1)
320  {
321  /* Release postmaster's signal FD; ignore any error */
322  (void) close(signal_fd);
323  signal_fd = -1;
325  }
326  }
327 
328  /* Block SIGURG, because we'll receive it through a signalfd. */
329  sigaddset(&UnBlockSig, SIGURG);
330 
331  /* Set up the signalfd to receive SIGURG notifications. */
332  sigemptyset(&signalfd_mask);
333  sigaddset(&signalfd_mask, SIGURG);
334  signal_fd = signalfd(-1, &signalfd_mask, SFD_NONBLOCK | SFD_CLOEXEC);
335  if (signal_fd < 0)
336  elog(FATAL, "signalfd() failed");
338 #endif
339 
340 #ifdef WAIT_USE_KQUEUE
341  /* Ignore SIGURG, because we'll receive it via kqueue. */
342  pqsignal(SIGURG, SIG_IGN);
343 #endif
344 }
sigset_t UnBlockSig
Definition: pqsignal.c:22
#define FATAL
Definition: elog.h:41
void ReserveExternalFD(void)
Definition: fd.c:1221
bool IsUnderPostmaster
Definition: globals.c:116
static void latch_sigurg_handler(SIGNAL_ARGS)
Definition: latch.c:2244
static int selfpipe_owner_pid
Definition: latch.c:177
static int selfpipe_writefd
Definition: latch.c:174
pqsigfunc pqsignal(int signo, pqsigfunc func)
#define SIG_IGN
Definition: win32_port.h:165

References Assert(), close, elog(), FATAL, IsUnderPostmaster, latch_sigurg_handler(), MyProcPid, pqsignal(), ReleaseExternalFD(), ReserveExternalFD(), selfpipe_owner_pid, selfpipe_readfd, selfpipe_writefd, SIG_IGN, and UnBlockSig.

Referenced by InitPostmasterChild(), InitStandaloneProcess(), and PostmasterMain().

◆ InitializeLatchWaitSet()

void InitializeLatchWaitSet ( void  )

Definition at line 347 of file latch.c.

348 {
349  int latch_pos PG_USED_FOR_ASSERTS_ONLY;
350 
351  Assert(LatchWaitSet == NULL);
352 
353  /* Set up the WaitEventSet used by WaitLatch(). */
354  LatchWaitSet = CreateWaitEventSet(NULL, 2);
356  MyLatch, NULL);
357  if (IsUnderPostmaster)
359  PGINVALID_SOCKET, NULL, NULL);
360 
361  Assert(latch_pos == LatchWaitSetLatchPos);
362 }
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:171
struct Latch * MyLatch
Definition: globals.c:59
#define LatchWaitSetLatchPos
Definition: latch.c:159
static WaitEventSet * LatchWaitSet
Definition: latch.c:156
WaitEventSet * CreateWaitEventSet(ResourceOwner resowner, int nevents)
Definition: latch.c:752
int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
Definition: latch.c:964

References AddWaitEventToSet(), Assert(), CreateWaitEventSet(), IsUnderPostmaster, LatchWaitSet, LatchWaitSetLatchPos, MyLatch, PG_USED_FOR_ASSERTS_ONLY, PGINVALID_SOCKET, WL_EXIT_ON_PM_DEATH, and WL_LATCH_SET.

Referenced by InitPostmasterChild(), and InitStandaloneProcess().

◆ InitLatch()

void InitLatch ( Latch latch)

Definition at line 395 of file latch.c.

396 {
397  latch->is_set = false;
398  latch->maybe_sleeping = false;
399  latch->owner_pid = MyProcPid;
400  latch->is_shared = false;
401 
402 #if defined(WAIT_USE_SELF_PIPE)
403  /* Assert InitializeLatchSupport has been called in this process */
405 #elif defined(WAIT_USE_SIGNALFD)
406  /* Assert InitializeLatchSupport has been called in this process */
407  Assert(signal_fd >= 0);
408 #elif defined(WAIT_USE_WIN32)
409  latch->event = CreateEvent(NULL, TRUE, FALSE, NULL);
410  if (latch->event == NULL)
411  elog(ERROR, "CreateEvent failed: error code %lu", GetLastError());
412 #endif /* WIN32 */
413 }
sig_atomic_t is_set
Definition: latch.h:114
sig_atomic_t maybe_sleeping
Definition: latch.h:115

References Assert(), elog(), ERROR, Latch::is_set, Latch::is_shared, Latch::maybe_sleeping, MyProcPid, Latch::owner_pid, selfpipe_owner_pid, and selfpipe_readfd.

Referenced by InitProcessLocalLatch().

◆ InitSharedLatch()

void InitSharedLatch ( Latch latch)

Definition at line 431 of file latch.c.

432 {
433 #ifdef WIN32
434  SECURITY_ATTRIBUTES sa;
435 
436  /*
437  * Set up security attributes to specify that the events are inherited.
438  */
439  ZeroMemory(&sa, sizeof(sa));
440  sa.nLength = sizeof(sa);
441  sa.bInheritHandle = TRUE;
442 
443  latch->event = CreateEvent(&sa, TRUE, FALSE, NULL);
444  if (latch->event == NULL)
445  elog(ERROR, "CreateEvent failed: error code %lu", GetLastError());
446 #endif
447 
448  latch->is_set = false;
449  latch->maybe_sleeping = false;
450  latch->owner_pid = 0;
451  latch->is_shared = true;
452 }

References elog(), ERROR, Latch::is_set, Latch::is_shared, Latch::maybe_sleeping, and Latch::owner_pid.

Referenced by InitProcGlobal(), and XLogRecoveryShmemInit().

◆ ModifyWaitEvent()

void ModifyWaitEvent ( WaitEventSet set,
int  pos,
uint32  events,
Latch latch 
)

Definition at line 1050 of file latch.c.

1051 {
1052  WaitEvent *event;
1053 #if defined(WAIT_USE_KQUEUE)
1054  int old_events;
1055 #endif
1056 
1057  Assert(pos < set->nevents);
1058 
1059  event = &set->events[pos];
1060 #if defined(WAIT_USE_KQUEUE)
1061  old_events = event->events;
1062 #endif
1063 
1064  /*
1065  * If neither the event mask nor the associated latch changes, return
1066  * early. That's an important optimization for some sockets, where
1067  * ModifyWaitEvent is frequently used to switch from waiting for reads to
1068  * waiting on writes.
1069  */
1070  if (events == event->events &&
1071  (!(event->events & WL_LATCH_SET) || set->latch == latch))
1072  return;
1073 
1074  if (event->events & WL_LATCH_SET &&
1075  events != event->events)
1076  {
1077  elog(ERROR, "cannot modify latch event");
1078  }
1079 
1080  if (event->events & WL_POSTMASTER_DEATH)
1081  {
1082  elog(ERROR, "cannot modify postmaster death event");
1083  }
1084 
1085  /* FIXME: validate event mask */
1086  event->events = events;
1087 
1088  if (events == WL_LATCH_SET)
1089  {
1090  if (latch && latch->owner_pid != MyProcPid)
1091  elog(ERROR, "cannot wait on a latch owned by another process");
1092  set->latch = latch;
1093 
1094  /*
1095  * On Unix, we don't need to modify the kernel object because the
1096  * underlying pipe (if there is one) is the same for all latches so we
1097  * can return immediately. On Windows, we need to update our array of
1098  * handles, but we leave the old one in place and tolerate spurious
1099  * wakeups if the latch is disabled.
1100  */
1101 #if defined(WAIT_USE_WIN32)
1102  if (!latch)
1103  return;
1104 #else
1105  return;
1106 #endif
1107  }
1108 
1109 #if defined(WAIT_USE_EPOLL)
1110  WaitEventAdjustEpoll(set, event, EPOLL_CTL_MOD);
1111 #elif defined(WAIT_USE_KQUEUE)
1112  WaitEventAdjustKqueue(set, event, old_events);
1113 #elif defined(WAIT_USE_POLL)
1114  WaitEventAdjustPoll(set, event);
1115 #elif defined(WAIT_USE_WIN32)
1116  WaitEventAdjustWin32(set, event);
1117 #endif
1118 }
uint32 events
Definition: latch.h:155

References Assert(), elog(), ERROR, WaitEventSet::events, WaitEvent::events, WaitEventSet::latch, MyProcPid, Latch::owner_pid, WaitEventAdjustPoll(), WL_LATCH_SET, and WL_POSTMASTER_DEATH.

Referenced by pq_check_connection(), secure_read(), secure_write(), SwitchBackToLocalLatch(), SwitchToSharedLatch(), WaitLatch(), and WalSndWait().

◆ OwnLatch()

void OwnLatch ( Latch latch)

Definition at line 464 of file latch.c.

465 {
466  int owner_pid;
467 
468  /* Sanity checks */
469  Assert(latch->is_shared);
470 
471 #if defined(WAIT_USE_SELF_PIPE)
472  /* Assert InitializeLatchSupport has been called in this process */
474 #elif defined(WAIT_USE_SIGNALFD)
475  /* Assert InitializeLatchSupport has been called in this process */
476  Assert(signal_fd >= 0);
477 #endif
478 
479  owner_pid = latch->owner_pid;
480  if (owner_pid != 0)
481  elog(PANIC, "latch already owned by PID %d", owner_pid);
482 
483  latch->owner_pid = MyProcPid;
484 }
#define PANIC
Definition: elog.h:42

References Assert(), elog(), Latch::is_shared, MyProcPid, Latch::owner_pid, PANIC, selfpipe_owner_pid, and selfpipe_readfd.

Referenced by InitAuxiliaryProcess(), InitProcess(), and InitWalRecovery().

◆ ResetLatch()

void ResetLatch ( Latch latch)

Definition at line 725 of file latch.c.

726 {
727  /* Only the owner should reset the latch */
728  Assert(latch->owner_pid == MyProcPid);
729  Assert(latch->maybe_sleeping == false);
730 
731  latch->is_set = false;
732 
733  /*
734  * Ensure that the write to is_set gets flushed to main memory before we
735  * examine any flag variables. Otherwise a concurrent SetLatch might
736  * falsely conclude that it needn't signal us, even though we have missed
737  * seeing some flag updates that SetLatch was supposed to inform us of.
738  */
740 }
#define pg_memory_barrier()
Definition: atomics.h:140

References Assert(), Latch::is_set, Latch::maybe_sleeping, MyProcPid, Latch::owner_pid, and pg_memory_barrier.

Referenced by ApplyLauncherMain(), autoprewarm_main(), AutoVacLauncherMain(), BackgroundWriterMain(), CheckpointerMain(), CheckpointWriteDelay(), ConditionVariableTimedSleep(), copy_read_data(), do_pg_backup_stop(), gather_readnext(), lazy_truncate_heap(), libpqrcv_connect(), libpqrcv_PQgetResult(), libpqsrv_connect_internal(), libpqsrv_get_result(), LogicalParallelApplyLoop(), logicalrep_worker_stop_internal(), LogicalRepApplyLoop(), mq_putmessage(), pa_send_data(), pa_wait_for_xact_state(), pg_promote(), pg_sleep(), pg_wait_until_termination(), pgarch_MainLoop(), pgfdw_get_cleanup_result(), pq_check_connection(), ProcessPendingWrites(), ProcSleep(), ProcWaitForSignal(), recoveryApplyDelay(), secure_read(), secure_write(), ServerLoop(), shm_mq_receive_bytes(), shm_mq_send_bytes(), shm_mq_wait_internal(), ShutDownSlotSync(), summarizer_wait_for_wal(), SyncRepWaitForLSN(), SysLoggerMain(), test_shm_mq_pipelined(), throttle(), wait_for_relation_state_change(), wait_for_slot_activity(), wait_for_worker_state_change(), wait_for_workers_to_become_ready(), WaitForBackgroundWorkerShutdown(), WaitForBackgroundWorkerStartup(), WaitForParallelWorkersToAttach(), WaitForParallelWorkersToFinish(), WaitForReplicationWorkerAttach(), WaitForWALToBecomeAvailable(), WalRcvWaitForStartPosition(), WalReceiverMain(), WalSndLoop(), WalSndWaitForWal(), WalWriterMain(), and worker_spi_main().

◆ SetLatch()

void SetLatch ( Latch latch)

Definition at line 633 of file latch.c.

634 {
635 #ifndef WIN32
636  pid_t owner_pid;
637 #else
638  HANDLE handle;
639 #endif
640 
641  /*
642  * The memory barrier has to be placed here to ensure that any flag
643  * variables possibly changed by this process have been flushed to main
644  * memory, before we check/set is_set.
645  */
647 
648  /* Quick exit if already set */
649  if (latch->is_set)
650  return;
651 
652  latch->is_set = true;
653 
655  if (!latch->maybe_sleeping)
656  return;
657 
658 #ifndef WIN32
659 
660  /*
661  * See if anyone's waiting for the latch. It can be the current process if
662  * we're in a signal handler. We use the self-pipe or SIGURG to ourselves
663  * to wake up WaitEventSetWaitBlock() without races in that case. If it's
664  * another process, send a signal.
665  *
666  * Fetch owner_pid only once, in case the latch is concurrently getting
667  * owned or disowned. XXX: This assumes that pid_t is atomic, which isn't
668  * guaranteed to be true! In practice, the effective range of pid_t fits
669  * in a 32 bit integer, and so should be atomic. In the worst case, we
670  * might end up signaling the wrong process. Even then, you're very
671  * unlucky if a process with that bogus pid exists and belongs to
672  * Postgres; and PG database processes should handle excess SIGUSR1
673  * interrupts without a problem anyhow.
674  *
675  * Another sort of race condition that's possible here is for a new
676  * process to own the latch immediately after we look, so we don't signal
677  * it. This is okay so long as all callers of ResetLatch/WaitLatch follow
678  * the standard coding convention of waiting at the bottom of their loops,
679  * not the top, so that they'll correctly process latch-setting events
680  * that happen before they enter the loop.
681  */
682  owner_pid = latch->owner_pid;
683  if (owner_pid == 0)
684  return;
685  else if (owner_pid == MyProcPid)
686  {
687 #if defined(WAIT_USE_SELF_PIPE)
688  if (waiting)
690 #else
691  if (waiting)
692  kill(MyProcPid, SIGURG);
693 #endif
694  }
695  else
696  kill(owner_pid, SIGURG);
697 
698 #else
699 
700  /*
701  * See if anyone's waiting for the latch. It can be the current process if
702  * we're in a signal handler.
703  *
704  * Use a local variable here just in case somebody changes the event field
705  * concurrently (which really should not happen).
706  */
707  handle = latch->event;
708  if (handle)
709  {
710  SetEvent(handle);
711 
712  /*
713  * Note that we silently ignore any errors. We might be in a signal
714  * handler or other critical path where it's not safe to call elog().
715  */
716  }
717 #endif
718 }
static void sendSelfPipeByte(void)
Definition: latch.c:2252
static volatile sig_atomic_t waiting
Definition: latch.c:163
#define kill(pid, sig)
Definition: win32_port.h:485

References Latch::is_set, kill, Latch::maybe_sleeping, MyProcPid, Latch::owner_pid, pg_memory_barrier, sendSelfPipeByte(), and waiting.

Referenced by avl_sigusr2_handler(), CheckDeadLockAlert(), ClientCheckTimeoutHandler(), ConditionVariableBroadcast(), ConditionVariableSignal(), die(), ForwardSyncRequest(), handle_pm_child_exit_signal(), handle_pm_pmsignal_signal(), handle_pm_reload_request_signal(), handle_pm_shutdown_request_signal(), handle_sig_alarm(), HandleCatchupInterrupt(), HandleNotifyInterrupt(), HandleParallelApplyMessageInterrupt(), HandleParallelMessageInterrupt(), IdleInTransactionSessionTimeoutHandler(), IdleSessionTimeoutHandler(), IdleStatsUpdateTimeoutHandler(), logicalrep_worker_wakeup_ptr(), pgarch_waken_stop(), PgArchWakeup(), ProcessClientReadInterrupt(), ProcessClientWriteInterrupt(), ProcessPendingWrites(), ProcSendSignal(), procsignal_sigusr1_handler(), ProcWakeup(), ReqCheckpointHandler(), RequestXLogStreaming(), SetWalSummarizerLatch(), shm_mq_detach_internal(), shm_mq_inc_bytes_read(), shm_mq_send_bytes(), shm_mq_sendv(), shm_mq_set_receiver(), shm_mq_set_sender(), SignalHandlerForConfigReload(), SignalHandlerForShutdownRequest(), sigUsr1Handler(), StatementCancelHandler(), StrategyGetBuffer(), SwitchBackToLocalLatch(), SwitchToSharedLatch(), SyncRepWakeQueue(), test_shm_mq_main(), TransactionTimeoutHandler(), WakeupRecovery(), WalRcvForceReply(), WalSndLastCycleHandler(), WalSndWaitForWal(), and XLogSetAsyncXactLSN().

◆ ShutdownLatchSupport()

void ShutdownLatchSupport ( void  )

Definition at line 365 of file latch.c.

366 {
367 #if defined(WAIT_USE_POLL)
368  pqsignal(SIGURG, SIG_IGN);
369 #endif
370 
371  if (LatchWaitSet)
372  {
374  LatchWaitSet = NULL;
375  }
376 
377 #if defined(WAIT_USE_SELF_PIPE)
380  selfpipe_readfd = -1;
381  selfpipe_writefd = -1;
383 #endif
384 
385 #if defined(WAIT_USE_SIGNALFD)
386  close(signal_fd);
387  signal_fd = -1;
388 #endif
389 }
void FreeWaitEventSet(WaitEventSet *set)
Definition: latch.c:875
#define InvalidPid
Definition: miscadmin.h:32

References close, FreeWaitEventSet(), InvalidPid, LatchWaitSet, pqsignal(), selfpipe_owner_pid, selfpipe_readfd, selfpipe_writefd, and SIG_IGN.

◆ WaitEventSetCanReportClosed()

bool WaitEventSetCanReportClosed ( void  )

Definition at line 2216 of file latch.c.

2217 {
2218 #if (defined(WAIT_USE_POLL) && defined(POLLRDHUP)) || \
2219  defined(WAIT_USE_EPOLL) || \
2220  defined(WAIT_USE_KQUEUE)
2221  return true;
2222 #else
2223  return false;
2224 #endif
2225 }

Referenced by check_client_connection_check_interval().

◆ WaitEventSetWait()

int WaitEventSetWait ( WaitEventSet set,
long  timeout,
WaitEvent occurred_events,
int  nevents,
uint32  wait_event_info 
)

Definition at line 1425 of file latch.c.

1428 {
1429  int returned_events = 0;
1431  instr_time cur_time;
1432  long cur_timeout = -1;
1433 
1434  Assert(nevents > 0);
1435 
1436  /*
1437  * Initialize timeout if requested. We must record the current time so
1438  * that we can determine the remaining timeout if interrupted.
1439  */
1440  if (timeout >= 0)
1441  {
1443  Assert(timeout >= 0 && timeout <= INT_MAX);
1444  cur_timeout = timeout;
1445  }
1446  else
1448 
1449  pgstat_report_wait_start(wait_event_info);
1450 
1451 #ifndef WIN32
1452  waiting = true;
1453 #else
1454  /* Ensure that signals are serviced even if latch is already set */
1456 #endif
1457  while (returned_events == 0)
1458  {
1459  int rc;
1460 
1461  /*
1462  * Check if the latch is set already. If so, leave the loop
1463  * immediately, avoid blocking again. We don't attempt to report any
1464  * other events that might also be satisfied.
1465  *
1466  * If someone sets the latch between this and the
1467  * WaitEventSetWaitBlock() below, the setter will write a byte to the
1468  * pipe (or signal us and the signal handler will do that), and the
1469  * readiness routine will return immediately.
1470  *
1471  * On unix, If there's a pending byte in the self pipe, we'll notice
1472  * whenever blocking. Only clearing the pipe in that case avoids
1473  * having to drain it every time WaitLatchOrSocket() is used. Should
1474  * the pipe-buffer fill up we're still ok, because the pipe is in
1475  * nonblocking mode. It's unlikely for that to happen, because the
1476  * self pipe isn't filled unless we're blocking (waiting = true), or
1477  * from inside a signal handler in latch_sigurg_handler().
1478  *
1479  * On windows, we'll also notice if there's a pending event for the
1480  * latch when blocking, but there's no danger of anything filling up,
1481  * as "Setting an event that is already set has no effect.".
1482  *
1483  * Note: we assume that the kernel calls involved in latch management
1484  * will provide adequate synchronization on machines with weak memory
1485  * ordering, so that we cannot miss seeing is_set if a notification
1486  * has already been queued.
1487  */
1488  if (set->latch && !set->latch->is_set)
1489  {
1490  /* about to sleep on a latch */
1491  set->latch->maybe_sleeping = true;
1493  /* and recheck */
1494  }
1495 
1496  if (set->latch && set->latch->is_set)
1497  {
1498  occurred_events->fd = PGINVALID_SOCKET;
1499  occurred_events->pos = set->latch_pos;
1500  occurred_events->user_data =
1501  set->events[set->latch_pos].user_data;
1502  occurred_events->events = WL_LATCH_SET;
1503  occurred_events++;
1504  returned_events++;
1505 
1506  /* could have been set above */
1507  set->latch->maybe_sleeping = false;
1508 
1509  break;
1510  }
1511 
1512  /*
1513  * Wait for events using the readiness primitive chosen at the top of
1514  * this file. If -1 is returned, a timeout has occurred, if 0 we have
1515  * to retry, everything >= 1 is the number of returned events.
1516  */
1517  rc = WaitEventSetWaitBlock(set, cur_timeout,
1518  occurred_events, nevents);
1519 
1520  if (set->latch)
1521  {
1522  Assert(set->latch->maybe_sleeping);
1523  set->latch->maybe_sleeping = false;
1524  }
1525 
1526  if (rc == -1)
1527  break; /* timeout occurred */
1528  else
1529  returned_events = rc;
1530 
1531  /* If we're not done, update cur_timeout for next iteration */
1532  if (returned_events == 0 && timeout >= 0)
1533  {
1534  INSTR_TIME_SET_CURRENT(cur_time);
1535  INSTR_TIME_SUBTRACT(cur_time, start_time);
1536  cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
1537  if (cur_timeout <= 0)
1538  break;
1539  }
1540  }
1541 #ifndef WIN32
1542  waiting = false;
1543 #endif
1544 
1546 
1547  return returned_events;
1548 }
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:122
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:181
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:191
#define INSTR_TIME_SET_ZERO(t)
Definition: instr_time.h:172
static int WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout, WaitEvent *occurred_events, int nevents)
Definition: latch.c:1851
static time_t start_time
Definition: pg_ctl.c:94
void pgwin32_dispatch_queued_signals(void)
Definition: signal.c:120
pgsocket fd
Definition: latch.h:156
void * user_data
Definition: latch.h:157
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:88
static void pgstat_report_wait_end(void)
Definition: wait_event.h:104

References Assert(), WaitEventSet::events, WaitEvent::events, WaitEvent::fd, INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, INSTR_TIME_SUBTRACT, Latch::is_set, WaitEventSet::latch, WaitEventSet::latch_pos, Latch::maybe_sleeping, pg_memory_barrier, PGINVALID_SOCKET, pgstat_report_wait_end(), pgstat_report_wait_start(), pgwin32_dispatch_queued_signals(), WaitEvent::pos, start_time, WaitEvent::user_data, WaitEventSetWaitBlock(), waiting, and WL_LATCH_SET.

Referenced by ExecAppendAsyncEventWait(), pq_check_connection(), secure_read(), secure_write(), ServerLoop(), SysLoggerMain(), WaitLatch(), WaitLatchOrSocket(), and WalSndWait().

◆ WaitLatch()

int WaitLatch ( Latch latch,
int  wakeEvents,
long  timeout,
uint32  wait_event_info 
)

Definition at line 518 of file latch.c.

520 {
521  WaitEvent event;
522 
523  /* Postmaster-managed callers must handle postmaster death somehow. */
525  (wakeEvents & WL_EXIT_ON_PM_DEATH) ||
526  (wakeEvents & WL_POSTMASTER_DEATH));
527 
528  /*
529  * Some callers may have a latch other than MyLatch, or no latch at all,
530  * or want to handle postmaster death differently. It's cheap to assign
531  * those, so just do it every time.
532  */
533  if (!(wakeEvents & WL_LATCH_SET))
534  latch = NULL;
537  ((wakeEvents & WL_EXIT_ON_PM_DEATH) != 0);
538 
540  (wakeEvents & WL_TIMEOUT) ? timeout : -1,
541  &event, 1,
542  wait_event_info) == 0)
543  return WL_TIMEOUT;
544  else
545  return event.events;
546 }
void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
Definition: latch.c:1050
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
Definition: latch.c:1425
#define WL_TIMEOUT
Definition: latch.h:130

References Assert(), WaitEventSet::exit_on_postmaster_death, IsUnderPostmaster, LatchWaitSet, LatchWaitSetLatchPos, ModifyWaitEvent(), WaitEventSetWait(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_TIMEOUT.

Referenced by ApplyLauncherMain(), autoprewarm_main(), AutoVacLauncherMain(), BackgroundWriterMain(), CheckpointerMain(), CheckpointWriteDelay(), ConditionVariableTimedSleep(), do_pg_backup_stop(), gather_readnext(), lazy_truncate_heap(), LogicalParallelApplyLoop(), logicalrep_worker_stop_internal(), mq_putmessage(), pa_send_data(), pa_wait_for_xact_state(), pg_promote(), pg_sleep(), pg_wait_until_termination(), pgarch_MainLoop(), ProcSleep(), ProcWaitForSignal(), recoveryApplyDelay(), RegisterSyncRequest(), shm_mq_receive_bytes(), shm_mq_send_bytes(), shm_mq_wait_internal(), ShutDownSlotSync(), summarizer_wait_for_wal(), SyncRepWaitForLSN(), test_shm_mq_pipelined(), throttle(), wait_for_relation_state_change(), wait_for_slot_activity(), wait_for_worker_state_change(), wait_for_workers_to_become_ready(), WaitForBackgroundWorkerShutdown(), WaitForBackgroundWorkerStartup(), WaitForParallelWorkersToAttach(), WaitForParallelWorkersToFinish(), WaitForReplicationWorkerAttach(), WaitForWALToBecomeAvailable(), WalRcvWaitForStartPosition(), WalSummarizerMain(), WalWriterMain(), and worker_spi_main().

◆ WaitLatchOrSocket()

int WaitLatchOrSocket ( Latch latch,
int  wakeEvents,
pgsocket  sock,
long  timeout,
uint32  wait_event_info 
)

Definition at line 566 of file latch.c.

568 {
569  int ret = 0;
570  int rc;
571  WaitEvent event;
573 
574  if (wakeEvents & WL_TIMEOUT)
575  Assert(timeout >= 0);
576  else
577  timeout = -1;
578 
579  if (wakeEvents & WL_LATCH_SET)
581  latch, NULL);
582 
583  /* Postmaster-managed callers must handle postmaster death somehow. */
585  (wakeEvents & WL_EXIT_ON_PM_DEATH) ||
586  (wakeEvents & WL_POSTMASTER_DEATH));
587 
588  if ((wakeEvents & WL_POSTMASTER_DEATH) && IsUnderPostmaster)
590  NULL, NULL);
591 
592  if ((wakeEvents & WL_EXIT_ON_PM_DEATH) && IsUnderPostmaster)
594  NULL, NULL);
595 
596  if (wakeEvents & WL_SOCKET_MASK)
597  {
598  int ev;
599 
600  ev = wakeEvents & WL_SOCKET_MASK;
601  AddWaitEventToSet(set, ev, sock, NULL, NULL);
602  }
603 
604  rc = WaitEventSetWait(set, timeout, &event, 1, wait_event_info);
605 
606  if (rc == 0)
607  ret |= WL_TIMEOUT;
608  else
609  {
610  ret |= event.events & (WL_LATCH_SET |
613  }
614 
615  FreeWaitEventSet(set);
616 
617  return ret;
618 }
ResourceOwner CurrentResourceOwner
Definition: resowner.c:165

References AddWaitEventToSet(), Assert(), CreateWaitEventSet(), CurrentResourceOwner, FreeWaitEventSet(), IsUnderPostmaster, PGINVALID_SOCKET, WaitEventSetWait(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, WL_POSTMASTER_DEATH, WL_SOCKET_MASK, and WL_TIMEOUT.

Referenced by be_tls_open_server(), copy_read_data(), libpqrcv_connect(), libpqrcv_PQgetResult(), libpqsrv_connect_internal(), libpqsrv_get_result(), LogicalRepApplyLoop(), pgfdw_get_cleanup_result(), read_or_wait(), secure_open_gssapi(), and WalReceiverMain().