PostgreSQL Source Code git master
Loading...
Searching...
No Matches
latch.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * latch.h
4 * Routines for interprocess latches
5 *
6 * A latch is a boolean variable, with operations that let processes sleep
7 * until it is set. A latch can be set from another process, or a signal
8 * handler within the same process.
9 *
10 * The latch interface is a reliable replacement for the common pattern of
11 * using pg_usleep() or select() to wait until a signal arrives, where the
12 * signal handler sets a flag variable. Because on some platforms an
13 * incoming signal doesn't interrupt sleep, and even on platforms where it
14 * does there is a race condition if the signal arrives just before
15 * entering the sleep, the common pattern must periodically wake up and
16 * poll the flag variable. The pselect() system call was invented to solve
17 * this problem, but it is not portable enough. Latches are designed to
18 * overcome these limitations, allowing you to sleep without polling and
19 * ensuring quick response to signals from other processes.
20 *
21 * There are two kinds of latches: local and shared. A local latch is
22 * initialized by InitLatch, and can only be set from the same process.
23 * A local latch can be used to wait for a signal to arrive, by calling
24 * SetLatch in the signal handler. A shared latch resides in shared memory,
25 * and must be initialized at postmaster startup by InitSharedLatch. Before
26 * a shared latch can be waited on, it must be associated with a process
27 * with OwnLatch. Only the process owning the latch can wait on it, but any
28 * process can set it.
29 *
30 * There are three basic operations on a latch:
31 *
32 * SetLatch - Sets the latch
33 * ResetLatch - Clears the latch, allowing it to be set again
34 * WaitLatch - Waits for the latch to become set
35 *
36 * WaitLatch includes a provision for timeouts (which should be avoided
37 * when possible, as they incur extra overhead) and a provision for
38 * postmaster child processes to wake up immediately on postmaster death.
39 * See latch.c for detailed specifications for the exported functions.
40 *
41 * The correct pattern to wait for event(s) is:
42 *
43 * for (;;)
44 * {
45 * ResetLatch();
46 * if (work to do)
47 * Do Stuff();
48 * WaitLatch();
49 * }
50 *
51 * It's important to reset the latch *before* checking if there's work to
52 * do. Otherwise, if someone sets the latch between the check and the
53 * ResetLatch call, you will miss it and Wait will incorrectly block.
54 *
55 * Another valid coding pattern looks like:
56 *
57 * for (;;)
58 * {
59 * if (work to do)
60 * Do Stuff(); // in particular, exit loop if some condition satisfied
61 * WaitLatch();
62 * ResetLatch();
63 * }
64 *
65 * This is useful to reduce latch traffic if it's expected that the loop's
66 * termination condition will often be satisfied in the first iteration;
67 * the cost is an extra loop iteration before blocking when it is not.
68 * What must be avoided is placing any checks for asynchronous events after
69 * WaitLatch and before ResetLatch, as that creates a race condition.
70 *
71 * To wake up the waiter, you must first set a global flag or something
72 * else that the wait loop tests in the "if (work to do)" part, and call
73 * SetLatch *after* that. SetLatch is designed to return quickly if the
74 * latch is already set.
75 *
76 * On some platforms, signals will not interrupt the latch wait primitive
77 * by themselves. Therefore, it is critical that any signal handler that
78 * is meant to terminate a WaitLatch wait calls SetLatch.
79 *
80 * Note that use of the process latch (PGPROC.procLatch) is generally better
81 * than an ad-hoc shared latch for signaling auxiliary processes. This is
82 * because generic signal handlers will call SetLatch on the process latch
83 * only, so using any latch other than the process latch effectively precludes
84 * use of any generic handler.
85 *
86 *
87 * See also WaitEventSets in waiteventset.h. They allow to wait for latches
88 * being set and additional events - postmaster dying and socket readiness of
89 * several sockets currently - at the same time. On many platforms using a
90 * long lived event set is more efficient than using WaitLatch or
91 * WaitLatchOrSocket.
92 *
93 *
94 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
95 * Portions Copyright (c) 1994, Regents of the University of California
96 *
97 * src/include/storage/latch.h
98 *
99 *-------------------------------------------------------------------------
100 */
101#ifndef LATCH_H
102#define LATCH_H
103
104#include <signal.h>
105
106#include "storage/waiteventset.h" /* for WL_* arguments to WaitLatch */
107#include "utils/wait_classes.h" /* for backward compatibility */ /* IWYU pragma: keep */
108
109
110/*
111 * Latch structure should be treated as opaque and only accessed through
112 * the public functions. It is defined here to allow embedding Latches as
113 * part of bigger structs.
114 */
115typedef struct Latch
116{
121#ifdef WIN32
122 HANDLE event;
123#endif
125
126/*
127 * prototypes for functions in latch.c
128 */
129extern void InitLatch(Latch *latch);
130extern void InitSharedLatch(Latch *latch);
131extern void OwnLatch(Latch *latch);
132extern void DisownLatch(Latch *latch);
133extern void SetLatch(Latch *latch);
134extern void ResetLatch(Latch *latch);
135
136extern int WaitLatch(Latch *latch, int wakeEvents, long timeout,
137 uint32 wait_event_info);
138extern int WaitLatchOrSocket(Latch *latch, int wakeEvents,
139 pgsocket sock, long timeout, uint32 wait_event_info);
140extern void InitializeLatchWaitSet(void);
141
142#endif /* LATCH_H */
uint32_t uint32
Definition c.h:618
void InitializeLatchWaitSet(void)
Definition latch.c:35
int WaitLatchOrSocket(Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
Definition latch.c:223
void OwnLatch(Latch *latch)
Definition latch.c:126
void DisownLatch(Latch *latch)
Definition latch.c:144
void InitSharedLatch(Latch *latch)
Definition latch.c:93
void SetLatch(Latch *latch)
Definition latch.c:290
void InitLatch(Latch *latch)
Definition latch.c:63
void ResetLatch(Latch *latch)
Definition latch.c:374
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition latch.c:172
int pgsocket
Definition port.h:29
static int fb(int x)
Definition latch.h:116
sig_atomic_t is_set
Definition latch.h:117
sig_atomic_t maybe_sleeping
Definition latch.h:118
bool is_shared
Definition latch.h:119
int owner_pid
Definition latch.h:120