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 "miscadmin.h"
#include "pgstat.h"
#include "port/atomics.h"
#include "portability/instr_time.h"
#include "postmaster/postmaster.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/pmsignal.h"
#include "storage/shmem.h"
Include dependency graph for latch.c:

Go to the source code of this file.

Data Structures

struct  WaitEventSet
 

Functions

static void sendSelfPipeByte (void)
 
static void drainSelfPipe (void)
 
static int WaitEventSetWaitBlock (WaitEventSet *set, int cur_timeout, WaitEvent *occurred_events, int nevents)
 
void InitializeLatchSupport (void)
 
void InitLatch (Latch *latch)
 
void InitSharedLatch (Latch *latch)
 
void OwnLatch (Latch *latch)
 
void DisownLatch (Latch *latch)
 
int WaitLatch (Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
 
int WaitLatchOrSocket (Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
 
void SetLatch (Latch *latch)
 
void ResetLatch (Latch *latch)
 
WaitEventSetCreateWaitEventSet (MemoryContext context, int nevents)
 
void FreeWaitEventSet (WaitEventSet *set)
 
int AddWaitEventToSet (WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
 
void ModifyWaitEvent (WaitEventSet *set, int pos, uint32 events, Latch *latch)
 
int WaitEventSetWait (WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
 
void latch_sigusr1_handler (void)
 

Variables

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

Function Documentation

◆ AddWaitEventToSet()

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

Definition at line 690 of file latch.c.

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

Referenced by ConditionVariablePrepareToSleep(), pq_init(), SysLoggerMain(), and WaitLatchOrSocket().

692 {
693  WaitEvent *event;
694 
695  /* not enough space */
696  Assert(set->nevents < set->nevents_space);
697 
698  if (events == WL_EXIT_ON_PM_DEATH)
699  {
700  events = WL_POSTMASTER_DEATH;
701  set->exit_on_postmaster_death = true;
702  }
703 
704  if (latch)
705  {
706  if (latch->owner_pid != MyProcPid)
707  elog(ERROR, "cannot wait on a latch owned by another process");
708  if (set->latch)
709  elog(ERROR, "cannot wait on more than one latch");
710  if ((events & WL_LATCH_SET) != WL_LATCH_SET)
711  elog(ERROR, "latch events only support being set");
712  }
713  else
714  {
715  if (events & WL_LATCH_SET)
716  elog(ERROR, "cannot wait on latch without a specified latch");
717  }
718 
719  /* waiting for socket readiness without a socket indicates a bug */
720  if (fd == PGINVALID_SOCKET && (events & WL_SOCKET_MASK))
721  elog(ERROR, "cannot wait on socket event without a socket");
722 
723  event = &set->events[set->nevents];
724  event->pos = set->nevents++;
725  event->fd = fd;
726  event->events = events;
727  event->user_data = user_data;
728 #ifdef WIN32
729  event->reset = false;
730 #endif
731 
732  if (events == WL_LATCH_SET)
733  {
734  set->latch = latch;
735  set->latch_pos = event->pos;
736 #ifndef WIN32
737  event->fd = selfpipe_readfd;
738 #endif
739  }
740  else if (events == WL_POSTMASTER_DEATH)
741  {
742 #ifndef WIN32
744 #endif
745  }
746 
747  /* perform wait primitive specific initialization, if needed */
748 #if defined(WAIT_USE_EPOLL)
749  WaitEventAdjustEpoll(set, event, EPOLL_CTL_ADD);
750 #elif defined(WAIT_USE_POLL)
751  WaitEventAdjustPoll(set, event);
752 #elif defined(WAIT_USE_WIN32)
753  WaitEventAdjustWin32(set, event);
754 #endif
755 
756  return event->pos;
757 }
int MyProcPid
Definition: globals.c:40
#define WL_SOCKET_MASK
Definition: latch.h:137
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define ERROR
Definition: elog.h:43
static int selfpipe_readfd
Definition: latch.c:126
int nevents
Definition: latch.c:78
int postmaster_alive_fds[2]
Definition: postmaster.c:557
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
#define PGINVALID_SOCKET
Definition: port.h:33
#define Assert(condition)
Definition: c.h:732
int nevents_space
Definition: latch.c:79
int owner_pid
Definition: latch.h:114
#define elog(elevel,...)
Definition: elog.h:226
Latch * latch
Definition: latch.c:93
#define WL_LATCH_SET
Definition: latch.h:124
#define POSTMASTER_FD_WATCH
Definition: postmaster.h:42
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129

◆ CreateWaitEventSet()

WaitEventSet* CreateWaitEventSet ( MemoryContext  context,
int  nevents 
)

Definition at line 542 of file latch.c.

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

Referenced by ConditionVariablePrepareToSleep(), pq_init(), SysLoggerMain(), and WaitLatchOrSocket().

543 {
544  WaitEventSet *set;
545  char *data;
546  Size sz = 0;
547 
548  /*
549  * Use MAXALIGN size/alignment to guarantee that later uses of memory are
550  * aligned correctly. E.g. epoll_event might need 8 byte alignment on some
551  * platforms, but earlier allocations like WaitEventSet and WaitEvent
552  * might not sized to guarantee that when purely using sizeof().
553  */
554  sz += MAXALIGN(sizeof(WaitEventSet));
555  sz += MAXALIGN(sizeof(WaitEvent) * nevents);
556 
557 #if defined(WAIT_USE_EPOLL)
558  sz += MAXALIGN(sizeof(struct epoll_event) * nevents);
559 #elif defined(WAIT_USE_POLL)
560  sz += MAXALIGN(sizeof(struct pollfd) * nevents);
561 #elif defined(WAIT_USE_WIN32)
562  /* need space for the pgwin32_signal_event */
563  sz += MAXALIGN(sizeof(HANDLE) * (nevents + 1));
564 #endif
565 
566  data = (char *) MemoryContextAllocZero(context, sz);
567 
568  set = (WaitEventSet *) data;
569  data += MAXALIGN(sizeof(WaitEventSet));
570 
571  set->events = (WaitEvent *) data;
572  data += MAXALIGN(sizeof(WaitEvent) * nevents);
573 
574 #if defined(WAIT_USE_EPOLL)
575  set->epoll_ret_events = (struct epoll_event *) data;
576  data += MAXALIGN(sizeof(struct epoll_event) * nevents);
577 #elif defined(WAIT_USE_POLL)
578  set->pollfds = (struct pollfd *) data;
579  data += MAXALIGN(sizeof(struct pollfd) * nevents);
580 #elif defined(WAIT_USE_WIN32)
581  set->handles = (HANDLE) data;
582  data += MAXALIGN(sizeof(HANDLE) * nevents);
583 #endif
584 
585  set->latch = NULL;
586  set->nevents_space = nevents;
587  set->exit_on_postmaster_death = false;
588 
589 #if defined(WAIT_USE_EPOLL)
590 #ifdef EPOLL_CLOEXEC
591  set->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
592  if (set->epoll_fd < 0)
593  elog(ERROR, "epoll_create1 failed: %m");
594 #else
595  /* cope with ancient glibc lacking epoll_create1 (e.g., RHEL5) */
596  set->epoll_fd = epoll_create(nevents);
597  if (set->epoll_fd < 0)
598  elog(ERROR, "epoll_create failed: %m");
599  if (fcntl(set->epoll_fd, F_SETFD, FD_CLOEXEC) == -1)
600  elog(ERROR, "fcntl(F_SETFD) failed on epoll descriptor: %m");
601 #endif /* EPOLL_CLOEXEC */
602 #elif defined(WAIT_USE_WIN32)
603 
604  /*
605  * To handle signals while waiting, we need to add a win32 specific event.
606  * We accounted for the additional event at the top of this routine. See
607  * port/win32/signal.c for more details.
608  *
609  * Note: pgwin32_signal_event should be first to ensure that it will be
610  * reported when multiple events are set. We want to guarantee that
611  * pending signals are serviced.
612  */
613  set->handles[0] = pgwin32_signal_event;
614  StaticAssertStmt(WSA_INVALID_EVENT == NULL, "");
615 #endif
616 
617  return set;
618 }
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:842
HANDLE pgwin32_signal_event
Definition: signal.c:27
#define ERROR
Definition: elog.h:43
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:814
size_t Size
Definition: c.h:466
#define MAXALIGN(LEN)
Definition: c.h:685
#define elog(elevel,...)
Definition: elog.h:226

◆ DisownLatch()

void DisownLatch ( Latch latch)

Definition at line 316 of file latch.c.

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

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

317 {
318  Assert(latch->is_shared);
319  Assert(latch->owner_pid == MyProcPid);
320 
321  latch->owner_pid = 0;
322 }
int MyProcPid
Definition: globals.c:40
bool is_shared
Definition: latch.h:113
#define Assert(condition)
Definition: c.h:732
int owner_pid
Definition: latch.h:114

◆ drainSelfPipe()

static void drainSelfPipe ( void  )
static

Definition at line 1564 of file latch.c.

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

Referenced by WaitEventSetWait().

1565 {
1566  /*
1567  * There shouldn't normally be more than one byte in the pipe, or maybe a
1568  * few bytes if multiple processes run SetLatch at the same instant.
1569  */
1570  char buf[16];
1571  int rc;
1572 
1573  for (;;)
1574  {
1575  rc = read(selfpipe_readfd, buf, sizeof(buf));
1576  if (rc < 0)
1577  {
1578  if (errno == EAGAIN || errno == EWOULDBLOCK)
1579  break; /* the pipe is empty */
1580  else if (errno == EINTR)
1581  continue; /* retry */
1582  else
1583  {
1584  waiting = false;
1585  elog(ERROR, "read() on self-pipe failed: %m");
1586  }
1587  }
1588  else if (rc == 0)
1589  {
1590  waiting = false;
1591  elog(ERROR, "unexpected EOF on self-pipe");
1592  }
1593  else if (rc < sizeof(buf))
1594  {
1595  /* we successfully drained the pipe; no need to read() again */
1596  break;
1597  }
1598  /* else buffer wasn't big enough, so read again */
1599  }
1600 }
#define EAGAIN
Definition: win32_port.h:330
#define ERROR
Definition: elog.h:43
static int selfpipe_readfd
Definition: latch.c:126
static char * buf
Definition: pg_test_fsync.c:68
#define elog(elevel,...)
Definition: elog.h:226
#define EWOULDBLOCK
Definition: win32_port.h:338
#define EINTR
Definition: win32_port.h:332
static volatile sig_atomic_t waiting
Definition: latch.c:123
#define read(a, b, c)
Definition: win32.h:13

◆ FreeWaitEventSet()

void FreeWaitEventSet ( WaitEventSet set)

Definition at line 630 of file latch.c.

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

Referenced by WaitLatchOrSocket().

631 {
632 #if defined(WAIT_USE_EPOLL)
633  close(set->epoll_fd);
634 #elif defined(WAIT_USE_WIN32)
635  WaitEvent *cur_event;
636 
637  for (cur_event = set->events;
638  cur_event < (set->events + set->nevents);
639  cur_event++)
640  {
641  if (cur_event->events & WL_LATCH_SET)
642  {
643  /* uses the latch's HANDLE */
644  }
645  else if (cur_event->events & WL_POSTMASTER_DEATH)
646  {
647  /* uses PostmasterHandle */
648  }
649  else
650  {
651  /* Clean up the event object we created for the socket */
652  WSAEventSelect(cur_event->fd, NULL, 0);
653  WSACloseEvent(set->handles[cur_event->pos + 1]);
654  }
655  }
656 #endif
657 
658  pfree(set);
659 }
pgsocket fd
Definition: latch.h:145
int pos
Definition: latch.h:143
void pfree(void *pointer)
Definition: mcxt.c:1031
uint32 events
Definition: latch.h:144
int nevents
Definition: latch.c:78
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
WaitEvent * events
Definition: latch.c:85
#define close(a)
Definition: win32.h:12
#define WL_LATCH_SET
Definition: latch.h:124

◆ InitializeLatchSupport()

void InitializeLatchSupport ( void  )

Definition at line 155 of file latch.c.

References Assert, close, elog, FATAL, IsUnderPostmaster, MyProcPid, selfpipe_owner_pid, selfpipe_readfd, and selfpipe_writefd.

Referenced by InitPostmasterChild(), and InitStandaloneProcess().

156 {
157 #ifndef WIN32
158  int pipefd[2];
159 
160  if (IsUnderPostmaster)
161  {
162  /*
163  * We might have inherited connections to a self-pipe created by the
164  * postmaster. It's critical that child processes create their own
165  * self-pipes, of course, and we really want them to close the
166  * inherited FDs for safety's sake.
167  */
168  if (selfpipe_owner_pid != 0)
169  {
170  /* Assert we go through here but once in a child process */
172  /* Release postmaster's pipe FDs; ignore any error */
173  (void) close(selfpipe_readfd);
174  (void) close(selfpipe_writefd);
175  /* Clean up, just for safety's sake; we'll set these below */
177  selfpipe_owner_pid = 0;
178  }
179  else
180  {
181  /*
182  * Postmaster didn't create a self-pipe ... or else we're in an
183  * EXEC_BACKEND build, in which case it doesn't matter since the
184  * postmaster's pipe FDs were closed by the action of FD_CLOEXEC.
185  */
186  Assert(selfpipe_readfd == -1);
187  }
188  }
189  else
190  {
191  /* In postmaster or standalone backend, assert we do this but once */
192  Assert(selfpipe_readfd == -1);
194  }
195 
196  /*
197  * Set up the self-pipe that allows a signal handler to wake up the
198  * poll()/epoll_wait() in WaitLatch. Make the write-end non-blocking, so
199  * that SetLatch won't block if the event has already been set many times
200  * filling the kernel buffer. Make the read-end non-blocking too, so that
201  * we can easily clear the pipe by reading until EAGAIN or EWOULDBLOCK.
202  * Also, make both FDs close-on-exec, since we surely do not want any
203  * child processes messing with them.
204  */
205  if (pipe(pipefd) < 0)
206  elog(FATAL, "pipe() failed: %m");
207  if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) == -1)
208  elog(FATAL, "fcntl(F_SETFL) failed on read-end of self-pipe: %m");
209  if (fcntl(pipefd[1], F_SETFL, O_NONBLOCK) == -1)
210  elog(FATAL, "fcntl(F_SETFL) failed on write-end of self-pipe: %m");
211  if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) == -1)
212  elog(FATAL, "fcntl(F_SETFD) failed on read-end of self-pipe: %m");
213  if (fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) == -1)
214  elog(FATAL, "fcntl(F_SETFD) failed on write-end of self-pipe: %m");
215 
216  selfpipe_readfd = pipefd[0];
217  selfpipe_writefd = pipefd[1];
219 #else
220  /* currently, nothing to do here for Windows */
221 #endif
222 }
int MyProcPid
Definition: globals.c:40
static int selfpipe_writefd
Definition: latch.c:127
#define FATAL
Definition: elog.h:52
static int selfpipe_readfd
Definition: latch.c:126
bool IsUnderPostmaster
Definition: globals.c:109
#define Assert(condition)
Definition: c.h:732
#define elog(elevel,...)
Definition: elog.h:226
#define close(a)
Definition: win32.h:12
static int selfpipe_owner_pid
Definition: latch.c:130

◆ InitLatch()

void InitLatch ( Latch latch)

Definition at line 228 of file latch.c.

References Assert, elog, ERROR, FALSE, Latch::is_set, Latch::is_shared, MyProcPid, Latch::owner_pid, selfpipe_owner_pid, selfpipe_readfd, and TRUE.

Referenced by InitPostmasterChild(), and InitStandaloneProcess().

229 {
230  latch->is_set = false;
231  latch->owner_pid = MyProcPid;
232  latch->is_shared = false;
233 
234 #ifndef WIN32
235  /* Assert InitializeLatchSupport has been called in this process */
237 #else
238  latch->event = CreateEvent(NULL, TRUE, FALSE, NULL);
239  if (latch->event == NULL)
240  elog(ERROR, "CreateEvent failed: error code %lu", GetLastError());
241 #endif /* WIN32 */
242 }
#define TRUE
Definition: ecpglib.h:35
int MyProcPid
Definition: globals.c:40
bool is_shared
Definition: latch.h:113
#define FALSE
Definition: ecpglib.h:39
#define ERROR
Definition: elog.h:43
static int selfpipe_readfd
Definition: latch.c:126
#define Assert(condition)
Definition: c.h:732
int owner_pid
Definition: latch.h:114
sig_atomic_t is_set
Definition: latch.h:112
#define elog(elevel,...)
Definition: elog.h:226
static int selfpipe_owner_pid
Definition: latch.c:130

◆ InitSharedLatch()

void InitSharedLatch ( Latch latch)

Definition at line 260 of file latch.c.

References elog, ERROR, FALSE, Latch::is_set, Latch::is_shared, Latch::owner_pid, and TRUE.

Referenced by InitProcGlobal(), and XLOGShmemInit().

261 {
262 #ifdef WIN32
263  SECURITY_ATTRIBUTES sa;
264 
265  /*
266  * Set up security attributes to specify that the events are inherited.
267  */
268  ZeroMemory(&sa, sizeof(sa));
269  sa.nLength = sizeof(sa);
270  sa.bInheritHandle = TRUE;
271 
272  latch->event = CreateEvent(&sa, TRUE, FALSE, NULL);
273  if (latch->event == NULL)
274  elog(ERROR, "CreateEvent failed: error code %lu", GetLastError());
275 #endif
276 
277  latch->is_set = false;
278  latch->owner_pid = 0;
279  latch->is_shared = true;
280 }
#define TRUE
Definition: ecpglib.h:35
bool is_shared
Definition: latch.h:113
#define FALSE
Definition: ecpglib.h:39
#define ERROR
Definition: elog.h:43
int owner_pid
Definition: latch.h:114
sig_atomic_t is_set
Definition: latch.h:112
#define elog(elevel,...)
Definition: elog.h:226

◆ latch_sigusr1_handler()

void latch_sigusr1_handler ( void  )

Definition at line 1515 of file latch.c.

References sendSelfPipeByte(), and waiting.

Referenced by bgworker_sigusr1_handler(), bgwriter_sigusr1_handler(), chkpt_sigusr1_handler(), procsignal_sigusr1_handler(), StartupProcSigUsr1Handler(), WalRcvSigUsr1Handler(), and walwriter_sigusr1_handler().

1516 {
1517  if (waiting)
1518  sendSelfPipeByte();
1519 }
static void sendSelfPipeByte(void)
Definition: latch.c:1525
static volatile sig_atomic_t waiting
Definition: latch.c:123

◆ ModifyWaitEvent()

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

Definition at line 766 of file latch.c.

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

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

767 {
768  WaitEvent *event;
769 
770  Assert(pos < set->nevents);
771 
772  event = &set->events[pos];
773 
774  /*
775  * If neither the event mask nor the associated latch changes, return
776  * early. That's an important optimization for some sockets, where
777  * ModifyWaitEvent is frequently used to switch from waiting for reads to
778  * waiting on writes.
779  */
780  if (events == event->events &&
781  (!(event->events & WL_LATCH_SET) || set->latch == latch))
782  return;
783 
784  if (event->events & WL_LATCH_SET &&
785  events != event->events)
786  {
787  /* we could allow to disable latch events for a while */
788  elog(ERROR, "cannot modify latch event");
789  }
790 
791  if (event->events & WL_POSTMASTER_DEATH)
792  {
793  elog(ERROR, "cannot modify postmaster death event");
794  }
795 
796  /* FIXME: validate event mask */
797  event->events = events;
798 
799  if (events == WL_LATCH_SET)
800  {
801  set->latch = latch;
802  }
803 
804 #if defined(WAIT_USE_EPOLL)
805  WaitEventAdjustEpoll(set, event, EPOLL_CTL_MOD);
806 #elif defined(WAIT_USE_POLL)
807  WaitEventAdjustPoll(set, event);
808 #elif defined(WAIT_USE_WIN32)
809  WaitEventAdjustWin32(set, event);
810 #endif
811 }
#define ERROR
Definition: elog.h:43
uint32 events
Definition: latch.h:144
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
#define Assert(condition)
Definition: c.h:732
#define elog(elevel,...)
Definition: elog.h:226
Latch * latch
Definition: latch.c:93
#define WL_LATCH_SET
Definition: latch.h:124

◆ OwnLatch()

void OwnLatch ( Latch latch)

Definition at line 296 of file latch.c.

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

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

297 {
298  /* Sanity checks */
299  Assert(latch->is_shared);
300 
301 #ifndef WIN32
302  /* Assert InitializeLatchSupport has been called in this process */
304 #endif
305 
306  if (latch->owner_pid != 0)
307  elog(ERROR, "latch already owned");
308 
309  latch->owner_pid = MyProcPid;
310 }
int MyProcPid
Definition: globals.c:40
bool is_shared
Definition: latch.h:113
#define ERROR
Definition: elog.h:43
static int selfpipe_readfd
Definition: latch.c:126
#define Assert(condition)
Definition: c.h:732
int owner_pid
Definition: latch.h:114
#define elog(elevel,...)
Definition: elog.h:226
static int selfpipe_owner_pid
Definition: latch.c:130

◆ ResetLatch()

void ResetLatch ( Latch latch)

Definition at line 519 of file latch.c.

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

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

520 {
521  /* Only the owner should reset the latch */
522  Assert(latch->owner_pid == MyProcPid);
523 
524  latch->is_set = false;
525 
526  /*
527  * Ensure that the write to is_set gets flushed to main memory before we
528  * examine any flag variables. Otherwise a concurrent SetLatch might
529  * falsely conclude that it needn't signal us, even though we have missed
530  * seeing some flag updates that SetLatch was supposed to inform us of.
531  */
533 }
int MyProcPid
Definition: globals.c:40
#define pg_memory_barrier()
Definition: atomics.h:148
#define Assert(condition)
Definition: c.h:732
int owner_pid
Definition: latch.h:114
sig_atomic_t is_set
Definition: latch.h:112

◆ sendSelfPipeByte()

static void sendSelfPipeByte ( void  )
static

Definition at line 1525 of file latch.c.

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

Referenced by latch_sigusr1_handler(), and SetLatch().

1526 {
1527  int rc;
1528  char dummy = 0;
1529 
1530 retry:
1531  rc = write(selfpipe_writefd, &dummy, 1);
1532  if (rc < 0)
1533  {
1534  /* If interrupted by signal, just retry */
1535  if (errno == EINTR)
1536  goto retry;
1537 
1538  /*
1539  * If the pipe is full, we don't need to retry, the data that's there
1540  * already is enough to wake up WaitLatch.
1541  */
1542  if (errno == EAGAIN || errno == EWOULDBLOCK)
1543  return;
1544 
1545  /*
1546  * Oops, the write() failed for some other reason. We might be in a
1547  * signal handler, so it's not safe to elog(). We have no choice but
1548  * silently ignore the error.
1549  */
1550  return;
1551  }
1552 }
static int selfpipe_writefd
Definition: latch.c:127
#define EAGAIN
Definition: win32_port.h:330
#define write(a, b, c)
Definition: win32.h:14
#define EWOULDBLOCK
Definition: win32_port.h:338
#define EINTR
Definition: win32_port.h:332

◆ SetLatch()

void SetLatch ( Latch latch)

Definition at line 436 of file latch.c.

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

Referenced by apw_sighup_handler(), apw_sigterm_handler(), ArchSigHupHandler(), ArchSigTermHandler(), av_sighup_handler(), avl_sigterm_handler(), avl_sigusr2_handler(), BgSigHupHandler(), CheckDeadLockAlert(), ChkptSigHupHandler(), ConditionVariableBroadcast(), ConditionVariableSignal(), die(), ForwardSyncRequest(), handle_sig_alarm(), handle_sigterm(), HandleCatchupInterrupt(), HandleNotifyInterrupt(), HandleParallelMessageInterrupt(), IdleInTransactionSessionTimeoutHandler(), logicalrep_launcher_sighup(), logicalrep_worker_sighup(), logicalrep_worker_wakeup_ptr(), pgarch_waken(), pgarch_waken_stop(), pgstat_exit(), pgstat_sighup_handler(), PostgresSigHupHandler(), ProcessClientReadInterrupt(), ProcessClientWriteInterrupt(), ProcSendSignal(), procsignal_sigusr1_handler(), ProcWakeup(), RecoveryConflictInterrupt(), ReqCheckpointHandler(), ReqShutdownHandler(), 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(), sigHupHandler(), sigUsr1Handler(), StatementCancelHandler(), StrategyGetBuffer(), SwitchBackToLocalLatch(), SwitchToSharedLatch(), SyncRepWakeQueue(), test_shm_mq_main(), WakeupRecovery(), WalRcvForceReply(), WalRcvShutdownHandler(), WalShutdownHandler(), WalSigHupHandler(), WalSndLastCycleHandler(), WalSndWaitForWal(), WalSndWakeup(), WalSndWriteData(), worker_spi_sighup(), worker_spi_sigterm(), write_syslogger_file(), and XLogSetAsyncXactLSN().

437 {
438 #ifndef WIN32
439  pid_t owner_pid;
440 #else
441  HANDLE handle;
442 #endif
443 
444  /*
445  * The memory barrier has to be placed here to ensure that any flag
446  * variables possibly changed by this process have been flushed to main
447  * memory, before we check/set is_set.
448  */
450 
451  /* Quick exit if already set */
452  if (latch->is_set)
453  return;
454 
455  latch->is_set = true;
456 
457 #ifndef WIN32
458 
459  /*
460  * See if anyone's waiting for the latch. It can be the current process if
461  * we're in a signal handler. We use the self-pipe to wake up the
462  * poll()/epoll_wait() in that case. If it's another process, send a
463  * signal.
464  *
465  * Fetch owner_pid only once, in case the latch is concurrently getting
466  * owned or disowned. XXX: This assumes that pid_t is atomic, which isn't
467  * guaranteed to be true! In practice, the effective range of pid_t fits
468  * in a 32 bit integer, and so should be atomic. In the worst case, we
469  * might end up signaling the wrong process. Even then, you're very
470  * unlucky if a process with that bogus pid exists and belongs to
471  * Postgres; and PG database processes should handle excess SIGUSR1
472  * interrupts without a problem anyhow.
473  *
474  * Another sort of race condition that's possible here is for a new
475  * process to own the latch immediately after we look, so we don't signal
476  * it. This is okay so long as all callers of ResetLatch/WaitLatch follow
477  * the standard coding convention of waiting at the bottom of their loops,
478  * not the top, so that they'll correctly process latch-setting events
479  * that happen before they enter the loop.
480  */
481  owner_pid = latch->owner_pid;
482  if (owner_pid == 0)
483  return;
484  else if (owner_pid == MyProcPid)
485  {
486  if (waiting)
488  }
489  else
490  kill(owner_pid, SIGUSR1);
491 #else
492 
493  /*
494  * See if anyone's waiting for the latch. It can be the current process if
495  * we're in a signal handler.
496  *
497  * Use a local variable here just in case somebody changes the event field
498  * concurrently (which really should not happen).
499  */
500  handle = latch->event;
501  if (handle)
502  {
503  SetEvent(handle);
504 
505  /*
506  * Note that we silently ignore any errors. We might be in a signal
507  * handler or other critical path where it's not safe to call elog().
508  */
509  }
510 #endif
511 
512 }
int MyProcPid
Definition: globals.c:40
#define SIGUSR1
Definition: win32_port.h:175
#define kill(pid, sig)
Definition: win32_port.h:435
static void sendSelfPipeByte(void)
Definition: latch.c:1525
#define pg_memory_barrier()
Definition: atomics.h:148
int owner_pid
Definition: latch.h:114
sig_atomic_t is_set
Definition: latch.h:112
static volatile sig_atomic_t waiting
Definition: latch.c:123

◆ WaitEventSetWait()

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

Definition at line 953 of file latch.c.

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

Referenced by ConditionVariableTimedSleep(), secure_read(), secure_write(), SysLoggerMain(), and WaitLatchOrSocket().

956 {
957  int returned_events = 0;
959  instr_time cur_time;
960  long cur_timeout = -1;
961 
962  Assert(nevents > 0);
963 
964  /*
965  * Initialize timeout if requested. We must record the current time so
966  * that we can determine the remaining timeout if interrupted.
967  */
968  if (timeout >= 0)
969  {
970  INSTR_TIME_SET_CURRENT(start_time);
971  Assert(timeout >= 0 && timeout <= INT_MAX);
972  cur_timeout = timeout;
973  }
974 
975  pgstat_report_wait_start(wait_event_info);
976 
977 #ifndef WIN32
978  waiting = true;
979 #else
980  /* Ensure that signals are serviced even if latch is already set */
982 #endif
983  while (returned_events == 0)
984  {
985  int rc;
986 
987  /*
988  * Check if the latch is set already. If so, leave the loop
989  * immediately, avoid blocking again. We don't attempt to report any
990  * other events that might also be satisfied.
991  *
992  * If someone sets the latch between this and the
993  * WaitEventSetWaitBlock() below, the setter will write a byte to the
994  * pipe (or signal us and the signal handler will do that), and the
995  * readiness routine will return immediately.
996  *
997  * On unix, If there's a pending byte in the self pipe, we'll notice
998  * whenever blocking. Only clearing the pipe in that case avoids
999  * having to drain it every time WaitLatchOrSocket() is used. Should
1000  * the pipe-buffer fill up we're still ok, because the pipe is in
1001  * nonblocking mode. It's unlikely for that to happen, because the
1002  * self pipe isn't filled unless we're blocking (waiting = true), or
1003  * from inside a signal handler in latch_sigusr1_handler().
1004  *
1005  * On windows, we'll also notice if there's a pending event for the
1006  * latch when blocking, but there's no danger of anything filling up,
1007  * as "Setting an event that is already set has no effect.".
1008  *
1009  * Note: we assume that the kernel calls involved in latch management
1010  * will provide adequate synchronization on machines with weak memory
1011  * ordering, so that we cannot miss seeing is_set if a notification
1012  * has already been queued.
1013  */
1014  if (set->latch && set->latch->is_set)
1015  {
1016  occurred_events->fd = PGINVALID_SOCKET;
1017  occurred_events->pos = set->latch_pos;
1018  occurred_events->user_data =
1019  set->events[set->latch_pos].user_data;
1020  occurred_events->events = WL_LATCH_SET;
1021  occurred_events++;
1022  returned_events++;
1023 
1024  break;
1025  }
1026 
1027  /*
1028  * Wait for events using the readiness primitive chosen at the top of
1029  * this file. If -1 is returned, a timeout has occurred, if 0 we have
1030  * to retry, everything >= 1 is the number of returned events.
1031  */
1032  rc = WaitEventSetWaitBlock(set, cur_timeout,
1033  occurred_events, nevents);
1034 
1035  if (rc == -1)
1036  break; /* timeout occurred */
1037  else
1038  returned_events = rc;
1039 
1040  /* If we're not done, update cur_timeout for next iteration */
1041  if (returned_events == 0 && timeout >= 0)
1042  {
1043  INSTR_TIME_SET_CURRENT(cur_time);
1044  INSTR_TIME_SUBTRACT(cur_time, start_time);
1045  cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
1046  if (cur_timeout <= 0)
1047  break;
1048  }
1049  }
1050 #ifndef WIN32
1051  waiting = false;
1052 #endif
1053 
1055 
1056  return returned_events;
1057 }
pgsocket fd
Definition: latch.h:145
int pos
Definition: latch.h:143
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:202
struct timeval instr_time
Definition: instr_time.h:150
static time_t start_time
Definition: pg_ctl.c:99
void pgwin32_dispatch_queued_signals(void)
Definition: signal.c:108
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
uint32 events
Definition: latch.h:144
static int WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout, WaitEvent *occurred_events, int nevents)
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1342
#define PGINVALID_SOCKET
Definition: port.h:33
#define Assert(condition)
Definition: c.h:732
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1318
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
void * user_data
Definition: latch.h:146
sig_atomic_t is_set
Definition: latch.h:112
Latch * latch
Definition: latch.c:93
#define WL_LATCH_SET
Definition: latch.h:124
static volatile sig_atomic_t waiting
Definition: latch.c:123

◆ WaitEventSetWaitBlock()

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

Referenced by WaitEventSetWait().

◆ WaitLatch()

◆ WaitLatchOrSocket()

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

Definition at line 369 of file latch.c.

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

Referenced by be_tls_open_server(), copy_read_data(), libpqrcv_connect(), libpqrcv_PQgetResult(), LogicalRepApplyLoop(), pgfdw_get_cleanup_result(), pgfdw_get_result(), PgstatCollectorMain(), read_or_wait(), secure_open_gssapi(), WaitLatch(), WalReceiverMain(), WalSndLoop(), WalSndWaitForWal(), and WalSndWriteData().

371 {
372  int ret = 0;
373  int rc;
374  WaitEvent event;
376 
377  if (wakeEvents & WL_TIMEOUT)
378  Assert(timeout >= 0);
379  else
380  timeout = -1;
381 
382  if (wakeEvents & WL_LATCH_SET)
383  AddWaitEventToSet(set, WL_LATCH_SET, PGINVALID_SOCKET,
384  latch, NULL);
385 
386  /* Postmaster-managed callers must handle postmaster death somehow. */
388  (wakeEvents & WL_EXIT_ON_PM_DEATH) ||
389  (wakeEvents & WL_POSTMASTER_DEATH));
390 
391  if ((wakeEvents & WL_POSTMASTER_DEATH) && IsUnderPostmaster)
392  AddWaitEventToSet(set, WL_POSTMASTER_DEATH, PGINVALID_SOCKET,
393  NULL, NULL);
394 
395  if ((wakeEvents & WL_EXIT_ON_PM_DEATH) && IsUnderPostmaster)
396  AddWaitEventToSet(set, WL_EXIT_ON_PM_DEATH, PGINVALID_SOCKET,
397  NULL, NULL);
398 
399  if (wakeEvents & WL_SOCKET_MASK)
400  {
401  int ev;
402 
403  ev = wakeEvents & WL_SOCKET_MASK;
404  AddWaitEventToSet(set, ev, sock, NULL, NULL);
405  }
406 
407  rc = WaitEventSetWait(set, timeout, &event, 1, wait_event_info);
408 
409  if (rc == 0)
410  ret |= WL_TIMEOUT;
411  else
412  {
413  ret |= event.events & (WL_LATCH_SET |
414  WL_POSTMASTER_DEATH |
416  }
417 
418  FreeWaitEventSet(set);
419 
420  return ret;
421 }
void FreeWaitEventSet(WaitEventSet *set)
Definition: latch.c:630
#define WL_TIMEOUT
Definition: latch.h:127
int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
Definition: latch.c:690
#define WL_SOCKET_MASK
Definition: latch.h:137
WaitEventSet * CreateWaitEventSet(MemoryContext context, int nevents)
Definition: latch.c:542
bool IsUnderPostmaster
Definition: globals.c:109
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
#define PGINVALID_SOCKET
Definition: port.h:33
#define Assert(condition)
Definition: c.h:732
#define WL_LATCH_SET
Definition: latch.h:124
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
Definition: latch.c:953

Variable Documentation

◆ selfpipe_owner_pid

int selfpipe_owner_pid = 0
static

Definition at line 130 of file latch.c.

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

◆ selfpipe_readfd

int selfpipe_readfd = -1
static

◆ selfpipe_writefd

int selfpipe_writefd = -1
static

Definition at line 127 of file latch.c.

Referenced by InitializeLatchSupport(), and sendSelfPipeByte().

◆ waiting

volatile sig_atomic_t waiting = false
static