dbus: increase player timer period
[vlc.git] / compat / clock_gettime.c
blobbcb0cc9df946485f768a9f14e7809835a3f2a3e7
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 *****************************************************************************/
23 #ifdef __APPLE__
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
29 #include <sys/errno.h>
30 #include <sys/types.h>
31 #include <sys/time.h>
32 #include <sys/sysctl.h>
34 /**
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
43 * \note
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);
53 if (ret != 0)
54 return errno;
56 return 0;
59 /**
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)
82 int ret;
83 struct timeval currenttime;
84 struct timeval boottime_begin;
85 struct timeval boottime_end;
87 do {
88 ret = vlc__get_system_boottime(&boottime_begin);
89 if (ret != 0)
90 return ret;
92 ret = gettimeofday(&currenttime, NULL);
93 if (ret != 0)
94 return ret;
96 ret = vlc__get_system_boottime(&boottime_end);
97 if (ret != 0)
98 return ret;
99 } while (timercmp(&boottime_begin, &boottime_end, !=));
101 timersub(&currenttime, &boottime_begin, tv);
102 return 0;
105 int clock_gettime(clockid_t clock_id, struct timespec *tp)
107 int ret = 0;
108 struct timeval tv;
110 switch (clock_id) {
111 case CLOCK_MONOTONIC:
112 ret = vlc__get_monotonic(&tv);
113 break;
114 case CLOCK_REALTIME:
115 ret = gettimeofday(&tv, NULL);
116 break;
117 default:
118 errno = EINVAL;
119 return -1;
122 if (ret == 0)
123 TIMEVAL_TO_TIMESPEC(&tv, tp);
124 return ret;
127 #endif