[master] 49308a9 Polish handling of timeval and timespec a bit.
Poul-Henning Kamp
phk at varnish-cache.org
Thu Feb 24 10:18:45 CET 2011
commit 49308a9e290ffd0d2868b250e0c282de4ad6264a
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Thu Feb 24 09:18:19 2011 +0000
Polish handling of timeval and timespec a bit.
diff --git a/bin/varnishd/cache_acceptor.c b/bin/varnishd/cache_acceptor.c
index 3204edd..e43f2c4 100644
--- a/bin/varnishd/cache_acceptor.c
+++ b/bin/varnishd/cache_acceptor.c
@@ -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;
diff --git a/bin/varnishd/cache_waiter_ports.c b/bin/varnishd/cache_waiter_ports.c
index a7a1686..a3fe421 100644
--- a/bin/varnishd/cache_waiter_ports.c
+++ b/bin/varnishd/cache_waiter_ports.c
@@ -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 {
diff --git a/include/libvarnish.h b/include/libvarnish.h
index 0493a2e..f89a763 100644
--- a/include/libvarnish.h
+++ b/include/libvarnish.h
@@ -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 *);
diff --git a/lib/libvarnish/time.c b/lib/libvarnish/time.c
index b322431..02d4430 100644
--- a/lib/libvarnish/time.c
+++ b/lib/libvarnish/time.c
@@ -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
More information about the varnish-commit
mailing list