[Asterisk-code-review] Replaces clock gettime() with ast tsnow() (asterisk[master])

David M. Lee asteriskteam at digium.com
Fri Jul 24 17:44:44 CDT 2015


David M. Lee has uploaded a new change for review.

  https://gerrit.asterisk.org/963

Change subject: Replaces clock_gettime() with ast_tsnow()
......................................................................

Replaces clock_gettime() with ast_tsnow()

clock_gettime() is, unfortunately, not portable. But I did like that
over our usual `ts.tv_nsec = tv.tv_usec * 1000` copy/paste code we
usually do when we want a timespec and all we have is ast_tvnow().

This patch adds ast_tsnow(), which mimics ast_tvnow(), but returns a
timespec. If clock_gettime() is available, it will use that. Otherwise
it falls back to using ast_tsnow().

Change-Id: Ibb1ee67ccf4826b9b76d5a5eb62e90b29b6c456e
---
M configure
M configure.ac
M include/asterisk/autoconfig.h.in
M include/asterisk/time.h
M tests/test_dns.c
M tests/test_dns_query_set.c
M tests/test_dns_recurring.c
7 files changed, 40 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/63/963/1

diff --git a/configure b/configure
index 731b2f7..1155214 100755
--- a/configure
+++ b/configure
@@ -15534,7 +15534,7 @@
 done
 
 
-for ac_func in asprintf atexit closefrom dup2 eaccess endpwent euidaccess ffsll ftruncate getcwd gethostbyname gethostname getloadavg gettimeofday glob ioperm inet_ntoa isascii memchr memmove memset mkdir mkdtemp munmap newlocale ppoll putenv re_comp regcomp select setenv socket strcasecmp strcasestr strchr strcspn strdup strerror strlcat strlcpy strncasecmp strndup strnlen strrchr strsep strspn strstr strtod strtol strtold strtoq unsetenv utime vasprintf getpeereid sysctl swapctl
+for ac_func in asprintf atexit clock_gettime closefrom dup2 eaccess endpwent euidaccess ffsll ftruncate getcwd gethostbyname gethostname getloadavg gettimeofday glob ioperm inet_ntoa isascii memchr memmove memset mkdir mkdtemp munmap newlocale ppoll putenv re_comp regcomp select setenv socket strcasecmp strcasestr strchr strcspn strdup strerror strlcat strlcpy strncasecmp strndup strnlen strrchr strsep strspn strstr strtod strtol strtold strtoq unsetenv utime vasprintf getpeereid sysctl swapctl
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/configure.ac b/configure.ac
index c09d30a..a0396d0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -665,7 +665,7 @@
 AC_FUNC_STRTOD
 AC_FUNC_UTIME_NULL
 AC_FUNC_VPRINTF
-AC_CHECK_FUNCS([asprintf atexit closefrom dup2 eaccess endpwent euidaccess ffsll ftruncate getcwd gethostbyname gethostname getloadavg gettimeofday glob ioperm inet_ntoa isascii memchr memmove memset mkdir mkdtemp munmap newlocale ppoll putenv re_comp regcomp select setenv socket strcasecmp strcasestr strchr strcspn strdup strerror strlcat strlcpy strncasecmp strndup strnlen strrchr strsep strspn strstr strtod strtol strtold strtoq unsetenv utime vasprintf getpeereid sysctl swapctl])
+AC_CHECK_FUNCS([asprintf atexit clock_gettime closefrom dup2 eaccess endpwent euidaccess ffsll ftruncate getcwd gethostbyname gethostname getloadavg gettimeofday glob ioperm inet_ntoa isascii memchr memmove memset mkdir mkdtemp munmap newlocale ppoll putenv re_comp regcomp select setenv socket strcasecmp strcasestr strchr strcspn strdup strerror strlcat strlcpy strncasecmp strndup strnlen strrchr strsep strspn strstr strtod strtol strtold strtoq unsetenv utime vasprintf getpeereid sysctl swapctl])
 
 AC_MSG_CHECKING(for htonll)
 AC_LINK_IFELSE(
diff --git a/include/asterisk/autoconfig.h.in b/include/asterisk/autoconfig.h.in
index 6b41a8c..b96a561 100644
--- a/include/asterisk/autoconfig.h.in
+++ b/include/asterisk/autoconfig.h.in
@@ -140,6 +140,9 @@
 /* Define to 1 if your system has a working `chown' function. */
 #undef HAVE_CHOWN
 
+/* Define to 1 if you have the `clock_gettime' function. */
+#undef HAVE_CLOCK_GETTIME
+
 /* Define to 1 if you have the `closefrom' function. */
 #undef HAVE_CLOSEFROM
 
diff --git a/include/asterisk/time.h b/include/asterisk/time.h
index f2382df..9b63d0f 100644
--- a/include/asterisk/time.h
+++ b/include/asterisk/time.h
@@ -140,6 +140,33 @@
 	return t;
 }
 )
+/*!
+ * \brief Returns current timespec. Meant to avoid calling ast_tvnow() just to
+ * create a timespace from the timeval it returns.
+ */
+#ifdef HAVE_CLOCK_GETTIME
+AST_INLINE_API(
+struct timespec ast_tsnow(void),
+{
+	struct timespec ts;
+	clock_gettime(CLOCK_REALTIME, &ts);
+	return ts;
+}
+)
+#else
+AST_INLINE_API(
+struct timespec ast_tsnow(void),
+{
+	struct timeval tv = ast_tvnow();
+	struct timespec ts;
+	/* Can't use designated initializer, because you can't have a comma
+	 * in an AST_INLINE_API. Go figure. */
+	ts.tv_sec = tv.tv_sec;
+	ts.tv_nsec = tv.tv_usec * 1000;
+	return ts;
+}
+)
+#endif
 
 /*!
  * \brief Returns the sum of two timevals a + b
diff --git a/tests/test_dns.c b/tests/test_dns.c
index 4e2c4a4..718cffc 100644
--- a/tests/test_dns.c
+++ b/tests/test_dns.c
@@ -694,7 +694,7 @@
 	static const size_t V4_BUFSIZE = sizeof(struct in_addr);
 	char v4_buf[V4_BUFSIZE];
 
-	clock_gettime(CLOCK_REALTIME, &timeout);
+	timeout = ast_tsnow();
 	timeout.tv_sec += 5;
 
 	ast_mutex_lock(&test_resolver_data.lock);
@@ -1089,7 +1089,7 @@
 		goto cleanup;
 	}
 
-	clock_gettime(CLOCK_REALTIME, &timeout);
+	timeout = ast_tsnow();
 	timeout.tv_sec += 10;
 	ast_mutex_lock(&async_data->lock);
 	while (!async_data->complete) {
@@ -1279,7 +1279,7 @@
 		goto cleanup;
 	}
 
-	clock_gettime(CLOCK_REALTIME, &timeout);
+	timeout = ast_tsnow();
 	timeout.tv_sec += 10;
 	ast_mutex_lock(&async_data->lock);
 	while (!async_data->complete) {
diff --git a/tests/test_dns_query_set.c b/tests/test_dns_query_set.c
index 98a6051..aeb61c7 100644
--- a/tests/test_dns_query_set.c
+++ b/tests/test_dns_query_set.c
@@ -245,7 +245,7 @@
 		}
 	}
 
-	clock_gettime(CLOCK_REALTIME, &timeout);
+	timeout = ast_tsnow();
 	timeout.tv_sec += 10;
 
 	ast_mutex_lock(&qsdata->lock);
diff --git a/tests/test_dns_recurring.c b/tests/test_dns_recurring.c
index b5736c2..d1add6e 100644
--- a/tests/test_dns_recurring.c
+++ b/tests/test_dns_recurring.c
@@ -210,7 +210,7 @@
 	struct timespec timeout;
 	int secdiff;
 
-	clock_gettime(CLOCK_REALTIME, &begin);
+	begin = ast_tsnow();
 
 	timeout.tv_sec = begin.tv_sec + 20;
 	timeout.tv_nsec = begin.tv_nsec;
@@ -229,7 +229,7 @@
 	}
 
 	rdata->query_complete = 0;
-	clock_gettime(CLOCK_REALTIME, &end);
+	end = ast_tsnow();
 
 	secdiff = end.tv_sec - begin.tv_sec;
 
@@ -504,7 +504,7 @@
 	/* Query has been canceled, so let's wait to make sure that we don't get
 	 * told another query has occurred.
 	 */
-	clock_gettime(CLOCK_REALTIME, &timeout);
+	timeout = ast_tsnow();
 	timeout.tv_sec += 10;
 
 	ast_mutex_lock(&rdata->lock);
@@ -602,7 +602,7 @@
 	}
 
 	/* Now ensure that no more queries get completed after cancellation. */
-	clock_gettime(CLOCK_REALTIME, &timeout);
+	timeout = ast_tsnow();
 	timeout.tv_sec += 10;
 
 	ast_mutex_lock(&rdata->lock);

-- 
To view, visit https://gerrit.asterisk.org/963
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibb1ee67ccf4826b9b76d5a5eb62e90b29b6c456e
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: David M. Lee <dlee at digium.com>



More information about the asterisk-code-review mailing list