[vlc-commits] compat: add clock_nanosleep for darwin
Marvin Scholz
git at videolan.org
Tue Mar 24 20:10:41 CET 2020
vlc | branch: master | Marvin Scholz <epirat07 at gmail.com> | Sun Mar 22 09:19:21 2020 +0100| [97fe588cbce580e5c49a33e565eb7b4918b06ab3] | committer: Marvin Scholz
compat: add clock_nanosleep for darwin
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=97fe588cbce580e5c49a33e565eb7b4918b06ab3
---
compat/clock_nanosleep.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++
configure.ac | 2 +-
include/vlc_fixups.h | 7 ++++
3 files changed, 104 insertions(+), 1 deletion(-)
diff --git a/compat/clock_nanosleep.c b/compat/clock_nanosleep.c
new file mode 100644
index 0000000000..494540b8b9
--- /dev/null
+++ b/compat/clock_nanosleep.c
@@ -0,0 +1,96 @@
+/*****************************************************************************
+ * 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.
+ *****************************************************************************/
+
+#ifdef __APPLE__
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#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>
+
+int clock_nanosleep(clockid_t clock_id, int flags,
+ const struct timespec *rqtp, struct timespec *rmtp)
+{
+ // 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;
+ }
+
+ // Validate clock
+ switch (clock_id) {
+ case CLOCK_MONOTONIC:
+ case CLOCK_REALTIME:
+ break;
+ default:
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (flags == TIMER_ABSTIME) {
+ struct timespec ts_rel;
+ struct timespec ts_now;
+
+ do {
+ // Get current time with requested clock
+ if (clock_gettime(clock_id, &ts_now) != 0)
+ return -1;
+
+ // Calculate relative timespec
+ ts_rel.tv_sec = rqtp->tv_sec - ts_now.tv_sec;
+ ts_rel.tv_nsec = rqtp->tv_nsec - ts_now.tv_nsec;
+ if (ts_rel.tv_nsec < 0) {
+ ts_rel.tv_sec -= 1;
+ ts_rel.tv_nsec += NSEC_PER_SEC;
+ }
+
+ // Check if time already elapsed
+ if (ts_rel.tv_sec < 0 || (ts_rel.tv_sec == 0 && ts_rel.tv_nsec == 0)) {
+ pthread_testcancel();
+ return 0;
+ }
+
+ // "The absolute clock_nanosleep() function has no effect on the
+ // structure referenced by rmtp", so do not pass rmtp here
+ } while (nanosleep(&ts_rel, NULL) == 0);
+
+ // If nanosleep failed or was interrupted by a signal,
+ // return so the caller can handle it appropriately
+ return -1;
+ } else if (flags == 0) {
+ return nanosleep(rqtp, rmtp);
+ } else {
+ // Invalid flags
+ errno = EINVAL;
+ return -1;
+ }
+}
+
+#endif
diff --git a/configure.ac b/configure.ac
index 8fdf1174b4..1dedd7c7a6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -930,7 +930,7 @@ AC_SUBST([LIBRT])
dnl Check for clock_* functions, needs to be done here,
dnl after the -lrt check
-AC_REPLACE_FUNCS([clock_gettime])
+AC_REPLACE_FUNCS([clock_gettime clock_nanosleep])
dnl
dnl Check for headers
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
More information about the vlc-commits
mailing list