On Sat, 28 Nov 2026 at 13:27, ylavic.apache.org via commits <
[email protected]> wrote:

> Author: ylavic
> Date: Sat Nov 28 12:27:25 2026
> New Revision: 1940858
>
> Log:
> apr_time: add apr_time_monotonic().
>
> apr_time_now() is the system time, which jumps when it's changed (by an
> administrator, NTP), so elapsed times and timeouts computed with it can go
> wrong.
>
> Add apr_time_monotonic(), microseconds from an unspecified origin that
> never jump, only differences being meaningful: on Unix with
> clock_gettime(CLOCK_MONOTONIC) (run by configure, in librt if needed),
> on Windows with QueryPerformanceCounter(), on OS/2 with its timer, else
> apr_time_now(). APR_HAS_MONOTONIC_TIME says whether a monotonic clock is
> used.
>
> Nice!

Some comments inline.


> +/**
> + * @return the time of a monotonic clock, in microseconds from an
> unspecified
> + *         origin: only the difference between two values is meaningful,
> and
> + *         it never jumps with changes of the system time
> + * @remark Where APR_HAS_MONOTONIC_TIME is 0 no monotonic clock is used,
> this
> + *         is apr_time_now().
> + */
> +APR_DECLARE(apr_time_t) apr_time_monotonic(void);
> +
>
The use of apr_time_t for apr_time_monotonic() can be confusing. I suggest
to add apr_monotonic_time_t or use apr_interval_time_t.



> +APR_DECLARE(apr_time_t) apr_time_monotonic(void)
> +{
> +    LARGE_INTEGER freq, now;
> +    LONGLONG secs, rest;
> +
> +    /* Both always succeed (Windows XP and later), the frequency being
> +     * fixed at boot
> +     */
> +    QueryPerformanceFrequency(&freq);
>
 The QueryPerformanceFrequence() function has caveats:
1. It can fail if the installed hardware does not supports a
high-resolution performance counter. [1]

There is comment that function actually never fails. But I can expect that
some virtualization scenarios may broke this guarantee. I suggest to handle
error and fallback to GetTickCount64().

[1]:
https://learn.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancefrequency#return-value


+    QueryPerformanceCounter(&now);
> +
> +    /* Whole seconds, then the remainder's microseconds, so that the
> +     * multiplication can't overflow
> +     */
> +    secs = now.QuadPart / freq.QuadPart;
> +    rest = now.QuadPart % freq.QuadPart;
> +    return (apr_time_t)(secs * APR_USEC_PER_SEC + rest * APR_USEC_PER_SEC
> / freq.QuadPart);
>
apr_time_make(secs,  rest * APR_USEC_PER_SEC / freq.QuadPart)

?


-- 
Ivan Zhakov

Reply via email to