PostgreSQL Source Code  git master
latch.c File Reference
#include "postgres.h"
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <unistd.h>
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "port/atomics.h"
#include "portability/instr_time.h"
#include "postmaster/postmaster.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/pmsignal.h"
#include "storage/shmem.h"
#include "utils/memutils.h"
Include dependency graph for latch.c:

Go to the source code of this file.

Data Structures

struct  WaitEventSet
 

Macros

#define LatchWaitSetLatchPos   0
 

Functions

static int WaitEventSetWaitBlock (WaitEventSet *set, int cur_timeout, WaitEvent *occurred_events, int nevents)
 
void InitializeLatchSupport (void)
 
void InitializeLatchWaitSet (void)
 
void ShutdownLatchSupport (void)
 
void InitLatch (Latch *latch)
 
void InitSharedLatch (Latch *latch)
 
void OwnLatch (Latch *latch)
 
void DisownLatch (Latch *latch)
 
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 SetLatch (Latch *latch)
 
void ResetLatch (Latch *latch)
 
WaitEventSetCreateWaitEventSet (MemoryContext context, int nevents)
 
void FreeWaitEventSet (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 GetNumRegisteredWaitEvents (WaitEventSet *set)
 

Variables

static WaitEventSetLatchWaitSet
 
static volatile sig_atomic_t waiting = false
 

Macro Definition Documentation

◆ LatchWaitSetLatchPos

#define LatchWaitSetLatchPos   0

Definition at line 143 of file latch.c.

Referenced by InitializeLatchWaitSet(), and WaitLatch().

Function Documentation

◆ AddWaitEventToSet()

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

Definition at line 862 of file latch.c.

References Assert, elog, ERROR, WaitEventSet::events, fd(), WaitEventSet::latch, MyProcPid, Latch::owner_pid, PGINVALID_SOCKET, postmaster_alive_fds, POSTMASTER_FD_WATCH, WaitEvent::user_data, WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_SOCKET_MASK.

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

864 {
865  WaitEvent *event;
866 
867  /* not enough space */
868  Assert(set->nevents < set->nevents_space);
869 
870  if (events == WL_EXIT_ON_PM_DEATH)
871  {
872  events = WL_POSTMASTER_DEATH;
873  set->exit_on_postmaster_death = true;
874  }
875 
876  if (latch)
877  {
878  if (latch->owner_pid != MyProcPid)
879  elog(ERROR, "cannot wait on a latch owned by another process");
880  if (set->latch)
881  elog(ERROR, "cannot wait on more than one latch");
882  if ((events & WL_LATCH_SET) != WL_LATCH_SET)
883  elog(ERROR, "latch events only support being set");
884  }
885  else
886  {
887  if (events & WL_LATCH_SET)
888  elog(ERROR, "cannot wait on latch without a specified latch");
889  }
890 
891  /* waiting for socket readiness without a socket indicates a bug */
892  if (fd == PGINVALID_SOCKET && (events & WL_SOCKET_MASK))
893  elog(ERROR, "cannot wait on socket event without a socket");
894 
895  event = &set->events[set->nevents];
896  event->pos = set->nevents++;
897  event->fd = fd;
898  event->events = events;
899  event->user_data = user_data;
900 #ifdef WIN32
901  event->reset = false;
902 #endif
903 
904  if (events == WL_LATCH_SET)
905  {
906  set->latch = latch;
907  set->latch_pos = event->pos;
908 #if defined(WAIT_USE_POLL)
909  event->fd = selfpipe_readfd;
910 #elif defined(WAIT_USE_EPOLL)
911  event->fd = signal_fd;
912 #else
913  event->fd = PGINVALID_SOCKET;
914 #ifdef WAIT_USE_EPOLL
915  return event->pos;
916 #endif
917 #endif
918  }
919  else if (events == WL_POSTMASTER_DEATH)
920  {
921 #ifndef WIN32
923 #endif
924  }
925 
926  /* perform wait primitive specific initialization, if needed */
927 #if defined(WAIT_USE_EPOLL)
928  WaitEventAdjustEpoll(set, event, EPOLL_CTL_ADD);
929 #elif defined(WAIT_USE_KQUEUE)
930  WaitEventAdjustKqueue(set, event, 0);
931 #elif defined(WAIT_USE_POLL)
932  WaitEventAdjustPoll(set, event);
933 #elif defined(WAIT_USE_WIN32)
934  WaitEventAdjustWin32(set, event);
935 #endif
936 
937  return event->pos;
938 }
int MyProcPid
Definition: globals.c:43
#define WL_SOCKET_MASK
Definition: latch.h:138
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define ERROR
Definition: elog.h:46
int nevents
Definition: latch.c:91
int postmaster_alive_fds[2]
Definition: postmaster.c:571
#define WL_POSTMASTER_DEATH
Definition: latch.h:129
#define PGINVALID_SOCKET
Definition: port.h:33
#define Assert(condition)
Definition: c.h:804
int nevents_space
Definition: latch.c:92
int owner_pid
Definition: latch.h:115
#define elog(elevel,...)
Definition: elog.h:232
Latch * latch
Definition: latch.c:106
#define WL_LATCH_SET
Definition: latch.h:125
#define POSTMASTER_FD_WATCH
Definition: postmaster.h:43
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130

◆ CreateWaitEventSet()

WaitEventSet* CreateWaitEventSet ( MemoryContext  context,
int  nevents 
)

Definition at line 684 of file latch.c.

References AcquireExternalFD(), close, elog, ERROR, MAXALIGN, MemoryContextAllocZero(), WaitEventSet::nevents, pgwin32_signal_event, ReleaseExternalFD(), and StaticAssertStmt.

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

685 {
686  WaitEventSet *set;
687  char *data;
688  Size sz = 0;
689 
690  /*
691  * Use MAXALIGN size/alignment to guarantee that later uses of memory are
692  * aligned correctly. E.g. epoll_event might need 8 byte alignment on some
693  * platforms, but earlier allocations like WaitEventSet and WaitEvent
694  * might not sized to guarantee that when purely using sizeof().
695  */
696  sz += MAXALIGN(sizeof(WaitEventSet));
697  sz += MAXALIGN(sizeof(WaitEvent) * nevents);
698 
699 #if defined(WAIT_USE_EPOLL)
700  sz += MAXALIGN(sizeof(struct epoll_event) * nevents);
701 #elif defined(WAIT_USE_KQUEUE)
702  sz += MAXALIGN(sizeof(struct kevent) * nevents);
703 #elif defined(WAIT_USE_POLL)
704  sz += MAXALIGN(sizeof(struct pollfd) * nevents);
705 #elif defined(WAIT_USE_WIN32)
706  /* need space for the pgwin32_signal_event */
707  sz += MAXALIGN(sizeof(HANDLE) * (nevents + 1));
708 #endif
709 
710  data = (char *) MemoryContextAllocZero(context, sz);
711 
712  set = (WaitEventSet *) data;
713  data += MAXALIGN(sizeof(WaitEventSet));
714 
715  set->events = (WaitEvent *) data;
716  data += MAXALIGN(sizeof(WaitEvent) * nevents);
717 
718 #if defined(WAIT_USE_EPOLL)
719  set->epoll_ret_events = (struct epoll_event *) data;
720  data += MAXALIGN(sizeof(struct epoll_event) * nevents);
721 #elif defined(WAIT_USE_KQUEUE)
722  set->kqueue_ret_events = (struct kevent *) data;
723  data += MAXALIGN(sizeof(struct kevent) * nevents);
724 #elif defined(WAIT_USE_POLL)
725  set->pollfds = (struct pollfd *) data;
726  data += MAXALIGN(sizeof(struct pollfd) * nevents);
727 #elif defined(WAIT_USE_WIN32)
728  set->handles = (HANDLE) data;
729  data += MAXALIGN(sizeof(HANDLE) * nevents);
730 #endif
731 
732  set->latch = NULL;
733  set->nevents_space = nevents;
734  set->exit_on_postmaster_death = false;
735 
736 #if defined(WAIT_USE_EPOLL)
737  if (!AcquireExternalFD())
738  {
739  /* treat this as though epoll_create1 itself returned EMFILE */
740  elog(ERROR, "epoll_create1 failed: %m");
741  }
742  set->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
743  if (set->epoll_fd < 0)
744  {
746  elog(ERROR, "epoll_create1 failed: %m");
747  }
748 #elif defined(WAIT_USE_KQUEUE)
749  if (!AcquireExternalFD())
750  {
751  /* treat this as though kqueue itself returned EMFILE */
752  elog(ERROR, "kqueue failed: %m");
753  }
754  set->kqueue_fd = kqueue();
755  if (set->kqueue_fd < 0)
756  {
758  elog(ERROR, "kqueue failed: %m");
759  }
760  if (fcntl(set->kqueue_fd, F_SETFD, FD_CLOEXEC) == -1)
761  {
762  int save_errno = errno;
763 
764  close(set->kqueue_fd);
766  errno = save_errno;
767  elog(ERROR, "fcntl(F_SETFD) failed on kqueue descriptor: %m");
768  }
769  set->report_postmaster_not_running = false;
770 #elif defined(WAIT_USE_WIN32)
771 
772  /*
773  * To handle signals while waiting, we need to add a win32 specific event.
774  * We accounted for the additional event at the top of this routine. See
775  * port/win32/signal.c for more details.
776  *
777  * Note: pgwin32_signal_event should be first to ensure that it will be
778  * reported when multiple events are set. We want to guarantee that
779  * pending signals are serviced.
780  */
781  set->handles[0] = pgwin32_signal_event;
782  StaticAssertStmt(WSA_INVALID_EVENT == NULL, "");
783 #endif
784 
785  return set;
786 }
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:918
HANDLE pgwin32_signal_event
Definition: signal.c:27
#define ERROR
Definition: elog.h:46
bool AcquireExternalFD(void)
Definition: fd.c:1139
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:906
size_t Size
Definition: c.h:540
#define MAXALIGN(LEN)
Definition: c.h:757
void ReleaseExternalFD(void)
Definition: fd.c:1192
#define elog(elevel,...)
Definition: elog.h:232
#define close(a)
Definition: win32.h:12

◆ DisownLatch()

void DisownLatch ( Latch latch)

Definition at line 424 of file latch.c.

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

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

425 {
426  Assert(latch->is_shared);
427  Assert(latch->owner_pid == MyProcPid);
428 
429  latch->owner_pid = 0;
430 }
int MyProcPid
Definition: globals.c:43
bool is_shared
Definition: latch.h:114
#define Assert(condition)
Definition: c.h:804
int owner_pid
Definition: latch.h:115

◆ FreeWaitEventSet()

void FreeWaitEventSet ( WaitEventSet set)

Definition at line 798 of file latch.c.

References close, WaitEvent::events, WaitEvent::fd, pfree(), WaitEvent::pos, ReleaseExternalFD(), WL_LATCH_SET, and WL_POSTMASTER_DEATH.

Referenced by ExecAppendAsyncEventWait(), PgstatCollectorMain(), ShutdownLatchSupport(), and WaitLatchOrSocket().

799 {
800 #if defined(WAIT_USE_EPOLL)
801  close(set->epoll_fd);
803 #elif defined(WAIT_USE_KQUEUE)
804  close(set->kqueue_fd);
806 #elif defined(WAIT_USE_WIN32)
807  WaitEvent *cur_event;
808 
809  for (cur_event = set->events;
810  cur_event < (set->events + set->nevents);
811  cur_event++)
812  {
813  if (cur_event->events & WL_LATCH_SET)
814  {
815  /* uses the latch's HANDLE */
816  }
817  else if (cur_event->events & WL_POSTMASTER_DEATH)
818  {
819  /* uses PostmasterHandle */
820  }
821  else
822  {
823  /* Clean up the event object we created for the socket */
824  WSAEventSelect(cur_event->fd, NULL, 0);
825  WSACloseEvent(set->handles[cur_event->pos + 1]);
826  }
827  }
828 #endif
829 
830  pfree(set);
831 }
pgsocket fd
Definition: latch.h:146
int pos
Definition: latch.h:144
void pfree(void *pointer)
Definition: mcxt.c:1169
uint32 events
Definition: latch.h:145
int nevents
Definition: latch.c:91
#define WL_POSTMASTER_DEATH
Definition: latch.h:129
WaitEvent * events
Definition: latch.c:98
void ReleaseExternalFD(void)
Definition: fd.c:1192
#define close(a)
Definition: win32.h:12
#define WL_LATCH_SET
Definition: latch.h:125

◆ GetNumRegisteredWaitEvents()

int GetNumRegisteredWaitEvents ( WaitEventSet set)

Definition at line 2022 of file latch.c.

References buf, EAGAIN, EINTR, elog, ERROR, EWOULDBLOCK, fd(), read, SIGNAL_ARGS, waiting, and write.

Referenced by postgresForeignAsyncConfigureWait().

2023 {
2024  return set->nevents;
2025 }

◆ InitializeLatchSupport()

void InitializeLatchSupport ( void  )

Definition at line 192 of file latch.c.

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

Referenced by InitPostmasterChild(), and InitStandaloneProcess().

193 {
194 #if defined(WAIT_USE_POLL)
195  int pipefd[2];
196 
197  if (IsUnderPostmaster)
198  {
199  /*
200  * We might have inherited connections to a self-pipe created by the
201  * postmaster. It's critical that child processes create their own
202  * self-pipes, of course, and we really want them to close the
203  * inherited FDs for safety's sake.
204  */
205  if (selfpipe_owner_pid != 0)
206  {
207  /* Assert we go through here but once in a child process */
208  Assert(selfpipe_owner_pid != MyProcPid);
209  /* Release postmaster's pipe FDs; ignore any error */
210  (void) close(selfpipe_readfd);
211  (void) close(selfpipe_writefd);
212  /* Clean up, just for safety's sake; we'll set these below */
213  selfpipe_readfd = selfpipe_writefd = -1;
214  selfpipe_owner_pid = 0;
215  /* Keep fd.c's accounting straight */
218  }
219  else
220  {
221  /*
222  * Postmaster didn't create a self-pipe ... or else we're in an
223  * EXEC_BACKEND build, in which case it doesn't matter since the
224  * postmaster's pipe FDs were closed by the action of FD_CLOEXEC.
225  * fd.c won't have state to clean up, either.
226  */
227  Assert(selfpipe_readfd == -1);
228  }
229  }
230  else
231  {
232  /* In postmaster or standalone backend, assert we do this but once */
233  Assert(selfpipe_readfd == -1);
234  Assert(selfpipe_owner_pid == 0);
235  }
236 
237  /*
238  * Set up the self-pipe that allows a signal handler to wake up the
239  * poll()/epoll_wait() in WaitLatch. Make the write-end non-blocking, so
240  * that SetLatch won't block if the event has already been set many times
241  * filling the kernel buffer. Make the read-end non-blocking too, so that
242  * we can easily clear the pipe by reading until EAGAIN or EWOULDBLOCK.
243  * Also, make both FDs close-on-exec, since we surely do not want any
244  * child processes messing with them.
245  */
246  if (pipe(pipefd) < 0)
247  elog(FATAL, "pipe() failed: %m");
248  if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) == -1)
249  elog(FATAL, "fcntl(F_SETFL) failed on read-end of self-pipe: %m");
250  if (fcntl(pipefd[1], F_SETFL, O_NONBLOCK) == -1)
251  elog(FATAL, "fcntl(F_SETFL) failed on write-end of self-pipe: %m");
252  if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) == -1)
253  elog(FATAL, "fcntl(F_SETFD) failed on read-end of self-pipe: %m");
254  if (fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) == -1)
255  elog(FATAL, "fcntl(F_SETFD) failed on write-end of self-pipe: %m");
256 
257  selfpipe_readfd = pipefd[0];
258  selfpipe_writefd = pipefd[1];
259  selfpipe_owner_pid = MyProcPid;
260 
261  /* Tell fd.c about these two long-lived FDs */
264 
265  pqsignal(SIGURG, latch_sigurg_handler);
266 #endif
267 
268 #ifdef WAIT_USE_EPOLL
269  sigset_t signalfd_mask;
270 
271  /* Block SIGURG, because we'll receive it through a signalfd. */
272  sigaddset(&UnBlockSig, SIGURG);
273 
274  /* Set up the signalfd to receive SIGURG notifications. */
275  sigemptyset(&signalfd_mask);
276  sigaddset(&signalfd_mask, SIGURG);
277  signal_fd = signalfd(-1, &signalfd_mask, SFD_NONBLOCK | SFD_CLOEXEC);
278  if (signal_fd < 0)
279  elog(FATAL, "signalfd() failed");
281 #endif
282 
283 #ifdef WAIT_USE_KQUEUE
284  /* Ignore SIGURG, because we'll receive it via kqueue. */
285  pqsignal(SIGURG, SIG_IGN);
286 #endif
287 }
int MyProcPid
Definition: globals.c:43
#define FATAL
Definition: elog.h:49
void ReserveExternalFD(void)
Definition: fd.c:1174
bool IsUnderPostmaster
Definition: globals.c:112
sigset_t UnBlockSig
Definition: pqsignal.c:22
#define SIG_IGN
Definition: win32_port.h:156
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
#define Assert(condition)
Definition: c.h:804
void ReleaseExternalFD(void)
Definition: fd.c:1192
#define elog(elevel,...)
Definition: elog.h:232
#define close(a)
Definition: win32.h:12

◆ InitializeLatchWaitSet()

void InitializeLatchWaitSet ( void  )

Definition at line 290 of file latch.c.

References AddWaitEventToSet(), Assert, CreateWaitEventSet(), IsUnderPostmaster, WaitEventSet::latch_pos, LatchWaitSetLatchPos, MyLatch, PG_USED_FOR_ASSERTS_ONLY, PGINVALID_SOCKET, TopMemoryContext, WL_EXIT_ON_PM_DEATH, and WL_LATCH_SET.

Referenced by InitPostmasterChild(), and InitStandaloneProcess().

291 {
292  int latch_pos PG_USED_FOR_ASSERTS_ONLY;
293 
294  Assert(LatchWaitSet == NULL);
295 
296  /* Set up the WaitEventSet used by WaitLatch(). */
299  MyLatch, NULL);
300  if (IsUnderPostmaster)
302  PGINVALID_SOCKET, NULL, NULL);
303 
304  Assert(latch_pos == LatchWaitSetLatchPos);
305 }
int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
Definition: latch.c:862
WaitEventSet * CreateWaitEventSet(MemoryContext context, int nevents)
Definition: latch.c:684
#define LatchWaitSetLatchPos
Definition: latch.c:143
bool IsUnderPostmaster
Definition: globals.c:112
MemoryContext TopMemoryContext
Definition: mcxt.c:48
#define PGINVALID_SOCKET
Definition: port.h:33
#define Assert(condition)
Definition: c.h:804
struct Latch * MyLatch
Definition: globals.c:57
static WaitEventSet * LatchWaitSet
Definition: latch.c:140
#define WL_LATCH_SET
Definition: latch.h:125
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:155

◆ InitLatch()

void InitLatch ( Latch latch)

Definition at line 338 of file latch.c.

References Assert, elog, ERROR, Latch::is_set, Latch::is_shared, Latch::maybe_sleeping, MyProcPid, and Latch::owner_pid.

Referenced by InitPostmasterChild(), and InitStandaloneProcess().

339 {
340  latch->is_set = false;
341  latch->maybe_sleeping = false;
342  latch->owner_pid = MyProcPid;
343  latch->is_shared = false;
344 
345 #if defined(WAIT_USE_POLL)
346  /* Assert InitializeLatchSupport has been called in this process */
347  Assert(selfpipe_readfd >= 0 && selfpipe_owner_pid == MyProcPid);
348 #elif defined(WAIT_USE_WIN32)
349  latch->event = CreateEvent(NULL, TRUE, FALSE, NULL);
350  if (latch->event == NULL)
351  elog(ERROR, "CreateEvent failed: error code %lu", GetLastError());
352 #endif /* WIN32 */
353 }
int MyProcPid
Definition: globals.c:43
bool is_shared
Definition: latch.h:114
#define ERROR
Definition: elog.h:46
sig_atomic_t maybe_sleeping
Definition: latch.h:113
#define Assert(condition)
Definition: c.h:804
int owner_pid
Definition: latch.h:115
sig_atomic_t is_set
Definition: latch.h:112
#define elog(elevel,...)
Definition: elog.h:232

◆ InitSharedLatch()

void InitSharedLatch ( Latch latch)

Definition at line 371 of file latch.c.

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

Referenced by InitProcGlobal(), and XLOGShmemInit().

372 {
373 #ifdef WIN32
374  SECURITY_ATTRIBUTES sa;
375 
376  /*
377  * Set up security attributes to specify that the events are inherited.
378  */
379  ZeroMemory(&sa, sizeof(sa));
380  sa.nLength = sizeof(sa);
381  sa.bInheritHandle = TRUE;
382 
383  latch->event = CreateEvent(&sa, TRUE, FALSE, NULL);
384  if (latch->event == NULL)
385  elog(ERROR, "CreateEvent failed: error code %lu", GetLastError());
386 #endif
387 
388  latch->is_set = false;
389  latch->maybe_sleeping = false;
390  latch->owner_pid = 0;
391  latch->is_shared = true;
392 }
bool is_shared
Definition: latch.h:114
#define ERROR
Definition: elog.h:46
sig_atomic_t maybe_sleeping
Definition: latch.h:113
int owner_pid
Definition: latch.h:115
sig_atomic_t is_set
Definition: latch.h:112
#define elog(elevel,...)
Definition: elog.h:232

◆ ModifyWaitEvent()

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

Definition at line 948 of file latch.c.

References generate_unaccent_rules::action, Assert, elog, ereport, errcode_for_socket_access(), errmsg(), ERROR, WaitEventSet::events, WaitEvent::events, WaitEvent::fd, WaitEventSet::latch, MyProcPid, WaitEventSet::nevents, Latch::owner_pid, PGINVALID_SOCKET, PostmasterIsAlive, PostmasterPid, WL_LATCH_SET, WL_POSTMASTER_DEATH, WL_SOCKET_CONNECTED, WL_SOCKET_READABLE, and WL_SOCKET_WRITEABLE.

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

949 {
950  WaitEvent *event;
951 #if defined(WAIT_USE_KQUEUE)
952  int old_events;
953 #endif
954 
955  Assert(pos < set->nevents);
956 
957  event = &set->events[pos];
958 #if defined(WAIT_USE_KQUEUE)
959  old_events = event->events;
960 #endif
961 
962  /*
963  * If neither the event mask nor the associated latch changes, return
964  * early. That's an important optimization for some sockets, where
965  * ModifyWaitEvent is frequently used to switch from waiting for reads to
966  * waiting on writes.
967  */
968  if (events == event->events &&
969  (!(event->events & WL_LATCH_SET) || set->latch == latch))
970  return;
971 
972  if (event->events & WL_LATCH_SET &&
973  events != event->events)
974  {
975  elog(ERROR, "cannot modify latch event");
976  }
977 
978  if (event->events & WL_POSTMASTER_DEATH)
979  {
980  elog(ERROR, "cannot modify postmaster death event");
981  }
982 
983  /* FIXME: validate event mask */
984  event->events = events;
985 
986  if (events == WL_LATCH_SET)
987  {
988  if (latch && latch->owner_pid != MyProcPid)
989  elog(ERROR, "cannot wait on a latch owned by another process");
990  set->latch = latch;
991 
992  /*
993  * On Unix, we don't need to modify the kernel object because the
994  * underlying pipe (if there is one) is the same for all latches so we
995  * can return immediately. On Windows, we need to update our array of
996  * handles, but we leave the old one in place and tolerate spurious
997  * wakeups if the latch is disabled.
998  */
999 #if defined(WAIT_USE_WIN32)
1000  if (!latch)
1001  return;
1002 #else
1003  return;
1004 #endif
1005  }
1006 
1007 #if defined(WAIT_USE_EPOLL)
1008  WaitEventAdjustEpoll(set, event, EPOLL_CTL_MOD);
1009 #elif defined(WAIT_USE_KQUEUE)
1010  WaitEventAdjustKqueue(set, event, old_events);
1011 #elif defined(WAIT_USE_POLL)
1012  WaitEventAdjustPoll(set, event);
1013 #elif defined(WAIT_USE_WIN32)
1014  WaitEventAdjustWin32(set, event);
1015 #endif
1016 }
int MyProcPid
Definition: globals.c:43
#define ERROR
Definition: elog.h:46
uint32 events
Definition: latch.h:145
#define WL_POSTMASTER_DEATH
Definition: latch.h:129
#define Assert(condition)
Definition: c.h:804
int owner_pid
Definition: latch.h:115
#define elog(elevel,...)
Definition: elog.h:232
Latch * latch
Definition: latch.c:106
#define WL_LATCH_SET
Definition: latch.h:125

◆ OwnLatch()

void OwnLatch ( Latch latch)

Definition at line 404 of file latch.c.

References Assert, elog, ERROR, Latch::is_shared, MyProcPid, and Latch::owner_pid.

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

405 {
406  /* Sanity checks */
407  Assert(latch->is_shared);
408 
409 #if defined(WAIT_USE_POLL)
410  /* Assert InitializeLatchSupport has been called in this process */
411  Assert(selfpipe_readfd >= 0 && selfpipe_owner_pid == MyProcPid);
412 #endif
413 
414  if (latch->owner_pid != 0)
415  elog(ERROR, "latch already owned");
416 
417  latch->owner_pid = MyProcPid;
418 }
int MyProcPid
Definition: globals.c:43
bool is_shared
Definition: latch.h:114
#define ERROR
Definition: elog.h:46
#define Assert(condition)
Definition: c.h:804
int owner_pid
Definition: latch.h:115
#define elog(elevel,...)
Definition: elog.h:232

◆ ResetLatch()

void ResetLatch ( Latch latch)

Definition at line 660 of file latch.c.

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

Referenced by ApplyLauncherMain(), autoprewarm_main(), AutoVacLauncherMain(), BackgroundWriterMain(), CheckpointerMain(), ConditionVariableTimedSleep(), copy_read_data(), do_pg_stop_backup(), gather_readnext(), initialize_worker_spi(), lazy_truncate_heap(), libpqrcv_connect(), libpqrcv_PQgetResult(), logicalrep_worker_stop(), LogicalRepApplyLoop(), mq_putmessage(), pg_promote(), pg_sleep(), pg_wait_until_termination(), pgarch_MainLoop(), pgfdw_get_cleanup_result(), pgfdw_get_result(), PgstatCollectorMain(), ProcSleep(), ProcWaitForSignal(), recoveryApplyDelay(), secure_read(), secure_write(), shm_mq_receive_bytes(), shm_mq_send_bytes(), shm_mq_wait_internal(), SyncRepWaitForLSN(), SysLoggerMain(), test_shm_mq_pipelined(), throttle(), vacuum_delay_point(), wait_for_relation_state_change(), wait_for_worker_state_change(), wait_for_workers_to_become_ready(), WaitForBackgroundWorkerShutdown(), WaitForBackgroundWorkerStartup(), WaitForParallelWorkersToAttach(), WaitForParallelWorkersToFinish(), WaitForReplicationWorkerAttach(), WaitForWALToBecomeAvailable(), WalRcvWaitForStartPosition(), WalReceiverMain(), WalSndLoop(), WalSndWaitForWal(), WalSndWriteData(), and WalWriterMain().

661 {
662  /* Only the owner should reset the latch */
663  Assert(latch->owner_pid == MyProcPid);
664  Assert(latch->maybe_sleeping == false);
665 
666  latch->is_set = false;
667 
668  /*
669  * Ensure that the write to is_set gets flushed to main memory before we
670  * examine any flag variables. Otherwise a concurrent SetLatch might
671  * falsely conclude that it needn't signal us, even though we have missed
672  * seeing some flag updates that SetLatch was supposed to inform us of.
673  */
675 }
int MyProcPid
Definition: globals.c:43
sig_atomic_t maybe_sleeping
Definition: latch.h:113
#define pg_memory_barrier()
Definition: atomics.h:145
#define Assert(condition)
Definition: c.h:804
int owner_pid
Definition: latch.h:115
sig_atomic_t is_set
Definition: latch.h:112

◆ SetLatch()

void SetLatch ( Latch latch)

Definition at line 567 of file latch.c.

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

Referenced by avl_sigusr2_handler(), CheckDeadLockAlert(), ClientCheckTimeoutHandler(), ConditionVariableBroadcast(), ConditionVariableSignal(), die(), ForwardSyncRequest(), handle_sig_alarm(), HandleCatchupInterrupt(), HandleNotifyInterrupt(), HandleParallelMessageInterrupt(), IdleInTransactionSessionTimeoutHandler(), IdleSessionTimeoutHandler(), logicalrep_worker_wakeup_ptr(), pgarch_waken_stop(), PgArchWakeup(), ProcessClientReadInterrupt(), ProcessClientWriteInterrupt(), ProcSendSignal(), procsignal_sigusr1_handler(), ProcWakeup(), RecoveryConflictInterrupt(), ReqCheckpointHandler(), RequestXLogStreaming(), 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(), WakeupRecovery(), WalRcvForceReply(), WalSndLastCycleHandler(), WalSndWaitForWal(), WalSndWakeup(), WalSndWriteData(), write_syslogger_file(), and XLogSetAsyncXactLSN().

568 {
569 #ifndef WIN32
570  pid_t owner_pid;
571 #else
572  HANDLE handle;
573 #endif
574 
575  /*
576  * The memory barrier has to be placed here to ensure that any flag
577  * variables possibly changed by this process have been flushed to main
578  * memory, before we check/set is_set.
579  */
581 
582  /* Quick exit if already set */
583  if (latch->is_set)
584  return;
585 
586  latch->is_set = true;
587 
589  if (!latch->maybe_sleeping)
590  return;
591 
592 #ifndef WIN32
593 
594  /*
595  * See if anyone's waiting for the latch. It can be the current process if
596  * we're in a signal handler. We use the self-pipe or SIGURG to ourselves
597  * to wake up WaitEventSetWaitBlock() without races in that case. If it's
598  * another process, send a signal.
599  *
600  * Fetch owner_pid only once, in case the latch is concurrently getting
601  * owned or disowned. XXX: This assumes that pid_t is atomic, which isn't
602  * guaranteed to be true! In practice, the effective range of pid_t fits
603  * in a 32 bit integer, and so should be atomic. In the worst case, we
604  * might end up signaling the wrong process. Even then, you're very
605  * unlucky if a process with that bogus pid exists and belongs to
606  * Postgres; and PG database processes should handle excess SIGUSR1
607  * interrupts without a problem anyhow.
608  *
609  * Another sort of race condition that's possible here is for a new
610  * process to own the latch immediately after we look, so we don't signal
611  * it. This is okay so long as all callers of ResetLatch/WaitLatch follow
612  * the standard coding convention of waiting at the bottom of their loops,
613  * not the top, so that they'll correctly process latch-setting events
614  * that happen before they enter the loop.
615  */
616  owner_pid = latch->owner_pid;
617  if (owner_pid == 0)
618  return;
619  else if (owner_pid == MyProcPid)
620  {
621 #if defined(WAIT_USE_POLL)
622  if (waiting)
623  sendSelfPipeByte();
624 #else
625  if (waiting)
626  kill(MyProcPid, SIGURG);
627 #endif
628  }
629  else
630  kill(owner_pid, SIGURG);
631 
632 #else
633 
634  /*
635  * See if anyone's waiting for the latch. It can be the current process if
636  * we're in a signal handler.
637  *
638  * Use a local variable here just in case somebody changes the event field
639  * concurrently (which really should not happen).
640  */
641  handle = latch->event;
642  if (handle)
643  {
644  SetEvent(handle);
645 
646  /*
647  * Note that we silently ignore any errors. We might be in a signal
648  * handler or other critical path where it's not safe to call elog().
649  */
650  }
651 #endif
652 
653 }
int MyProcPid
Definition: globals.c:43
#define kill(pid, sig)
Definition: win32_port.h:454
sig_atomic_t maybe_sleeping
Definition: latch.h:113
#define pg_memory_barrier()
Definition: atomics.h:145
int owner_pid
Definition: latch.h:115
sig_atomic_t is_set
Definition: latch.h:112
static volatile sig_atomic_t waiting
Definition: latch.c:147

◆ ShutdownLatchSupport()

void ShutdownLatchSupport ( void  )

Definition at line 308 of file latch.c.

References close, FreeWaitEventSet(), InvalidPid, pqsignal(), and SIG_IGN.

Referenced by StartBackgroundWorker().

309 {
310 #if defined(WAIT_USE_POLL)
311  pqsignal(SIGURG, SIG_IGN);
312 #endif
313 
314  if (LatchWaitSet)
315  {
317  LatchWaitSet = NULL;
318  }
319 
320 #if defined(WAIT_USE_POLL)
321  close(selfpipe_readfd);
322  close(selfpipe_writefd);
323  selfpipe_readfd = -1;
324  selfpipe_writefd = -1;
325  selfpipe_owner_pid = InvalidPid;
326 #endif
327 
328 #if defined(WAIT_USE_EPOLL)
329  close(signal_fd);
330  signal_fd = -1;
331 #endif
332 }
void FreeWaitEventSet(WaitEventSet *set)
Definition: latch.c:798
#define SIG_IGN
Definition: win32_port.h:156
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
static WaitEventSet * LatchWaitSet
Definition: latch.c:140
#define close(a)
Definition: win32.h:12
#define InvalidPid
Definition: miscadmin.h:32

◆ WaitEventSetWait()

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

Definition at line 1306 of file latch.c.

References Assert, buf, EINTR, elog, ereport, errcode_for_socket_access(), errmsg(), ERROR, WaitEvent::events, WaitEvent::fd, INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SUBTRACT, WaitEventSet::nevents, pg_memory_barrier, PGINVALID_SOCKET, pgstat_report_wait_end(), pgstat_report_wait_start(), pgwin32_dispatch_queued_signals(), WaitEvent::pos, PostmasterIsAliveInternal(), proc_exit(), start_time, unlikely, WaitEvent::user_data, WaitEventSetWaitBlock(), waiting, WL_LATCH_SET, WL_POSTMASTER_DEATH, WL_SOCKET_CONNECTED, WL_SOCKET_MASK, WL_SOCKET_READABLE, and WL_SOCKET_WRITEABLE.

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

1309 {
1310  int returned_events = 0;
1312  instr_time cur_time;
1313  long cur_timeout = -1;
1314 
1315  Assert(nevents > 0);
1316 
1317  /*
1318  * Initialize timeout if requested. We must record the current time so
1319  * that we can determine the remaining timeout if interrupted.
1320  */
1321  if (timeout >= 0)
1322  {
1323  INSTR_TIME_SET_CURRENT(start_time);
1324  Assert(timeout >= 0 && timeout <= INT_MAX);
1325  cur_timeout = timeout;
1326  }
1327 
1328  pgstat_report_wait_start(wait_event_info);
1329 
1330 #ifndef WIN32
1331  waiting = true;
1332 #else
1333  /* Ensure that signals are serviced even if latch is already set */
1335 #endif
1336  while (returned_events == 0)
1337  {
1338  int rc;
1339 
1340  /*
1341  * Check if the latch is set already. If so, leave the loop
1342  * immediately, avoid blocking again. We don't attempt to report any
1343  * other events that might also be satisfied.
1344  *
1345  * If someone sets the latch between this and the
1346  * WaitEventSetWaitBlock() below, the setter will write a byte to the
1347  * pipe (or signal us and the signal handler will do that), and the
1348  * readiness routine will return immediately.
1349  *
1350  * On unix, If there's a pending byte in the self pipe, we'll notice
1351  * whenever blocking. Only clearing the pipe in that case avoids
1352  * having to drain it every time WaitLatchOrSocket() is used. Should
1353  * the pipe-buffer fill up we're still ok, because the pipe is in
1354  * nonblocking mode. It's unlikely for that to happen, because the
1355  * self pipe isn't filled unless we're blocking (waiting = true), or
1356  * from inside a signal handler in latch_sigurg_handler().
1357  *
1358  * On windows, we'll also notice if there's a pending event for the
1359  * latch when blocking, but there's no danger of anything filling up,
1360  * as "Setting an event that is already set has no effect.".
1361  *
1362  * Note: we assume that the kernel calls involved in latch management
1363  * will provide adequate synchronization on machines with weak memory
1364  * ordering, so that we cannot miss seeing is_set if a notification
1365  * has already been queued.
1366  */
1367  if (set->latch && !set->latch->is_set)
1368  {
1369  /* about to sleep on a latch */
1370  set->latch->maybe_sleeping = true;
1372  /* and recheck */
1373  }
1374 
1375  if (set->latch && set->latch->is_set)
1376  {
1377  occurred_events->fd = PGINVALID_SOCKET;
1378  occurred_events->pos = set->latch_pos;
1379  occurred_events->user_data =
1380  set->events[set->latch_pos].user_data;
1381  occurred_events->events = WL_LATCH_SET;
1382  occurred_events++;
1383  returned_events++;
1384 
1385  /* could have been set above */
1386  set->latch->maybe_sleeping = false;
1387 
1388  break;
1389  }
1390 
1391  /*
1392  * Wait for events using the readiness primitive chosen at the top of
1393  * this file. If -1 is returned, a timeout has occurred, if 0 we have
1394  * to retry, everything >= 1 is the number of returned events.
1395  */
1396  rc = WaitEventSetWaitBlock(set, cur_timeout,
1397  occurred_events, nevents);
1398 
1399  if (set->latch)
1400  {
1401  Assert(set->latch->maybe_sleeping);
1402  set->latch->maybe_sleeping = false;
1403  }
1404 
1405  if (rc == -1)
1406  break; /* timeout occurred */
1407  else
1408  returned_events = rc;
1409 
1410  /* If we're not done, update cur_timeout for next iteration */
1411  if (returned_events == 0 && timeout >= 0)
1412  {
1413  INSTR_TIME_SET_CURRENT(cur_time);
1414  INSTR_TIME_SUBTRACT(cur_time, start_time);
1415  cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
1416  if (cur_timeout <= 0)
1417  break;
1418  }
1419  }
1420 #ifndef WIN32
1421  waiting = false;
1422 #endif
1423 
1425 
1426  return returned_events;
1427 }
pgsocket fd
Definition: latch.h:146
static void pgstat_report_wait_end(void)
Definition: wait_event.h:278
int pos
Definition: latch.h:144
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:202
struct timeval instr_time
Definition: instr_time.h:150
static time_t start_time
Definition: pg_ctl.c:99
void pgwin32_dispatch_queued_signals(void)
Definition: signal.c:108
sig_atomic_t maybe_sleeping
Definition: latch.h:113
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
uint32 events
Definition: latch.h:145
static int WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout, WaitEvent *occurred_events, int nevents)
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:262
#define PGINVALID_SOCKET
Definition: port.h:33
#define pg_memory_barrier()
Definition: atomics.h:145
#define Assert(condition)
Definition: c.h:804
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
void * user_data
Definition: latch.h:147
sig_atomic_t is_set
Definition: latch.h:112
Latch * latch
Definition: latch.c:106
#define WL_LATCH_SET
Definition: latch.h:125
static volatile sig_atomic_t waiting
Definition: latch.c:147

◆ WaitEventSetWaitBlock()

static int WaitEventSetWaitBlock ( WaitEventSet set,
int  cur_timeout,
WaitEvent occurred_events,
int  nevents 
)
inlinestatic

Referenced by WaitEventSetWait().

◆ WaitLatch()

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

Definition at line 452 of file latch.c.

References Assert, WaitEventSet::exit_on_postmaster_death, IsUnderPostmaster, 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(), ConditionVariableTimedSleep(), do_pg_stop_backup(), gather_readnext(), initialize_worker_spi(), lazy_truncate_heap(), logicalrep_worker_stop(), mq_putmessage(), pg_promote(), pg_sleep(), pg_wait_until_termination(), pgarch_MainLoop(), ProcSleep(), ProcWaitForSignal(), recoveryApplyDelay(), shm_mq_receive_bytes(), shm_mq_send_bytes(), shm_mq_wait_internal(), SyncRepWaitForLSN(), test_shm_mq_pipelined(), throttle(), vacuum_delay_point(), wait_for_relation_state_change(), wait_for_worker_state_change(), wait_for_workers_to_become_ready(), WaitForBackgroundWorkerShutdown(), WaitForBackgroundWorkerStartup(), WaitForParallelWorkersToAttach(), WaitForParallelWorkersToFinish(), WaitForReplicationWorkerAttach(), WaitForWALToBecomeAvailable(), WalRcvWaitForStartPosition(), and WalWriterMain().

454 {
455  WaitEvent event;
456 
457  /* Postmaster-managed callers must handle postmaster death somehow. */
459  (wakeEvents & WL_EXIT_ON_PM_DEATH) ||
460  (wakeEvents & WL_POSTMASTER_DEATH));
461 
462  /*
463  * Some callers may have a latch other than MyLatch, or no latch at all,
464  * or want to handle postmaster death differently. It's cheap to assign
465  * those, so just do it every time.
466  */
467  if (!(wakeEvents & WL_LATCH_SET))
468  latch = NULL;
469  ModifyWaitEvent(LatchWaitSet, LatchWaitSetLatchPos, WL_LATCH_SET, latch);
471  ((wakeEvents & WL_EXIT_ON_PM_DEATH) != 0);
472 
474  (wakeEvents & WL_TIMEOUT) ? timeout : -1,
475  &event, 1,
476  wait_event_info) == 0)
477  return WL_TIMEOUT;
478  else
479  return event.events;
480 }
#define WL_TIMEOUT
Definition: latch.h:128
void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
Definition: latch.c:948
#define LatchWaitSetLatchPos
Definition: latch.c:143
bool exit_on_postmaster_death
Definition: latch.c:114
bool IsUnderPostmaster
Definition: globals.c:112
#define WL_POSTMASTER_DEATH
Definition: latch.h:129
#define Assert(condition)
Definition: c.h:804
static WaitEventSet * LatchWaitSet
Definition: latch.c:140
#define WL_LATCH_SET
Definition: latch.h:125
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
Definition: latch.c:1306

◆ WaitLatchOrSocket()

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

Definition at line 500 of file latch.c.

References AddWaitEventToSet(), Assert, CreateWaitEventSet(), CurrentMemoryContext, 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(), LogicalRepApplyLoop(), pgfdw_get_cleanup_result(), pgfdw_get_result(), read_or_wait(), secure_open_gssapi(), and WalReceiverMain().

502 {
503  int ret = 0;
504  int rc;
505  WaitEvent event;
507 
508  if (wakeEvents & WL_TIMEOUT)
509  Assert(timeout >= 0);
510  else
511  timeout = -1;
512 
513  if (wakeEvents & WL_LATCH_SET)
514  AddWaitEventToSet(set, WL_LATCH_SET, PGINVALID_SOCKET,
515  latch, NULL);
516 
517  /* Postmaster-managed callers must handle postmaster death somehow. */
519  (wakeEvents & WL_EXIT_ON_PM_DEATH) ||
520  (wakeEvents & WL_POSTMASTER_DEATH));
521 
522  if ((wakeEvents & WL_POSTMASTER_DEATH) && IsUnderPostmaster)
523  AddWaitEventToSet(set, WL_POSTMASTER_DEATH, PGINVALID_SOCKET,
524  NULL, NULL);
525 
526  if ((wakeEvents & WL_EXIT_ON_PM_DEATH) && IsUnderPostmaster)
527  AddWaitEventToSet(set, WL_EXIT_ON_PM_DEATH, PGINVALID_SOCKET,
528  NULL, NULL);
529 
530  if (wakeEvents & WL_SOCKET_MASK)
531  {
532  int ev;
533 
534  ev = wakeEvents & WL_SOCKET_MASK;
535  AddWaitEventToSet(set, ev, sock, NULL, NULL);
536  }
537 
538  rc = WaitEventSetWait(set, timeout, &event, 1, wait_event_info);
539 
540  if (rc == 0)
541  ret |= WL_TIMEOUT;
542  else
543  {
544  ret |= event.events & (WL_LATCH_SET |
545  WL_POSTMASTER_DEATH |
547  }
548 
549  FreeWaitEventSet(set);
550 
551  return ret;
552 }
void FreeWaitEventSet(WaitEventSet *set)
Definition: latch.c:798
#define WL_TIMEOUT
Definition: latch.h:128
int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
Definition: latch.c:862
#define WL_SOCKET_MASK
Definition: latch.h:138
WaitEventSet * CreateWaitEventSet(MemoryContext context, int nevents)
Definition: latch.c:684
bool IsUnderPostmaster
Definition: globals.c:112
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
#define WL_POSTMASTER_DEATH
Definition: latch.h:129
#define PGINVALID_SOCKET
Definition: port.h:33
#define Assert(condition)
Definition: c.h:804
#define WL_LATCH_SET
Definition: latch.h:125
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
Definition: latch.c:1306

Variable Documentation

◆ LatchWaitSet

WaitEventSet* LatchWaitSet
static

Definition at line 140 of file latch.c.

◆ waiting

volatile sig_atomic_t waiting = false
static