]> xenbits.xensource.com Git - unikraft/libs/newlib.git/commitdiff
Implement gettimeofday, clock_gettime, sleep
authorFlorian Schmidt <florian.schmidt@neclab.eu>
Wed, 3 Apr 2019 12:16:39 +0000 (14:16 +0200)
committerFelipe Huici <felipe.huici@neclab.eu>
Fri, 5 Apr 2019 07:43:34 +0000 (09:43 +0200)
The implementations are taken from nolibc.

Signed-off-by: Florian Schmidt <florian.schmidt@neclab.eu>
Reviewed-by: Felipe Huici <felipe.huici@neclab.eu>
include/time.h
time.c

index dd2eef2835ccc85c64555c7db58eaaff705403db..2718515c8b41b497631d148e15cdd5ed327ad04b 100644 (file)
@@ -59,6 +59,7 @@
 #endif /* __DYNAMIC_REENT__ */
 #endif /* __rtems__ */
 
+#define _POSIX_MONOTONIC_CLOCK 1
 #include_next <time.h>
 
 /* cleanup __rtems__ */
diff --git a/time.c b/time.c
index fa887bae3bb4459745def9ca38320833c93fb6b8..dccf8a15e879cc66b8b2a2db75556dc41f85d7b2 100644 (file)
--- a/time.c
+++ b/time.c
@@ -36,6 +36,7 @@
  */
 
 #include <errno.h>
+#include <time.h>
 #include <sys/time.h>
 #include <utime.h>
 #include <uk/plat/time.h>
 int
 gettimeofday(struct timeval *tv __unused, void *tz __unused)
 {
+       __nsec now = ukplat_wall_clock();
+
+       if (!tv) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       tv->tv_sec = ukarch_time_nsec_to_sec(now);
+       tv->tv_usec = ukarch_time_nsec_to_usec(ukarch_time_subsec(now));
        return 0;
 }
 
@@ -103,7 +113,40 @@ int nanosleep(const struct timespec *req, struct timespec *rem)
        return 0;
 }
 
+unsigned int sleep(unsigned int seconds)
+{
+       struct timespec ts;
+
+       ts.tv_sec = seconds;
+       ts.tv_nsec = 0;
+       if (nanosleep(&ts, &ts))
+               return ts.tv_sec;
+
+       return 0;
+}
+
 int clock_gettime(clockid_t clk_id __unused, struct timespec *tp __unused)
 {
+       __nsec now;
+
+       if (!tp) {
+               errno = EFAULT;
+               return -1;
+       }
+
+       switch (clk_id) {
+       case CLOCK_MONOTONIC:
+               now = ukplat_monotonic_clock();
+               break;
+       case CLOCK_REALTIME:
+               now = ukplat_wall_clock();
+               break;
+       default:
+               errno = EINVAL;
+               return -1;
+       }
+
+       tp->tv_sec = ukarch_time_nsec_to_sec(now);
+       tp->tv_nsec = ukarch_time_subsec(now);
        return 0;
 }