[vlc-devel] [PATCH 2/3] compat: add clock_nanosleep for darwin
Rémi Denis-Courmont
remi at remlab.net
Tue Mar 10 17:18:16 CET 2020
Le tiistaina 10. maaliskuuta 2020, 11.50.07 EET Marvin Scholz a écrit :
> ---
> compat/clock_nanosleep.c | 109 +++++++++++++++++++++++++++++++++++++++
> configure.ac | 4 +-
> include/vlc_fixups.h | 7 +++
> 3 files changed, 119 insertions(+), 1 deletion(-)
> create mode 100644 compat/clock_nanosleep.c
>
> diff --git a/compat/clock_nanosleep.c b/compat/clock_nanosleep.c
> new file mode 100644
> index 0000000000..374e4668e2
> --- /dev/null
> +++ b/compat/clock_nanosleep.c
> @@ -0,0 +1,109 @@
> +/**************************************************************************
> *** + * clock_nanosleep.c: POSIX clock_nanosleep() 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. +
> ***************************************************************************
> **/ +
> +#ifndef __APPLE__
> +# error clock_nanosleep not implemented on your platform!
> +#endif
Same as 1/3
> +
> +#ifdef HAVE_CONFIG_H
> +# include <config.h>
> +#endif
> +
> +#define _DARWIN_C_SOURCE
> +#define _POSIX_C_SOURCE 200809L
> +
> +#include <assert.h>
> +#include <pthread.h>
> +#include <sys/errno.h>
> +#include <sys/types.h>
> +#include <sys/time.h>
> +#include <sys/sysctl.h>
> +#include <mach/clock_types.h>
> +
> +#define timespecs_sub(tsa, tsb, tsr) \
> + do { \
> + (tsr)->tv_sec = (tsa)->tv_sec - (tsb)->tv_sec; \
> + (tsr)->tv_nsec = (tsa)->tv_nsec - (tsb)->tv_nsec; \
> + if ((tsr)->tv_nsec < 0) { \
> + (tsr)->tv_sec--; \
> + (tsr)->tv_nsec += NSEC_PER_SEC; \
> + } \
> + } while (0)
> +
> +#define timespec_zero(ts) \
> + ((ts)->tv_sec == 0 && (ts)->tv_nsec == 0)
> +
> +int clock_nanosleep(clockid_t clock_id, int flags,
> + const struct timespec *rqtp, struct timespec *rmtp)
> +{
> + struct timespec ts_rel;
> +
> + // Validate timespec
> + if (rqtp == NULL || rqtp->tv_sec < 0 ||
> + rqtp->tv_nsec < 0 || (unsigned long)rqtp->tv_nsec >=
> NSEC_PER_SEC) { + errno = EINVAL;
> + return -1;
> + }
> +
> + if (flags == TIMER_ABSTIME) {
> + struct timespec ts_now;
> +
> + // We don't support waiting for a absolute wallclock time currently
> + if (clock_id == CLOCK_REALTIME) {
> + assert(!!"Absolute wait for wallclock not implemented on
> Darwin!");
Should probably be only one exclamation mark. But making a compat function
abort with valid input parameters strikes me as a bad idea.
> + errno = ENOTSUP;
...and AFAIK, that's not a permitted error in this case.
You could handle the case that the RTC was shifted backward just fine.
And if it was shifted forward, returning late is permissible.
> + return -1;
> + }
> +
> + // Get current time with requested clock
> + if (clock_gettime(clock_id, &ts_now) != 0)
> + return -1;
> +
> + // Calculate relative timespec
> + timespecs_sub(rqtp, &ts_now, &ts_rel);
> +
> + // Check if time already elapsed
> + if (ts_rel.tv_sec < 0 || timespec_zero(&ts_rel)) {
> + pthread_testcancel();
> + return 0;
> + }
> + } else if (flags == 0) {
> + ts_rel = *rqtp;
> + } else {
> + // Invalid flags
> + errno = EINVAL;
> + return -1;
> + }
> +
> + switch (clock_id) {
> + case CLOCK_MONOTONIC:
> + case CLOCK_REALTIME:
> + return nanosleep(&ts_rel, rmtp);
> + default:
> + // Invalid clock
> + errno = EINVAL;
> + return -1;
> + }
> +
> + assert(0);
> + return -1;
> +}
> diff --git a/configure.ac b/configure.ac
> index 134cae50a6..c20ab3c75c 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -181,7 +181,9 @@ case "${host_os}" in
>
> dnl macOS 10.11 lacks clock_gettime, introduced with 10.12
> AC_LIBOBJ([clock_gettime])
> - AC_CHECK_FUNCS([clock_gettime])
> + dnl macOS lacks clock_nanosleep
> + AC_LIBOBJ([clock_nanosleep])
> + AC_CHECK_FUNCS([clock_gettime clock_nanosleep])
>
> AC_EGREP_CPP(yes,
> [#import <TargetConditionals.h>
> diff --git a/include/vlc_fixups.h b/include/vlc_fixups.h
> index 055bcf84d6..a296db0c32 100644
> --- a/include/vlc_fixups.h
> +++ b/include/vlc_fixups.h
> @@ -660,6 +660,9 @@ FILE *vlc_win32_tmpfile(void);
> # define fdatasync fsync
>
> # include <time.h>
> +# ifndef TIMER_ABSTIME
> +# define TIMER_ABSTIME 0x01
> +# endif
> # ifndef CLOCK_REALTIME
> # define CLOCK_REALTIME 0
> # endif
> @@ -669,6 +672,10 @@ FILE *vlc_win32_tmpfile(void);
> # ifndef HAVE_CLOCK_GETTIME
> int clock_gettime(clockid_t clock_id, struct timespec *tp);
> # endif
> +# ifndef HAVE_CLOCK_NANOSLEEP
> +int clock_nanosleep(clockid_t clock_id, int flags,
> + const struct timespec *rqtp, struct timespec *rmtp);
> +# endif
> #endif
>
> #ifdef __cplusplus
--
雷米‧德尼-库尔蒙
http://www.remlab.net/
More information about the vlc-devel
mailing list