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

Go to the source code of this file.

Data Structures

struct  Latch
 
struct  WaitEvent
 

Macros

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

Typedefs

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

Functions

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

Macro Definition Documentation

◆ WL_EXIT_ON_PM_DEATH

#define WL_EXIT_ON_PM_DEATH   (1 << 5)

Definition at line 130 of file latch.h.

◆ WL_LATCH_SET

#define WL_LATCH_SET   (1 << 0)

Definition at line 125 of file latch.h.

◆ WL_POSTMASTER_DEATH

#define WL_POSTMASTER_DEATH   (1 << 4)

Definition at line 129 of file latch.h.

◆ WL_SOCKET_CLOSED

#define WL_SOCKET_CLOSED   (1 << 7)

Definition at line 137 of file latch.h.

◆ WL_SOCKET_CONNECTED

#define WL_SOCKET_CONNECTED   WL_SOCKET_WRITEABLE

Definition at line 135 of file latch.h.

◆ WL_SOCKET_MASK

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

Definition at line 138 of file latch.h.

◆ WL_SOCKET_READABLE

#define WL_SOCKET_READABLE   (1 << 1)

Definition at line 126 of file latch.h.

◆ WL_SOCKET_WRITEABLE

#define WL_SOCKET_WRITEABLE   (1 << 2)

Definition at line 127 of file latch.h.

◆ WL_TIMEOUT

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

Definition at line 128 of file latch.h.

Typedef Documentation

◆ Latch

typedef struct Latch Latch

◆ WaitEvent

typedef struct WaitEvent WaitEvent

◆ WaitEventSet

typedef struct WaitEventSet WaitEventSet

Definition at line 1 of file latch.h.

Function Documentation

◆ AddWaitEventToSet()

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

Definition at line 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:33
#define elog(elevel,...)
Definition: elog.h:218
int MyProcPid
Definition: globals.c:44
#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:564
#define POSTMASTER_FD_WATCH
Definition: postmaster.h:43
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, 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:757
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:918
size_t Size
Definition: c.h:540
void ReleaseExternalFD(void)
Definition: fd.c:1230
bool AcquireExternalFD(void)
Definition: fd.c:1177
#define close(a)
Definition: win32.h:12
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:906
const void * data
HANDLE pgwin32_signal_event
Definition: signal.c:27

References AcquireExternalFD(), close, data, elog, ERROR, WaitEventSet::events, WaitEventSet::exit_on_postmaster_death, WaitEventSet::latch, MAXALIGN, MemoryContextAllocZero(), WaitEventSet::nevents_space, pgwin32_signal_event, 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().

◆ 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:1175
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 */
223  Assert(selfpipe_owner_pid != MyProcPid);
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 */
228  selfpipe_readfd = selfpipe_writefd = -1;
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);
249  Assert(selfpipe_owner_pid == 0);
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];
274  selfpipe_owner_pid = MyProcPid;
275 
276  /* Tell fd.c about these two long-lived FDs */
279 
280  pqsignal(SIGURG, latch_sigurg_handler);
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:35
void ReserveExternalFD(void)
Definition: fd.c:1212
bool IsUnderPostmaster
Definition: globals.c:113
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:180
#define SIG_IGN
Definition: win32_port.h:164

References Assert(), close, elog, FATAL, IsUnderPostmaster, MyProcPid, pqsignal(), ReleaseExternalFD(), ReserveExternalFD(), 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:155
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:48

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 */
362  Assert(selfpipe_readfd >= 0 && selfpipe_owner_pid == MyProcPid);
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, and Latch::owner_pid.

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

◆ 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, 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 */
431  Assert(selfpipe_readfd >= 0 && selfpipe_owner_pid == MyProcPid);
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:36

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

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:145

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

◆ 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)
647  sendSelfPipeByte();
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 }
static volatile sig_atomic_t waiting
Definition: latch.c:162
#define kill(pid, sig)
Definition: win32_port.h:464

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

Referenced by avl_sigusr2_handler(), CheckDeadLockAlert(), ClientCheckTimeoutHandler(), ConditionVariableBroadcast(), ConditionVariableSignal(), die(), ForwardSyncRequest(), handle_sig_alarm(), HandleCatchupInterrupt(), HandleNotifyInterrupt(), HandleParallelMessageInterrupt(), IdleInTransactionSessionTimeoutHandler(), IdleSessionTimeoutHandler(), 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)
336  close(selfpipe_readfd);
337  close(selfpipe_writefd);
338  selfpipe_readfd = -1;
339  selfpipe_writefd = -1;
340  selfpipe_owner_pid = InvalidPid;
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(), and SIG_IGN.

◆ 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:156
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
struct timeval instr_time
Definition: instr_time.h:150
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:202
static int WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout, WaitEvent *occurred_events, int nevents)
static time_t start_time
Definition: pg_ctl.c:99
void pgwin32_dispatch_queued_signals(void)
Definition: signal.c:118
void * user_data
Definition: latch.h:148
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:266
static void pgstat_report_wait_end(void)
Definition: wait_event.h:282

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

◆ 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:42

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