1 /*****************************************************************************
2 * clock_gettime.c: POSIX clock_gettime() replacement
3 *****************************************************************************
4 * Copyright © 2020 VLC authors and VideoLAN
6 * Author: Marvin Scholz <epirat07 at gmail dot com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
29 #include <sys/errno.h>
30 #include <sys/types.h>
32 #include <sys/sysctl.h>
35 * Get the absolute time at which the system was booted
37 * This time is changed whenever the clock is adjusted to
38 * correctly reflect the boot time with the adjusted clock,
39 * so just querying it once and reusing the value is not safe.
41 * \param[out] tv Timeval struct to write the boottime to
44 * The boot time only has microsecond precision
46 * \return 0 on success, else -1 and errno set
48 static int vlc__get_system_boottime(struct timeval
*tv
)
50 int ret
= sysctl((int[]){ CTL_KERN
, KERN_BOOTTIME
}, 2,
51 tv
, &(size_t){ sizeof(*tv
) }, NULL
, 0);
60 * Get the monotonic time (CLOCK_MONOTONIC)
62 * Calculates a monotically incrasing time since system boot
63 * that continues to increment when the system is asleep.
65 * Warnings to everyone trying to simplify this:
66 * - Using mach_continuous_time is not equivalent to this, see
67 * the darwin manpage about CLOCK_MONOTONIC_RAW for an explanation.
68 * - Using mach_absolute_time is not equivalent to this, as it does
69 * not continue to increment when the system is asleep.
70 * - The boot time is queried twice in a loop and only used if both
71 * match. This is done to detect if the boot time change since we
72 * obtained the current time of day, as the boot time can change
73 * when the system wallclock is adjusted, as that will adjust the
74 * boot time accordingly.
76 * \param[out] tv Timeval struct to write the monotonic time to
78 * \return 0 on success, else -1 and errno set
80 static int vlc__get_monotonic(struct timeval
*tv
)
83 struct timeval currenttime
;
84 struct timeval boottime_begin
;
85 struct timeval boottime_end
;
88 ret
= vlc__get_system_boottime(&boottime_begin
);
92 ret
= gettimeofday(¤ttime
, NULL
);
96 ret
= vlc__get_system_boottime(&boottime_end
);
99 } while (timercmp(&boottime_begin
, &boottime_end
, !=));
101 timersub(¤ttime
, &boottime_begin
, tv
);
105 int clock_gettime(clockid_t clock_id
, struct timespec
*tp
)
111 case CLOCK_MONOTONIC
:
112 ret
= vlc__get_monotonic(&tv
);
115 ret
= gettimeofday(&tv
, NULL
);
123 TIMEVAL_TO_TIMESPEC(&tv
, tp
);