PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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)
 

Typedefs

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

Functions

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

Macro Definition Documentation

Typedef Documentation

Definition at line 142 of file latch.h.

Function Documentation

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

Definition at line 601 of file latch.c.

References Assert, elog, ERROR, WaitEventSet::events, 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, WL_LATCH_SET, WL_POSTMASTER_DEATH, WL_SOCKET_READABLE, and WL_SOCKET_WRITEABLE.

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

603 {
604  WaitEvent *event;
605 
606  /* not enough space */
607  Assert(set->nevents < set->nevents_space);
608 
609  if (latch)
610  {
611  if (latch->owner_pid != MyProcPid)
612  elog(ERROR, "cannot wait on a latch owned by another process");
613  if (set->latch)
614  elog(ERROR, "cannot wait on more than one latch");
615  if ((events & WL_LATCH_SET) != WL_LATCH_SET)
616  elog(ERROR, "latch events only support being set");
617  }
618  else
619  {
620  if (events & WL_LATCH_SET)
621  elog(ERROR, "cannot wait on latch without a specified latch");
622  }
623 
624  /* waiting for socket readiness without a socket indicates a bug */
625  if (fd == PGINVALID_SOCKET &&
627  elog(ERROR, "cannot wait on socket event without a socket");
628 
629  event = &set->events[set->nevents];
630  event->pos = set->nevents++;
631  event->fd = fd;
632  event->events = events;
633  event->user_data = user_data;
634 #ifdef WIN32
635  event->reset = false;
636 #endif
637 
638  if (events == WL_LATCH_SET)
639  {
640  set->latch = latch;
641  set->latch_pos = event->pos;
642 #ifndef WIN32
643  event->fd = selfpipe_readfd;
644 #endif
645  }
646  else if (events == WL_POSTMASTER_DEATH)
647  {
648 #ifndef WIN32
650 #endif
651  }
652 
653  /* perform wait primitive specific initialization, if needed */
654 #if defined(WAIT_USE_EPOLL)
655  WaitEventAdjustEpoll(set, event, EPOLL_CTL_ADD);
656 #elif defined(WAIT_USE_POLL)
657  WaitEventAdjustPoll(set, event);
658 #elif defined(WAIT_USE_WIN32)
659  WaitEventAdjustWin32(set, event);
660 #endif
661 
662  return event->pos;
663 }
int latch_pos
Definition: latch.c:93
#define WL_SOCKET_WRITEABLE
Definition: latch.h:126
int MyProcPid
Definition: globals.c:38
int pos
Definition: latch.h:132
#define WL_SOCKET_READABLE
Definition: latch.h:125
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:118
int nevents
Definition: latch.c:77
int postmaster_alive_fds[2]
Definition: postmaster.c:557
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
#define PGINVALID_SOCKET
Definition: port.h:24
#define Assert(condition)
Definition: c.h:675
WaitEvent * events
Definition: latch.c:84
int nevents_space
Definition: latch.c:78
int owner_pid
Definition: latch.h:114
#define elog
Definition: elog.h:219
Latch * latch
Definition: latch.c:92
#define WL_LATCH_SET
Definition: latch.h:124
#define POSTMASTER_FD_WATCH
Definition: postmaster.h:42
WaitEventSet* CreateWaitEventSet ( MemoryContext  context,
int  nevents 
)

Definition at line 473 of file latch.c.

References elog, ERROR, WaitEventSet::events, WaitEventSet::latch, MAXALIGN, MemoryContextAllocZero(), WaitEventSet::nevents_space, NULL, pgwin32_signal_event, and StaticAssertStmt.

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

474 {
475  WaitEventSet *set;
476  char *data;
477  Size sz = 0;
478 
479  /*
480  * Use MAXALIGN size/alignment to guarantee that later uses of memory are
481  * aligned correctly. E.g. epoll_event might need 8 byte alignment on some
482  * platforms, but earlier allocations like WaitEventSet and WaitEvent
483  * might not sized to guarantee that when purely using sizeof().
484  */
485  sz += MAXALIGN(sizeof(WaitEventSet));
486  sz += MAXALIGN(sizeof(WaitEvent) * nevents);
487 
488 #if defined(WAIT_USE_EPOLL)
489  sz += MAXALIGN(sizeof(struct epoll_event) * nevents);
490 #elif defined(WAIT_USE_POLL)
491  sz += MAXALIGN(sizeof(struct pollfd) * nevents);
492 #elif defined(WAIT_USE_WIN32)
493  /* need space for the pgwin32_signal_event */
494  sz += MAXALIGN(sizeof(HANDLE) * (nevents + 1));
495 #endif
496 
497  data = (char *) MemoryContextAllocZero(context, sz);
498 
499  set = (WaitEventSet *) data;
500  data += MAXALIGN(sizeof(WaitEventSet));
501 
502  set->events = (WaitEvent *) data;
503  data += MAXALIGN(sizeof(WaitEvent) * nevents);
504 
505 #if defined(WAIT_USE_EPOLL)
506  set->epoll_ret_events = (struct epoll_event *) data;
507  data += MAXALIGN(sizeof(struct epoll_event) * nevents);
508 #elif defined(WAIT_USE_POLL)
509  set->pollfds = (struct pollfd *) data;
510  data += MAXALIGN(sizeof(struct pollfd) * nevents);
511 #elif defined(WAIT_USE_WIN32)
512  set->handles = (HANDLE) data;
513  data += MAXALIGN(sizeof(HANDLE) * nevents);
514 #endif
515 
516  set->latch = NULL;
517  set->nevents_space = nevents;
518 
519 #if defined(WAIT_USE_EPOLL)
520  set->epoll_fd = epoll_create(nevents);
521  if (set->epoll_fd < 0)
522  elog(ERROR, "epoll_create failed: %m");
523 #elif defined(WAIT_USE_WIN32)
524 
525  /*
526  * To handle signals while waiting, we need to add a win32 specific event.
527  * We accounted for the additional event at the top of this routine. See
528  * port/win32/signal.c for more details.
529  *
530  * Note: pgwin32_signal_event should be first to ensure that it will be
531  * reported when multiple events are set. We want to guarantee that
532  * pending signals are serviced.
533  */
534  set->handles[0] = pgwin32_signal_event;
535  StaticAssertStmt(WSA_INVALID_EVENT == NULL, "");
536 #endif
537 
538  return set;
539 }
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:757
HANDLE pgwin32_signal_event
Definition: signal.c:27
#define ERROR
Definition: elog.h:43
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:742
#define NULL
Definition: c.h:229
WaitEvent * events
Definition: latch.c:84
size_t Size
Definition: c.h:356
#define MAXALIGN(LEN)
Definition: c.h:588
int nevents_space
Definition: latch.c:78
#define elog
Definition: elog.h:219
Latch * latch
Definition: latch.c:92
void DisownLatch ( volatile Latch latch)

Definition at line 260 of file latch.c.

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

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

261 {
262  Assert(latch->is_shared);
263  Assert(latch->owner_pid == MyProcPid);
264 
265  latch->owner_pid = 0;
266 }
int MyProcPid
Definition: globals.c:38
#define Assert(condition)
Definition: c.h:675
void FreeWaitEventSet ( WaitEventSet set)

Definition at line 545 of file latch.c.

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

Referenced by WaitLatchOrSocket().

546 {
547 #if defined(WAIT_USE_EPOLL)
548  close(set->epoll_fd);
549 #elif defined(WAIT_USE_WIN32)
550  WaitEvent *cur_event;
551 
552  for (cur_event = set->events;
553  cur_event < (set->events + set->nevents);
554  cur_event++)
555  {
556  if (cur_event->events & WL_LATCH_SET)
557  {
558  /* uses the latch's HANDLE */
559  }
560  else if (cur_event->events & WL_POSTMASTER_DEATH)
561  {
562  /* uses PostmasterHandle */
563  }
564  else
565  {
566  /* Clean up the event object we created for the socket */
567  WSAEventSelect(cur_event->fd, NULL, 0);
568  WSACloseEvent(set->handles[cur_event->pos + 1]);
569  }
570  }
571 #endif
572 
573  pfree(set);
574 }
pgsocket fd
Definition: latch.h:134
int pos
Definition: latch.h:132
void pfree(void *pointer)
Definition: mcxt.c:950
uint32 events
Definition: latch.h:133
int nevents
Definition: latch.c:77
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
#define NULL
Definition: c.h:229
WaitEvent * events
Definition: latch.c:84
#define close(a)
Definition: win32.h:12
#define WL_LATCH_SET
Definition: latch.h:124
void InitializeLatchSupport ( void  )

Definition at line 144 of file latch.c.

References Assert, elog, FATAL, selfpipe_readfd, and selfpipe_writefd.

Referenced by InitPostmasterChild(), and InitStandaloneProcess().

145 {
146 #ifndef WIN32
147  int pipefd[2];
148 
149  Assert(selfpipe_readfd == -1);
150 
151  /*
152  * Set up the self-pipe that allows a signal handler to wake up the
153  * poll()/epoll_wait() in WaitLatch. Make the write-end non-blocking, so
154  * that SetLatch won't block if the event has already been set many times
155  * filling the kernel buffer. Make the read-end non-blocking too, so that
156  * we can easily clear the pipe by reading until EAGAIN or EWOULDBLOCK.
157  */
158  if (pipe(pipefd) < 0)
159  elog(FATAL, "pipe() failed: %m");
160  if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) == -1)
161  elog(FATAL, "fcntl() failed on read-end of self-pipe: %m");
162  if (fcntl(pipefd[1], F_SETFL, O_NONBLOCK) == -1)
163  elog(FATAL, "fcntl() failed on write-end of self-pipe: %m");
164 
165  selfpipe_readfd = pipefd[0];
166  selfpipe_writefd = pipefd[1];
167 #else
168  /* currently, nothing to do here for Windows */
169 #endif
170 }
static int selfpipe_writefd
Definition: latch.c:119
#define FATAL
Definition: elog.h:52
static int selfpipe_readfd
Definition: latch.c:118
#define Assert(condition)
Definition: c.h:675
#define elog
Definition: elog.h:219
void InitLatch ( volatile Latch latch)

Definition at line 176 of file latch.c.

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

Referenced by InitPostmasterChild(), and InitStandaloneProcess().

177 {
178  latch->is_set = false;
179  latch->owner_pid = MyProcPid;
180  latch->is_shared = false;
181 
182 #ifndef WIN32
183  /* Assert InitializeLatchSupport has been called in this process */
184  Assert(selfpipe_readfd >= 0);
185 #else
186  latch->event = CreateEvent(NULL, TRUE, FALSE, NULL);
187  if (latch->event == NULL)
188  elog(ERROR, "CreateEvent failed: error code %lu", GetLastError());
189 #endif /* WIN32 */
190 }
int MyProcPid
Definition: globals.c:38
#define ERROR
Definition: elog.h:43
#define FALSE
Definition: c.h:221
static int selfpipe_readfd
Definition: latch.c:118
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define TRUE
Definition: c.h:217
#define elog
Definition: elog.h:219
void InitSharedLatch ( volatile Latch latch)

Definition at line 204 of file latch.c.

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

Referenced by InitProcGlobal(), and XLOGShmemInit().

205 {
206 #ifdef WIN32
207  SECURITY_ATTRIBUTES sa;
208 
209  /*
210  * Set up security attributes to specify that the events are inherited.
211  */
212  ZeroMemory(&sa, sizeof(sa));
213  sa.nLength = sizeof(sa);
214  sa.bInheritHandle = TRUE;
215 
216  latch->event = CreateEvent(&sa, TRUE, FALSE, NULL);
217  if (latch->event == NULL)
218  elog(ERROR, "CreateEvent failed: error code %lu", GetLastError());
219 #endif
220 
221  latch->is_set = false;
222  latch->owner_pid = 0;
223  latch->is_shared = true;
224 }
#define ERROR
Definition: elog.h:43
#define FALSE
Definition: c.h:221
#define NULL
Definition: c.h:229
#define TRUE
Definition: c.h:217
#define elog
Definition: elog.h:219
void latch_sigusr1_handler ( void  )

Definition at line 1404 of file latch.c.

References sendSelfPipeByte(), and waiting.

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

1405 {
1406  if (waiting)
1407  sendSelfPipeByte();
1408 }
static void sendSelfPipeByte(void)
Definition: latch.c:1414
static volatile sig_atomic_t waiting
Definition: latch.c:115
void ModifyWaitEvent ( WaitEventSet set,
int  pos,
uint32  events,
Latch latch 
)

Definition at line 672 of file latch.c.

References Assert, elog, ERROR, WaitEventSet::events, WaitEvent::events, WaitEventSet::latch, WL_LATCH_SET, and WL_POSTMASTER_DEATH.

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

673 {
674  WaitEvent *event;
675 
676  Assert(pos < set->nevents);
677 
678  event = &set->events[pos];
679 
680  /*
681  * If neither the event mask nor the associated latch changes, return
682  * early. That's an important optimization for some sockets, where
683  * ModifyWaitEvent is frequently used to switch from waiting for reads to
684  * waiting on writes.
685  */
686  if (events == event->events &&
687  (!(event->events & WL_LATCH_SET) || set->latch == latch))
688  return;
689 
690  if (event->events & WL_LATCH_SET &&
691  events != event->events)
692  {
693  /* we could allow to disable latch events for a while */
694  elog(ERROR, "cannot modify latch event");
695  }
696 
697  if (event->events & WL_POSTMASTER_DEATH)
698  {
699  elog(ERROR, "cannot modify postmaster death event");
700  }
701 
702  /* FIXME: validate event mask */
703  event->events = events;
704 
705  if (events == WL_LATCH_SET)
706  {
707  set->latch = latch;
708  }
709 
710 #if defined(WAIT_USE_EPOLL)
711  WaitEventAdjustEpoll(set, event, EPOLL_CTL_MOD);
712 #elif defined(WAIT_USE_POLL)
713  WaitEventAdjustPoll(set, event);
714 #elif defined(WAIT_USE_WIN32)
715  WaitEventAdjustWin32(set, event);
716 #endif
717 }
#define ERROR
Definition: elog.h:43
uint32 events
Definition: latch.h:133
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
#define Assert(condition)
Definition: c.h:675
WaitEvent * events
Definition: latch.c:84
#define elog
Definition: elog.h:219
Latch * latch
Definition: latch.c:92
#define WL_LATCH_SET
Definition: latch.h:124
void OwnLatch ( volatile Latch latch)

Definition at line 240 of file latch.c.

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

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

241 {
242  /* Sanity checks */
243  Assert(latch->is_shared);
244 
245 #ifndef WIN32
246  /* Assert InitializeLatchSupport has been called in this process */
247  Assert(selfpipe_readfd >= 0);
248 #endif
249 
250  if (latch->owner_pid != 0)
251  elog(ERROR, "latch already owned");
252 
253  latch->owner_pid = MyProcPid;
254 }
int MyProcPid
Definition: globals.c:38
#define ERROR
Definition: elog.h:43
static int selfpipe_readfd
Definition: latch.c:118
#define Assert(condition)
Definition: c.h:675
#define elog
Definition: elog.h:219
void ResetLatch ( volatile Latch latch)

Definition at line 450 of file latch.c.

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

Referenced by ApplyLauncherMain(), AutoVacLauncherMain(), BackgroundWriterMain(), CheckpointerMain(), ConditionVariablePrepareToSleep(), ConditionVariableSleep(), copy_read_data(), gather_readnext(), libpqrcv_connect(), libpqrcv_PQexec(), logicalrep_worker_stop(), LogicalRepApplyLoop(), mq_putmessage(), pg_sleep(), pgarch_MainLoop(), 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_sync_status_change(), wait_for_workers_to_become_ready(), WaitForBackgroundWorkerShutdown(), WaitForBackgroundWorkerStartup(), WaitForParallelWorkersToFinish(), WaitForReplicationWorkerAttach(), WaitForWALToBecomeAvailable(), WalRcvWaitForStartPosition(), WalReceiverMain(), WalSndLoop(), WalSndWaitForWal(), WalSndWriteData(), and WalWriterMain().

451 {
452  /* Only the owner should reset the latch */
453  Assert(latch->owner_pid == MyProcPid);
454 
455  latch->is_set = false;
456 
457  /*
458  * Ensure that the write to is_set gets flushed to main memory before we
459  * examine any flag variables. Otherwise a concurrent SetLatch might
460  * falsely conclude that it needn't signal us, even though we have missed
461  * seeing some flag updates that SetLatch was supposed to inform us of.
462  */
464 }
int MyProcPid
Definition: globals.c:38
#define pg_memory_barrier()
Definition: atomics.h:148
#define Assert(condition)
Definition: c.h:675
void SetLatch ( volatile Latch latch)

Definition at line 367 of file latch.c.

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

Referenced by ArchSigHupHandler(), ArchSigTermHandler(), av_sighup_handler(), avl_sigterm_handler(), avl_sigusr2_handler(), BgSigHupHandler(), CheckDeadLockAlert(), ChkptSigHupHandler(), ConditionVariableSignal(), die(), ForwardFsyncRequest(), handle_sig_alarm(), handle_sigterm(), HandleCatchupInterrupt(), HandleNotifyInterrupt(), HandleParallelMessageInterrupt(), IdleInTransactionSessionTimeoutHandler(), logicalrep_worker_sighup(), logicalrep_worker_sigterm(), logicalrep_worker_wakeup_ptr(), pgarch_waken(), pgarch_waken_stop(), pgstat_exit(), pgstat_sighup_handler(), ProcSendSignal(), procsignal_sigusr1_handler(), ProcWakeup(), RecoveryConflictInterrupt(), ReqCheckpointHandler(), ReqShutdownHandler(), RequestXLogStreaming(), shm_mq_detach(), shm_mq_inc_bytes_read(), shm_mq_notify_receiver(), shm_mq_set_receiver(), shm_mq_set_sender(), sigHupHandler(), SigHupHandler(), sigUsr1Handler(), StatementCancelHandler(), StrategyGetBuffer(), SwitchBackToLocalLatch(), SwitchToSharedLatch(), SyncRepWakeQueue(), test_shm_mq_main(), WakeupRecovery(), WalRcvForceReply(), WalRcvShutdownHandler(), WalShutdownHandler(), WalSigHupHandler(), WalSndLastCycleHandler(), WalSndSigHupHandler(), WalSndWaitForWal(), WalSndWakeup(), WalSndWriteData(), worker_spi_sighup(), worker_spi_sigterm(), and XLogSetAsyncXactLSN().

368 {
369 #ifndef WIN32
370  pid_t owner_pid;
371 #else
372  HANDLE handle;
373 #endif
374 
375  /*
376  * The memory barrier has to be placed here to ensure that any flag
377  * variables possibly changed by this process have been flushed to main
378  * memory, before we check/set is_set.
379  */
381 
382  /* Quick exit if already set */
383  if (latch->is_set)
384  return;
385 
386  latch->is_set = true;
387 
388 #ifndef WIN32
389 
390  /*
391  * See if anyone's waiting for the latch. It can be the current process if
392  * we're in a signal handler. We use the self-pipe to wake up the
393  * poll()/epoll_wait() in that case. If it's another process, send a
394  * signal.
395  *
396  * Fetch owner_pid only once, in case the latch is concurrently getting
397  * owned or disowned. XXX: This assumes that pid_t is atomic, which isn't
398  * guaranteed to be true! In practice, the effective range of pid_t fits
399  * in a 32 bit integer, and so should be atomic. In the worst case, we
400  * might end up signaling the wrong process. Even then, you're very
401  * unlucky if a process with that bogus pid exists and belongs to
402  * Postgres; and PG database processes should handle excess SIGUSR1
403  * interrupts without a problem anyhow.
404  *
405  * Another sort of race condition that's possible here is for a new
406  * process to own the latch immediately after we look, so we don't signal
407  * it. This is okay so long as all callers of ResetLatch/WaitLatch follow
408  * the standard coding convention of waiting at the bottom of their loops,
409  * not the top, so that they'll correctly process latch-setting events
410  * that happen before they enter the loop.
411  */
412  owner_pid = latch->owner_pid;
413  if (owner_pid == 0)
414  return;
415  else if (owner_pid == MyProcPid)
416  {
417  if (waiting)
419  }
420  else
421  kill(owner_pid, SIGUSR1);
422 #else
423 
424  /*
425  * See if anyone's waiting for the latch. It can be the current process if
426  * we're in a signal handler.
427  *
428  * Use a local variable here just in case somebody changes the event field
429  * concurrently (which really should not happen).
430  */
431  handle = latch->event;
432  if (handle)
433  {
434  SetEvent(handle);
435 
436  /*
437  * Note that we silently ignore any errors. We might be in a signal
438  * handler or other critical path where it's not safe to call elog().
439  */
440  }
441 #endif
442 
443 }
#define SIGUSR1
Definition: win32.h:202
int MyProcPid
Definition: globals.c:38
static void sendSelfPipeByte(void)
Definition: latch.c:1414
#define pg_memory_barrier()
Definition: atomics.h:148
static volatile sig_atomic_t waiting
Definition: latch.c:115
int WaitEventSetWait ( WaitEventSet set,
long  timeout,
WaitEvent occurred_events,
int  nevents,
uint32  wait_event_info 
)

Definition at line 855 of file latch.c.

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, 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 ConditionVariableSleep(), secure_read(), secure_write(), and WaitLatchOrSocket().

858 {
859  int returned_events = 0;
861  instr_time cur_time;
862  long cur_timeout = -1;
863 
864  Assert(nevents > 0);
865 
866  /*
867  * Initialize timeout if requested. We must record the current time so
868  * that we can determine the remaining timeout if interrupted.
869  */
870  if (timeout >= 0)
871  {
872  INSTR_TIME_SET_CURRENT(start_time);
873  Assert(timeout >= 0 && timeout <= INT_MAX);
874  cur_timeout = timeout;
875  }
876 
877  pgstat_report_wait_start(wait_event_info);
878 
879 #ifndef WIN32
880  waiting = true;
881 #else
882  /* Ensure that signals are serviced even if latch is already set */
884 #endif
885  while (returned_events == 0)
886  {
887  int rc;
888 
889  /*
890  * Check if the latch is set already. If so, leave the loop
891  * immediately, avoid blocking again. We don't attempt to report any
892  * other events that might also be satisfied.
893  *
894  * If someone sets the latch between this and the
895  * WaitEventSetWaitBlock() below, the setter will write a byte to the
896  * pipe (or signal us and the signal handler will do that), and the
897  * readiness routine will return immediately.
898  *
899  * On unix, If there's a pending byte in the self pipe, we'll notice
900  * whenever blocking. Only clearing the pipe in that case avoids
901  * having to drain it every time WaitLatchOrSocket() is used. Should
902  * the pipe-buffer fill up we're still ok, because the pipe is in
903  * nonblocking mode. It's unlikely for that to happen, because the
904  * self pipe isn't filled unless we're blocking (waiting = true), or
905  * from inside a signal handler in latch_sigusr1_handler().
906  *
907  * On windows, we'll also notice if there's a pending event for the
908  * latch when blocking, but there's no danger of anything filling up,
909  * as "Setting an event that is already set has no effect.".
910  *
911  * Note: we assume that the kernel calls involved in latch management
912  * will provide adequate synchronization on machines with weak memory
913  * ordering, so that we cannot miss seeing is_set if a notification
914  * has already been queued.
915  */
916  if (set->latch && set->latch->is_set)
917  {
918  occurred_events->fd = PGINVALID_SOCKET;
919  occurred_events->pos = set->latch_pos;
920  occurred_events->user_data =
921  set->events[set->latch_pos].user_data;
922  occurred_events->events = WL_LATCH_SET;
923  occurred_events++;
924  returned_events++;
925 
926  break;
927  }
928 
929  /*
930  * Wait for events using the readiness primitive chosen at the top of
931  * this file. If -1 is returned, a timeout has occurred, if 0 we have
932  * to retry, everything >= 1 is the number of returned events.
933  */
934  rc = WaitEventSetWaitBlock(set, cur_timeout,
935  occurred_events, nevents);
936 
937  if (rc == -1)
938  break; /* timeout occurred */
939  else
940  returned_events = rc;
941 
942  /* If we're not done, update cur_timeout for next iteration */
943  if (returned_events == 0 && timeout >= 0)
944  {
945  INSTR_TIME_SET_CURRENT(cur_time);
946  INSTR_TIME_SUBTRACT(cur_time, start_time);
947  cur_timeout = timeout - (long) INSTR_TIME_GET_MILLISEC(cur_time);
948  if (cur_timeout <= 0)
949  break;
950  }
951  }
952 #ifndef WIN32
953  waiting = false;
954 #endif
955 
957 
958  return returned_events;
959 }
int latch_pos
Definition: latch.c:93
pgsocket fd
Definition: latch.h:134
int pos
Definition: latch.h:132
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:199
struct timeval instr_time
Definition: instr_time.h:147
static time_t start_time
Definition: pg_ctl.c:91
void pgwin32_dispatch_queued_signals(void)
Definition: signal.c:107
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:167
uint32 events
Definition: latch.h:133
static int WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout, WaitEvent *occurred_events, int nevents)
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1232
#define PGINVALID_SOCKET
Definition: port.h:24
#define Assert(condition)
Definition: c.h:675
WaitEvent * events
Definition: latch.c:84
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1208
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:153
void * user_data
Definition: latch.h:135
sig_atomic_t is_set
Definition: latch.h:112
Latch * latch
Definition: latch.c:92
#define WL_LATCH_SET
Definition: latch.h:124
static volatile sig_atomic_t waiting
Definition: latch.c:115
int WaitLatchOrSocket ( volatile Latch latch,
int  wakeEvents,
pgsocket  sock,
long  timeout,
uint32  wait_event_info 
)

Definition at line 308 of file latch.c.

References AddWaitEventToSet(), Assert, CreateWaitEventSet(), CurrentMemoryContext, FreeWaitEventSet(), NULL, PGINVALID_SOCKET, WaitEventSetWait(), WL_LATCH_SET, WL_POSTMASTER_DEATH, WL_SOCKET_READABLE, WL_SOCKET_WRITEABLE, and WL_TIMEOUT.

Referenced by be_tls_open_server(), copy_read_data(), libpqrcv_connect(), libpqrcv_PQexec(), LogicalRepApplyLoop(), pgfdw_get_result(), PgstatCollectorMain(), SysLoggerMain(), WaitLatch(), WalReceiverMain(), WalSndLoop(), WalSndWaitForWal(), and WalSndWriteData().

310 {
311  int ret = 0;
312  int rc;
313  WaitEvent event;
315 
316  if (wakeEvents & WL_TIMEOUT)
317  Assert(timeout >= 0);
318  else
319  timeout = -1;
320 
321  if (wakeEvents & WL_LATCH_SET)
322  AddWaitEventToSet(set, WL_LATCH_SET, PGINVALID_SOCKET,
323  (Latch *) latch, NULL);
324 
325  if (wakeEvents & WL_POSTMASTER_DEATH)
326  AddWaitEventToSet(set, WL_POSTMASTER_DEATH, PGINVALID_SOCKET,
327  NULL, NULL);
328 
329  if (wakeEvents & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE))
330  {
331  int ev;
332 
333  ev = wakeEvents & (WL_SOCKET_READABLE | WL_SOCKET_WRITEABLE);
334  AddWaitEventToSet(set, ev, sock, NULL, NULL);
335  }
336 
337  rc = WaitEventSetWait(set, timeout, &event, 1, wait_event_info);
338 
339  if (rc == 0)
340  ret |= WL_TIMEOUT;
341  else
342  {
343  ret |= event.events & (WL_LATCH_SET |
344  WL_POSTMASTER_DEATH |
347  }
348 
349  FreeWaitEventSet(set);
350 
351  return ret;
352 }
#define WL_SOCKET_WRITEABLE
Definition: latch.h:126
void FreeWaitEventSet(WaitEventSet *set)
Definition: latch.c:545
#define WL_TIMEOUT
Definition: latch.h:127
int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
Definition: latch.c:601
#define WL_SOCKET_READABLE
Definition: latch.h:125
WaitEventSet * CreateWaitEventSet(MemoryContext context, int nevents)
Definition: latch.c:473
Definition: latch.h:110
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
#define PGINVALID_SOCKET
Definition: port.h:24
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define WL_LATCH_SET
Definition: latch.h:124
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
Definition: latch.c:855