Skip to content

Commit 5a26c7b

Browse files
committed
Refactor DetermineSleepTime() to use milliseconds.
Since we're not using select() anymore, we don't need to bother with struct timeval. We can work directly in milliseconds, which the latch API wants. Discussion: https://postgr.es/m/CA%2BhUKG%2BZ-HpOj1JsO9eWUP%2Bar7npSVinsC_npxSy%2BjdOMsx%3DGg%40mail.gmail.com
1 parent 7389aad commit 5a26c7b

File tree

1 file changed

+18
-39
lines changed

1 file changed

+18
-39
lines changed

src/backend/postmaster/postmaster.c

+18-39
Original file line numberDiff line numberDiff line change
@@ -1586,16 +1586,16 @@ checkControlFile(void)
15861586
}
15871587

15881588
/*
1589-
* Determine how long should we let ServerLoop sleep.
1589+
* Determine how long should we let ServerLoop sleep, in milliseconds.
15901590
*
15911591
* In normal conditions we wait at most one minute, to ensure that the other
15921592
* background tasks handled by ServerLoop get done even when no requests are
15931593
* arriving. However, if there are background workers waiting to be started,
15941594
* we don't actually sleep so that they are quickly serviced. Other exception
15951595
* cases are as shown in the code.
15961596
*/
1597-
static void
1598-
DetermineSleepTime(struct timeval *timeout)
1597+
static int
1598+
DetermineSleepTime(void)
15991599
{
16001600
TimestampTz next_wakeup = 0;
16011601

@@ -1608,26 +1608,20 @@ DetermineSleepTime(struct timeval *timeout)
16081608
{
16091609
if (AbortStartTime != 0)
16101610
{
1611+
int seconds;
1612+
16111613
/* time left to abort; clamp to 0 in case it already expired */
1612-
timeout->tv_sec = SIGKILL_CHILDREN_AFTER_SECS -
1614+
seconds = SIGKILL_CHILDREN_AFTER_SECS -
16131615
(time(NULL) - AbortStartTime);
1614-
timeout->tv_sec = Max(timeout->tv_sec, 0);
1615-
timeout->tv_usec = 0;
1616+
1617+
return Max(seconds * 1000, 0);
16161618
}
16171619
else
1618-
{
1619-
timeout->tv_sec = 60;
1620-
timeout->tv_usec = 0;
1621-
}
1622-
return;
1620+
return 60 * 1000;
16231621
}
16241622

16251623
if (StartWorkerNeeded)
1626-
{
1627-
timeout->tv_sec = 0;
1628-
timeout->tv_usec = 0;
1629-
return;
1630-
}
1624+
return 0;
16311625

16321626
if (HaveCrashedWorker)
16331627
{
@@ -1665,26 +1659,14 @@ DetermineSleepTime(struct timeval *timeout)
16651659

16661660
if (next_wakeup != 0)
16671661
{
1668-
long secs;
1669-
int microsecs;
1670-
1671-
TimestampDifference(GetCurrentTimestamp(), next_wakeup,
1672-
&secs, &microsecs);
1673-
timeout->tv_sec = secs;
1674-
timeout->tv_usec = microsecs;
1675-
1676-
/* Ensure we don't exceed one minute */
1677-
if (timeout->tv_sec > 60)
1678-
{
1679-
timeout->tv_sec = 60;
1680-
timeout->tv_usec = 0;
1681-
}
1682-
}
1683-
else
1684-
{
1685-
timeout->tv_sec = 60;
1686-
timeout->tv_usec = 0;
1662+
/* Ensure we don't exceed one minute, or go under 0. */
1663+
return Max(0,
1664+
Min(60 * 1000,
1665+
TimestampDifferenceMilliseconds(GetCurrentTimestamp(),
1666+
next_wakeup)));
16871667
}
1668+
1669+
return 60 * 1000;
16881670
}
16891671

16901672
/*
@@ -1743,12 +1725,9 @@ ServerLoop(void)
17431725
for (;;)
17441726
{
17451727
time_t now;
1746-
struct timeval timeout;
1747-
1748-
DetermineSleepTime(&timeout);
17491728

17501729
nevents = WaitEventSetWait(pm_wait_set,
1751-
timeout.tv_sec * 1000 + timeout.tv_usec / 1000,
1730+
DetermineSleepTime(),
17521731
events,
17531732
lengthof(events),
17541733
0 /* postmaster posts no wait_events */ );

0 commit comments

Comments
 (0)