Commit 37cb94d1 authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

src: Use clock_gettime instead of gettimeofday if available

parent c235800a
...@@ -72,6 +72,9 @@ AM_CONDITIONAL([HAVE_STDCXX_11], ...@@ -72,6 +72,9 @@ AM_CONDITIONAL([HAVE_STDCXX_11],
# Additional libraries required for tests. # Additional libraries required for tests.
TESTS_LIBS= TESTS_LIBS=
# Additional libraries required for programs under src directory.
SRC_LIBS=
LIBS_OLD=$LIBS LIBS_OLD=$LIBS
# Search for dlsym function, which is used in tests. Linux needs -ldl, # Search for dlsym function, which is used in tests. Linux needs -ldl,
# but netbsd does not need it. # but netbsd does not need it.
...@@ -79,6 +82,13 @@ AC_SEARCH_LIBS([dlsym], [dl]) ...@@ -79,6 +82,13 @@ AC_SEARCH_LIBS([dlsym], [dl])
TESTS_LIBS=$LIBS $TESTS_LIBS TESTS_LIBS=$LIBS $TESTS_LIBS
LIBS=$LIBS_OLD LIBS=$LIBS_OLD
LIBS_OLD=$LIBS
AC_SEARCH_LIBS([clock_gettime], [rt],
[AC_DEFINE([HAVE_CLOCK_GETTIME], [1],
[Define to 1 if you have the `clock_gettime`.])])
SRC_LIBS=$LIBS $SRC_LIBS
LIBS=$LIBS_OLD
# zlib # zlib
PKG_CHECK_MODULES([ZLIB], [zlib >= 1.2.3]) PKG_CHECK_MODULES([ZLIB], [zlib >= 1.2.3])
LIBS="$ZLIB_LIBS $LIBS" LIBS="$ZLIB_LIBS $LIBS"
...@@ -242,6 +252,7 @@ if test "x$maintainer_mode" != "xno"; then ...@@ -242,6 +252,7 @@ if test "x$maintainer_mode" != "xno"; then
fi fi
AC_SUBST([TESTS_LIBS]) AC_SUBST([TESTS_LIBS])
AC_SUBST([SRC_LIBS])
AC_CONFIG_FILES([ AC_CONFIG_FILES([
Makefile Makefile
......
...@@ -26,7 +26,8 @@ if ENABLE_SRC ...@@ -26,7 +26,8 @@ if ENABLE_SRC
AM_CFLAGS = -Wall AM_CFLAGS = -Wall
AM_CPPFLAGS = -Wall -I$(srcdir)/../lib/includes -I$(builddir)/../lib/includes \ AM_CPPFLAGS = -Wall -I$(srcdir)/../lib/includes -I$(builddir)/../lib/includes \
@OPENSSL_CFLAGS@ @XML_CPPFLAGS@ @LIBEVENT_OPENSSL_CFLAGS@ @DEFS@ @OPENSSL_CFLAGS@ @XML_CPPFLAGS@ @LIBEVENT_OPENSSL_CFLAGS@ @DEFS@
AM_LDFLAGS = @OPENSSL_LIBS@ @XML_LIBS@ @LIBEVENT_OPENSSL_LIBS@ -pthread AM_LDFLAGS = @OPENSSL_LIBS@ @XML_LIBS@ @LIBEVENT_OPENSSL_LIBS@ @SRC_LIBS@ \
-pthread
LDADD = $(top_builddir)/lib/libspdylay.la LDADD = $(top_builddir)/lib/libspdylay.la
bin_PROGRAMS = spdycat spdyd bin_PROGRAMS = spdycat spdyd
......
...@@ -105,7 +105,7 @@ struct RequestStat { ...@@ -105,7 +105,7 @@ struct RequestStat {
void record_time(timeval *tv) void record_time(timeval *tv)
{ {
gettimeofday(tv, 0); get_time(tv);
} }
bool has_uri_field(const http_parser_url &u, http_parser_url_fields field) bool has_uri_field(const http_parser_url &u, http_parser_url_fields field)
...@@ -601,7 +601,7 @@ int spdy_evloop(int fd, SSL *ssl, int spdy_version, SpdySession& spdySession, ...@@ -601,7 +601,7 @@ int spdy_evloop(int fd, SSL *ssl, int spdy_version, SpdySession& spdySession,
timeval tv1, tv2; timeval tv1, tv2;
while(!sc.finish()) { while(!sc.finish()) {
if(config.timeout != -1) { if(config.timeout != -1) {
gettimeofday(&tv1, 0); get_time(&tv1);
} }
int nfds = poll(pollfds, npollfds, timeout); int nfds = poll(pollfds, npollfds, timeout);
if(nfds == -1) { if(nfds == -1) {
...@@ -627,7 +627,7 @@ int spdy_evloop(int fd, SSL *ssl, int spdy_version, SpdySession& spdySession, ...@@ -627,7 +627,7 @@ int spdy_evloop(int fd, SSL *ssl, int spdy_version, SpdySession& spdySession,
break; break;
} }
if(config.timeout != -1) { if(config.timeout != -1) {
gettimeofday(&tv2, 0); get_time(&tv2);
timeout -= time_delta(tv2, tv1); timeout -= time_delta(tv2, tv1);
if (timeout <= 0) { if (timeout <= 0) {
std::cerr << "Requests to " << spdySession.hostport << " timed out." std::cerr << "Requests to " << spdySession.hostport << " timed out."
......
...@@ -299,7 +299,7 @@ int nonblock_connect_to(const std::string& host, uint16_t port, int timeout) ...@@ -299,7 +299,7 @@ int nonblock_connect_to(const std::string& host, uint16_t port, int timeout)
struct timeval tv1, tv2; struct timeval tv1, tv2;
struct pollfd pfd = {fd, POLLOUT, 0}; struct pollfd pfd = {fd, POLLOUT, 0};
if(timeout != -1) { if(timeout != -1) {
gettimeofday(&tv1, 0); get_time(&tv1);
} }
r = poll(&pfd, 1, timeout); r = poll(&pfd, 1, timeout);
if(r == 0) { if(r == 0) {
...@@ -308,7 +308,7 @@ int nonblock_connect_to(const std::string& host, uint16_t port, int timeout) ...@@ -308,7 +308,7 @@ int nonblock_connect_to(const std::string& host, uint16_t port, int timeout)
return -1; return -1;
} else { } else {
if(timeout != -1) { if(timeout != -1) {
gettimeofday(&tv2, 0); get_time(&tv2);
timeout -= time_delta(tv2, tv1); timeout -= time_delta(tv2, tv1);
if(timeout <= 0) { if(timeout <= 0) {
return -2; return -2;
...@@ -828,7 +828,7 @@ int ssl_nonblock_handshake(SSL *ssl, int fd, int& timeout) ...@@ -828,7 +828,7 @@ int ssl_nonblock_handshake(SSL *ssl, int fd, int& timeout)
timeval tv1, tv2; timeval tv1, tv2;
while(1) { while(1) {
if(timeout != -1) { if(timeout != -1) {
gettimeofday(&tv1, 0); get_time(&tv1);
} }
int rv = poll(&pfd, 1, timeout); int rv = poll(&pfd, 1, timeout);
if(rv == 0) { if(rv == 0) {
...@@ -843,7 +843,7 @@ int ssl_nonblock_handshake(SSL *ssl, int fd, int& timeout) ...@@ -843,7 +843,7 @@ int ssl_nonblock_handshake(SSL *ssl, int fd, int& timeout)
return -1; return -1;
} else if(rv < 0) { } else if(rv < 0) {
if(timeout != -1) { if(timeout != -1) {
gettimeofday(&tv2, 0); get_time(&tv2);
timeout -= time_delta(tv2, tv1); timeout -= time_delta(tv2, tv1);
if(timeout <= 0) { if(timeout <= 0) {
return -2; return -2;
...@@ -892,12 +892,12 @@ timeval base_tv; ...@@ -892,12 +892,12 @@ timeval base_tv;
void reset_timer() void reset_timer()
{ {
gettimeofday(&base_tv, 0); get_time(&base_tv);
} }
void get_timer(timeval* tv) void get_timer(timeval* tv)
{ {
gettimeofday(tv, 0); get_time(tv);
tv->tv_usec -= base_tv.tv_usec; tv->tv_usec -= base_tv.tv_usec;
tv->tv_sec -= base_tv.tv_sec; tv->tv_sec -= base_tv.tv_sec;
if(tv->tv_usec < 0) { if(tv->tv_usec < 0) {
...@@ -906,4 +906,18 @@ void get_timer(timeval* tv) ...@@ -906,4 +906,18 @@ void get_timer(timeval* tv)
} }
} }
int get_time(timeval *tv)
{
int rv;
#ifdef HAVE_CLOCK_GETTIME
timespec ts;
rv = clock_gettime(CLOCK_MONOTONIC, &ts);
tv->tv_sec = ts.tv_sec;
tv->tv_usec = ts.tv_nsec/1000;
#else // !HAVE_CLOCK_GETTIME
rv = gettimeofday(&base_tv, 0);
#endif // !HAVE_CLOCK_GETTIME
return rv;
}
} // namespace spdylay } // namespace spdylay
...@@ -150,6 +150,8 @@ void reset_timer(); ...@@ -150,6 +150,8 @@ void reset_timer();
void get_timer(timeval *tv); void get_timer(timeval *tv);
int get_time(timeval *tv);
void print_timer(); void print_timer();
enum { enum {
......
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