os: Compute timeout in milliseconds instead of struct timeval

The timeout resolution offered in the AdjustWaitForDelay call is
only milliseconds, so passing around the timeout as a pointer to a
struct timeval is not helpful. Doing everything in milliseconds up to
the point of the select call simplifies the code without affecting
functionality at all.

Signed-off-by: Keith Packard <keithp@keithp.com>
Reviewed-by: Adam Jackson <ajax@redhat.com>
This commit is contained in:
Keith Packard 2016-05-26 10:30:56 -07:00 committed by Adam Jackson
parent a414db0215
commit e6636b4383
3 changed files with 23 additions and 33 deletions

View File

@ -182,8 +182,7 @@ extern void ForceClockId(clockid_t /* forced_clockid */);
extern _X_EXPORT CARD32 GetTimeInMillis(void);
extern _X_EXPORT CARD64 GetTimeInMicros(void);
extern _X_EXPORT void AdjustWaitForDelay(void *waitTime,
unsigned long newdelay);
extern _X_EXPORT void AdjustWaitForDelay(void *waitTime, int newdelay);
typedef struct _OsTimerRec *OsTimerPtr;

View File

@ -147,7 +147,7 @@ WaitForSomething(int *pClientsReady)
{
int i;
struct timeval waittime, *wt;
INT32 timeout = 0;
int timeout;
fd_set clientsReadable;
fd_set clientsWritable;
int curclient;
@ -175,17 +175,15 @@ WaitForSomething(int *pClientsReady)
if (workQueue)
ProcessWorkQueue();
if (XFD_ANYSET(&ClientsWithInput)) {
timeout = 0;
someReady = TRUE;
waittime.tv_sec = 0;
waittime.tv_usec = 0;
wt = &waittime;
}
if (someReady) {
XFD_COPYSET(&AllSockets, &LastSelectMask);
XFD_UNSET(&LastSelectMask, &ClientsWithInput);
}
else {
wt = NULL;
timeout = -1;
if (timers) {
now = GetTimeInMillis();
timeout = timers->expires - now;
@ -198,16 +196,20 @@ WaitForSomething(int *pClientsReady)
timeout = timers->expires - now;
if (timeout < 0)
timeout = 0;
waittime.tv_sec = timeout / MILLI_PER_SECOND;
waittime.tv_usec = (timeout % MILLI_PER_SECOND) *
(1000000 / MILLI_PER_SECOND);
wt = &waittime;
}
}
XFD_COPYSET(&AllSockets, &LastSelectMask);
}
BlockHandler(&wt);
BlockHandler(&timeout);
if (timeout < 0)
wt = NULL;
else {
waittime.tv_sec = timeout / MILLI_PER_SECOND;
waittime.tv_usec = (timeout % MILLI_PER_SECOND) *
(1000000 / MILLI_PER_SECOND);
wt = &waittime;
}
if (NewOutputPending)
FlushAllOutput();
/* keep this check close to select() call to minimize race */
@ -359,6 +361,16 @@ WaitForSomething(int *pClientsReady)
return nready;
}
void
AdjustWaitForDelay(void *waitTime, int newdelay)
{
int *timeoutp = waitTime;
int timeout = *timeoutp;
if (timeout < 0 || newdelay < timeout)
*timeoutp = newdelay;
}
/* If time has rewound, re-run every affected timer.
* Timers might drop out of the list, so we have to restart every time. */
static void

View File

@ -512,27 +512,6 @@ GetTimeInMicros(void)
}
#endif
void
AdjustWaitForDelay(void *waitTime, unsigned long newdelay)
{
static struct timeval delay_val;
struct timeval **wt = (struct timeval **) waitTime;
unsigned long olddelay;
if (*wt == NULL) {
delay_val.tv_sec = newdelay / 1000;
delay_val.tv_usec = 1000 * (newdelay % 1000);
*wt = &delay_val;
}
else {
olddelay = (*wt)->tv_sec * 1000 + (*wt)->tv_usec / 1000;
if (newdelay < olddelay) {
(*wt)->tv_sec = newdelay / 1000;
(*wt)->tv_usec = 1000 * (newdelay % 1000);
}
}
}
void
UseMsg(void)
{