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

Mark Harris mark.hsj@gmail.com
Tue Nov 25 12:18:55 GMT 2025


Adhemerval Zanella wrote:
>
> The TIME_MONOTONIC maps to POSIX's CLOCK_MONOTONIC, TIME_ACTIVE to
> CLOCK_PROCESS_CPUTIME_ID, and TIME_THREAD_ACTIVE to
> CLOCK_THREAD_CPUTIME_ID.
>
> No Linux specific timer are added as extension.
>
> Co-authored-by: Yonggang Luo <luoyonggang@gmail.com>

This may be a good opportunity to correct the longstanding discrepancy
with the monotonic clock.

Although POSIX requires CLOCK_MONOTONIC to represent a time since some
point in the past that does not change after system start-up, Linux
does not conform to this because it excludes time while the system is
suspended.  There was an attempt to correct this in the kernel in 2018
(https://lore.kernel.org/lkml/20180301163331.987775783@linutronix.de/T/),
but it was reverted due to dependence on the nonconforming behavior.
Instead, Linux provides CLOCK_BOOTTIME with the behavior that POSIX
requires for CLOCK_MONOTONIC.  Other systems including macOS and the
BSDs have a conforming CLOCK_MONOTONIC, and may also provide
CLOCK_UPTIME or CLOCK_UPTIME_RAW that behaves like Linux
CLOCK_MONOTONIC.  The difference in naming complicates software that
attempts to be portable.

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 .


 - Mark


More information about the Libc-alpha mailing list