Quoth [email protected]:
> But is clock_gettime(2) available in APE in any instance of Plan9 or is
> there a routine achieving the same purpose?
Here's a quick sketch of something that we could add; note
that the implemented monotonicity is really crude.
diff 03d870e0283299404b0eb46689d13f8538e83a2f uncommitted
--- a//sys/include/ape/time.h
+++ b//sys/include/ape/time.h
@@ -53,15 +53,20 @@
#ifdef _POSIX_SOURCE
extern void tzset(void);
+#define CLOCK_REALTIME 1
+#define CLOCK_MONOTONIC 2
+
+typedef int clockid_t;
+
struct timespec {
time_t tv_sec;
long tv_nsec;
};
extern int nanosleep(const struct timespec *req, struct timespec *rem);
-#endif
-#ifdef __cplusplus
-}
+extern int clock_getres(clockid_t, struct timespec*);
+extern int clock_gettime(clockid_t, struct timespec*);
+extern int clock_settime(clockid_t, struct timespec*);
#endif
#ifdef _POSIX_SOURCE
@@ -69,6 +74,10 @@
extern long timezone;
extern long altzone;
extern int daylight;
+#endif
+
+#ifdef __cplusplus
+}
#endif
#endif /* __TIME_H */
--- /tmp/diff100001791256
+++ b/sys/src/ape/lib/bsd/clock.c
@@ -1,0 +1,60 @@
+#include <sys/types.h>
+#include <time.h>
+#include <errno.h>
+#include <lock.h>
+
+/* ap/plan9/9nsec.c */
+extern long long _NSEC(void);
+
+#define Ns2sec (1000*1000*1000)
+
+int
+clock_getres(clockid_t id, struct timespec *ts)
+{
+ if(id != CLOCK_REALTIME && id != CLOCK_MONOTONIC){
+ errno = EINVAL;
+ return -1;
+ }
+ ts->tv_sec = 0;
+ ts->tv_nsec = 1;
+ return 0;
+}
+
+int
+clock_settime(clockid_t, struct timespec *)
+{
+ errno = EPERM;
+ return -1;
+}
+
+int
+clock_gettime(clockid_t clk, struct timespec *ts)
+{
+ static Lock maxlk;
+ static long long max;
+ long long t;
+
+ t = _NSEC();
+ switch(clk){
+ case CLOCK_REALTIME:
+ /* nothing */
+ break;
+ case CLOCK_MONOTONIC:
+ /* Bug: we should support a real monotonic clock */
+ lock(&maxlk);
+ if(t < max)
+ t = max;
+ else
+ max = t;
+ unlock(&maxlk);
+ break;
+ default:
+ errno = EINVAL;
+ return -1;
+ }
+
+ ts->tv_sec = t/Ns2sec;
+ ts->tv_nsec = t%Ns2sec;
+
+ return 0;
+}
------------------------------------------
9fans: 9fans
Permalink:
https://9fans.topicbox.com/groups/9fans/T1992d89fd8b382c7-Mf08f6aaf225fc08a5763ecb2
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription