lib/util: add time_mono() for monotonic time a la time()
authorBjörn Jacke <bj@sernet.de>
Mon, 6 Sep 2010 18:27:24 +0000 (20:27 +0200)
committerBjörn Jacke <bj@sernet.de>
Tue, 7 Sep 2010 18:29:13 +0000 (20:29 +0200)
lib/util/time.c
lib/util/time.h

index 5ecf93cb77caba269514ebe5b30d28571f140062..ed3b4f8e301c7c09021ce677cd3fe5395ba48baa 100644 (file)
@@ -65,6 +65,27 @@ _PUBLIC_ void clock_gettime_mono(struct timespec *tp)
        }
 }
 
+/**
+a wrapper to preferably get the monotonic time in seconds
+as this is only second resolution we can use the cached
+(and much faster) COARS clock variant
+**/
+_PUBLIC_ time_t time_mono(time_t *t)
+{
+       struct timespec tp;
+       int rc = -1;
+#ifdef CLOCK_MONOTONIC_COARSE
+       rc = clock_gettime(CLOCK_MONOTONIC_COARSE,&tp);
+#endif
+       if (rc != 0) {
+               clock_gettime_mono(&tp);
+       }
+       if (t != NULL) {
+               *t = tp.tv_sec;
+       }
+       return tp.tv_sec;
+}
+
 
 #define TIME_FIXUP_CONSTANT 11644473600LL
 
index 720a262b2927d8b15a7cd848f8a62cec5aefbf88..345382b80a0893b30128e346f9c26c13fe7f744f 100644 (file)
@@ -55,6 +55,11 @@ a wrapper to preferably get the monotonic time
 **/
 _PUBLIC_ void clock_gettime_mono(struct timespec *tp);
 
+/**
+a wrapper to preferably get the monotonic time in s
+**/
+_PUBLIC_ time_t time_mono(time_t *t);
+
 /**
 interpret an 8 byte "filetime" structure to a time_t
 It's originally in "100ns units since jan 1st 1601"