vss: Add documentation of MAP_POPULATE.
[paraslash.git] / time.c
diff --git a/time.c b/time.c
index e0f6c756e21ae87d553c7a3a3e4a99e04e34e977..bb47b31b7b262ca8a988377d899e166abdd5d029 100644 (file)
--- a/time.c
+++ b/time.c
@@ -1,8 +1,4 @@
-/*
- * Copyright (C) 2005-2012 Andre Noll <maan@systemlinux.org>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
 
 #include "para.h"
 /** \file time.c Helper functions for dealing with time values. */
@@ -31,18 +27,6 @@ void ms2tv(long unsigned n, struct timeval *tv)
        tv->tv_usec = (n % 1000) * 1000;
 }
 
-/**
- * Convert a double to a struct timeval.
- *
- * \param x The value to convert.
- * \param tv Result pointer.
- */
-void d2tv(double x, struct timeval *tv)
-{
-       tv->tv_sec = x;
-       tv->tv_usec = (x - (double)tv->tv_sec) * 1000.0 * 1000.0 + 0.5;
-}
-
 /**
  * Compute the difference of two time values.
  *
@@ -55,7 +39,8 @@ void d2tv(double x, struct timeval *tv)
  *
  * \return If \a b < \a a, this function returns -1, otherwise it returns 1.
  */
-int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff)
+int tv_diff(const struct timeval *b, const struct timeval *a,
+               struct timeval *diff)
 {
        int ret = 1;
 
@@ -85,7 +70,7 @@ int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *di
  * \param sum Contains the sum \a a + \a b on return.
  */
 void tv_add(const struct timeval *a, const struct timeval *b,
-       struct timeval *sum)
+               struct timeval *sum)
 {
        sum->tv_sec = a->tv_sec + b->tv_sec;
        if (a->tv_usec + b->tv_usec >= 1000 * 1000) {
@@ -104,7 +89,7 @@ void tv_add(const struct timeval *a, const struct timeval *b,
  * \param result Contains \a mult * \a tv on return.
  */
 void tv_scale(const unsigned long mult, const struct timeval *tv,
-       struct timeval *result)
+               struct timeval *result)
 {
        uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) * mult;
 
@@ -120,7 +105,7 @@ void tv_scale(const unsigned long mult, const struct timeval *tv,
  * \param result Contains (1 / mult) * tv on return.
  */
 void tv_divide(const unsigned long divisor, const struct timeval *tv,
-       struct timeval *result)
+               struct timeval *result)
 {
        uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor;
 
@@ -191,3 +176,32 @@ void compute_chunk_time(long unsigned chunk_num,
        tv_scale(chunk_num, chunk_tv, &tmp);
        tv_add(&tmp, stream_start, result);
 }
+
+/**
+ * Retrieve the time of the realtime clock.
+ *
+ * \param tv Where to store the result.
+ *
+ * Gets the current value of the system-wide real-time clock (identified by id
+ * \p CLOCK_REALTIME). If \a tv is \p NULL, the value is stored in a static
+ * buffer, otherwise it is stored at the location given by \a tv.
+ *
+ * \return This function aborts on errors. On success it returns a pointer to
+ * memory containing the current time.
+ *
+ * \sa clock_gettime(2), gettimeofday(2).
+ */
+struct timeval *clock_get_realtime(struct timeval *tv)
+{
+       static struct timeval user_friendly;
+       struct timespec t;
+       int ret;
+
+       if (!tv)
+               tv = &user_friendly;
+       ret = clock_gettime(CLOCK_REALTIME, &t);
+       assert(ret == 0);
+       tv->tv_sec = t.tv_sec;
+       tv->tv_usec = t.tv_nsec / 1000;
+       return tv;
+}