9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: ori@eigenstate.org
To: 9fans@9fans.net
Subject: Re: [9fans] Is POSIX:clock_gettime(2) in APE somewhere/anywhere?
Date: Thu, 14 Oct 2021 17:54:44 -0400	[thread overview]
Message-ID: <555AC0ACE5AB14D2940B742B6C01BE5A@eigenstate.org> (raw)
In-Reply-To: <YWSE46m1qM3FPQrB@polynum.com>

Quoth tlaronde@polynum.com:
> 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

  parent reply	other threads:[~2021-10-14 21:54 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-11 18:39 tlaronde
2021-10-11 19:12 ` ori
2021-10-12  7:04   ` tlaronde
2021-10-11 19:20 ` Sigrid Solveig Haflínudóttir
2021-10-12  7:05   ` tlaronde
2021-10-12 18:05 ` tlaronde
2021-10-14 19:58   ` Thaddeus Woskowiak
2021-10-14 21:54 ` ori [this message]
2021-10-15 11:15   ` tlaronde

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=555AC0ACE5AB14D2940B742B6C01BE5A@eigenstate.org \
    --to=ori@eigenstate.org \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).