Introduce symbolic names for FeBeWaitSet positions.
authorThomas Munro <tmunro@postgresql.org>
Mon, 1 Mar 2021 02:16:56 +0000 (15:16 +1300)
committerThomas Munro <tmunro@postgresql.org>
Mon, 1 Mar 2021 03:10:16 +0000 (16:10 +1300)
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

src/backend/libpq/be-secure.c
src/backend/libpq/pqcomm.c
src/backend/utils/init/miscinit.c
src/include/libpq/libpq.h

index d1545a2ad6ade3f75240df0583a0e5152cc15c2d..bb603ad209b73d7ca8a6b9ffafb440c54b6d0cf6 100644 (file)
@@ -180,7 +180,7 @@ retry:
 
        Assert(waitfor);
 
-       ModifyWaitEvent(FeBeWaitSet, 0, waitfor, NULL);
+       ModifyWaitEvent(FeBeWaitSet, FeBeWaitSetSocketPos, waitfor, NULL);
 
        WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1,
                         WAIT_EVENT_CLIENT_READ);
@@ -292,7 +292,7 @@ retry:
 
        Assert(waitfor);
 
-       ModifyWaitEvent(FeBeWaitSet, 0, waitfor, NULL);
+       ModifyWaitEvent(FeBeWaitSet, FeBeWaitSetSocketPos, waitfor, NULL);
 
        WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1,
                         WAIT_EVENT_CLIENT_WRITE);
index 1e6b6db54002f77d4603adb66d3e1abc42daf993..27a298f11013f3a5545f9c7478ee601497c7a5e6 100644 (file)
@@ -191,6 +191,9 @@ WaitEventSet *FeBeWaitSet;
 void
 pq_init(void)
 {
+   int     socket_pos PG_USED_FOR_ASSERTS_ONLY;
+   int     latch_pos PG_USED_FOR_ASSERTS_ONLY;
+
    /* initialize state variables */
    PqSendBufferSize = PQ_SEND_BUFFER_SIZE;
    PqSendBuffer = MemoryContextAlloc(TopMemoryContext, PqSendBufferSize);
@@ -219,10 +222,19 @@ pq_init(void)
 #endif
 
    FeBeWaitSet = CreateWaitEventSet(TopMemoryContext, 3);
-   AddWaitEventToSet(FeBeWaitSet, WL_SOCKET_WRITEABLE, MyProcPort->sock,
+   socket_pos = AddWaitEventToSet(FeBeWaitSet, WL_SOCKET_WRITEABLE,
+                                  MyProcPort->sock, NULL, NULL);
+   latch_pos = AddWaitEventToSet(FeBeWaitSet, WL_LATCH_SET, PGINVALID_SOCKET,
+                                 MyLatch, NULL);
+   AddWaitEventToSet(FeBeWaitSet, WL_POSTMASTER_DEATH, PGINVALID_SOCKET,
                      NULL, NULL);
-   AddWaitEventToSet(FeBeWaitSet, WL_LATCH_SET, -1, MyLatch, NULL);
-   AddWaitEventToSet(FeBeWaitSet, WL_POSTMASTER_DEATH, -1, NULL, NULL);
+
+   /*
+    * The event positions match the order we added them, but let's sanity
+    * check them to be sure.
+    */
+   Assert(socket_pos == FeBeWaitSetSocketPos);
+   Assert(latch_pos == FeBeWaitSetLatchPos);
 }
 
 /* --------------------------------
index e6550f99eec718fe556d02b8622d742be6c0486a..8b73850d0df10a9e3c5fec3aca3ca1deda006d9b 100644 (file)
@@ -202,7 +202,8 @@ SwitchToSharedLatch(void)
    MyLatch = &MyProc->procLatch;
 
    if (FeBeWaitSet)
-       ModifyWaitEvent(FeBeWaitSet, 1, WL_LATCH_SET, MyLatch);
+       ModifyWaitEvent(FeBeWaitSet, FeBeWaitSetLatchPos, WL_LATCH_SET,
+                       MyLatch);
 
    /*
     * Set the shared latch as the local one might have been set. This
@@ -221,7 +222,8 @@ SwitchBackToLocalLatch(void)
    MyLatch = &LocalLatchData;
 
    if (FeBeWaitSet)
-       ModifyWaitEvent(FeBeWaitSet, 1, WL_LATCH_SET, MyLatch);
+       ModifyWaitEvent(FeBeWaitSet, FeBeWaitSetLatchPos, WL_LATCH_SET,
+                       MyLatch);
 
    SetLatch(MyLatch);
 }
index b41b10620aae9766407060f8173a2948c58dd9cc..e4e5c2156553699f1fc5674495df64e2b61c6cee 100644 (file)
@@ -55,6 +55,9 @@ extern const PGDLLIMPORT PQcommMethods *PqCommMethods;
  */
 extern WaitEventSet *FeBeWaitSet;
 
+#define FeBeWaitSetSocketPos 0
+#define FeBeWaitSetLatchPos 1
+
 extern int StreamServerPort(int family, const char *hostName,
                             unsigned short portNumber, const char *unixSocketDir,
                             pgsocket ListenSocket[], int MaxListen);