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) ...@@ -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, * 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) * so that even if there are outstanding write data on the socket, a close(2)
* will return immediately. * will return immediately.
...@@ -203,6 +203,7 @@ vca_acct(void *arg) ...@@ -203,6 +203,7 @@ vca_acct(void *arg)
socklen_t l; socklen_t l;
struct sockaddr_storage addr_s; struct sockaddr_storage addr_s;
struct sockaddr *addr; struct sockaddr *addr;
double send_timeout = 0, sess_timeout = 0;
int i; int i;
struct pollfd *pfd; struct pollfd *pfd;
struct listen_sock *ls; struct listen_sock *ls;
...@@ -231,9 +232,10 @@ vca_acct(void *arg) ...@@ -231,9 +232,10 @@ vca_acct(void *arg)
t0 = TIM_real(); t0 = TIM_real();
while (1) { while (1) {
#ifdef SO_SNDTIMEO_WORKS #ifdef SO_SNDTIMEO_WORKS
if (params->send_timeout != tv_sndtimeo.tv_sec) { if (params->send_timeout != send_timeout) {
need_test = 1; 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) { VTAILQ_FOREACH(ls, &heritage.socks, list) {
if (ls->sock < 0) if (ls->sock < 0)
continue; continue;
...@@ -244,9 +246,10 @@ vca_acct(void *arg) ...@@ -244,9 +246,10 @@ vca_acct(void *arg)
} }
#endif #endif
#ifdef SO_RCVTIMEO_WORKS #ifdef SO_RCVTIMEO_WORKS
if (params->sess_timeout != tv_rcvtimeo.tv_sec) { if (params->sess_timeout != sess_timeout) {
need_test = 1; 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) { VTAILQ_FOREACH(ls, &heritage.socks, list) {
if (ls->sock < 0) if (ls->sock < 0)
continue; continue;
......
...@@ -236,9 +236,7 @@ vca_main(void *arg) ...@@ -236,9 +236,7 @@ vca_main(void *arg)
} else if (tmo > max_t) { } else if (tmo > max_t) {
timeout = &max_ts; timeout = &max_ts;
} else { } else {
/* TIM_t2ts() ? see #630 */ ts = TIM_timespec(tmo);
ts.tv_sec = (int)floor(tmo);
ts.tv_nsec = 1e9 * (tmo - ts.tv_sec);
timeout = &ts; timeout = &ts;
} }
} else { } else {
......
...@@ -92,6 +92,8 @@ time_t TIM_parse(const char *p); ...@@ -92,6 +92,8 @@ time_t TIM_parse(const char *p);
double TIM_mono(void); double TIM_mono(void);
double TIM_real(void); double TIM_real(void);
void TIM_sleep(double t); void TIM_sleep(double t);
struct timespec TIM_timespec(double t);
struct timeval TIM_timeval(double t);
/* from libvarnish/version.c */ /* from libvarnish/version.c */
void varnish_version(const char *); void varnish_version(const char *);
......
...@@ -161,22 +161,44 @@ TIM_parse(const char *p) ...@@ -161,22 +161,44 @@ TIM_parse(const char *p)
void void
TIM_sleep(double t) TIM_sleep(double t)
{ {
#ifdef HAVE_NANOSLEEP
struct timespec ts; struct timespec ts;
ts.tv_sec = (time_t)floor(t); ts = TIM_timespec(t);
ts.tv_nsec = (long)floor((t - ts.tv_sec) * 1e9);
#ifdef HAVE_NANOSLEEP
(void)nanosleep(&ts, NULL); (void)nanosleep(&ts, NULL);
#else #else
if (ts.tv_sec > 0) if (t >= 1.) {
(void)sleep(ts.tv_sec); (void)sleep(floor(t);
ts.tv_nsec /= 1000; t -= floor(t);
if (ts.tv_nsec > 0) }
(void)usleep(ts.tv_nsec); /* XXX: usleep() is not mandated to be thread safe */
t *= 1e6;
if (t > 0)
(void)usleep(floor(t));
#endif #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 #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