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 <poll.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 WAIT_USE_POLL
 
#define WAIT_USE_SELF_PIPE
 
#define LatchWaitSetLatchPos   0
 

Functions

static void latch_sigurg_handler (SIGNAL_ARGS)
 
static void sendSelfPipeByte (void)
 
static void drain (void)
 
static void WaitEventAdjustPoll (WaitEventSet *set, WaitEvent *event)
 
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)
 
bool WaitEventSetCanReportClosed (void)
 
int GetNumRegisteredWaitEvents (WaitEventSet *set)
 

Variables

static WaitEventSetLatchWaitSet
 
static volatile sig_atomic_t waiting = false
 
static int selfpipe_readfd = -1
 
static int selfpipe_writefd = -1
 
static int selfpipe_owner_pid = 0
 

Macro Definition Documentation

◆ LatchWaitSetLatchPos

#define LatchWaitSetLatchPos   0

Definition at line 158 of file latch.c.

◆ WAIT_USE_POLL

#define WAIT_USE_POLL

Definition at line 80 of file latch.c.

◆ WAIT_USE_SELF_PIPE

#define WAIT_USE_SELF_PIPE

Definition at line 99 of file latch.c.

Function Documentation

◆ AddWaitEventToSet()

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

Definition at line 886 of file latch.c.

888 {
889  WaitEvent *event;
890 
891  /* not enough space */
892  Assert(set->nevents < set->nevents_space);
893 
894  if (events == WL_EXIT_ON_PM_DEATH)
895  {
896  events = WL_POSTMASTER_DEATH;
897  set->exit_on_postmaster_death = true;
898  }
899 
900  if (latch)
901  {
902  if (latch->owner_pid != MyProcPid)
903  elog(ERROR, "cannot wait on a latch owned by another process");
904  if (set->latch)
905  elog(ERROR, "cannot wait on more than one latch");
906  if ((events & WL_LATCH_SET) != WL_LATCH_SET)
907  elog(ERROR, "latch events only support being set");
908  }
909  else
910  {
911  if (events & WL_LATCH_SET)
912  elog(ERROR, "cannot wait on latch without a specified latch");
913  }
914 
915  /* waiting for socket readiness without a socket indicates a bug */
916  if (fd == PGINVALID_SOCKET && (events & WL_SOCKET_MASK))
917  elog(ERROR, "cannot wait on socket event without a socket");
918 
919  event = &set->events[set->nevents];
920  event->pos = set->nevents++;
921  event->fd = fd;
922  event->events = events;
923  event->user_data = user_data;
924 #ifdef WIN32
925  event->reset = false;
926 #endif
927 
928  if (events == WL_LATCH_SET)
929  {
930  set->latch = latch;
931  set->latch_pos = event->pos;
932 #if defined(WAIT_USE_SELF_PIPE)
933  event->fd = selfpipe_readfd;
934 #elif defined(WAIT_USE_SIGNALFD)
935  event->fd = signal_fd;
936 #else
937  event->fd = PGINVALID_SOCKET;
938 #ifdef WAIT_USE_EPOLL
939  return event->pos;
940 #endif
941 #endif
942  }
943  else if (events == WL_POSTMASTER_DEATH)
944  {
945 #ifndef WIN32
947 #endif
948  }
949 
950  /* perform wait primitive specific initialization, if needed */
951 #if defined(WAIT_USE_EPOLL)
952  WaitEventAdjustEpoll(set, event, EPOLL_CTL_ADD);
953 #elif defined(WAIT_USE_KQUEUE)
954  WaitEventAdjustKqueue(set, event, 0);
955 #elif defined(WAIT_USE_POLL)
956  WaitEventAdjustPoll(set, event);
957 #elif defined(WAIT_USE_WIN32)
958  WaitEventAdjustWin32(set, event);
959 #endif
960 
961  return event->pos;
962 }
#define ERROR
Definition: elog.h:35
int MyProcPid
Definition: globals.c:44
static int selfpipe_readfd
Definition: latch.c:172
static void WaitEventAdjustPoll(WaitEventSet *set, WaitEvent *event)
Definition: latch.c:1099
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130
#define WL_LATCH_SET
Definition: latch.h:125
#define WL_POSTMASTER_DEATH
Definition: latch.h:129
#define WL_SOCKET_MASK
Definition: latch.h:138
Assert(fmt[strlen(fmt) - 1] !='\n')
#define PGINVALID_SOCKET
Definition: port.h:31
int postmaster_alive_fds[2]
Definition: postmaster.c:553
#define POSTMASTER_FD_WATCH
Definition: postmaster.h:45
static int fd(const char *x, int i)
Definition: preproc-init.c:105
int owner_pid
Definition: latch.h:115
Latch * latch
Definition: latch.c:121
bool exit_on_postmaster_death
Definition: latch.c:129
int nevents
Definition: latch.c:106
int latch_pos
Definition: latch.c:122
int nevents_space
Definition: latch.c:107
WaitEvent * events
Definition: latch.c:113
int pos
Definition: latch.h:145

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 ExecAppendAsyncEventWait(), InitializeLatchWaitSet(), postgresForeignAsyncConfigureWait(), pq_init(), SysLoggerMain(), and WaitLatchOrSocket().

◆ CreateWaitEventSet()

WaitEventSet* CreateWaitEventSet ( MemoryContext  context,
int  nevents 
)

Definition at line 707 of file latch.c.

708 {
709  WaitEventSet *set;
710  char *data;
711  Size sz = 0;
712 
713  /*
714  * Use MAXALIGN size/alignment to guarantee that later uses of memory are
715  * aligned correctly. E.g. epoll_event might need 8 byte alignment on some
716  * platforms, but earlier allocations like WaitEventSet and WaitEvent
717  * might not be sized to guarantee that when purely using sizeof().
718  */
719  sz += MAXALIGN(sizeof(WaitEventSet));
720  sz += MAXALIGN(sizeof(WaitEvent) * nevents);
721 
722 #if defined(WAIT_USE_EPOLL)
723  sz += MAXALIGN(sizeof(struct epoll_event) * nevents);
724 #elif defined(WAIT_USE_KQUEUE)
725  sz += MAXALIGN(sizeof(struct kevent) * nevents);
726 #elif defined(WAIT_USE_POLL)
727  sz += MAXALIGN(sizeof(struct pollfd) * nevents);
728 #elif defined(WAIT_USE_WIN32)
729  /* need space for the pgwin32_signal_event */
730  sz += MAXALIGN(sizeof(HANDLE) * (nevents + 1));
731 #endif
732 
733  data = (char *) MemoryContextAllocZero(context, sz);
734 
735  set = (WaitEventSet *) data;
736  data += MAXALIGN(sizeof(WaitEventSet));
737 
738  set->events = (WaitEvent *) data;
739  data += MAXALIGN(sizeof(WaitEvent) * nevents);
740 
741 #if defined(WAIT_USE_EPOLL)
742  set->epoll_ret_events = (struct epoll_event *) data;
743  data += MAXALIGN(sizeof(struct epoll_event) * nevents);
744 #elif defined(WAIT_USE_KQUEUE)
745  set->kqueue_ret_events = (struct kevent *) data;
746  data += MAXALIGN(sizeof(struct kevent) * nevents);
747 #elif defined(WAIT_USE_POLL)
748  set->pollfds = (struct pollfd *) data;
749  data += MAXALIGN(sizeof(struct pollfd) * nevents);
750 #elif defined(WAIT_USE_WIN32)
751  set->handles = (HANDLE) data;
752  data += MAXALIGN(sizeof(HANDLE) * nevents);
753 #endif
754 
755  set->latch = NULL;
756  set->nevents_space = nevents;
757  set->exit_on_postmaster_death = false;
758 
759 #if defined(WAIT_USE_EPOLL)
760  if (!AcquireExternalFD())
761  {
762  /* treat this as though epoll_create1 itself returned EMFILE */
763  elog(ERROR, "epoll_create1 failed: %m");
764  }
765  set->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
766  if (set->epoll_fd < 0)
767  {
769  elog(ERROR, "epoll_create1 failed: %m");
770  }
771 #elif defined(WAIT_USE_KQUEUE)
772  if (!AcquireExternalFD())
773  {
774  /* treat this as though kqueue itself returned EMFILE */
775  elog(ERROR, "kqueue failed: %m");
776  }
777  set->kqueue_fd = kqueue();
778  if (set->kqueue_fd < 0)
779  {
781  elog(ERROR, "kqueue failed: %m");
782  }
783  if (fcntl(set->kqueue_fd, F_SETFD, FD_CLOEXEC) == -1)
784  {
785  int save_errno = errno;
786 
787  close(set->kqueue_fd);
789  errno = save_errno;
790  elog(ERROR, "fcntl(F_SETFD) failed on kqueue descriptor: %m");
791  }
792  set->report_postmaster_not_running = false;
793 #elif defined(WAIT_USE_WIN32)
794 
795  /*
796  * To handle signals while waiting, we need to add a win32 specific event.
797  * We accounted for the additional event at the top of this routine. See
798  * port/win32/signal.c for more details.
799  *
800  * Note: pgwin32_signal_event should be first to ensure that it will be
801  * reported when multiple events are set. We want to guarantee that
802  * pending signals are serviced.
803  */
804  set->handles[0] = pgwin32_signal_event;
805  StaticAssertStmt(WSA_INVALID_EVENT == NULL, "");
806 #endif
807 
808  return set;
809 }
#define MAXALIGN(LEN)
Definition: c.h:747
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:869
size_t Size
Definition: c.h:541
void ReleaseExternalFD(void)
Definition: fd.c:1152
bool AcquireExternalFD(void)
Definition: fd.c:1099
#define close(a)
Definition: win32.h:12
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1037
const void * data
HANDLE pgwin32_signal_event
Definition: signal.c:27
struct pollfd * pollfds
Definition: latch.c:142

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

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

◆ DisownLatch()

void DisownLatch ( Latch latch)

Definition at line 448 of file latch.c.

449 {
450  Assert(latch->is_shared);
451  Assert(latch->owner_pid == MyProcPid);
452 
453  latch->owner_pid = 0;
454 }
bool is_shared
Definition: latch.h:114

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

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

◆ drain()

static void drain ( void  )
static

Definition at line 2172 of file latch.c.

2173 {
2174  char buf[1024];
2175  int rc;
2176  int fd;
2177 
2178 #ifdef WAIT_USE_SELF_PIPE
2179  fd = selfpipe_readfd;
2180 #else
2181  fd = signal_fd;
2182 #endif
2183 
2184  for (;;)
2185  {
2186  rc = read(fd, buf, sizeof(buf));
2187  if (rc < 0)
2188  {
2189  if (errno == EAGAIN || errno == EWOULDBLOCK)
2190  break; /* the descriptor is empty */
2191  else if (errno == EINTR)
2192  continue; /* retry */
2193  else
2194  {
2195  waiting = false;
2196 #ifdef WAIT_USE_SELF_PIPE
2197  elog(ERROR, "read() on self-pipe failed: %m");
2198 #else
2199  elog(ERROR, "read() on signalfd failed: %m");
2200 #endif
2201  }
2202  }
2203  else if (rc == 0)
2204  {
2205  waiting = false;
2206 #ifdef WAIT_USE_SELF_PIPE
2207  elog(ERROR, "unexpected EOF on self-pipe");
2208 #else
2209  elog(ERROR, "unexpected EOF on signalfd");
2210 #endif
2211  }
2212  else if (rc < sizeof(buf))
2213  {
2214  /* we successfully drained the pipe; no need to read() again */
2215  break;
2216  }
2217  /* else buffer wasn't big enough, so read again */
2218  }
2219 }
#define read(a, b, c)
Definition: win32.h:13
static volatile sig_atomic_t waiting
Definition: latch.c:162
static char * buf
Definition: pg_test_fsync.c:67
#define EINTR
Definition: win32_port.h:369
#define EWOULDBLOCK
Definition: win32_port.h:375
#define EAGAIN
Definition: win32_port.h:367

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

Referenced by WaitEventSetWaitBlock().

◆ FreeWaitEventSet()

void FreeWaitEventSet ( WaitEventSet set)

Definition at line 821 of file latch.c.

822 {
823 #if defined(WAIT_USE_EPOLL)
824  close(set->epoll_fd);
826 #elif defined(WAIT_USE_KQUEUE)
827  close(set->kqueue_fd);
829 #elif defined(WAIT_USE_WIN32)
830  WaitEvent *cur_event;
831 
832  for (cur_event = set->events;
833  cur_event < (set->events + set->nevents);
834  cur_event++)
835  {
836  if (cur_event->events & WL_LATCH_SET)
837  {
838  /* uses the latch's HANDLE */
839  }
840  else if (cur_event->events & WL_POSTMASTER_DEATH)
841  {
842  /* uses PostmasterHandle */
843  }
844  else
845  {
846  /* Clean up the event object we created for the socket */
847  WSAEventSelect(cur_event->fd, NULL, 0);
848  WSACloseEvent(set->handles[cur_event->pos + 1]);
849  }
850  }
851 #endif
852 
853  pfree(set);
854 }
void pfree(void *pointer)
Definition: mcxt.c:1306
pgsocket fd
Definition: latch.h:147
uint32 events
Definition: latch.h:146

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

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

◆ GetNumRegisteredWaitEvents()

int GetNumRegisteredWaitEvents ( WaitEventSet set)

Definition at line 2106 of file latch.c.

2107 {
2108  return set->nevents;
2109 }

References WaitEventSet::nevents.

Referenced by ExecAppendAsyncEventWait(), and postgresForeignAsyncConfigureWait().

◆ InitializeLatchSupport()

void InitializeLatchSupport ( void  )

Definition at line 207 of file latch.c.

208 {
209 #if defined(WAIT_USE_SELF_PIPE)
210  int pipefd[2];
211 
212  if (IsUnderPostmaster)
213  {
214  /*
215  * We might have inherited connections to a self-pipe created by the
216  * postmaster. It's critical that child processes create their own
217  * self-pipes, of course, and we really want them to close the
218  * inherited FDs for safety's sake.
219  */
220  if (selfpipe_owner_pid != 0)
221  {
222  /* Assert we go through here but once in a child process */
224  /* Release postmaster's pipe FDs; ignore any error */
225  (void) close(selfpipe_readfd);
226  (void) close(selfpipe_writefd);
227  /* Clean up, just for safety's sake; we'll set these below */
229  selfpipe_owner_pid = 0;
230  /* Keep fd.c's accounting straight */
233  }
234  else
235  {
236  /*
237  * Postmaster didn't create a self-pipe ... or else we're in an
238  * EXEC_BACKEND build, in which case it doesn't matter since the
239  * postmaster's pipe FDs were closed by the action of FD_CLOEXEC.
240  * fd.c won't have state to clean up, either.
241  */
242  Assert(selfpipe_readfd == -1);
243  }
244  }
245  else
246  {
247  /* In postmaster or standalone backend, assert we do this but once */
248  Assert(selfpipe_readfd == -1);
250  }
251 
252  /*
253  * Set up the self-pipe that allows a signal handler to wake up the
254  * poll()/epoll_wait() in WaitLatch. Make the write-end non-blocking, so
255  * that SetLatch won't block if the event has already been set many times
256  * filling the kernel buffer. Make the read-end non-blocking too, so that
257  * we can easily clear the pipe by reading until EAGAIN or EWOULDBLOCK.
258  * Also, make both FDs close-on-exec, since we surely do not want any
259  * child processes messing with them.
260  */
261  if (pipe(pipefd) < 0)
262  elog(FATAL, "pipe() failed: %m");
263  if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) == -1)
264  elog(FATAL, "fcntl(F_SETFL) failed on read-end of self-pipe: %m");
265  if (fcntl(pipefd[1], F_SETFL, O_NONBLOCK) == -1)
266  elog(FATAL, "fcntl(F_SETFL) failed on write-end of self-pipe: %m");
267  if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) == -1)
268  elog(FATAL, "fcntl(F_SETFD) failed on read-end of self-pipe: %m");
269  if (fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) == -1)
270  elog(FATAL, "fcntl(F_SETFD) failed on write-end of self-pipe: %m");
271 
272  selfpipe_readfd = pipefd[0];
273  selfpipe_writefd = pipefd[1];
275 
276  /* Tell fd.c about these two long-lived FDs */
279 
281 #endif
282 
283 #ifdef WAIT_USE_SIGNALFD
284  sigset_t signalfd_mask;
285 
286  /* Block SIGURG, because we'll receive it through a signalfd. */
287  sigaddset(&UnBlockSig, SIGURG);
288 
289  /* Set up the signalfd to receive SIGURG notifications. */
290  sigemptyset(&signalfd_mask);
291  sigaddset(&signalfd_mask, SIGURG);
292  signal_fd = signalfd(-1, &signalfd_mask, SFD_NONBLOCK | SFD_CLOEXEC);
293  if (signal_fd < 0)
294  elog(FATAL, "signalfd() failed");
296 #endif
297 
298 #ifdef WAIT_USE_KQUEUE
299  /* Ignore SIGURG, because we'll receive it via kqueue. */
300  pqsignal(SIGURG, SIG_IGN);
301 #endif
302 }
sigset_t UnBlockSig
Definition: pqsignal.c:22
#define FATAL
Definition: elog.h:37
void ReserveExternalFD(void)
Definition: fd.c:1134
bool IsUnderPostmaster
Definition: globals.c:113
static void latch_sigurg_handler(SIGNAL_ARGS)
Definition: latch.c:2119
static int selfpipe_owner_pid
Definition: latch.c:176
static int selfpipe_writefd
Definition: latch.c:173
pqsigfunc pqsignal(int signo, pqsigfunc func)
#define SIG_IGN
Definition: win32_port.h:173

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(), and InitStandaloneProcess().

◆ InitializeLatchWaitSet()

void InitializeLatchWaitSet ( void  )

Definition at line 305 of file latch.c.

306 {
307  int latch_pos PG_USED_FOR_ASSERTS_ONLY;
308 
309  Assert(LatchWaitSet == NULL);
310 
311  /* Set up the WaitEventSet used by WaitLatch(). */
314  MyLatch, NULL);
315  if (IsUnderPostmaster)
317  PGINVALID_SOCKET, NULL, NULL);
318 
319  Assert(latch_pos == LatchWaitSetLatchPos);
320 }
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:166
struct Latch * MyLatch
Definition: globals.c:58
#define LatchWaitSetLatchPos
Definition: latch.c:158
WaitEventSet * CreateWaitEventSet(MemoryContext context, int nevents)
Definition: latch.c:707
static WaitEventSet * LatchWaitSet
Definition: latch.c:155
int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
Definition: latch.c:886
MemoryContext TopMemoryContext
Definition: mcxt.c:130

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

Referenced by InitPostmasterChild(), and InitStandaloneProcess().

◆ InitLatch()

void InitLatch ( Latch latch)

Definition at line 353 of file latch.c.

354 {
355  latch->is_set = false;
356  latch->maybe_sleeping = false;
357  latch->owner_pid = MyProcPid;
358  latch->is_shared = false;
359 
360 #if defined(WAIT_USE_SELF_PIPE)
361  /* Assert InitializeLatchSupport has been called in this process */
363 #elif defined(WAIT_USE_SIGNALFD)
364  /* Assert InitializeLatchSupport has been called in this process */
365  Assert(signal_fd >= 0);
366 #elif defined(WAIT_USE_WIN32)
367  latch->event = CreateEvent(NULL, TRUE, FALSE, NULL);
368  if (latch->event == NULL)
369  elog(ERROR, "CreateEvent failed: error code %lu", GetLastError());
370 #endif /* WIN32 */
371 }
sig_atomic_t is_set
Definition: latch.h:112
sig_atomic_t maybe_sleeping
Definition: latch.h:113

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 InitPostmasterChild(), and InitStandaloneProcess().

◆ InitSharedLatch()

void InitSharedLatch ( Latch latch)

Definition at line 389 of file latch.c.

390 {
391 #ifdef WIN32
392  SECURITY_ATTRIBUTES sa;
393 
394  /*
395  * Set up security attributes to specify that the events are inherited.
396  */
397  ZeroMemory(&sa, sizeof(sa));
398  sa.nLength = sizeof(sa);
399  sa.bInheritHandle = TRUE;
400 
401  latch->event = CreateEvent(&sa, TRUE, FALSE, NULL);
402  if (latch->event == NULL)
403  elog(ERROR, "CreateEvent failed: error code %lu", GetLastError());
404 #endif
405 
406  latch->is_set = false;
407  latch->maybe_sleeping = false;
408  latch->owner_pid = 0;
409  latch->is_shared = true;
410 }

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

Referenced by InitProcGlobal(), and XLogRecoveryShmemInit().

◆ latch_sigurg_handler()

static void latch_sigurg_handler ( SIGNAL_ARGS  )
static

Definition at line 2119 of file latch.c.

2120 {
2121  int save_errno = errno;
2122 
2123  if (waiting)
2124  sendSelfPipeByte();
2125 
2126  errno = save_errno;
2127 }
static void sendSelfPipeByte(void)
Definition: latch.c:2131

References sendSelfPipeByte(), and waiting.

Referenced by InitializeLatchSupport().

◆ ModifyWaitEvent()

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

Definition at line 972 of file latch.c.

973 {
974  WaitEvent *event;
975 #if defined(WAIT_USE_KQUEUE)
976  int old_events;
977 #endif
978 
979  Assert(pos < set->nevents);
980 
981  event = &set->events[pos];
982 #if defined(WAIT_USE_KQUEUE)
983  old_events = event->events;
984 #endif
985 
986  /*
987  * If neither the event mask nor the associated latch changes, return
988  * early. That's an important optimization for some sockets, where
989  * ModifyWaitEvent is frequently used to switch from waiting for reads to
990  * waiting on writes.
991  */
992  if (events == event->events &&
993  (!(event->events & WL_LATCH_SET) || set->latch == latch))
994  return;
995 
996  if (event->events & WL_LATCH_SET &&
997  events != event->events)
998  {
999  elog(ERROR, "cannot modify latch event");
1000  }
1001 
1002  if (event->events & WL_POSTMASTER_DEATH)
1003  {
1004  elog(ERROR, "cannot modify postmaster death event");
1005  }
1006 
1007  /* FIXME: validate event mask */
1008  event->events = events;
1009 
1010  if (events == WL_LATCH_SET)
1011  {
1012  if (latch && latch->owner_pid != MyProcPid)
1013  elog(ERROR, "cannot wait on a latch owned by another process");
1014  set->latch = latch;
1015 
1016  /*
1017  * On Unix, we don't need to modify the kernel object because the
1018  * underlying pipe (if there is one) is the same for all latches so we
1019  * can return immediately. On Windows, we need to update our array of
1020  * handles, but we leave the old one in place and tolerate spurious
1021  * wakeups if the latch is disabled.
1022  */
1023 #if defined(WAIT_USE_WIN32)
1024  if (!latch)
1025  return;
1026 #else
1027  return;
1028 #endif
1029  }
1030 
1031 #if defined(WAIT_USE_EPOLL)
1032  WaitEventAdjustEpoll(set, event, EPOLL_CTL_MOD);
1033 #elif defined(WAIT_USE_KQUEUE)
1034  WaitEventAdjustKqueue(set, event, old_events);
1035 #elif defined(WAIT_USE_POLL)
1036  WaitEventAdjustPoll(set, event);
1037 #elif defined(WAIT_USE_WIN32)
1038  WaitEventAdjustWin32(set, event);
1039 #endif
1040 }

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 422 of file latch.c.

423 {
424  int owner_pid;
425 
426  /* Sanity checks */
427  Assert(latch->is_shared);
428 
429 #if defined(WAIT_USE_SELF_PIPE)
430  /* Assert InitializeLatchSupport has been called in this process */
432 #elif defined(WAIT_USE_SIGNALFD)
433  /* Assert InitializeLatchSupport has been called in this process */
434  Assert(signal_fd >= 0);
435 #endif
436 
437  owner_pid = latch->owner_pid;
438  if (owner_pid != 0)
439  elog(PANIC, "latch already owned by PID %d", owner_pid);
440 
441  latch->owner_pid = MyProcPid;
442 }
#define PANIC
Definition: elog.h:38

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 683 of file latch.c.

684 {
685  /* Only the owner should reset the latch */
686  Assert(latch->owner_pid == MyProcPid);
687  Assert(latch->maybe_sleeping == false);
688 
689  latch->is_set = false;
690 
691  /*
692  * Ensure that the write to is_set gets flushed to main memory before we
693  * examine any flag variables. Otherwise a concurrent SetLatch might
694  * falsely conclude that it needn't signal us, even though we have missed
695  * seeing some flag updates that SetLatch was supposed to inform us of.
696  */
698 }
#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(), logicalrep_worker_stop(), LogicalRepApplyLoop(), mq_putmessage(), pg_promote(), pg_sleep(), pg_wait_until_termination(), pgarch_MainLoop(), pgfdw_get_cleanup_result(), pgfdw_get_result(), pq_check_connection(), ProcessPendingWrites(), 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(), and WalWriterMain().

◆ sendSelfPipeByte()

static void sendSelfPipeByte ( void  )
static

Definition at line 2131 of file latch.c.

2132 {
2133  int rc;
2134  char dummy = 0;
2135 
2136 retry:
2137  rc = write(selfpipe_writefd, &dummy, 1);
2138  if (rc < 0)
2139  {
2140  /* If interrupted by signal, just retry */
2141  if (errno == EINTR)
2142  goto retry;
2143 
2144  /*
2145  * If the pipe is full, we don't need to retry, the data that's there
2146  * already is enough to wake up WaitLatch.
2147  */
2148  if (errno == EAGAIN || errno == EWOULDBLOCK)
2149  return;
2150 
2151  /*
2152  * Oops, the write() failed for some other reason. We might be in a
2153  * signal handler, so it's not safe to elog(). We have no choice but
2154  * silently ignore the error.
2155  */
2156  return;
2157  }
2158 }
#define write(a, b, c)
Definition: win32.h:14

References EAGAIN, EINTR, EWOULDBLOCK, selfpipe_writefd, and write.

Referenced by latch_sigurg_handler(), and SetLatch().

◆ SetLatch()

void SetLatch ( Latch latch)

Definition at line 591 of file latch.c.

592 {
593 #ifndef WIN32
594  pid_t owner_pid;
595 #else
596  HANDLE handle;
597 #endif
598 
599  /*
600  * The memory barrier has to be placed here to ensure that any flag
601  * variables possibly changed by this process have been flushed to main
602  * memory, before we check/set is_set.
603  */
605 
606  /* Quick exit if already set */
607  if (latch->is_set)
608  return;
609 
610  latch->is_set = true;
611 
613  if (!latch->maybe_sleeping)
614  return;
615 
616 #ifndef WIN32
617 
618  /*
619  * See if anyone's waiting for the latch. It can be the current process if
620  * we're in a signal handler. We use the self-pipe or SIGURG to ourselves
621  * to wake up WaitEventSetWaitBlock() without races in that case. If it's
622  * another process, send a signal.
623  *
624  * Fetch owner_pid only once, in case the latch is concurrently getting
625  * owned or disowned. XXX: This assumes that pid_t is atomic, which isn't
626  * guaranteed to be true! In practice, the effective range of pid_t fits
627  * in a 32 bit integer, and so should be atomic. In the worst case, we
628  * might end up signaling the wrong process. Even then, you're very
629  * unlucky if a process with that bogus pid exists and belongs to
630  * Postgres; and PG database processes should handle excess SIGUSR1
631  * interrupts without a problem anyhow.
632  *
633  * Another sort of race condition that's possible here is for a new
634  * process to own the latch immediately after we look, so we don't signal
635  * it. This is okay so long as all callers of ResetLatch/WaitLatch follow
636  * the standard coding convention of waiting at the bottom of their loops,
637  * not the top, so that they'll correctly process latch-setting events
638  * that happen before they enter the loop.
639  */
640  owner_pid = latch->owner_pid;
641  if (owner_pid == 0)
642  return;
643  else if (owner_pid == MyProcPid)
644  {
645 #if defined(WAIT_USE_SELF_PIPE)
646  if (waiting)
648 #else
649  if (waiting)
650  kill(MyProcPid, SIGURG);
651 #endif
652  }
653  else
654  kill(owner_pid, SIGURG);
655 
656 #else
657 
658  /*
659  * See if anyone's waiting for the latch. It can be the current process if
660  * we're in a signal handler.
661  *
662  * Use a local variable here just in case somebody changes the event field
663  * concurrently (which really should not happen).
664  */
665  handle = latch->event;
666  if (handle)
667  {
668  SetEvent(handle);
669 
670  /*
671  * Note that we silently ignore any errors. We might be in a signal
672  * handler or other critical path where it's not safe to call elog().
673  */
674  }
675 #endif
676 }
#define kill(pid, sig)
Definition: win32_port.h:482

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_sig_alarm(), HandleCatchupInterrupt(), HandleNotifyInterrupt(), HandleParallelMessageInterrupt(), IdleInTransactionSessionTimeoutHandler(), IdleSessionTimeoutHandler(), IdleStatsUpdateTimeoutHandler(), logicalrep_worker_wakeup_ptr(), pgarch_waken_stop(), PgArchWakeup(), ProcessClientReadInterrupt(), ProcessClientWriteInterrupt(), ProcessPendingWrites(), 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(), and XLogSetAsyncXactLSN().

◆ ShutdownLatchSupport()

void ShutdownLatchSupport ( void  )

Definition at line 323 of file latch.c.

324 {
325 #if defined(WAIT_USE_POLL)
326  pqsignal(SIGURG, SIG_IGN);
327 #endif
328 
329  if (LatchWaitSet)
330  {
332  LatchWaitSet = NULL;
333  }
334 
335 #if defined(WAIT_USE_SELF_PIPE)
338  selfpipe_readfd = -1;
339  selfpipe_writefd = -1;
341 #endif
342 
343 #if defined(WAIT_USE_SIGNALFD)
344  close(signal_fd);
345  signal_fd = -1;
346 #endif
347 }
void FreeWaitEventSet(WaitEventSet *set)
Definition: latch.c:821
#define InvalidPid
Definition: miscadmin.h:32

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

◆ WaitEventAdjustPoll()

static void WaitEventAdjustPoll ( WaitEventSet set,
WaitEvent event 
)
static

Definition at line 1099 of file latch.c.

1100 {
1101  struct pollfd *pollfd = &set->pollfds[event->pos];
1102 
1103  pollfd->revents = 0;
1104  pollfd->fd = event->fd;
1105 
1106  /* prepare pollfd entry once */
1107  if (event->events == WL_LATCH_SET)
1108  {
1109  Assert(set->latch != NULL);
1110  pollfd->events = POLLIN;
1111  }
1112  else if (event->events == WL_POSTMASTER_DEATH)
1113  {
1114  pollfd->events = POLLIN;
1115  }
1116  else
1117  {
1118  Assert(event->events & (WL_SOCKET_READABLE |
1120  WL_SOCKET_CLOSED));
1121  pollfd->events = 0;
1122  if (event->events & WL_SOCKET_READABLE)
1123  pollfd->events |= POLLIN;
1124  if (event->events & WL_SOCKET_WRITEABLE)
1125  pollfd->events |= POLLOUT;
1126 #ifdef POLLRDHUP
1127  if (event->events & WL_SOCKET_CLOSED)
1128  pollfd->events |= POLLRDHUP;
1129 #endif
1130  }
1131 
1132  Assert(event->fd != PGINVALID_SOCKET);
1133 }
#define WL_SOCKET_READABLE
Definition: latch.h:126
#define WL_SOCKET_CLOSED
Definition: latch.h:137
#define WL_SOCKET_WRITEABLE
Definition: latch.h:127

References Assert(), WaitEvent::events, WaitEvent::fd, WaitEventSet::latch, PGINVALID_SOCKET, WaitEventSet::pollfds, WL_LATCH_SET, WL_POSTMASTER_DEATH, WL_SOCKET_CLOSED, WL_SOCKET_READABLE, and WL_SOCKET_WRITEABLE.

Referenced by AddWaitEventToSet(), and ModifyWaitEvent().

◆ WaitEventSetCanReportClosed()

bool WaitEventSetCanReportClosed ( void  )

Definition at line 2091 of file latch.c.

2092 {
2093 #if (defined(WAIT_USE_POLL) && defined(POLLRDHUP)) || \
2094  defined(WAIT_USE_EPOLL) || \
2095  defined(WAIT_USE_KQUEUE)
2096  return true;
2097 #else
2098  return false;
2099 #endif
2100 }

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 1345 of file latch.c.

1348 {
1349  int returned_events = 0;
1351  instr_time cur_time;
1352  long cur_timeout = -1;
1353 
1354  Assert(nevents > 0);
1355 
1356  /*
1357  * Initialize timeout if requested. We must record the current time so
1358  * that we can determine the remaining timeout if interrupted.
1359  */
1360  if (timeout >= 0)
1361  {
1363  Assert(timeout >= 0 && timeout <= INT_MAX);
1364  cur_timeout = timeout;
1365  }
1366 
1367  pgstat_report_wait_start(wait_event_info);
1368 
1369 #ifndef WIN32
1370  waiting = true;
1371 #else
1372  /* Ensure that signals are serviced even if latch is already set */
1374 #endif
1375  while (returned_events == 0)
1376  {
1377  int rc;
1378 
1379  /*
1380  * Check if the latch is set already. If so, leave the loop
1381  * immediately, avoid blocking again. We don't attempt to report any
1382  * other events that might also be satisfied.
1383  *
1384  * If someone sets the latch between this and the
1385  * WaitEventSetWaitBlock() below, the setter will write a byte to the
1386  * pipe (or signal us and the signal handler will do that), and the
1387  * readiness routine will return immediately.
1388  *
1389  * On unix, If there's a pending byte in the self pipe, we'll notice
1390  * whenever blocking. Only clearing the pipe in that case avoids
1391  * having to drain it every time WaitLatchOrSocket() is used. Should
1392  * the pipe-buffer fill up we're still ok, because the pipe is in
1393  * nonblocking mode. It's unlikely for that to happen, because the
1394  * self pipe isn't filled unless we're blocking (waiting = true), or
1395  * from inside a signal handler in latch_sigurg_handler().
1396  *
1397  * On windows, we'll also notice if there's a pending event for the
1398  * latch when blocking, but there's no danger of anything filling up,
1399  * as "Setting an event that is already set has no effect.".
1400  *
1401  * Note: we assume that the kernel calls involved in latch management
1402  * will provide adequate synchronization on machines with weak memory
1403  * ordering, so that we cannot miss seeing is_set if a notification
1404  * has already been queued.
1405  */
1406  if (set->latch && !set->latch->is_set)
1407  {
1408  /* about to sleep on a latch */
1409  set->latch->maybe_sleeping = true;
1411  /* and recheck */
1412  }
1413 
1414  if (set->latch && set->latch->is_set)
1415  {
1416  occurred_events->fd = PGINVALID_SOCKET;
1417  occurred_events->pos = set->latch_pos;
1418  occurred_events->user_data =
1419  set->events[set->latch_pos].user_data;
1420  occurred_events->events = WL_LATCH_SET;
1421  occurred_events++;
1422  returned_events++;
1423 
1424  /* could have been set above */
1425  set->latch->maybe_sleeping = false;
1426 
1427  break;
1428  }
1429 
1430  /*
1431  * Wait for events using the readiness primitive chosen at the top of
1432  * this file. If -1 is returned, a timeout has occurred, if 0 we have
1433  * to retry, everything >= 1 is the number of returned events.
1434  */
1435  rc = WaitEventSetWaitBlock(set, cur_timeout,
1436  occurred_events, nevents);
1437 
1438  if (set->latch)
1439  {
1440  Assert(set->latch->maybe_sleeping);
1441  set->latch->maybe_sleeping = false;
1442  }
1443 
1444  if (rc == -1)
1445  break; /* timeout occurred */
1446  else
1447  returned_events = rc;
1448 
1449  /* If we're not done, update cur_timeout for next iteration */
1450  if (returned_events == 0 && timeout >= 0)
1451  {
1452  INSTR_TIME_SET_CURRENT(cur_time);
1453  INSTR_TIME_SUBTRACT(cur_time, start_time);
1454  cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
1455  if (cur_timeout <= 0)
1456  break;
1457  }
1458  }
1459 #ifndef WIN32
1460  waiting = false;
1461 #endif
1462 
1464 
1465  return returned_events;
1466 }
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:89
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:103
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:135
struct timespec instr_time
Definition: instr_time.h:83
static int WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout, WaitEvent *occurred_events, int nevents)
Definition: latch.c:1768
static time_t start_time
Definition: pg_ctl.c:94
void pgwin32_dispatch_queued_signals(void)
Definition: signal.c:120
void * user_data
Definition: latch.h:148
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:268
static void pgstat_report_wait_end(void)
Definition: wait_event.h:284

References Assert(), WaitEventSet::events, WaitEvent::events, WaitEvent::fd, INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, 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(), SysLoggerMain(), WaitLatch(), WaitLatchOrSocket(), and WalSndWait().

◆ WaitEventSetWaitBlock()

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

Definition at line 1768 of file latch.c.

1770 {
1771  int returned_events = 0;
1772  int rc;
1773  WaitEvent *cur_event;
1774  struct pollfd *cur_pollfd;
1775 
1776  /* Sleep */
1777  rc = poll(set->pollfds, set->nevents, (int) cur_timeout);
1778 
1779  /* Check return code */
1780  if (rc < 0)
1781  {
1782  /* EINTR is okay, otherwise complain */
1783  if (errno != EINTR)
1784  {
1785  waiting = false;
1786  ereport(ERROR,
1788  errmsg("%s() failed: %m",
1789  "poll")));
1790  }
1791  return 0;
1792  }
1793  else if (rc == 0)
1794  {
1795  /* timeout exceeded */
1796  return -1;
1797  }
1798 
1799  for (cur_event = set->events, cur_pollfd = set->pollfds;
1800  cur_event < (set->events + set->nevents) &&
1801  returned_events < nevents;
1802  cur_event++, cur_pollfd++)
1803  {
1804  /* no activity on this FD, skip */
1805  if (cur_pollfd->revents == 0)
1806  continue;
1807 
1808  occurred_events->pos = cur_event->pos;
1809  occurred_events->user_data = cur_event->user_data;
1810  occurred_events->events = 0;
1811 
1812  if (cur_event->events == WL_LATCH_SET &&
1813  (cur_pollfd->revents & (POLLIN | POLLHUP | POLLERR | POLLNVAL)))
1814  {
1815  /* There's data in the self-pipe, clear it. */
1816  drain();
1817 
1818  if (set->latch && set->latch->is_set)
1819  {
1820  occurred_events->fd = PGINVALID_SOCKET;
1821  occurred_events->events = WL_LATCH_SET;
1822  occurred_events++;
1823  returned_events++;
1824  }
1825  }
1826  else if (cur_event->events == WL_POSTMASTER_DEATH &&
1827  (cur_pollfd->revents & (POLLIN | POLLHUP | POLLERR | POLLNVAL)))
1828  {
1829  /*
1830  * We expect an POLLHUP when the remote end is closed, but because
1831  * we don't expect the pipe to become readable or to have any
1832  * errors either, treat those cases as postmaster death, too.
1833  *
1834  * Be paranoid about a spurious event signaling the postmaster as
1835  * being dead. There have been reports about that happening with
1836  * older primitives (select(2) to be specific), and a spurious
1837  * WL_POSTMASTER_DEATH event would be painful. Re-checking doesn't
1838  * cost much.
1839  */
1841  {
1842  if (set->exit_on_postmaster_death)
1843  proc_exit(1);
1844  occurred_events->fd = PGINVALID_SOCKET;
1845  occurred_events->events = WL_POSTMASTER_DEATH;
1846  occurred_events++;
1847  returned_events++;
1848  }
1849  }
1850  else if (cur_event->events & (WL_SOCKET_READABLE |
1853  {
1854  int errflags = POLLHUP | POLLERR | POLLNVAL;
1855 
1856  Assert(cur_event->fd >= PGINVALID_SOCKET);
1857 
1858  if ((cur_event->events & WL_SOCKET_READABLE) &&
1859  (cur_pollfd->revents & (POLLIN | errflags)))
1860  {
1861  /* data available in socket, or EOF */
1862  occurred_events->events |= WL_SOCKET_READABLE;
1863  }
1864 
1865  if ((cur_event->events & WL_SOCKET_WRITEABLE) &&
1866  (cur_pollfd->revents & (POLLOUT | errflags)))
1867  {
1868  /* writeable, or EOF */
1869  occurred_events->events |= WL_SOCKET_WRITEABLE;
1870  }
1871 
1872 #ifdef POLLRDHUP
1873  if ((cur_event->events & WL_SOCKET_CLOSED) &&
1874  (cur_pollfd->revents & (POLLRDHUP | errflags)))
1875  {
1876  /* remote peer closed, or error */
1877  occurred_events->events |= WL_SOCKET_CLOSED;
1878  }
1879 #endif
1880 
1881  if (occurred_events->events != 0)
1882  {
1883  occurred_events->fd = cur_event->fd;
1884  occurred_events++;
1885  returned_events++;
1886  }
1887  }
1888  }
1889  return returned_events;
1890 }
int errcode_for_socket_access(void)
Definition: elog.c:829
int errmsg(const char *fmt,...)
Definition: elog.c:946
#define ereport(elevel,...)
Definition: elog.h:145
void proc_exit(int code)
Definition: ipc.c:104
static void drain(void)
Definition: latch.c:2172
bool PostmasterIsAliveInternal(void)
Definition: pmsignal.c:376

References Assert(), drain(), EINTR, ereport, errcode_for_socket_access(), errmsg(), ERROR, WaitEventSet::events, WaitEvent::events, WaitEventSet::exit_on_postmaster_death, WaitEvent::fd, Latch::is_set, WaitEventSet::latch, WaitEventSet::nevents, PGINVALID_SOCKET, WaitEventSet::pollfds, WaitEvent::pos, PostmasterIsAliveInternal(), proc_exit(), WaitEvent::user_data, waiting, WL_LATCH_SET, WL_POSTMASTER_DEATH, WL_SOCKET_CLOSED, WL_SOCKET_READABLE, and WL_SOCKET_WRITEABLE.

Referenced by WaitEventSetWait().

◆ WaitLatch()

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

Definition at line 476 of file latch.c.

478 {
479  WaitEvent event;
480 
481  /* Postmaster-managed callers must handle postmaster death somehow. */
483  (wakeEvents & WL_EXIT_ON_PM_DEATH) ||
484  (wakeEvents & WL_POSTMASTER_DEATH));
485 
486  /*
487  * Some callers may have a latch other than MyLatch, or no latch at all,
488  * or want to handle postmaster death differently. It's cheap to assign
489  * those, so just do it every time.
490  */
491  if (!(wakeEvents & WL_LATCH_SET))
492  latch = NULL;
495  ((wakeEvents & WL_EXIT_ON_PM_DEATH) != 0);
496 
498  (wakeEvents & WL_TIMEOUT) ? timeout : -1,
499  &event, 1,
500  wait_event_info) == 0)
501  return WL_TIMEOUT;
502  else
503  return event.events;
504 }
void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
Definition: latch.c:972
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
Definition: latch.c:1345
#define WL_TIMEOUT
Definition: latch.h:128

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(), logicalrep_worker_stop(), mq_putmessage(), 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(), 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().

◆ WaitLatchOrSocket()

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

Definition at line 524 of file latch.c.

526 {
527  int ret = 0;
528  int rc;
529  WaitEvent event;
531 
532  if (wakeEvents & WL_TIMEOUT)
533  Assert(timeout >= 0);
534  else
535  timeout = -1;
536 
537  if (wakeEvents & WL_LATCH_SET)
539  latch, NULL);
540 
541  /* Postmaster-managed callers must handle postmaster death somehow. */
543  (wakeEvents & WL_EXIT_ON_PM_DEATH) ||
544  (wakeEvents & WL_POSTMASTER_DEATH));
545 
546  if ((wakeEvents & WL_POSTMASTER_DEATH) && IsUnderPostmaster)
548  NULL, NULL);
549 
550  if ((wakeEvents & WL_EXIT_ON_PM_DEATH) && IsUnderPostmaster)
552  NULL, NULL);
553 
554  if (wakeEvents & WL_SOCKET_MASK)
555  {
556  int ev;
557 
558  ev = wakeEvents & WL_SOCKET_MASK;
559  AddWaitEventToSet(set, ev, sock, NULL, NULL);
560  }
561 
562  rc = WaitEventSetWait(set, timeout, &event, 1, wait_event_info);
563 
564  if (rc == 0)
565  ret |= WL_TIMEOUT;
566  else
567  {
568  ret |= event.events & (WL_LATCH_SET |
571  }
572 
573  FreeWaitEventSet(set);
574 
575  return ret;
576 }
MemoryContext CurrentMemoryContext
Definition: mcxt.c:124

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().

Variable Documentation

◆ LatchWaitSet

WaitEventSet* LatchWaitSet
static

Definition at line 155 of file latch.c.

Referenced by InitializeLatchWaitSet(), ShutdownLatchSupport(), and WaitLatch().

◆ selfpipe_owner_pid

int selfpipe_owner_pid = 0
static

Definition at line 176 of file latch.c.

Referenced by InitializeLatchSupport(), InitLatch(), OwnLatch(), and ShutdownLatchSupport().

◆ selfpipe_readfd

int selfpipe_readfd = -1
static

◆ selfpipe_writefd

int selfpipe_writefd = -1
static

Definition at line 173 of file latch.c.

Referenced by InitializeLatchSupport(), sendSelfPipeByte(), and ShutdownLatchSupport().

◆ waiting

volatile sig_atomic_t waiting = false
static