PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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  * To wake up the waiter, you must first set a global flag or something
56  * else that the wait loop tests in the "if (work to do)" part, and call
57  * SetLatch *after* that. SetLatch is designed to return quickly if the
58  * latch is already set.
59  *
60  * On some platforms, signals will not interrupt the latch wait primitive
61  * by themselves. Therefore, it is critical that any signal handler that
62  * is meant to terminate a WaitLatch wait calls SetLatch.
63  *
64  * Note that use of the process latch (PGPROC.procLatch) is generally better
65  * than an ad-hoc shared latch for signaling auxiliary processes. This is
66  * because generic signal handlers will call SetLatch on the process latch
67  * only, so using any latch other than the process latch effectively precludes
68  * use of any generic handler.
69  *
70  *
71  * WaitEventSets allow to wait for latches being set and additional events -
72  * postmaster dying and socket readiness of several sockets currently - at the
73  * same time. On many platforms using a long lived event set is more
74  * efficient than using WaitLatch or WaitLatchOrSocket.
75  *
76  *
77  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
78  * Portions Copyright (c) 1994, Regents of the University of California
79  *
80  * src/include/storage/latch.h
81  *
82  *-------------------------------------------------------------------------
83  */
84 #ifndef LATCH_H
85 #define LATCH_H
86 
87 #include <signal.h>
88 
89 /*
90  * Latch structure should be treated as opaque and only accessed through
91  * the public functions. It is defined here to allow embedding Latches as
92  * part of bigger structs.
93  */
94 typedef struct Latch
95 {
96  sig_atomic_t is_set;
97  bool is_shared;
98  int owner_pid;
99 #ifdef WIN32
100  HANDLE event;
101 #endif
102 } Latch;
103 
104 /*
105  * Bitmasks for events that may wake-up WaitLatch(), WaitLatchOrSocket(), or
106  * WaitEventSetWait().
107  */
108 #define WL_LATCH_SET (1 << 0)
109 #define WL_SOCKET_READABLE (1 << 1)
110 #define WL_SOCKET_WRITEABLE (1 << 2)
111 #define WL_TIMEOUT (1 << 3) /* not for WaitEventSetWait() */
112 #define WL_POSTMASTER_DEATH (1 << 4)
113 
114 typedef struct WaitEvent
115 {
116  int pos; /* position in the event data structure */
117  uint32 events; /* triggered events */
118  pgsocket fd; /* socket fd associated with event */
119  void *user_data; /* pointer provided in AddWaitEventToSet */
120 } WaitEvent;
121 
122 /* forward declaration to avoid exposing latch.c implementation details */
123 typedef struct WaitEventSet WaitEventSet;
124 
125 /*
126  * prototypes for functions in latch.c
127  */
128 extern void InitializeLatchSupport(void);
129 extern void InitLatch(volatile Latch *latch);
130 extern void InitSharedLatch(volatile Latch *latch);
131 extern void OwnLatch(volatile Latch *latch);
132 extern void DisownLatch(volatile Latch *latch);
133 extern void SetLatch(volatile Latch *latch);
134 extern void ResetLatch(volatile Latch *latch);
135 
137 extern void FreeWaitEventSet(WaitEventSet *set);
139  Latch *latch, void *user_data);
140 extern void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch);
141 
142 extern int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents);
143 extern int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout);
144 extern int WaitLatchOrSocket(volatile Latch *latch, int wakeEvents,
145  pgsocket sock, long timeout);
146 
147 /*
148  * Unix implementation uses SIGUSR1 for inter-process signaling.
149  * Win32 doesn't need this.
150  */
151 #ifndef WIN32
152 extern void latch_sigusr1_handler(void);
153 #else
154 #define latch_sigusr1_handler() ((void) 0)
155 #endif
156 
157 #endif /* LATCH_H */
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout)
Definition: latch.c:300
pgsocket fd
Definition: latch.h:118
void DisownLatch(volatile Latch *latch)
Definition: latch.c:272
int pos
Definition: latch.h:116
WaitEventSet * CreateWaitEventSet(MemoryContext context, int nevents)
Definition: latch.c:482
int WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock, long timeout)
Definition: latch.c:318
bool is_shared
Definition: latch.h:97
void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
Definition: latch.c:674
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void FreeWaitEventSet(WaitEventSet *set)
Definition: latch.c:548
void latch_sigusr1_handler(void)
Definition: latch.c:1500
int AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, void *user_data)
Definition: latch.c:604
struct Latch Latch
void InitLatch(volatile Latch *latch)
Definition: latch.c:188
uint32 events
Definition: latch.h:117
Definition: latch.h:94
void OwnLatch(volatile Latch *latch)
Definition: latch.c:252
unsigned int uint32
Definition: c.h:265
int pgsocket
Definition: port.h:22
void InitSharedLatch(volatile Latch *latch)
Definition: latch.c:216
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents)
Definition: latch.c:859
int nevents
Definition: latch.c:89
WaitEvent * events
Definition: latch.c:96
void SetLatch(volatile Latch *latch)
Definition: latch.c:377
void * user_data
Definition: latch.h:119
struct WaitEvent WaitEvent
int owner_pid
Definition: latch.h:98
sig_atomic_t is_set
Definition: latch.h:96
void ResetLatch(volatile Latch *latch)
Definition: latch.c:459
void InitializeLatchSupport(void)
Definition: latch.c:156
Latch * latch
Definition: latch.c:104