Commit 49308a9e authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Polish handling of timeval and timespec a bit.

parent 543e3dfe
......@@ -85,7 +85,7 @@ VCA_waiter_name(void)
/*--------------------------------------------------------------------
* We want to get out of any kind of touble-hit TCP connections as fast
* We want to get out of any kind of trouble-hit TCP connections as fast
* as absolutely possible, so we set them LINGER enabled with zero timeout,
* so that even if there are outstanding write data on the socket, a close(2)
* will return immediately.
......@@ -203,6 +203,7 @@ vca_acct(void *arg)
socklen_t l;
struct sockaddr_storage addr_s;
struct sockaddr *addr;
double send_timeout = 0, sess_timeout = 0;
int i;
struct pollfd *pfd;
struct listen_sock *ls;
......@@ -231,9 +232,10 @@ vca_acct(void *arg)
t0 = TIM_real();
while (1) {
#ifdef SO_SNDTIMEO_WORKS
if (params->send_timeout != tv_sndtimeo.tv_sec) {
if (params->send_timeout != send_timeout) {
need_test = 1;
tv_sndtimeo.tv_sec = params->send_timeout;
send_timeout = params->send_timeout;
tv_sndtimeo = TIM_timeval(send_timeout);
VTAILQ_FOREACH(ls, &heritage.socks, list) {
if (ls->sock < 0)
continue;
......@@ -244,9 +246,10 @@ vca_acct(void *arg)
}
#endif
#ifdef SO_RCVTIMEO_WORKS
if (params->sess_timeout != tv_rcvtimeo.tv_sec) {
if (params->sess_timeout != sess_timeout) {
need_test = 1;
tv_rcvtimeo.tv_sec = params->sess_timeout;
sess_timeout = params->sess_timeout;
tv_rcvtimeo = TIM_timeval(sess_timeout);
VTAILQ_FOREACH(ls, &heritage.socks, list) {
if (ls->sock < 0)
continue;
......
......@@ -236,9 +236,7 @@ vca_main(void *arg)
} else if (tmo > max_t) {
timeout = &max_ts;
} else {
/* TIM_t2ts() ? see #630 */
ts.tv_sec = (int)floor(tmo);
ts.tv_nsec = 1e9 * (tmo - ts.tv_sec);
ts = TIM_timespec(tmo);
timeout = &ts;
}
} else {
......
......@@ -92,6 +92,8 @@ time_t TIM_parse(const char *p);
double TIM_mono(void);
double TIM_real(void);
void TIM_sleep(double t);
struct timespec TIM_timespec(double t);
struct timeval TIM_timeval(double t);
/* from libvarnish/version.c */
void varnish_version(const char *);
......
......@@ -161,22 +161,44 @@ TIM_parse(const char *p)
void
TIM_sleep(double t)
{
#ifdef HAVE_NANOSLEEP
struct timespec ts;
ts.tv_sec = (time_t)floor(t);
ts.tv_nsec = (long)floor((t - ts.tv_sec) * 1e9);
ts = TIM_timespec(t);
#ifdef HAVE_NANOSLEEP
(void)nanosleep(&ts, NULL);
#else
if (ts.tv_sec > 0)
(void)sleep(ts.tv_sec);
ts.tv_nsec /= 1000;
if (ts.tv_nsec > 0)
(void)usleep(ts.tv_nsec);
if (t >= 1.) {
(void)sleep(floor(t);
t -= floor(t);
}
/* XXX: usleep() is not mandated to be thread safe */
t *= 1e6;
if (t > 0)
(void)usleep(floor(t));
#endif
}
struct timeval
TIM_timeval(double t)
{
struct timeval tv;
tv.tv_sec = (time_t)trunc(t);
tv.tv_usec = (int)(1e6 * (t - tv.tv_sec));
return (tv);
}
struct timespec
TIM_timespec(double t)
{
struct timespec tv;
tv.tv_sec = (time_t)trunc(t);
tv.tv_nsec = (int)(1e9 * (t - tv.tv_sec));
return (tv);
}
#ifdef TEST_DRIVER
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment