Measure WaitLatch's timeout parameter in milliseconds, not microseconds.
authorTom Lane <[email protected]>
Tue, 9 Aug 2011 22:52:29 +0000 (18:52 -0400)
committerTom Lane <[email protected]>
Tue, 9 Aug 2011 22:52:29 +0000 (18:52 -0400)
The original definition had the problem that timeouts exceeding about 2100
seconds couldn't be specified on 32-bit machines.  Milliseconds seem like
sufficient resolution, and finer grain than that would be fantasy anyway
on many platforms.

Back-patch to 9.1 so that this aspect of the latch API won't change between
9.1 and later releases.

Peter Geoghegan

src/backend/access/transam/xlog.c
src/backend/port/unix_latch.c
src/backend/port/win32_latch.c
src/backend/postmaster/pgarch.c
src/backend/replication/syncrep.c
src/backend/replication/walsender.c

index 6a6959f728cfc00caa83c9e1b61b40a240118a37..4d2bfdf4f171a8eccfca1feca3b6cd6516acea55 100644 (file)
@@ -10210,7 +10210,9 @@ retry:
                                        /*
                                         * Wait for more WAL to arrive, or timeout to be reached
                                         */
-                                       WaitLatch(&XLogCtl->recoveryWakeupLatch, WL_LATCH_SET | WL_TIMEOUT, 5000000L);
+                                       WaitLatch(&XLogCtl->recoveryWakeupLatch,
+                                                         WL_LATCH_SET | WL_TIMEOUT,
+                                                         5000L);
                                        ResetLatch(&XLogCtl->recoveryWakeupLatch);
                                }
                                else
index 950a3a40117a79c9ab17b726d25c7e2e5ed2db67..ff4ed8f16cfa69a27508045d71863412b867b506 100644 (file)
@@ -137,7 +137,7 @@ DisownLatch(volatile Latch *latch)
  * to wait for. If the latch is already set (and WL_LATCH_SET is given), the
  * function returns immediately.
  *
- * The 'timeout' is given in microseconds. It must be >= 0 if WL_TIMEOUT flag
+ * The 'timeout' is given in milliseconds. It must be >= 0 if WL_TIMEOUT flag
  * is given.  On some platforms, signals cause the timeout to be restarted,
  * so beware that the function can sleep for several times longer than the
  * specified timeout.
@@ -156,6 +156,7 @@ DisownLatch(volatile Latch *latch)
  * have been satisfied. That should be rare in practice, but the caller
  * should not use the return value for anything critical, re-checking the
  * situation with PostmasterIsAlive() or read() on a socket as necessary.
+ * The latch and timeout flag bits can be trusted, however.
  */
 int
 WaitLatch(volatile Latch *latch, int wakeEvents, long timeout)
@@ -191,8 +192,8 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
        if (wakeEvents & WL_TIMEOUT)
        {
                Assert(timeout >= 0);
-               tv.tv_sec = timeout / 1000000L;
-               tv.tv_usec = timeout % 1000000L;
+               tv.tv_sec = timeout / 1000L;
+               tv.tv_usec = (timeout % 1000L) * 1000L;
                tvp = &tv;
        }
 
index eeb85a96ceeabd2c8d7bec36215810b3da486828..10a400670149d8cf29f731a5ef04d58d42220f03 100644 (file)
@@ -99,7 +99,6 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
        int                     numevents;
        int                     result = 0;
        int                     pmdeath_eventno = 0;
-       long            timeout_ms;
 
        /* Ignore WL_SOCKET_* events if no valid socket is given */
        if (sock == PGINVALID_SOCKET)
@@ -110,14 +109,11 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
        if ((wakeEvents & WL_LATCH_SET) && latch->owner_pid != MyProcPid)
                elog(ERROR, "cannot wait on a latch owned by another process");
 
-       /* Convert timeout to milliseconds for WaitForMultipleObjects() */
+       /* Convert timeout to form used by WaitForMultipleObjects() */
        if (wakeEvents & WL_TIMEOUT)
-       {
                Assert(timeout >= 0);
-               timeout_ms = timeout / 1000;
-       }
        else
-               timeout_ms = INFINITE;
+               timeout = INFINITE;
 
        /* Construct an array of event handles for WaitforMultipleObjects() */
        latchevent = latch->event;
@@ -165,7 +161,7 @@ WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock,
                        break;
                }
 
-               rc = WaitForMultipleObjects(numevents, events, FALSE, timeout_ms);
+               rc = WaitForMultipleObjects(numevents, events, FALSE, timeout);
 
                if (rc == WAIT_FAILED)
                        elog(ERROR, "WaitForMultipleObjects() failed: error code %d", (int) GetLastError());
index 2070fbb375b67675c99c06506d7f2c1a002cac35..8ccfc37fe9d45b960cb430117e17df61d79fad4c 100644 (file)
@@ -406,10 +406,11 @@ pgarch_MainLoop(void)
                        timeout = PGARCH_AUTOWAKE_INTERVAL - (curtime - last_copy_time);
                        if (timeout > 0)
                        {
-                               int rc;
+                               int             rc;
+
                                rc = WaitLatch(&mainloop_latch,
                                                           WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
-                                                          timeout * 1000000L);
+                                                          timeout * 1000L);
                                if (rc & WL_TIMEOUT)
                                        wakened = true;
                        }
index 56af4237e80f1c998f7e84e97975f096bed92ba7..7d7f340274bfc2028d0e16372bcede3659a2b3ad 100644 (file)
@@ -251,7 +251,7 @@ SyncRepWaitForLSN(XLogRecPtr XactCommitLSN)
                 * cancel/die signal or postmaster death regularly while waiting. Note
                 * that timeout here does not necessarily release from loop.
                 */
-               WaitLatch(&MyProc->waitLatch, WL_LATCH_SET | WL_TIMEOUT, 60000000L);
+               WaitLatch(&MyProc->waitLatch, WL_LATCH_SET | WL_TIMEOUT, 60000L);
        }
 
        /*
index 7f83a32c0c4facc1fc02b98815079fa71a82e7ea..27577529eb3b82c7ec17ba6ee08d91c3cdfe6718 100644 (file)
@@ -812,7 +812,7 @@ WalSndLoop(void)
                        if (pq_is_send_pending())
                                wakeEvents |= WL_SOCKET_WRITEABLE;
                        WaitLatchOrSocket(&MyWalSnd->latch, wakeEvents,
-                                                         MyProcPort->sock, sleeptime * 1000L);
+                                                         MyProcPort->sock, sleeptime);
 
                        /* Check for replication timeout */
                        if (replication_timeout > 0 &&