<html><head></head><body>Hi,<br><br>- Using inline in a C file seems useless.<br>- Testing for cancellation on error other than EINTR is suspicious.<br>- Not sure LIBOBJ can be called conditionally.<br><br><div class="gmail_quote">Le 27 février 2020 16:56:01 GMT+02:00, Marvin Scholz <epirat07@gmail.com> a écrit :<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<pre class="k9mail"><hr> compat/clock_gettime.c | 131 +++++++++++++++++++++++++++++++++++++++<br> compat/clock_nanosleep.c | 108 ++++++++++++++++++++++++++++++++<br> configure.ac | 6 ++<br> include/vlc_fixups.h | 18 ++++++<br> 4 files changed, 263 insertions(+)<br> create mode 100644 compat/clock_gettime.c<br> create mode 100644 compat/clock_nanosleep.c<br><br>diff --git a/compat/clock_gettime.c b/compat/clock_gettime.c<br>new file mode 100644<br>index 0000000000..fb3312fd2d<br>--- /dev/null<br>+++ b/compat/clock_gettime.c<br>@@ -0,0 +1,131 @@<br>+/*****************************************************************************<br>+ * clock_gettime.c: POSIX clock_gettime() replacement<br>+ *****************************************************************************<br>+ * Copyright © 2020 VLC authors and VideoLAN<br>+ *<br>+ * Author: Marvin Scholz <epirat07 at gmail dot com><br>+ *<br>+ * This program is free software; you can redistribute it and/or modify it<br>+ * under the terms of the GNU Lesser General Public License as published by<br>+ * the Free Software Foundation; either version 2.1 of the License, or<br>+ * (at your option) any later version.<br>+ *<br>+ * This program is distributed in the hope that it will be useful,<br>+ * but WITHOUT ANY WARRANTY; without even the implied warranty of<br>+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>+ * GNU Lesser General Public License for more details.<br>+ *<br>+ * You should have received a copy of the GNU Lesser General Public License<br>+ * along with this program; if not, write to the Free Software Foundation,<br>+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.<br>+ *****************************************************************************/<br>+<br>+#ifndef __APPLE__<br>+# error clock_gettime not implemented on your platform!<br>+#endif<br>+<br>+#ifdef HAVE_CONFIG_H<br>+# include <config.h><br>+#endif<br>+<br>+#define _DARWIN_C_SOURCE<br>+#define _POSIX_C_SOURCE 200809L<br>+<br>+#include <sys/errno.h><br>+#include <sys/types.h><br>+#include <sys/time.h><br>+#include <sys/sysctl.h><br>+#include <time.h><br>+<br>+/**<br>+ * Get the absolute time at which the system was booted<br>+ * <br>+ * This time is changed whenever the clock is adjusted to<br>+ * correctly reflect the boot time with the adjusted clock,<br>+ * so just querying it once and reusing the value is not safe.<br>+ *<br>+ * \param[out] tv Timeval struct to write the boottime to<br>+ * <br>+ * \note<br>+ * The boot time only has microsecond precision<br>+ *<br>+ * \return 0 on success, else -1 and errno set<br>+ */<br>+static inline int vlc__get_system_boottime(struct timeval *tv)<br>+{<br>+ int ret = sysctl((int[]){ CTL_KERN, KERN_BOOTTIME }, 2,<br>+ tv, &(size_t){ sizeof(*tv) }, NULL, 0);<br>+<br>+ if (ret != 0)<br>+ return errno;<br>+<br>+ return 0;<br>+}<br>+<br>+/**<br>+ * Get the monotonic time (CLOCK_MONOTONIC)<br>+ * <br>+ * Calculates a monotically incrasing time since system boot<br>+ * that continues to increment when the syste is asleep.<br>+ * <br>+ * Warnings to everyone trying to simplify this:<br>+ * - Using mach_continuous_time is not equivalent to this, see<br>+ * the darwin manpage about CLOCK_MONOTONIC_RAW for an explanation.<br>+ * - Using mach_absolute_time is not equivalent to this, as it does<br>+ * not continue to increment when the system is asleep.<br>+ * - The boot time is queried twice in a loop and only used if both<br>+ * match. This is done to detect if the boot time change since we<br>+ * obtained the current time of day, as the boot time can change<br>+ * when the system wallclock is adjusted, as that will adjust the<br>+ * boot time accordingly.<br>+ *<br>+ * \param[out] tv Timeval struct to write the monotonic time to<br>+ *<br>+ * \return 0 on success, else -1 and errno set<br>+ */<br>+static int vlc__get_monotonic(struct timeval *tv)<br>+{<br>+ int ret;<br>+ struct timeval currenttime;<br>+ struct timeval boottime_begin;<br>+ struct timeval boottime_end;<br>+<br>+ do {<br>+ ret = vlc__get_system_boottime(&boottime_begin);<br>+ if (ret != 0)<br>+ return ret;<br>+<br>+ ret = gettimeofday(¤ttime, NULL);<br>+ if (ret != 0)<br>+ return ret;<br>+<br>+ ret = vlc__get_system_boottime(&boottime_end);<br>+ if (ret != 0)<br>+ return ret;<br>+ } while (timercmp(&boottime_begin, &boottime_end, !=));<br>+<br>+ timersub(¤ttime, &boottime_begin, tv);<br>+ return 0;<br>+}<br>+<br>+int clock_gettime(clockid_t clock_id, struct timespec *tp)<br>+{<br>+ int ret = 0;<br>+ struct timeval tv;<br>+<br>+ switch (clock_id) {<br>+ case CLOCK_MONOTONIC:<br>+ ret = vlc__get_monotonic(&tv);<br>+ break;<br>+ case CLOCK_REALTIME:<br>+ ret = gettimeofday(&tv, NULL);<br>+ break;<br>+ default:<br>+ errno = EINVAL;<br>+ return -1;<br>+ }<br>+<br>+ if (ret == 0)<br>+ TIMEVAL_TO_TIMESPEC(&tv, tp);<br>+ return ret;<br>+}<br>diff --git a/compat/clock_nanosleep.c b/compat/clock_nanosleep.c<br>new file mode 100644<br>index 0000000000..f5cab11039<br>--- /dev/null<br>+++ b/compat/clock_nanosleep.c<br>@@ -0,0 +1,108 @@<br>+/*****************************************************************************<br>+ * clock_nanosleep.c: POSIX clock_nanosleep() replacement<br>+ *****************************************************************************<br>+ * Copyright © 2020 VLC authors and VideoLAN<br>+ *<br>+ * Author: Marvin Scholz <epirat07 at gmail dot com><br>+ *<br>+ * This program is free software; you can redistribute it and/or modify it<br>+ * under the terms of the GNU Lesser General Public License as published by<br>+ * the Free Software Foundation; either version 2.1 of the License, or<br>+ * (at your option) any later version.<br>+ *<br>+ * This program is distributed in the hope that it will be useful,<br>+ * but WITHOUT ANY WARRANTY; without even the implied warranty of<br>+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>+ * GNU Lesser General Public License for more details.<br>+ *<br>+ * You should have received a copy of the GNU Lesser General Public License<br>+ * along with this program; if not, write to the Free Software Foundation,<br>+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.<br>+ *****************************************************************************/<br>+<br>+#ifndef __APPLE__<br>+# error clock_nanosleep not implemented on your platform!<br>+#endif<br>+<br>+#ifdef HAVE_CONFIG_H<br>+# include <config.h><br>+#endif<br>+<br>+#define _DARWIN_C_SOURCE<br>+#define _POSIX_C_SOURCE 200809L<br>+<br>+#include <pthread.h><br>+#include <sys/errno.h><br>+#include <sys/types.h><br>+#include <sys/time.h><br>+#include <sys/sysctl.h><br>+#include <mach/clock_types.h><br>+<br>+#define timespecs_sub(tsa, tsb, tsr) \<br>+ do { \<br>+ (tsr)->tv_sec = (tsa)->tv_sec - (tsb)->tv_sec; \<br>+ (tsr)->tv_nsec = (tsa)->tv_nsec - (tsb)->tv_nsec; \<br>+ if ((tsr)->tv_nsec < 0) { \<br>+ (tsr)->tv_sec--; \<br>+ (tsr)->tv_nsec += NSEC_PER_SEC; \<br>+ } \<br>+ } while (0)<br>+<br>+#define timespec_zero(ts) \<br>+ ((ts)->tv_sec == 0 && (ts)->tv_nsec == 0)<br>+<br>+int clock_nanosleep(clockid_t clock_id, int flags,<br>+ const struct timespec *rqtp, struct timespec *rmtp)<br>+{<br>+ int err = EINVAL;<br>+ struct timespec ts_rel;<br>+<br>+ // Validate timespec<br>+ if (rqtp == NULL || rqtp->tv_sec < 0 ||<br>+ rqtp->tv_nsec < 0 || (unsigned long)rqtp->tv_nsec >= NSEC_PER_SEC)<br>+ goto error;<br>+<br>+ if (flags == TIMER_ABSTIME) {<br>+ struct timespec ts_now;<br>+<br>+ // We don't support waiting for a absolute<br>+ // wallclock time currently<br>+ if (clock_id == CLOCK_REALTIME) {<br>+ err = ENOTSUP;<br>+ goto error;<br>+ }<br>+<br>+ // Get current time with requested clock<br>+ if (clock_gettime(clock_id, &ts_now) != 0) {<br>+ err = errno;<br>+ goto error;<br>+ }<br>+<br>+ // Calculate relative timespec<br>+ timespecs_sub(rqtp, &ts_now, &ts_rel);<br>+<br>+ // Check if time already elapsed<br>+ if (ts_rel.tv_sec < 0 || timespec_zero(&ts_rel)) {<br>+ pthread_testcancel();<br>+ return 0;<br>+ }<br>+ } else if (flags == 0) {<br>+ ts_rel = *rqtp;<br>+ } else {<br>+ // Invalid flags<br>+ goto error;<br>+ }<br>+<br>+ switch (clock_id) {<br>+ case CLOCK_MONOTONIC:<br>+ case CLOCK_REALTIME:<br>+ return nanosleep(&ts_rel, rmtp);<br>+ default:<br>+ goto error;<br>+ }<br>+<br>+error:<br>+ pthread_testcancel();<br>+ errno = err;<br>+ return -1;<br>+}<br>diff --git a/configure.ac b/configure.ac<br>index bc1874cb97..c20ab3c75c 100644<br>--- a/configure.ac<br>+++ b/configure.ac<br>@@ -179,6 +179,12 @@ case "${host_os}" in<br> VLC_ADD_LIBS([libvlc vlc],[-Wl,-undefined,dynamic_lookup,-framework,AppKit])<br> VLC_ADD_LIBS([libvlccore],[-Wl,-framework,CoreFoundation,-framework,CoreServices])<br> <br>+ dnl macOS 10.11 lacks clock_gettime, introduced with 10.12<br>+ AC_LIBOBJ([clock_gettime])<br>+ dnl macOS lacks clock_nanosleep<br>+ AC_LIBOBJ([clock_nanosleep])<br>+ AC_CHECK_FUNCS([clock_gettime clock_nanosleep])<br>+<br> AC_EGREP_CPP(yes,<br> [#import <TargetConditionals.h><br> #if TARGET_OS_IPHONE<br>diff --git a/include/vlc_fixups.h b/include/vlc_fixups.h<br>index 8fd01da0ce..a296db0c32 100644<br>--- a/include/vlc_fixups.h<br>+++ b/include/vlc_fixups.h<br>@@ -658,6 +658,24 @@ FILE *vlc_win32_tmpfile(void);<br> <br> #ifdef __APPLE__<br> # define fdatasync fsync<br>+<br>+# include <time.h><br>+# ifndef TIMER_ABSTIME<br>+# define TIMER_ABSTIME 0x01<br>+# endif<br>+# ifndef CLOCK_REALTIME<br>+# define CLOCK_REALTIME 0<br>+# endif<br>+# ifndef CLOCK_MONOTONIC<br>+# define CLOCK_MONOTONIC 6<br>+# endif<br>+# ifndef HAVE_CLOCK_GETTIME<br>+int clock_gettime(clockid_t clock_id, struct timespec *tp);<br>+# endif<br>+# ifndef HAVE_CLOCK_NANOSLEEP<br>+int clock_nanosleep(clockid_t clock_id, int flags,<br>+ const struct timespec *rqtp, struct timespec *rmtp);<br>+# endif<br> #endif<br> <br> #ifdef __cplusplus</pre></blockquote></div><br>-- <br>Envoyé de mon appareil Android avec Courriel K-9 Mail. Veuillez excuser ma brièveté.</body></html>