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 "utils/memutils.h"
#include "utils/resowner.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)
 
static void ResOwnerReleaseWaitEventSet (Datum res)
 
static void ResourceOwnerRememberWaitEventSet (ResourceOwner owner, WaitEventSet *set)
 
static void ResourceOwnerForgetWaitEventSet (ResourceOwner owner, WaitEventSet *set)
 
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 (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)
 
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
 
static const ResourceOwnerDesc wait_event_set_resowner_desc
 

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

Function Documentation

◆ AddWaitEventToSet()

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

Definition at line 963 of file latch.c.

965 {
966  WaitEvent *event;
967 
968  /* not enough space */
969  Assert(set->nevents < set->nevents_space);
970 
971  if (events == WL_EXIT_ON_PM_DEATH)
972  {
973  events = WL_POSTMASTER_DEATH;
974  set->exit_on_postmaster_death = true;
975  }
976 
977  if (latch)
978  {
979  if (latch->owner_pid != MyProcPid)
980  elog(ERROR, "cannot wait on a latch owned by another process");
981  if (set->latch)
982  elog(ERROR, "cannot wait on more than one latch");
983  if ((events & WL_LATCH_SET) != WL_LATCH_SET)
984  elog(ERROR, "latch events only support being set");
985  }
986  else
987  {
988  if (events & WL_LATCH_SET)
989  elog(ERROR, "cannot wait on latch without a specified latch");
990  }
991 
992  /* waiting for socket readiness without a socket indicates a bug */
993  if (fd == PGINVALID_SOCKET && (events & WL_SOCKET_MASK))
994  elog(ERROR, "cannot wait on socket event without a socket");
995 
996  event = &set->events[set->nevents];
997  event->pos = set->nevents++;
998  event->fd = fd;
999  event->events = events;
1000  event->user_data = user_data;
1001 #ifdef WIN32
1002  event->reset = false;
1003 #endif
1004 
1005  if (events == WL_LATCH_SET)
1006  {
1007  set->latch = latch;
1008  set->latch_pos = event->pos;
1009 #if defined(WAIT_USE_SELF_PIPE)
1010  event->fd = selfpipe_readfd;
1011 #elif defined(WAIT_USE_SIGNALFD)
1012  event->fd = signal_fd;
1013 #else
1014  event->fd = PGINVALID_SOCKET;
1015 #ifdef WAIT_USE_EPOLL
1016  return event->pos;
1017 #endif
1018 #endif
1019  }
1020  else if (events == WL_POSTMASTER_DEATH)
1021  {
1022 #ifndef WIN32
1024 #endif
1025  }
1026 
1027  /* perform wait primitive specific initialization, if needed */
1028 #if defined(WAIT_USE_EPOLL)
1029  WaitEventAdjustEpoll(set, event, EPOLL_CTL_ADD);
1030 #elif defined(WAIT_USE_KQUEUE)
1031  WaitEventAdjustKqueue(set, event, 0);
1032 #elif defined(WAIT_USE_POLL)
1033  WaitEventAdjustPoll(set, event);
1034 #elif defined(WAIT_USE_WIN32)
1035  WaitEventAdjustWin32(set, event);
1036 #endif
1037 
1038  return event->pos;
1039 }
#define Assert(condition)
Definition: c.h:849
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
int MyProcPid
Definition: globals.c:46
static int selfpipe_readfd
Definition: latch.c:172
static void WaitEventAdjustPoll(WaitEventSet *set, WaitEvent *event)
Definition: latch.c:1176
#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
#define PGINVALID_SOCKET
Definition: port.h:31
int postmaster_alive_fds[2]
Definition: postmaster.c:450
#define POSTMASTER_FD_WATCH
Definition: postmaster.h:48
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: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: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 751 of file latch.c.

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

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

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

2325 {
2326  char buf[1024];
2327  int rc;
2328  int fd;
2329 
2330 #ifdef WAIT_USE_SELF_PIPE
2331  fd = selfpipe_readfd;
2332 #else
2333  fd = signal_fd;
2334 #endif
2335 
2336  for (;;)
2337  {
2338  rc = read(fd, buf, sizeof(buf));
2339  if (rc < 0)
2340  {
2341  if (errno == EAGAIN || errno == EWOULDBLOCK)
2342  break; /* the descriptor is empty */
2343  else if (errno == EINTR)
2344  continue; /* retry */
2345  else
2346  {
2347  waiting = false;
2348 #ifdef WAIT_USE_SELF_PIPE
2349  elog(ERROR, "read() on self-pipe failed: %m");
2350 #else
2351  elog(ERROR, "read() on signalfd failed: %m");
2352 #endif
2353  }
2354  }
2355  else if (rc == 0)
2356  {
2357  waiting = false;
2358 #ifdef WAIT_USE_SELF_PIPE
2359  elog(ERROR, "unexpected EOF on self-pipe");
2360 #else
2361  elog(ERROR, "unexpected EOF on signalfd");
2362 #endif
2363  }
2364  else if (rc < sizeof(buf))
2365  {
2366  /* we successfully drained the pipe; no need to read() again */
2367  break;
2368  }
2369  /* else buffer wasn't big enough, so read again */
2370  }
2371 }
#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:72
#define EINTR
Definition: win32_port.h:374
#define EWOULDBLOCK
Definition: win32_port.h:380
#define EAGAIN
Definition: win32_port.h:372

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

Referenced by WaitEventSetWaitBlock().

◆ FreeWaitEventSet()

void FreeWaitEventSet ( WaitEventSet set)

Definition at line 874 of file latch.c.

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

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

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

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

Referenced by ClosePostmasterPorts().

◆ GetNumRegisteredWaitEvents()

int GetNumRegisteredWaitEvents ( WaitEventSet set)

Definition at line 2262 of file latch.c.

2263 {
2264  return set->nevents;
2265 }

References WaitEventSet::nevents.

Referenced by ExecAppendAsyncEventWait(), and postgresForeignAsyncConfigureWait().

◆ InitializeLatchSupport()

void InitializeLatchSupport ( void  )

Definition at line 232 of file latch.c.

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

347 {
348  int latch_pos PG_USED_FOR_ASSERTS_ONLY;
349 
350  Assert(LatchWaitSet == NULL);
351 
352  /* Set up the WaitEventSet used by WaitLatch(). */
353  LatchWaitSet = CreateWaitEventSet(NULL, 2);
355  MyLatch, NULL);
356  if (IsUnderPostmaster)
358  PGINVALID_SOCKET, NULL, NULL);
359 
360  Assert(latch_pos == LatchWaitSetLatchPos);
361 }
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:185
struct Latch * MyLatch
Definition: globals.c:62
#define LatchWaitSetLatchPos
Definition: latch.c:158
static WaitEventSet * LatchWaitSet
Definition: latch.c:155
WaitEventSet * CreateWaitEventSet(ResourceOwner resowner, int nevents)
Definition: latch.c:751
int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
Definition: latch.c:963

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

395 {
396  latch->is_set = false;
397  latch->maybe_sleeping = false;
398  latch->owner_pid = MyProcPid;
399  latch->is_shared = false;
400 
401 #if defined(WAIT_USE_SELF_PIPE)
402  /* Assert InitializeLatchSupport has been called in this process */
404 #elif defined(WAIT_USE_SIGNALFD)
405  /* Assert InitializeLatchSupport has been called in this process */
406  Assert(signal_fd >= 0);
407 #elif defined(WAIT_USE_WIN32)
408  latch->event = CreateEvent(NULL, TRUE, FALSE, NULL);
409  if (latch->event == NULL)
410  elog(ERROR, "CreateEvent failed: error code %lu", GetLastError());
411 #endif /* WIN32 */
412 }
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 430 of file latch.c.

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

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

2276 {
2277  if (waiting)
2278  sendSelfPipeByte();
2279 }
static void sendSelfPipeByte(void)
Definition: latch.c:2283

References sendSelfPipeByte(), and waiting.

Referenced by InitializeLatchSupport().

◆ ModifyWaitEvent()

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

Definition at line 1049 of file latch.c.

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

464 {
465  int owner_pid;
466 
467  /* Sanity checks */
468  Assert(latch->is_shared);
469 
470 #if defined(WAIT_USE_SELF_PIPE)
471  /* Assert InitializeLatchSupport has been called in this process */
473 #elif defined(WAIT_USE_SIGNALFD)
474  /* Assert InitializeLatchSupport has been called in this process */
475  Assert(signal_fd >= 0);
476 #endif
477 
478  owner_pid = latch->owner_pid;
479  if (owner_pid != 0)
480  elog(PANIC, "latch already owned by PID %d", owner_pid);
481 
482  latch->owner_pid = MyProcPid;
483 }
#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 724 of file latch.c.

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

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

Referenced by ApplyLauncherMain(), autoprewarm_main(), BackgroundWriterMain(), CheckpointerMain(), CheckpointWriteDelay(), ConditionVariableTimedSleep(), copy_read_data(), do_pg_backup_stop(), gather_readnext(), lazy_truncate_heap(), libpqrcv_connect(), libpqrcv_PQgetResult(), libpqsrv_cancel(), 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().

◆ ResourceOwnerForgetWaitEventSet()

static void ResourceOwnerForgetWaitEventSet ( ResourceOwner  owner,
WaitEventSet set 
)
inlinestatic

Definition at line 219 of file latch.c.

220 {
222 }
static const ResourceOwnerDesc wait_event_set_resowner_desc
Definition: latch.c:203
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
void ResourceOwnerForget(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
Definition: resowner.c:554

References PointerGetDatum(), ResourceOwnerForget(), and wait_event_set_resowner_desc.

Referenced by FreeWaitEventSet().

◆ ResourceOwnerRememberWaitEventSet()

static void ResourceOwnerRememberWaitEventSet ( ResourceOwner  owner,
WaitEventSet set 
)
inlinestatic

Definition at line 214 of file latch.c.

215 {
217 }
void ResourceOwnerRemember(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
Definition: resowner.c:514

References PointerGetDatum(), ResourceOwnerRemember(), and wait_event_set_resowner_desc.

Referenced by CreateWaitEventSet().

◆ ResOwnerReleaseWaitEventSet()

static void ResOwnerReleaseWaitEventSet ( Datum  res)
static

Definition at line 2376 of file latch.c.

2377 {
2379 
2380  Assert(set->owner != NULL);
2381  set->owner = NULL;
2382  FreeWaitEventSet(set);
2383 }
void FreeWaitEventSet(WaitEventSet *set)
Definition: latch.c:874
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312

References Assert, DatumGetPointer(), FreeWaitEventSet(), WaitEventSet::owner, and res.

◆ sendSelfPipeByte()

static void sendSelfPipeByte ( void  )
static

Definition at line 2283 of file latch.c.

2284 {
2285  int rc;
2286  char dummy = 0;
2287 
2288 retry:
2289  rc = write(selfpipe_writefd, &dummy, 1);
2290  if (rc < 0)
2291  {
2292  /* If interrupted by signal, just retry */
2293  if (errno == EINTR)
2294  goto retry;
2295 
2296  /*
2297  * If the pipe is full, we don't need to retry, the data that's there
2298  * already is enough to wake up WaitLatch.
2299  */
2300  if (errno == EAGAIN || errno == EWOULDBLOCK)
2301  return;
2302 
2303  /*
2304  * Oops, the write() failed for some other reason. We might be in a
2305  * signal handler, so it's not safe to elog(). We have no choice but
2306  * silently ignore the error.
2307  */
2308  return;
2309  }
2310 }
#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 632 of file latch.c.

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

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(), 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(), WakeupWalSummarizer(), WalRcvForceReply(), WalSndLastCycleHandler(), WalSndWaitForWal(), and XLogSetAsyncXactLSN().

◆ ShutdownLatchSupport()

void ShutdownLatchSupport ( void  )

Definition at line 364 of file latch.c.

365 {
366 #if defined(WAIT_USE_POLL)
367  pqsignal(SIGURG, SIG_IGN);
368 #endif
369 
370  if (LatchWaitSet)
371  {
373  LatchWaitSet = NULL;
374  }
375 
376 #if defined(WAIT_USE_SELF_PIPE)
379  selfpipe_readfd = -1;
380  selfpipe_writefd = -1;
382 #endif
383 
384 #if defined(WAIT_USE_SIGNALFD)
385  close(signal_fd);
386  signal_fd = -1;
387 #endif
388 }
#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 1176 of file latch.c.

1177 {
1178  struct pollfd *pollfd = &set->pollfds[event->pos];
1179 
1180  pollfd->revents = 0;
1181  pollfd->fd = event->fd;
1182 
1183  /* prepare pollfd entry once */
1184  if (event->events == WL_LATCH_SET)
1185  {
1186  Assert(set->latch != NULL);
1187  pollfd->events = POLLIN;
1188  }
1189  else if (event->events == WL_POSTMASTER_DEATH)
1190  {
1191  pollfd->events = POLLIN;
1192  }
1193  else
1194  {
1195  Assert(event->events & (WL_SOCKET_READABLE |
1197  WL_SOCKET_CLOSED));
1198  pollfd->events = 0;
1199  if (event->events & WL_SOCKET_READABLE)
1200  pollfd->events |= POLLIN;
1201  if (event->events & WL_SOCKET_WRITEABLE)
1202  pollfd->events |= POLLOUT;
1203 #ifdef POLLRDHUP
1204  if (event->events & WL_SOCKET_CLOSED)
1205  pollfd->events |= POLLRDHUP;
1206 #endif
1207  }
1208 
1209  Assert(event->fd != PGINVALID_SOCKET);
1210 }
#define WL_SOCKET_READABLE
Definition: latch.h:128
#define WL_SOCKET_CLOSED
Definition: latch.h:139
#define WL_SOCKET_WRITEABLE
Definition: latch.h:129
pgsocket fd
Definition: latch.h:156

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

2248 {
2249 #if (defined(WAIT_USE_POLL) && defined(POLLRDHUP)) || \
2250  defined(WAIT_USE_EPOLL) || \
2251  defined(WAIT_USE_KQUEUE)
2252  return true;
2253 #else
2254  return false;
2255 #endif
2256 }

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

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

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

◆ WaitEventSetWaitBlock()

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

Definition at line 1850 of file latch.c.

1852 {
1853  int returned_events = 0;
1854  int rc;
1855  WaitEvent *cur_event;
1856  struct pollfd *cur_pollfd;
1857 
1858  /* Sleep */
1859  rc = poll(set->pollfds, set->nevents, (int) cur_timeout);
1860 
1861  /* Check return code */
1862  if (rc < 0)
1863  {
1864  /* EINTR is okay, otherwise complain */
1865  if (errno != EINTR)
1866  {
1867  waiting = false;
1868  ereport(ERROR,
1870  errmsg("%s() failed: %m",
1871  "poll")));
1872  }
1873  return 0;
1874  }
1875  else if (rc == 0)
1876  {
1877  /* timeout exceeded */
1878  return -1;
1879  }
1880 
1881  for (cur_event = set->events, cur_pollfd = set->pollfds;
1882  cur_event < (set->events + set->nevents) &&
1883  returned_events < nevents;
1884  cur_event++, cur_pollfd++)
1885  {
1886  /* no activity on this FD, skip */
1887  if (cur_pollfd->revents == 0)
1888  continue;
1889 
1890  occurred_events->pos = cur_event->pos;
1891  occurred_events->user_data = cur_event->user_data;
1892  occurred_events->events = 0;
1893 
1894  if (cur_event->events == WL_LATCH_SET &&
1895  (cur_pollfd->revents & (POLLIN | POLLHUP | POLLERR | POLLNVAL)))
1896  {
1897  /* There's data in the self-pipe, clear it. */
1898  drain();
1899 
1900  if (set->latch && set->latch->is_set)
1901  {
1902  occurred_events->fd = PGINVALID_SOCKET;
1903  occurred_events->events = WL_LATCH_SET;
1904  occurred_events++;
1905  returned_events++;
1906  }
1907  }
1908  else if (cur_event->events == WL_POSTMASTER_DEATH &&
1909  (cur_pollfd->revents & (POLLIN | POLLHUP | POLLERR | POLLNVAL)))
1910  {
1911  /*
1912  * We expect an POLLHUP when the remote end is closed, but because
1913  * we don't expect the pipe to become readable or to have any
1914  * errors either, treat those cases as postmaster death, too.
1915  *
1916  * Be paranoid about a spurious event signaling the postmaster as
1917  * being dead. There have been reports about that happening with
1918  * older primitives (select(2) to be specific), and a spurious
1919  * WL_POSTMASTER_DEATH event would be painful. Re-checking doesn't
1920  * cost much.
1921  */
1923  {
1924  if (set->exit_on_postmaster_death)
1925  proc_exit(1);
1926  occurred_events->fd = PGINVALID_SOCKET;
1927  occurred_events->events = WL_POSTMASTER_DEATH;
1928  occurred_events++;
1929  returned_events++;
1930  }
1931  }
1932  else if (cur_event->events & (WL_SOCKET_READABLE |
1935  {
1936  int errflags = POLLHUP | POLLERR | POLLNVAL;
1937 
1938  Assert(cur_event->fd >= PGINVALID_SOCKET);
1939 
1940  if ((cur_event->events & WL_SOCKET_READABLE) &&
1941  (cur_pollfd->revents & (POLLIN | errflags)))
1942  {
1943  /* data available in socket, or EOF */
1944  occurred_events->events |= WL_SOCKET_READABLE;
1945  }
1946 
1947  if ((cur_event->events & WL_SOCKET_WRITEABLE) &&
1948  (cur_pollfd->revents & (POLLOUT | errflags)))
1949  {
1950  /* writeable, or EOF */
1951  occurred_events->events |= WL_SOCKET_WRITEABLE;
1952  }
1953 
1954 #ifdef POLLRDHUP
1955  if ((cur_event->events & WL_SOCKET_CLOSED) &&
1956  (cur_pollfd->revents & (POLLRDHUP | errflags)))
1957  {
1958  /* remote peer closed, or error */
1959  occurred_events->events |= WL_SOCKET_CLOSED;
1960  }
1961 #endif
1962 
1963  if (occurred_events->events != 0)
1964  {
1965  occurred_events->fd = cur_event->fd;
1966  occurred_events++;
1967  returned_events++;
1968  }
1969  }
1970  }
1971  return returned_events;
1972 }
int errcode_for_socket_access(void)
Definition: elog.c:953
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
void proc_exit(int code)
Definition: ipc.c:104
static void drain(void)
Definition: latch.c:2324
bool PostmasterIsAliveInternal(void)
Definition: pmsignal.c:385

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

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

567 {
568  int ret = 0;
569  int rc;
570  WaitEvent event;
572 
573  if (wakeEvents & WL_TIMEOUT)
574  Assert(timeout >= 0);
575  else
576  timeout = -1;
577 
578  if (wakeEvents & WL_LATCH_SET)
580  latch, NULL);
581 
582  /* Postmaster-managed callers must handle postmaster death somehow. */
584  (wakeEvents & WL_EXIT_ON_PM_DEATH) ||
585  (wakeEvents & WL_POSTMASTER_DEATH));
586 
587  if ((wakeEvents & WL_POSTMASTER_DEATH) && IsUnderPostmaster)
589  NULL, NULL);
590 
591  if ((wakeEvents & WL_EXIT_ON_PM_DEATH) && IsUnderPostmaster)
593  NULL, NULL);
594 
595  if (wakeEvents & WL_SOCKET_MASK)
596  {
597  int ev;
598 
599  ev = wakeEvents & WL_SOCKET_MASK;
600  AddWaitEventToSet(set, ev, sock, NULL, NULL);
601  }
602 
603  rc = WaitEventSetWait(set, timeout, &event, 1, wait_event_info);
604 
605  if (rc == 0)
606  ret |= WL_TIMEOUT;
607  else
608  {
609  ret |= event.events & (WL_LATCH_SET |
612  }
613 
614  FreeWaitEventSet(set);
615 
616  return ret;
617 }
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_cancel(), libpqsrv_connect_internal(), libpqsrv_get_result(), LogicalRepApplyLoop(), pgfdw_get_cleanup_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().

◆ wait_event_set_resowner_desc

const ResourceOwnerDesc wait_event_set_resowner_desc
static
Initial value:
=
{
.name = "WaitEventSet",
.release_phase = RESOURCE_RELEASE_AFTER_LOCKS,
.release_priority = RELEASE_PRIO_WAITEVENTSETS,
.ReleaseResource = ResOwnerReleaseWaitEventSet,
.DebugPrint = NULL
}
static void ResOwnerReleaseWaitEventSet(Datum res)
Definition: latch.c:2376
#define RELEASE_PRIO_WAITEVENTSETS
Definition: resowner.h:77
@ RESOURCE_RELEASE_AFTER_LOCKS
Definition: resowner.h:56

Definition at line 203 of file latch.c.

Referenced by ResourceOwnerForgetWaitEventSet(), and ResourceOwnerRememberWaitEventSet().

◆ waiting