Skip to content

Commit a042ba2

Browse files
committed
Introduce symbolic names for FeBeWaitSet positions.
Previously we used 0 and 1 to refer to the socket and latch in far flung parts of the tree, without any explanation. Also use PGINVALID_SOCKET rather than -1 in a couple of places that didn't already do that. Reviewed-by: Kyotaro Horiguchi <horikyota.ntt@gmail.com> Discussion: https://postgr.es/m/CA%2BhUKGJAC4Oqao%3DqforhNey20J8CiG2R%3DoBPqvfR0vOJrFysGw%40mail.gmail.com
1 parent cf54e04 commit a042ba2

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

src/backend/libpq/be-secure.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ secure_read(Port *port, void *ptr, size_t len)
180180

181181
Assert(waitfor);
182182

183-
ModifyWaitEvent(FeBeWaitSet, 0, waitfor, NULL);
183+
ModifyWaitEvent(FeBeWaitSet, FeBeWaitSetSocketPos, waitfor, NULL);
184184

185185
WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1,
186186
WAIT_EVENT_CLIENT_READ);
@@ -292,7 +292,7 @@ secure_write(Port *port, void *ptr, size_t len)
292292

293293
Assert(waitfor);
294294

295-
ModifyWaitEvent(FeBeWaitSet, 0, waitfor, NULL);
295+
ModifyWaitEvent(FeBeWaitSet, FeBeWaitSetSocketPos, waitfor, NULL);
296296

297297
WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1,
298298
WAIT_EVENT_CLIENT_WRITE);

src/backend/libpq/pqcomm.c

+15-3
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ WaitEventSet *FeBeWaitSet;
191191
void
192192
pq_init(void)
193193
{
194+
int socket_pos PG_USED_FOR_ASSERTS_ONLY;
195+
int latch_pos PG_USED_FOR_ASSERTS_ONLY;
196+
194197
/* initialize state variables */
195198
PqSendBufferSize = PQ_SEND_BUFFER_SIZE;
196199
PqSendBuffer = MemoryContextAlloc(TopMemoryContext, PqSendBufferSize);
@@ -219,10 +222,19 @@ pq_init(void)
219222
#endif
220223

221224
FeBeWaitSet = CreateWaitEventSet(TopMemoryContext, 3);
222-
AddWaitEventToSet(FeBeWaitSet, WL_SOCKET_WRITEABLE, MyProcPort->sock,
225+
socket_pos = AddWaitEventToSet(FeBeWaitSet, WL_SOCKET_WRITEABLE,
226+
MyProcPort->sock, NULL, NULL);
227+
latch_pos = AddWaitEventToSet(FeBeWaitSet, WL_LATCH_SET, PGINVALID_SOCKET,
228+
MyLatch, NULL);
229+
AddWaitEventToSet(FeBeWaitSet, WL_POSTMASTER_DEATH, PGINVALID_SOCKET,
223230
NULL, NULL);
224-
AddWaitEventToSet(FeBeWaitSet, WL_LATCH_SET, -1, MyLatch, NULL);
225-
AddWaitEventToSet(FeBeWaitSet, WL_POSTMASTER_DEATH, -1, NULL, NULL);
231+
232+
/*
233+
* The event positions match the order we added them, but let's sanity
234+
* check them to be sure.
235+
*/
236+
Assert(socket_pos == FeBeWaitSetSocketPos);
237+
Assert(latch_pos == FeBeWaitSetLatchPos);
226238
}
227239

228240
/* --------------------------------

src/backend/utils/init/miscinit.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ SwitchToSharedLatch(void)
202202
MyLatch = &MyProc->procLatch;
203203

204204
if (FeBeWaitSet)
205-
ModifyWaitEvent(FeBeWaitSet, 1, WL_LATCH_SET, MyLatch);
205+
ModifyWaitEvent(FeBeWaitSet, FeBeWaitSetLatchPos, WL_LATCH_SET,
206+
MyLatch);
206207

207208
/*
208209
* Set the shared latch as the local one might have been set. This
@@ -221,7 +222,8 @@ SwitchBackToLocalLatch(void)
221222
MyLatch = &LocalLatchData;
222223

223224
if (FeBeWaitSet)
224-
ModifyWaitEvent(FeBeWaitSet, 1, WL_LATCH_SET, MyLatch);
225+
ModifyWaitEvent(FeBeWaitSet, FeBeWaitSetLatchPos, WL_LATCH_SET,
226+
MyLatch);
225227

226228
SetLatch(MyLatch);
227229
}

src/include/libpq/libpq.h

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ extern const PGDLLIMPORT PQcommMethods *PqCommMethods;
5555
*/
5656
extern WaitEventSet *FeBeWaitSet;
5757

58+
#define FeBeWaitSetSocketPos 0
59+
#define FeBeWaitSetLatchPos 1
60+
5861
extern int StreamServerPort(int family, const char *hostName,
5962
unsigned short portNumber, const char *unixSocketDir,
6063
pgsocket ListenSocket[], int MaxListen);

0 commit comments

Comments
 (0)