On Sat, Nov 28, 2026 at 6:09 PM Ivan Zhakov <[email protected]> wrote:
>
> 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!

Thanks!

>>
>> +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.

Makes sense, changed to apr_interval_time_t in r1940960, which aligns
with "microseconds from an unspecified origin".

>>
>> +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().

Also in r1940960, added apr__time_init() (called from
apr_initialize()) to check QueryPerformanceFrequency() at startup and
fall back to GetTickCount64() at runtime if it fails.

>
> [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)

Done (r1940960), thanks for the review!


Regards;
Yann.

Reply via email to