[vlc-devel] [PATCH v3 1/4] compat: add clock_gettime for darwin
Rémi Denis-Courmont
remi at remlab.net
Sat Mar 21 19:50:16 CET 2020
Le lauantaina 21. maaliskuuta 2020, 15.35.06 EET Marvin Scholz a écrit :
> ---
> compat/clock_gettime.c | 127 +++++++++++++++++++++++++++++++++++++++++
> configure.ac | 2 +-
> include/vlc_fixups.h | 11 ++++
> 3 files changed, 139 insertions(+), 1 deletion(-)
> create mode 100644 compat/clock_gettime.c
>
> diff --git a/compat/clock_gettime.c b/compat/clock_gettime.c
> new file mode 100644
> index 0000000000..bcb0cc9df9
> --- /dev/null
> +++ b/compat/clock_gettime.c
> @@ -0,0 +1,127 @@
> +/**************************************************************************
> *** + * clock_gettime.c: POSIX clock_gettime() replacement
> +
> ***************************************************************************
> ** + * Copyright © 2020 VLC authors and VideoLAN
> + *
> + * Author: Marvin Scholz <epirat07 at gmail dot com>
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms of the GNU Lesser General Public License as published by
> + * the Free Software Foundation; either version 2.1 of the License, or + *
> (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public License
> + * along with this program; if not, write to the Free Software Foundation,
> + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. +
> ***************************************************************************
> **/ +
> +#ifdef __APPLE__
> +
> +#ifdef HAVE_CONFIG_H
> +# include <config.h>
> +#endif
> +
> +#include <sys/errno.h>
> +#include <sys/types.h>
> +#include <sys/time.h>
> +#include <sys/sysctl.h>
> +
> +/**
> + * Get the absolute time at which the system was booted
> + *
> + * This time is changed whenever the clock is adjusted to
> + * correctly reflect the boot time with the adjusted clock,
> + * so just querying it once and reusing the value is not safe.
> + *
> + * \param[out] tv Timeval struct to write the boottime to
> + *
> + * \note
> + * The boot time only has microsecond precision
> + *
> + * \return 0 on success, else -1 and errno set
> + */
> +static int vlc__get_system_boottime(struct timeval *tv)
> +{
> + int ret = sysctl((int[]){ CTL_KERN, KERN_BOOTTIME }, 2,
> + tv, &(size_t){ sizeof(*tv) }, NULL, 0);
> +
> + if (ret != 0)
> + return errno;
> +
> + return 0;
> +}
> +
> +/**
> + * Get the monotonic time (CLOCK_MONOTONIC)
> + *
> + * Calculates a monotically incrasing time since system boot
> + * that continues to increment when the system is asleep.
> + *
> + * Warnings to everyone trying to simplify this:
> + * - Using mach_continuous_time is not equivalent to this, see
> + * the darwin manpage about CLOCK_MONOTONIC_RAW for an explanation.
> + * - Using mach_absolute_time is not equivalent to this, as it does
> + * not continue to increment when the system is asleep.
> + * - The boot time is queried twice in a loop and only used if both
> + * match. This is done to detect if the boot time change since we
> + * obtained the current time of day, as the boot time can change
> + * when the system wallclock is adjusted, as that will adjust the
> + * boot time accordingly.
> + *
> + * \param[out] tv Timeval struct to write the monotonic time to
> + *
> + * \return 0 on success, else -1 and errno set
> + */
> +static int vlc__get_monotonic(struct timeval *tv)
> +{
> + int ret;
> + struct timeval currenttime;
> + struct timeval boottime_begin;
> + struct timeval boottime_end;
> +
> + do {
> + ret = vlc__get_system_boottime(&boottime_begin);
> + if (ret != 0)
> + return ret;
> +
> + ret = gettimeofday(¤ttime, NULL);
> + if (ret != 0)
> + return ret;
> +
> + ret = vlc__get_system_boottime(&boottime_end);
> + if (ret != 0)
> + return ret;
> + } while (timercmp(&boottime_begin, &boottime_end, !=));
> +
> + timersub(¤ttime, &boottime_begin, tv);
> + return 0;
> +}
> +
> +int clock_gettime(clockid_t clock_id, struct timespec *tp)
> +{
> + int ret = 0;
> + struct timeval tv;
> +
> + switch (clock_id) {
> + case CLOCK_MONOTONIC:
> + ret = vlc__get_monotonic(&tv);
> + break;
> + case CLOCK_REALTIME:
> + ret = gettimeofday(&tv, NULL);
> + break;
> + default:
> + errno = EINVAL;
> + return -1;
> + }
> +
> + if (ret == 0)
> + TIMEVAL_TO_TIMESPEC(&tv, tp);
> + return ret;
> +}
> +
> +#endif
> diff --git a/configure.ac b/configure.ac
> index bc1874cb97..4fd1a2058c 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -650,7 +650,7 @@ need_libc=false
> dnl Check for usual libc functions
> AC_CHECK_FUNCS([accept4 daemon fcntl flock fstatat fstatvfs fork
> getmntent_r getenv getpwuid_r isatty memalign mkostemp mmap open_memstream
> newlocale pipe2 pread posix_fadvise posix_madvise setlocale stricmp
> strnicmp strptime uselocale]) AC_REPLACE_FUNCS([aligned_alloc atof atoll
> dirfd fdopendir flockfile fsync getdelim getpid lfind lldiv memrchr nrand48
> poll posix_memalign recvmsg rewind sendmsg setenv strcasecmp strcasestr
> strdup strlcpy strndup strnlen strnstr strsep strtof strtok_r strtoll swab
> tdestroy tfind timegm timespec_get strverscmp pathconf])
> -AC_REPLACE_FUNCS([gettimeofday])
> +AC_REPLACE_FUNCS([gettimeofday clock_gettime])
You can't check this before -lrt.
> AC_CHECK_FUNC(fdatasync,,
> [AC_DEFINE(fdatasync, fsync, [Alias fdatasync() to fsync() if missing.])
> ])
> diff --git a/include/vlc_fixups.h b/include/vlc_fixups.h
> index 8fd01da0ce..055bcf84d6 100644
> --- a/include/vlc_fixups.h
> +++ b/include/vlc_fixups.h
> @@ -658,6 +658,17 @@ FILE *vlc_win32_tmpfile(void);
>
> #ifdef __APPLE__
> # define fdatasync fsync
> +
> +# include <time.h>
> +# ifndef CLOCK_REALTIME
> +# define CLOCK_REALTIME 0
> +# endif
> +# ifndef CLOCK_MONOTONIC
> +# define CLOCK_MONOTONIC 6
> +# endif
> +# ifndef HAVE_CLOCK_GETTIME
> +int clock_gettime(clockid_t clock_id, struct timespec *tp);
> +# endif
> #endif
>
> #ifdef __cplusplus
--
レミ・デニ-クールモン
http://www.remlab.net/
More information about the vlc-devel
mailing list