libc: More of the POSIX time APIs
authorAlan Cox <alan@linux.intel.com>
Fri, 2 Jan 2015 18:06:27 +0000 (18:06 +0000)
committerAlan Cox <alan@linux.intel.com>
Fri, 2 Jan 2015 18:06:27 +0000 (18:06 +0000)
Library/libs/clock_getres.c [new file with mode: 0644]
Library/libs/clock_gettime.c [new file with mode: 0644]
Library/libs/clock_settime.c [new file with mode: 0644]
Library/libs/gettimeofday.c [new file with mode: 0644]
Library/libs/nanosleep.c [new file with mode: 0644]
Library/libs/settimeofday.c [new file with mode: 0644]

diff --git a/Library/libs/clock_getres.c b/Library/libs/clock_getres.c
new file mode 100644 (file)
index 0000000..c5bc700
--- /dev/null
@@ -0,0 +1,17 @@
+#include <time.h>
+#include <unistd.h>
+#include <errno.h>
+
+int clock_getres(clockid_t clk_id, struct timespec *res)
+{
+  res->tv_nsec = 0;
+  res->tv_sec = 1;
+  switch(clk_id) {
+  case CLOCK_REALTIME:
+  case CLOCK_MONOTONIC:
+    return 0;
+  default:
+    errno = EINVAL;
+  }
+  return -1;
+}
\ No newline at end of file
diff --git a/Library/libs/clock_gettime.c b/Library/libs/clock_gettime.c
new file mode 100644 (file)
index 0000000..6b60576
--- /dev/null
@@ -0,0 +1,19 @@
+#include <time.h>
+#include <unistd.h>
+#include <errno.h>
+
+int clock_gettime(clockid_t clk_id, struct timespec *res)
+{
+  res->tv_nsec = 0;
+  switch(clk_id) {
+  case CLOCK_REALTIME:
+    _time(&res->tv_sec, 0);
+    return 0;
+  case CLOCK_MONOTONIC:
+    _time(&res->tv_sec, 1);
+    return 0;
+  default:
+    errno = EINVAL;
+  }
+  return -1;
+}
\ No newline at end of file
diff --git a/Library/libs/clock_settime.c b/Library/libs/clock_settime.c
new file mode 100644 (file)
index 0000000..084fa35
--- /dev/null
@@ -0,0 +1,17 @@
+#include <time.h>
+#include <unistd.h>
+#include <errno.h>
+
+int clock_settime(clockid_t clk_id, const struct timespec *tp)
+{
+  switch(clk_id) {
+  case CLOCK_REALTIME:
+    _stime(&tp->tv_sec, 0);
+    return 0;
+  case CLOCK_MONOTONIC:
+    return -EPERM;
+  default:
+    errno = EINVAL;
+  }
+  return -1;
+}
\ No newline at end of file
diff --git a/Library/libs/gettimeofday.c b/Library/libs/gettimeofday.c
new file mode 100644 (file)
index 0000000..22db97f
--- /dev/null
@@ -0,0 +1,21 @@
+#define _BSD_SOURCE
+
+#include <sys/time.h>
+#include <unistd.h>
+
+int gettimeofday(struct timeval *tv, struct timezone *tz)
+{
+  if (tv) {
+    _time(&tv->tv_sec, 0);
+    tv->tv_usec = 0;
+  }
+  if (tz) {
+    /* FIXME: although this is obsolete */
+    tz->tz_minuteswest = 0;
+    /* and this is not used */
+    tz->tz_dsttime = 0;
+  }
+  return 0;
+}
+
+  
\ No newline at end of file
diff --git a/Library/libs/nanosleep.c b/Library/libs/nanosleep.c
new file mode 100644 (file)
index 0000000..c85e599
--- /dev/null
@@ -0,0 +1,49 @@
+#include <time.h>
+#include <unistd.h>
+#include <errno.h>
+
+/* Ok so this doesn't score too highly on accuracy but it'll wrap code using
+   the modern API's for long sleeps just fine */
+
+int clock_nanosleep(clockid_t clock, int flags, const struct timespec *req, struct timespec *rem)
+{
+  time_t tbase;
+  time_t tend;
+  uint16_t t = req->tv_sec * 10;
+  long nsec = req->tv_nsec;
+
+  if (clock != CLOCK_REALTIME && clock != CLOCK_MONOTONIC) {
+   errno = EINVAL;
+   return -1;
+  }
+
+  /* Will be 0-9 range so avoid costly divides */
+  while(nsec > 50000000UL) {
+    nsec -= 50000000UL;
+    t++;
+  }
+  if (_time(&tbase, clock) == -1)
+    return -1;
+
+  if (flags & TIMER_ABSTIME) {
+   if (tbase < req->tv_sec)
+    return 0;
+   t += (tbase - req->tv_sec) * 10;
+  }
+  if (_pause(t) == 0)
+    return 0;
+  _time(&tend, clock);
+  if (rem) {
+    rem->tv_sec = tbase + t/10 - tend;
+    rem->tv_nsec = 0;
+  }
+  return -1;
+}
+
+
+int nanosleep(const struct timespec *req, struct timespec *rem)
+{
+ return clock_nanosleep(CLOCK_REALTIME, 0, req, rem);
+}
+
diff --git a/Library/libs/settimeofday.c b/Library/libs/settimeofday.c
new file mode 100644 (file)
index 0000000..8611573
--- /dev/null
@@ -0,0 +1,13 @@
+#define _BSD_SOURCE
+
+#include <sys/time.h>
+#include <unistd.h>
+
+int settimeofday(struct timeval *tv, const struct timezone *tz)
+{
+  int ret = 0;
+  if (tv) {
+    ret = _stime(&tv->tv_sec, 0);
+  }
+  return ret;
+}