commit 270355eb0dae2cbf6373145470fa24cfd216914d
parent d02775863afa8efc751e5f31618fb7e517e3bb61
Author: vaplv <vaplv@free.fr>
Date: Sat, 8 Jul 2017 17:04:10 +0200
Update the clock_time API
Return the time in the time_current, time_add and time_sub functions and
add the time_zero function.
Diffstat:
3 files changed, 42 insertions(+), 14 deletions(-)
diff --git a/src/clock_time.c b/src/clock_time.c
@@ -42,7 +42,7 @@
#define NSEC_PER_HOUR__ ((int64_t)60 * NSEC_PER_MIN__)
#define NSEC_PER_DAY__ ((int64_t)24 * NSEC_PER_HOUR__)
-void
+struct time*
time_current(struct time* t)
{
#ifdef CLOCK_TIME_WINDOWS
@@ -82,9 +82,10 @@ time_current(struct time* t)
t->sec = (int64_t)time.tv_sec;
t->nsec = (int64_t)time.tv_nsec;
#endif
+ return t;
}
-void
+struct time*
time_sub(struct time* res, const struct time* a, const struct time* b)
{
ASSERT(res && a && b);
@@ -94,9 +95,10 @@ time_sub(struct time* res, const struct time* a, const struct time* b)
--res->sec;
res->nsec += 1000000000L;
}
+ return res;
}
-void
+struct time*
time_add(struct time* res, const struct time* a, const struct time* b)
{
ASSERT(res && a && b);
@@ -107,6 +109,7 @@ time_add(struct time* res, const struct time* a, const struct time* b)
++res->sec;
res->nsec -= 1000000000L;
}
+ return res;
}
int64_t
diff --git a/src/clock_time.h b/src/clock_time.h
@@ -39,17 +39,25 @@ enum time_unit {
BEGIN_DECLS
-RSYS_API void
+static FINLINE struct time*
+time_zero(struct time* time)
+{
+ ASSERT(time);
+ time->sec = time->nsec = 0;
+ return time;
+}
+
+RSYS_API struct time*
time_current
(struct time* time);
-RSYS_API void
+RSYS_API struct time*
time_sub
(struct time* res,
const struct time* a,
const struct time* b);
-RSYS_API void
+RSYS_API struct time*
time_add
(struct time* res,
const struct time* a,
diff --git a/src/test_time.c b/src/test_time.c
@@ -25,11 +25,11 @@ main(int argc, char** argv)
int64_t i = 0;
(void)argc, (void)argv;
- time_current(&start);
+ CHECK(time_current(&start), &start);
FOR_EACH(i, 0, INT32_MAX / 64); /* Active wait */
- time_current(&end);
+ CHECK(time_current(&end), &end);
- time_sub(&res, &end, &start);
+ CHECK(time_sub(&res, &end, &start), &res);
time = time_val(&res, TIME_NSEC);
CHECK(time >= 0, 1 );
CHECK(time_val(&res, TIME_USEC), time / 1000);
@@ -38,14 +38,31 @@ main(int argc, char** argv)
time_dump
(&res, TIME_SEC|TIME_MSEC|TIME_USEC, NULL, dump, sizeof(dump));
- printf("%s--\n", dump);
+ printf(">>> %s\n", dump);
time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump));
- printf("%s--\n", dump);
+ printf(">>> %s\n", dump);
- time_sub(&res, &end, &end);
+ CHECK(time_add(&res, &res, &res), &res);
+ CHECK(time_val(&res, TIME_NSEC), 2*time);
time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump));
- printf("%s--\n", dump);
+ printf(">>> %s\n", dump);
+
+ time = time_val(&res, TIME_NSEC);
+ CHECK(time_zero(&start), &start);
+ CHECK(time_val(&start, TIME_NSEC), 0);
+ CHECK(time_add(&res, &res, &start), &res);
+ CHECK(time_val(&res, TIME_NSEC), time);
+ CHECK(time_add(&start, &res, &start), &start);
+ CHECK(time_val(&start, TIME_NSEC), time);
+ CHECK(time_sub(&res, time_zero(&start), &res), &res);
+ CHECK(time_val(&res, TIME_NSEC), -time);
+
+ CHECK(time_sub(&res, &end, &end), &res);
+ CHECK(time_val(&res, TIME_NSEC), 0);
+ time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump));
+ printf(">>> %s\n", dump);
time_dump(&res, 0, NULL, dump, sizeof(dump));
- printf("%s--\n", dump);
+ printf(">>> %s\n", dump);
+
return 0;
}