src: Use clock_gettime instead of gettimeofday if available

This commit is contained in:
Tatsuhiro Tsujikawa 2013-01-27 17:16:13 +09:00
parent c235800a1a
commit 37cb94d154
5 changed files with 38 additions and 10 deletions

View File

@ -72,6 +72,9 @@ AM_CONDITIONAL([HAVE_STDCXX_11],
# Additional libraries required for tests.
TESTS_LIBS=
# Additional libraries required for programs under src directory.
SRC_LIBS=
LIBS_OLD=$LIBS
# Search for dlsym function, which is used in tests. Linux needs -ldl,
# but netbsd does not need it.
@ -79,6 +82,13 @@ AC_SEARCH_LIBS([dlsym], [dl])
TESTS_LIBS=$LIBS $TESTS_LIBS
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
PKG_CHECK_MODULES([ZLIB], [zlib >= 1.2.3])
LIBS="$ZLIB_LIBS $LIBS"
@ -242,6 +252,7 @@ if test "x$maintainer_mode" != "xno"; then
fi
AC_SUBST([TESTS_LIBS])
AC_SUBST([SRC_LIBS])
AC_CONFIG_FILES([
Makefile

View File

@ -26,7 +26,8 @@ if ENABLE_SRC
AM_CFLAGS = -Wall
AM_CPPFLAGS = -Wall -I$(srcdir)/../lib/includes -I$(builddir)/../lib/includes \
@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
bin_PROGRAMS = spdycat spdyd

View File

@ -105,7 +105,7 @@ struct RequestStat {
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)
@ -601,7 +601,7 @@ int spdy_evloop(int fd, SSL *ssl, int spdy_version, SpdySession& spdySession,
timeval tv1, tv2;
while(!sc.finish()) {
if(config.timeout != -1) {
gettimeofday(&tv1, 0);
get_time(&tv1);
}
int nfds = poll(pollfds, npollfds, timeout);
if(nfds == -1) {
@ -627,7 +627,7 @@ int spdy_evloop(int fd, SSL *ssl, int spdy_version, SpdySession& spdySession,
break;
}
if(config.timeout != -1) {
gettimeofday(&tv2, 0);
get_time(&tv2);
timeout -= time_delta(tv2, tv1);
if (timeout <= 0) {
std::cerr << "Requests to " << spdySession.hostport << " timed out."

View File

@ -299,7 +299,7 @@ int nonblock_connect_to(const std::string& host, uint16_t port, int timeout)
struct timeval tv1, tv2;
struct pollfd pfd = {fd, POLLOUT, 0};
if(timeout != -1) {
gettimeofday(&tv1, 0);
get_time(&tv1);
}
r = poll(&pfd, 1, timeout);
if(r == 0) {
@ -308,7 +308,7 @@ int nonblock_connect_to(const std::string& host, uint16_t port, int timeout)
return -1;
} else {
if(timeout != -1) {
gettimeofday(&tv2, 0);
get_time(&tv2);
timeout -= time_delta(tv2, tv1);
if(timeout <= 0) {
return -2;
@ -828,7 +828,7 @@ int ssl_nonblock_handshake(SSL *ssl, int fd, int& timeout)
timeval tv1, tv2;
while(1) {
if(timeout != -1) {
gettimeofday(&tv1, 0);
get_time(&tv1);
}
int rv = poll(&pfd, 1, timeout);
if(rv == 0) {
@ -843,7 +843,7 @@ int ssl_nonblock_handshake(SSL *ssl, int fd, int& timeout)
return -1;
} else if(rv < 0) {
if(timeout != -1) {
gettimeofday(&tv2, 0);
get_time(&tv2);
timeout -= time_delta(tv2, tv1);
if(timeout <= 0) {
return -2;
@ -892,12 +892,12 @@ timeval base_tv;
void reset_timer()
{
gettimeofday(&base_tv, 0);
get_time(&base_tv);
}
void get_timer(timeval* tv)
{
gettimeofday(tv, 0);
get_time(tv);
tv->tv_usec -= base_tv.tv_usec;
tv->tv_sec -= base_tv.tv_sec;
if(tv->tv_usec < 0) {
@ -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

View File

@ -150,6 +150,8 @@ void reset_timer();
void get_timer(timeval *tv);
int get_time(timeval *tv);
void print_timer();
enum {