[PATCH v2] time: Add TIME_MONOTONIC, TIME_ACTIVE, and TIME_THREAD_ACTIVE

Florian Weimer fweimer@redhat.com
Mon Dec 1 14:33:02 GMT 2025


* Adhemerval Zanella Netto:

>> timespec_get() offers another chance for an actually portable API for
>> monotonic time.  The ISO C committee was aware of the discrepancy on
>> Linux with CLOCK_MONOTONIC, and specifically defined TIME_MONOTONIC to
>> allow either behavior, however it would benefit future users of the
>> API if the behavior was actually consistent between systems.
>> Otherwise everyone might as well stick to the #ifdefs that are
>> currently required.  On FreeBSD, TIME_MONOTONIC corresponds to its
>> POSIX-conforming CLOCK_MONOTONIC, and the same behavior could be
>> provided on Linux by mapping TIME_MONOTONIC to CLOCK_BOOTTIME.  It
>> could still be mapped by subtracting 1 if TIME_MONOTONIC is defined to
>> (CLOCK_BOOTTIME+1).  Windows and macOS support C11's TIME_UTC but do
>> not yet support C23's TIME_MONOTONIC .
>
> Thanks for bringing this up and it is reasonable.  However this will add
> a different discrepancy where from FreeBSD documentation TIME_MONOTONIC
> maps to CLOCK_MONOTONIC, while for glibc it will map to CLOCK_BOOTIME.
>
> This will make usage as described by the one in libc-coord [1] even
> more confusing (exchanging clock id between C and POSIX interfaces);
> even if CLOCK_MONOTONIC does not map semantically between systems.

I agree that making TIME_MONOTONIC to mean CLOCK_BOOTTIME would be too
confusing.

I don't think POSIX requires a constant rate of increments (and it would
be difficult to mandate it), so I'm not sure if the Linux behavior is
non-conforming.

Thanks,
Florian



More information about the Libc-alpha mailing list