[v1,1/4] support: Add xclock_now helper function.

Message ID 8d0cf9eafcb53a98f684feffe54b78057fa9d1df.1559564077.git-series.mac@mcrowe.com
State Superseded
Headers

Commit Message

Mike Crowe June 3, 2019, 12:15 p.m. UTC
  It's easier to read and write tests with:

 const struct timespec ts = xclock_now(CLOCK_REALTIME);

than

 struct timespec ts;
 xclock_gettime(CLOCK_REALTIME, &ts);

* support/xtime.h: Add xclock_now() helper function.
---
 ChangeLog       |  4 ++++
 support/xtime.h | 10 ++++++++++
 2 files changed, 14 insertions(+)
  

Comments

Adhemerval Zanella June 4, 2019, 8:10 p.m. UTC | #1
On 03/06/2019 09:15, Mike Crowe wrote:
> It's easier to read and write tests with:
> 
>  const struct timespec ts = xclock_now(CLOCK_REALTIME);
> 
> than
> 
>  struct timespec ts;
>  xclock_gettime(CLOCK_REALTIME, &ts);
> 
> * support/xtime.h: Add xclock_now() helper function.

LGTM, thanks.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>

> ---
>  ChangeLog       |  4 ++++
>  support/xtime.h | 10 ++++++++++
>  2 files changed, 14 insertions(+)
> 
> diff --git a/ChangeLog b/ChangeLog
> index 2f5deee..abb958d 100644
> --- a/ChangeLog
> +++ b/ChangeLog
> @@ -1,3 +1,7 @@
> +2019-05-30  Mike Crowe  <mac@mcrowe.com>
> +
> +	* support/xtime.h: Add xclock_now() helper function.
> +
>  2019-05-27  Mike Crowe  <mac@mcrowe.com>
>  
>  	* NEWS: Mention new pthread_cond_clockwait,
> diff --git a/support/xtime.h b/support/xtime.h
> index 68af1a5..6e19ce1 100644
> --- a/support/xtime.h
> +++ b/support/xtime.h
> @@ -28,6 +28,16 @@ __BEGIN_DECLS
>  
>  void xclock_gettime (clockid_t clock, struct timespec *ts);
>  
> +/* This helper can often simplify tests by avoiding an explicit
> +   variable declaration or allowing that declaration to be const. */
> +
> +static inline struct timespec xclock_now (clockid_t clock)
> +{
> +  struct timespec ts;
> +  xclock_gettime (clock, &ts);
> +  return ts;
> +}
> +
>  __END_DECLS
>  
>  #endif /* SUPPORT_TIME_H */
>
  

Patch

diff --git a/ChangeLog b/ChangeLog
index 2f5deee..abb958d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@ 
+2019-05-30  Mike Crowe  <mac@mcrowe.com>
+
+	* support/xtime.h: Add xclock_now() helper function.
+
 2019-05-27  Mike Crowe  <mac@mcrowe.com>
 
 	* NEWS: Mention new pthread_cond_clockwait,
diff --git a/support/xtime.h b/support/xtime.h
index 68af1a5..6e19ce1 100644
--- a/support/xtime.h
+++ b/support/xtime.h
@@ -28,6 +28,16 @@  __BEGIN_DECLS
 
 void xclock_gettime (clockid_t clock, struct timespec *ts);
 
+/* This helper can often simplify tests by avoiding an explicit
+   variable declaration or allowing that declaration to be const. */
+
+static inline struct timespec xclock_now (clockid_t clock)
+{
+  struct timespec ts;
+  xclock_gettime (clock, &ts);
+  return ts;
+}
+
 __END_DECLS
 
 #endif /* SUPPORT_TIME_H */