1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-09 06:21:09 +03:00

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
This commit is contained in:
Thomas Munro
2023-01-12 15:04:08 +13:00
parent 7389aad636
commit 5a26c7b310

View File

@@ -1586,7 +1586,7 @@ checkControlFile(void)
} }
/* /*
* Determine how long should we let ServerLoop sleep. * Determine how long should we let ServerLoop sleep, in milliseconds.
* *
* In normal conditions we wait at most one minute, to ensure that the other * In normal conditions we wait at most one minute, to ensure that the other
* background tasks handled by ServerLoop get done even when no requests are * background tasks handled by ServerLoop get done even when no requests are
@@ -1594,8 +1594,8 @@ checkControlFile(void)
* we don't actually sleep so that they are quickly serviced. Other exception * we don't actually sleep so that they are quickly serviced. Other exception
* cases are as shown in the code. * cases are as shown in the code.
*/ */
static void static int
DetermineSleepTime(struct timeval *timeout) DetermineSleepTime(void)
{ {
TimestampTz next_wakeup = 0; TimestampTz next_wakeup = 0;
@@ -1608,26 +1608,20 @@ DetermineSleepTime(struct timeval *timeout)
{ {
if (AbortStartTime != 0) if (AbortStartTime != 0)
{ {
int seconds;
/* time left to abort; clamp to 0 in case it already expired */ /* time left to abort; clamp to 0 in case it already expired */
timeout->tv_sec = SIGKILL_CHILDREN_AFTER_SECS - seconds = SIGKILL_CHILDREN_AFTER_SECS -
(time(NULL) - AbortStartTime); (time(NULL) - AbortStartTime);
timeout->tv_sec = Max(timeout->tv_sec, 0);
timeout->tv_usec = 0; return Max(seconds * 1000, 0);
} }
else else
{ return 60 * 1000;
timeout->tv_sec = 60;
timeout->tv_usec = 0;
}
return;
} }
if (StartWorkerNeeded) if (StartWorkerNeeded)
{ return 0;
timeout->tv_sec = 0;
timeout->tv_usec = 0;
return;
}
if (HaveCrashedWorker) if (HaveCrashedWorker)
{ {
@@ -1665,26 +1659,14 @@ DetermineSleepTime(struct timeval *timeout)
if (next_wakeup != 0) if (next_wakeup != 0)
{ {
long secs; /* Ensure we don't exceed one minute, or go under 0. */
int microsecs; return Max(0,
Min(60 * 1000,
TimestampDifferenceMilliseconds(GetCurrentTimestamp(),
next_wakeup)));
}
TimestampDifference(GetCurrentTimestamp(), next_wakeup, return 60 * 1000;
&secs, &microsecs);
timeout->tv_sec = secs;
timeout->tv_usec = microsecs;
/* Ensure we don't exceed one minute */
if (timeout->tv_sec > 60)
{
timeout->tv_sec = 60;
timeout->tv_usec = 0;
}
}
else
{
timeout->tv_sec = 60;
timeout->tv_usec = 0;
}
} }
/* /*
@@ -1743,12 +1725,9 @@ ServerLoop(void)
for (;;) for (;;)
{ {
time_t now; time_t now;
struct timeval timeout;
DetermineSleepTime(&timeout);
nevents = WaitEventSetWait(pm_wait_set, nevents = WaitEventSetWait(pm_wait_set,
timeout.tv_sec * 1000 + timeout.tv_usec / 1000, DetermineSleepTime(),
events, events,
lengthof(events), lengthof(events),
0 /* postmaster posts no wait_events */ ); 0 /* postmaster posts no wait_events */ );