Le Thu, Oct 14, 2021 at 05:54:44PM -0400, o...@eigenstate.org a écrit :
> Quoth tlaro...@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;
> +}

Since only CLOCK_REALTIME is mandatory (up to current POSIX 7), you could drop 
the MONOTONIC
variant. And nsec(2) is good enough for REALTIME.

Thanks for working on this!

Best,
-- 
        Thierry Laronde <tlaronde +AT+ polynum +dot+ com>
                     http://www.kergis.com/
                    http://kertex.kergis.com/
                       http://www.sbfa.fr/
Key fingerprint = 0FF7 E906 FBAF FE95 FD89  250D 52B1 AE95 6006 F40C

------------------------------------------
9fans: 9fans
Permalink: 
https://9fans.topicbox.com/groups/9fans/T1992d89fd8b382c7-Me0d4b6affdd5977cbe6d2c8b
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription

Reply via email to