diff mbox series

[2/5] time: Implement c23 timespec_get base

Message ID 20230619222052.682-3-luoyonggang@gmail.com
State New
Headers show
Series c2y proposal add monotonicwait support for mtx and ctx | expand

Commit Message

Yonggang Luo June 19, 2023, 10:20 p.m. UTC
These newly implement base are:
#define TIME_MONOTONIC          2
#define TIME_PROCESS_CPUTIME_ID 3
#define TIME_THREAD_CPUTIME_ID  4
#define TIME_MONOTONIC_RAW      5
#define TIME_REALTIME_COARSE    6
#define TIME_MONOTONIC_COARSE   7
#define TIME_BOOTTIME           8
#define TIME_REALTIME_ALARM     9
#define TIME_BOOTTIME_ALARM     10
#define TIME_SGI_CYCLE          11
#define TIME_TAI                12

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
---
 time/time.h         | 13 ++++++++++++
 time/timespec_get.c | 48 ++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 58 insertions(+), 3 deletions(-)

Comments

Florian Weimer June 20, 2023, 12:44 p.m. UTC | #1
* Yonggang Luo via Libc-alpha:

> diff --git a/time/timespec_get.c b/time/timespec_get.c
> index 9b1d4f22ed..a57b1ee0b8 100644
> --- a/time/timespec_get.c
> +++ b/time/timespec_get.c
> @@ -22,10 +22,52 @@
>  int
>  timespec_get (struct timespec *ts, int base)
>  {
> -  if (base == TIME_UTC)
> +	clockid_t clockid = -1;
> +	switch (base) {
> +	default:
> +		break;
> +	case TIME_UTC:
> +		clockid = CLOCK_REALTIME;
> +		break;
> +	case TIME_MONOTONIC:
> +		clockid = CLOCK_MONOTONIC;
> +		break;
> +	case TIME_PROCESS_CPUTIME_ID:
> +		clockid = CLOCK_PROCESS_CPUTIME_ID;
> +		break;
> +	case TIME_THREAD_CPUTIME_ID:
> +		clockid = CLOCK_THREAD_CPUTIME_ID;
> +		break;
> +	case TIME_MONOTONIC_RAW:
> +		clockid = CLOCK_MONOTONIC_RAW;
> +		break;
> +	case TIME_REALTIME_COARSE:
> +		clockid = CLOCK_REALTIME_COARSE;
> +		break;
> +	case TIME_MONOTONIC_COARSE:
> +		clockid = CLOCK_MONOTONIC_COARSE;
> +		break;
> +	case TIME_BOOTTIME:
> +		clockid = CLOCK_BOOTTIME;
> +		break;
> +	case TIME_REALTIME_ALARM:
> +		clockid = CLOCK_REALTIME_ALARM;
> +		break;
> +	case TIME_BOOTTIME_ALARM:
> +		clockid = CLOCK_BOOTTIME_ALARM;
> +		break;
> +	case TIME_SGI_CYCLE:
> +		clockid = CLOCK_SGI_CYCLE;
> +		break;
> +	case TIME_TAI:
> +		clockid = CLOCK_TAI;
> +		break;
> +	}
> +  if (clockid >= 0)
>      {
> -      __clock_gettime (CLOCK_REALTIME, ts);
> -      return base;
> +      if (__clock_gettime (clockid, ts) >= 0) {
> +        return base;
> +      }
>      }
>    return 0;
>  }

This was recently discussed on the libc-coord list:

  Relation between C timespec_get time bases and POSIX clockid_t values.
  <https://www.openwall.com/lists/libc-coord/2023/04/25/1>

I'm not sure if we should extend this because it's just a bizarre WG14
not-invent-here interface, designed to be incompatible with existing
implementation practice.

Thanks,
Florian
develop--- via Libc-alpha June 20, 2023, 4:10 p.m. UTC | #2
On Tue, Jun 20, 2023 at 8:44 PM Florian Weimer <fweimer@redhat.com> wrote:
>
> * Yonggang Luo via Libc-alpha:
>
> > diff --git a/time/timespec_get.c b/time/timespec_get.c
> > index 9b1d4f22ed..a57b1ee0b8 100644
> > --- a/time/timespec_get.c
> > +++ b/time/timespec_get.c
> > @@ -22,10 +22,52 @@
> >  int
> >  timespec_get (struct timespec *ts, int base)
> >  {
> > -  if (base == TIME_UTC)
> > +     clockid_t clockid = -1;
> > +     switch (base) {
> > +     default:
> > +             break;
> > +     case TIME_UTC:
> > +             clockid = CLOCK_REALTIME;
> > +             break;
> > +     case TIME_MONOTONIC:
> > +             clockid = CLOCK_MONOTONIC;
> > +             break;
> > +     case TIME_PROCESS_CPUTIME_ID:
> > +             clockid = CLOCK_PROCESS_CPUTIME_ID;
> > +             break;
> > +     case TIME_THREAD_CPUTIME_ID:
> > +             clockid = CLOCK_THREAD_CPUTIME_ID;
> > +             break;
> > +     case TIME_MONOTONIC_RAW:
> > +             clockid = CLOCK_MONOTONIC_RAW;
> > +             break;
> > +     case TIME_REALTIME_COARSE:
> > +             clockid = CLOCK_REALTIME_COARSE;
> > +             break;
> > +     case TIME_MONOTONIC_COARSE:
> > +             clockid = CLOCK_MONOTONIC_COARSE;
> > +             break;
> > +     case TIME_BOOTTIME:
> > +             clockid = CLOCK_BOOTTIME;
> > +             break;
> > +     case TIME_REALTIME_ALARM:
> > +             clockid = CLOCK_REALTIME_ALARM;
> > +             break;
> > +     case TIME_BOOTTIME_ALARM:
> > +             clockid = CLOCK_BOOTTIME_ALARM;
> > +             break;
> > +     case TIME_SGI_CYCLE:
> > +             clockid = CLOCK_SGI_CYCLE;
> > +             break;
> > +     case TIME_TAI:
> > +             clockid = CLOCK_TAI;
> > +             break;
> > +     }
> > +  if (clockid >= 0)
> >      {
> > -      __clock_gettime (CLOCK_REALTIME, ts);
> > -      return base;
> > +      if (__clock_gettime (clockid, ts) >= 0) {
> > +        return base;
> > +      }
> >      }
> >    return 0;
> >  }
>
> This was recently discussed on the libc-coord list:
>
>   Relation between C timespec_get time bases and POSIX clockid_t values.
>   <https://www.openwall.com/lists/libc-coord/2023/04/25/1>
>
> I'm not sure if we should extend this because it's just a bizarre WG14
> not-invent-here interface, designed to be incompatible with existing
> implementation practice.

These are implemented
https://gustedt.gitlabpages.inria.fr/c23-library/#time_monotonic-time_active-time_thread_active
So time_base = clock_id + 1 for linux/posix.
But do we need guarantee that, because there so much posix implementation.

Maybe we need a function named timebase_to_clockid/clockid_to_timebase in
posix standard, for c standard just keep as is


>
> Thanks,
> Florian
>


--
         此致
礼
罗勇刚
Yours
    sincerely,
Yonggang Luo
Joseph Myers June 20, 2023, 8:25 p.m. UTC | #3
On Tue, 20 Jun 2023, Yonggang Luo via Libc-alpha wrote:

> +#ifdef __GLIBC_USE_ISOC23
> +#define TIME_MONOTONIC          2

The only standard values new in C2x are TIME_MONOTONIC, TIME_ACTIVE and 
TIME_THREAD_ACTIVE.  All others should probably be conditioned on 
__USE_GNU if defined at all.
diff mbox series

Patch

diff --git a/time/time.h b/time/time.h
index 368f4dc588..189a560199 100644
--- a/time/time.h
+++ b/time/time.h
@@ -64,6 +64,19 @@  typedef __pid_t pid_t;
 /* Time base values for timespec_get.  */
 # define TIME_UTC 1
 #endif
+#ifdef __GLIBC_USE_ISOC23
+#define TIME_MONOTONIC          2
+#define TIME_PROCESS_CPUTIME_ID 3
+#define TIME_THREAD_CPUTIME_ID  4
+#define TIME_MONOTONIC_RAW      5
+#define TIME_REALTIME_COARSE    6
+#define TIME_MONOTONIC_COARSE   7
+#define TIME_BOOTTIME           8
+#define TIME_REALTIME_ALARM     9
+#define TIME_BOOTTIME_ALARM     10
+#define TIME_SGI_CYCLE          11
+#define TIME_TAI                12
+#endif
 
 __BEGIN_DECLS
 
diff --git a/time/timespec_get.c b/time/timespec_get.c
index 9b1d4f22ed..a57b1ee0b8 100644
--- a/time/timespec_get.c
+++ b/time/timespec_get.c
@@ -22,10 +22,52 @@ 
 int
 timespec_get (struct timespec *ts, int base)
 {
-  if (base == TIME_UTC)
+	clockid_t clockid = -1;
+	switch (base) {
+	default:
+		break;
+	case TIME_UTC:
+		clockid = CLOCK_REALTIME;
+		break;
+	case TIME_MONOTONIC:
+		clockid = CLOCK_MONOTONIC;
+		break;
+	case TIME_PROCESS_CPUTIME_ID:
+		clockid = CLOCK_PROCESS_CPUTIME_ID;
+		break;
+	case TIME_THREAD_CPUTIME_ID:
+		clockid = CLOCK_THREAD_CPUTIME_ID;
+		break;
+	case TIME_MONOTONIC_RAW:
+		clockid = CLOCK_MONOTONIC_RAW;
+		break;
+	case TIME_REALTIME_COARSE:
+		clockid = CLOCK_REALTIME_COARSE;
+		break;
+	case TIME_MONOTONIC_COARSE:
+		clockid = CLOCK_MONOTONIC_COARSE;
+		break;
+	case TIME_BOOTTIME:
+		clockid = CLOCK_BOOTTIME;
+		break;
+	case TIME_REALTIME_ALARM:
+		clockid = CLOCK_REALTIME_ALARM;
+		break;
+	case TIME_BOOTTIME_ALARM:
+		clockid = CLOCK_BOOTTIME_ALARM;
+		break;
+	case TIME_SGI_CYCLE:
+		clockid = CLOCK_SGI_CYCLE;
+		break;
+	case TIME_TAI:
+		clockid = CLOCK_TAI;
+		break;
+	}
+  if (clockid >= 0)
     {
-      __clock_gettime (CLOCK_REALTIME, ts);
-      return base;
+      if (__clock_gettime (clockid, ts) >= 0) {
+        return base;
+      }
     }
   return 0;
 }